Skip to content

fix(cli): task-setup resolves repo via git-common-dir, not script cwd#899

Merged
buremba merged 1 commit into
mainfrom
fix/task-setup-worktree-cwd
May 19, 2026
Merged

fix(cli): task-setup resolves repo via git-common-dir, not script cwd#899
buremba merged 1 commit into
mainfrom
fix/task-setup-worktree-cwd

Conversation

@buremba
Copy link
Copy Markdown
Member

@buremba buremba commented May 19, 2026

Summary

task-setup.sh resolved the target repo as $script_dir/.., which only works when invoked from the main checkout. Worktrees share the full working tree including scripts/, so running make task-setup NAME=foo from inside an existing worktree landed the new worktree at <calling-worktree>/.claude/worktrees/foo/ — nested — instead of ~/Code/lobu/.claude/worktrees/foo/.

Fix: resolve repo via git rev-parse --git-common-dir, which returns the shared .git directory regardless of which worktree the call originates from. Its parent is always the main checkout.

Reproducer

Before:

cd ~/Code/lobu/.claude/worktrees/some-existing-task
make task-setup NAME=foo
# worktree created at ~/Code/lobu/.claude/worktrees/some-existing-task/.claude/worktrees/foo  ← nested

After:

cd ~/Code/lobu/.claude/worktrees/some-existing-task
make task-setup NAME=foo
# worktree created at ~/Code/lobu/.claude/worktrees/foo  ← flat

Real-world fallout (from the session that uncovered it)

Two nested worktrees (feat/frontend and feat/landing-page) had been created via this bug from inside feat/chrome-extension-e2e. When make task-clean NAME=chrome-extension-e2e FORCE=1 ran, rm -rf of the outer worktree clipped tracked files inside the inner worktrees before bailing on "Directory not empty". Tracked files were recoverable via git checkout -- .; any untracked WIP would have been lost. This fix prevents the nesting from happening at all.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed setup script to correctly resolve repository paths when invoked from within an existing worktree, ensuring proper configuration sources and preventing incorrect path assignments.

Review Change Stack

The script computed the target repo as $script_dir/.., which only works
when invoked from the main checkout. Worktrees share the full working tree
including scripts/, so running make task-setup NAME=foo from inside an
existing worktree resolved repo to the calling worktree, not ~/Code/lobu.
The new worktree then landed at <calling-worktree>/.claude/worktrees/foo/ —
nested instead of flat at ~/Code/lobu/.claude/worktrees/foo/.

Symptoms in practice:
- make task-clean NAME=<outer> refusing because the outer dir contains
  another active worktree.
- A FORCE=1 task-clean of the outer worktree clipping files inside the
  inner worktrees before bailing on Directory not empty.

Fix: git rev-parse --git-common-dir returns the shared .git directory
regardless of which worktree the call comes from. Its parent is the main
checkout — the right answer for both worktree-add target and .env source.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 19, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 22c9094f-bdb5-415c-a1a2-0eb370f370ef

📥 Commits

Reviewing files that changed from the base of the PR and between aa5a71f and af1dc55.

📒 Files selected for processing (1)
  • scripts/task-setup.sh

📝 Walkthrough

Walkthrough

The script now resolves the repository root by querying Git's shared .git directory location using git rev-parse --git-common-dir and deriving the parent directory, instead of assuming a fixed relative path. This prevents incorrect worktree paths when the script runs from within an existing worktree.

Changes

Worktree root resolution fix

Layer / File(s) Summary
Git worktree root resolution logic
scripts/task-setup.sh
Lines 74–86 replace simple relative path navigation with Git-aware logic that resolves the common .git directory location, then walks to its parent to establish the correct repository root for worktree and environment file operations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A worktree walks in, confused and turned around,
But now we ask Git where the real root is found,
No more nested paths leading nowhere to go—
Just one solid checkout, with .git in the know! 🌿✨

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/task-setup-worktree-cwd

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

@buremba buremba merged commit 8e26abd into main May 19, 2026
19 of 20 checks passed
@buremba buremba deleted the fix/task-setup-worktree-cwd branch May 19, 2026 00:49
@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

buremba added a commit that referenced this pull request May 19, 2026
…900)

The previous fix (#899) called `git rev-parse --git-common-dir` without
forcing absolute output. Without that, git returns a path relative to its
cwd ("../.git"), and `dirname` of that resolves against whatever the
caller's cwd happens to be — so running `make task-setup` from the main
repo landed worktrees at /Users/burakemre/Code/.claude/worktrees/<name>/
(parent of the lobu checkout) instead of /Users/burakemre/Code/lobu/.claude/worktrees/<name>/.

Fix: pass `--path-format=absolute` so git always returns the full path.
`dirname` of an absolute /.../lobu/.git is reliably the main checkout.

Verified by hand:
  from main:      /Users/burakemre/Code/lobu
  from worktree:  /Users/burakemre/Code/lobu
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