Skip to content

Commit 0736f04

Browse files
committed
fix: resolve_target fallback via git worktree list (#127)
Add a git worktree list --porcelain fallback as last resort in resolve_target() before returning "not found". This catches worktrees created outside the gtr-managed directory (e.g. via raw git worktree add) and provides a safety net for edge cases where the fast-path checks miss a match. Closes #127
1 parent 5bc568f commit 0736f04

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/core.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,24 @@ resolve_target() {
299299
done
300300
fi
301301

302+
# Last resort: ask git for all worktrees (catches non-gtr-managed worktrees)
303+
local wt_path wt_branch
304+
while IFS= read -r line; do
305+
case "$line" in
306+
"worktree "*) wt_path="${line#worktree }" ;;
307+
"branch "*)
308+
wt_branch="${line#branch refs/heads/}"
309+
if [ "$wt_branch" = "$identifier" ]; then
310+
local is_main=0
311+
[ "$wt_path" = "$repo_root" ] && is_main=1
312+
printf "%s\t%s\t%s\n" "$is_main" "$wt_path" "$wt_branch"
313+
return 0
314+
fi
315+
;;
316+
"") wt_path="" ; wt_branch="" ;;
317+
esac
318+
done < <(git -C "$repo_root" worktree list --porcelain 2>/dev/null)
319+
302320
log_error "Worktree not found for branch: $identifier"
303321
return 1
304322
}

tests/core_resolve_target.bats

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ teardown() {
8181
[ "$status" -eq 1 ]
8282
}
8383

84+
# ── porcelain fallback ─────────────────────────────────────────────────────────
85+
86+
@test "resolve_target finds externally-created worktree via porcelain fallback" {
87+
# Create a worktree with raw git (outside gtr-managed directory)
88+
local ext_dir="${TEST_REPO}-external"
89+
git -C "$TEST_REPO" worktree add "$ext_dir" -b external-branch --quiet
90+
local result
91+
result=$(resolve_target "external-branch" "$TEST_REPO" "$TEST_WORKTREES_DIR" "")
92+
# Assert: found with is_main=0, correct branch, path ends with expected suffix
93+
[[ "$result" == "0"$'\t'*"-external"$'\t'"external-branch" ]]
94+
}
95+
8496
# ── discover_repo_root from worktree ──────────────────────────────────────────
8597

8698
@test "discover_repo_root returns main repo root when called from a worktree" {

0 commit comments

Comments
 (0)