Coverage for src/workstack/cli/commands/up.py: 91%

23 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-19 09:31 -0400

1from pathlib import Path 

2 

3import click 

4 

5from workstack.cli.commands.switch import ( 

6 _activate_worktree, 

7 _ensure_graphite_enabled, 

8 _resolve_up_navigation, 

9) 

10from workstack.cli.core import discover_repo_context 

11from workstack.cli.graphite import find_worktree_for_branch 

12from workstack.core.context import WorkstackContext 

13 

14 

15@click.command("up") 

16@click.option( 

17 "--script", is_flag=True, help="Print only the activation script without usage instructions." 

18) 

19@click.pass_obj 

20def up_cmd(ctx: WorkstackContext, script: bool) -> None: 

21 """Move to child branch in Graphite stack. 

22 

23 With shell integration (recommended): 

24 workstack up 

25 

26 The shell wrapper function automatically activates the worktree. 

27 Run 'workstack init --shell' to set up shell integration. 

28 

29 Without shell integration: 

30 source <(workstack up --script) 

31 

32 This will cd to the child branch's worktree, create/activate .venv, and load .env variables. 

33 Requires Graphite to be enabled: 'workstack config set use_graphite true' 

34 """ 

35 _ensure_graphite_enabled(ctx) 

36 repo = discover_repo_context(ctx, Path.cwd()) 

37 

38 # Get current branch 

39 current_branch = ctx.git_ops.get_current_branch(Path.cwd()) 

40 if current_branch is None: 

41 click.echo("Error: Not currently on a branch (detached HEAD)", err=True) 

42 raise SystemExit(1) 

43 

44 # Get all worktrees for checking if target has a worktree 

45 worktrees = ctx.git_ops.list_worktrees(repo.root) 

46 

47 # Resolve navigation to get target branch 

48 target_name = _resolve_up_navigation(ctx, repo, current_branch, worktrees) 

49 

50 # Resolve target branch to actual worktree path 

51 target_wt_path = find_worktree_for_branch(worktrees, target_name) 

52 if target_wt_path is None: 

53 # This should not happen because _resolve_up_navigation already checks 

54 # But include defensive error handling 

55 click.echo( 

56 f"Error: Branch '{target_name}' has no worktree. This should not happen.", 

57 err=True, 

58 ) 

59 raise SystemExit(1) 

60 

61 _activate_worktree(repo, target_wt_path, script, "up")