Skip to content

Commit e97652c

Browse files
committed
Address CodeRabbit review: worktree root context
1 parent 8a3fb79 commit e97652c

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

lib/commands/copy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ cmd_copy() {
6262
# Build target list for --all mode
6363
if [ "$all_mode" -eq 1 ]; then
6464
local all_branches
65-
all_branches=$(list_worktree_branches "$base_dir" "$prefix")
65+
all_branches=$(list_worktree_branches "$base_dir" "$prefix" "$repo_root")
6666
if [ -z "$all_branches" ]; then
6767
log_error "No worktrees found"
6868
exit 1

lib/core.sh

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,11 @@ _emit_worktree_record() {
214214

215215
[ -z "$wt_path" ] && return 0
216216

217-
local is_main=0 branch="$wt_branch" status
218-
[ "$wt_path" = "$repo_root" ] && is_main=1
217+
local is_main=0 branch="$wt_branch" status wt_path_canonical
218+
wt_path_canonical=$(canonicalize_path "$wt_path" || printf "%s" "$wt_path")
219+
if [ "$wt_path" = "$repo_root" ] || [ "$wt_path_canonical" = "$repo_root" ]; then
220+
is_main=1
221+
fi
219222
[ -z "$branch" ] && branch="(detached)"
220223
status=$(_worktree_record_status "$wt_detached" "$wt_locked" "$wt_prunable")
221224

@@ -660,13 +663,15 @@ resolve_repo_context() {
660663
}
661664

662665
# List all worktree branch names (excluding main repo)
663-
# Usage: list_worktree_branches base_dir prefix
666+
# Usage: list_worktree_branches base_dir prefix [repo_root]
664667
# Returns: newline-separated list of branch names
665668
list_worktree_branches() {
666669
local base_dir="$1"
667670
local prefix="$2"
668-
local repo_root
669-
repo_root=$(_resolve_main_repo_root) || return 0
671+
local repo_root="${3:-}"
672+
if [ -z "$repo_root" ]; then
673+
repo_root=$(_resolve_main_repo_root) || return 0
674+
fi
670675

671676
# base_dir and prefix are kept for the public helper contract. Worktree
672677
# discovery itself comes from Git's registry so nested registered worktrees

tests/integration_lifecycle.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ teardown() {
5050
[[ "$branches" != *"(detached)"* ]]
5151
}
5252

53+
@test "list_worktree_branches uses explicit repo root outside repo" {
54+
local base_dir="${TEST_REPO}-worktrees"
55+
create_worktree "$base_dir" "" "outside-context" "HEAD" "none" "1" "0" "" "" >/dev/null
56+
cd /tmp || false
57+
58+
local branches
59+
branches=$(list_worktree_branches "$base_dir" "" "$TEST_REPO")
60+
61+
[[ "$branches" == *"outside-context"* ]]
62+
}
63+
5364
@test "resolve_target finds worktree by branch name" {
5465
local default_branch
5566
default_branch=$(git rev-parse --abbrev-ref HEAD)

0 commit comments

Comments
 (0)