Skip to content

Add git worktree path to gb output#358

Open
jafinn3 wants to merge 1 commit intoscmbreeze:mainfrom
jafinn3:worktree-support
Open

Add git worktree path to gb output#358
jafinn3 wants to merge 1 commit intoscmbreeze:mainfrom
jafinn3:worktree-support

Conversation

@jafinn3
Copy link

@jafinn3 jafinn3 commented Dec 16, 2025

If using git worktrees, appends the path of the branch's checkout to the output of gb. If not using git worktrees, the output is unaffected.

Sample output:

» gb
* [1] main 
   [2] tmp
+ [3] worktree-support (/tmp/worktree-support)

Summary by CodeRabbit

Refactor

  • Enhanced formatting and alignment for git branch shortcuts with numeric indexing for better readability.
  • Improved handling and display of multiple worktrees with optional path annotations for clearer context.

✏️ Tip: You can customize this high-level summary in your review settings.

If using git worktrees, appends the path of the branch's checkout
to the output of gb. If not using git worktrees, the output is
unaffected.

Sample output:

» gb
* [1] main
  [2] tmp
+ [3] worktree-support (/tmp/worktree-support)
@ndbroadbent
Copy link
Member

@coderabbitai /review

@coderabbitai
Copy link

coderabbitai bot commented Jan 28, 2026

@ndbroadbent: I'll review the changes in this PR.

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link

coderabbitai bot commented Jan 28, 2026

📝 Walkthrough

Walkthrough

The git branch shortcuts shell script undergoes comprehensive refactoring, replacing simple line numbering with Ruby-driven formatting logic. New helper functions parse worktrees, map branches, detect current branch, compute alignment widths, and format output with conditional worktree path annotations.

Changes

Cohort / File(s) Summary
Git Branch Shortcuts Rework
lib/git/branch_shortcuts.sh
Injects Ruby-driven formatting layer with helper functions for worktree-to-branch mapping, branch name sanitization, current branch detection, maximum display width computation, and line formatting with optional worktree path annotation. Main flow now fetches colored output, builds worktree map, determines current branch status, computes alignment width conditionally, formats each line with index and spacing, and appends worktree paths for multi-worktree scenarios.

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Poem

🐰 A rabbit hops through branches bright,
With formatting that feels just right,
Worktrees aligned, in perfect rows,
The shortcuts sing as progress flows!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change—adding worktree paths to gb output—and aligns with the primary objective of the pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@lib/git/branch_shortcuts.sh`:
- Around line 26-39: The build_worktree_to_branch_map function currently only
records worktrees that have a branch line (worktree_map), so detached current
worktrees are ignored; update this routine to also count total worktrees
discovered from git worktree list --porcelain (e.g., increment a total_worktrees
counter whenever you see a "worktree " line) and return that count alongside
worktree_map (or expose it via a renamed return like [worktree_map,
total_worktrees]) so callers (where show_worktrees is decided) can use
total_worktrees instead of worktree_map.size to decide whether to show paths;
keep using the existing variables (worktree_output, current_worktree,
worktree_map) and only add a total counter and the corresponding return change
so callers at the other usage sites (lines ~91-96) can be updated to read the
new count.

Comment on lines +26 to +39
def build_worktree_to_branch_map(git_cmd)
worktree_map = {}
worktree_output = %x(#{git_cmd} worktree list --porcelain 2>/dev/null)
current_worktree = nil
worktree_output.lines.each do |line|
if line.start_with?('worktree ')
current_worktree = line[9..-1].strip
elsif line.start_with?('branch refs/heads/') && current_worktree
branch_name = line[18..-1].strip
worktree_map[branch_name] = current_worktree
current_worktree = nil
end
end
worktree_map
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Handle detached-current-worktree cases when deciding to show paths.

show_worktrees is keyed off worktree_map.size, which only counts worktrees that have a branch line. If the current worktree is detached and there is only one other worktree branch, paths won’t be shown even though worktrees are in use. Consider tracking the total number of worktrees from git worktree list --porcelain and use that to gate show_worktrees.

🔧 Suggested fix
     def build_worktree_to_branch_map(git_cmd)
       worktree_map = {}
       worktree_output = %x(#{git_cmd} worktree list --porcelain 2>/dev/null)
+      worktree_count = 0
       current_worktree = nil
       worktree_output.lines.each do |line|
         if line.start_with?('worktree ')
+          worktree_count += 1
           current_worktree = line[9..-1].strip
         elsif line.start_with?('branch refs/heads/') && current_worktree
           branch_name = line[18..-1].strip
           worktree_map[branch_name] = current_worktree
           current_worktree = nil
         end
       end
-      worktree_map
+      [worktree_map, worktree_count]
     end
@@
-    worktree_map = build_worktree_to_branch_map('$_git_cmd')
+    worktree_map, worktree_count = build_worktree_to_branch_map('$_git_cmd')
@@
-    show_worktrees = worktree_map.size > 1
+    show_worktrees = worktree_count > 1

Also applies to: 91-96

🤖 Prompt for AI Agents
In `@lib/git/branch_shortcuts.sh` around lines 26 - 39, The
build_worktree_to_branch_map function currently only records worktrees that have
a branch line (worktree_map), so detached current worktrees are ignored; update
this routine to also count total worktrees discovered from git worktree list
--porcelain (e.g., increment a total_worktrees counter whenever you see a
"worktree " line) and return that count alongside worktree_map (or expose it via
a renamed return like [worktree_map, total_worktrees]) so callers (where
show_worktrees is decided) can use total_worktrees instead of worktree_map.size
to decide whether to show paths; keep using the existing variables
(worktree_output, current_worktree, worktree_map) and only add a total counter
and the corresponding return change so callers at the other usage sites (lines
~91-96) can be updated to read the new count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants