Coverage for src/workstack/status/collectors/graphite.py: 32%

31 statements  

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

1"""Graphite stack collector.""" 

2 

3from pathlib import Path 

4 

5from workstack.cli.graphite import get_branch_stack 

6from workstack.core.context import WorkstackContext 

7from workstack.status.collectors.base import StatusCollector 

8from workstack.status.models.status_data import StackPosition 

9 

10 

11class GraphiteStackCollector(StatusCollector): 

12 """Collects Graphite stack position information.""" 

13 

14 @property 

15 def name(self) -> str: 

16 """Name identifier for this collector.""" 

17 return "stack" 

18 

19 def is_available(self, ctx: WorkstackContext, worktree_path: Path) -> bool: 

20 """Check if Graphite is enabled and available. 

21 

22 Args: 

23 ctx: Workstack context 

24 worktree_path: Path to worktree 

25 

26 Returns: 

27 True if Graphite is enabled 

28 """ 

29 if not ctx.global_config_ops.get_use_graphite(): 

30 return False 

31 

32 if not worktree_path.exists(): 

33 return False 

34 

35 return True 

36 

37 def collect( 

38 self, ctx: WorkstackContext, worktree_path: Path, repo_root: Path 

39 ) -> StackPosition | None: 

40 """Collect Graphite stack information. 

41 

42 Args: 

43 ctx: Workstack context 

44 worktree_path: Path to worktree 

45 repo_root: Repository root path 

46 

47 Returns: 

48 StackPosition with stack information or None if collection fails 

49 """ 

50 branch = ctx.git_ops.get_current_branch(worktree_path) 

51 if branch is None: 

52 return None 

53 

54 # Get the stack for current branch 

55 stack = get_branch_stack(ctx, repo_root, branch) 

56 if stack is None: 

57 return None 

58 

59 # Find current branch position in stack 

60 if branch not in stack: 

61 return None 

62 

63 current_idx = stack.index(branch) 

64 

65 # Determine parent and children 

66 parent_branch = stack[current_idx - 1] if current_idx > 0 else None 

67 

68 children_branches = [] 

69 if current_idx < len(stack) - 1: 

70 children_branches.append(stack[current_idx + 1]) 

71 

72 # Check if this is trunk 

73 is_trunk = current_idx == 0 

74 

75 return StackPosition( 

76 stack=stack, 

77 current_branch=branch, 

78 parent_branch=parent_branch, 

79 children_branches=children_branches, 

80 is_trunk=is_trunk, 

81 )