Repository navigation
Fix relative worktree dir from linked worktrees - #11
Conversation
melonamin
left a comment
There was a problem hiding this comment.
Two path-resolution regressions need fixes before merge.
|
|
||
| func relPathWithin(basePath, targetPath string) (string, bool) { | ||
| relPath, err := filepath.Rel(basePath, targetPath) | ||
| if err != nil || relPath == "" || relPath == "." || filepath.IsAbs(relPath) { |
There was a problem hiding this comment.
worktree_dir: "." now resolves to the repository root, but relPathWithin returns false for .. That skips both the tracked-file guard and exclude update, allowing a nested worktree to be created directly in the repository and left as ?? branch/. The previous implementation rejected this configuration. Treat equality with the repository root as invalid.
There was a problem hiding this comment.
Addressed in 8d1b6ac and covered by a regression test: worktree_dir: "." now fails before directory creation, so the repository root cannot be used as a worktree directory.
| commonDir := filepath.Clean(strings.TrimSpace(string(output))) | ||
| if filepath.Base(commonDir) != ".git" { | ||
| return "", false | ||
| } |
There was a problem hiding this comment.
This can misidentify a separate Git directory as the main worktree when its path ends in .git (for example, git init --separate-git-dir=/metadata/.git /checkout). Relative worktrees are then created under /metadata/.worktrees instead of falling back to /checkout/.worktrees. Verify that the common directory belongs to the actual main checkout before returning this path.
There was a problem hiding this comment.
Addressed in 8d1b6ac and covered by a regression test: when the active checkout uses a separate Git directory, relative worktrees resolve under that checkout rather than the metadata directory.
There was a problem hiding this comment.
Confirmed a remaining linked-worktree variant at 4917ff5.
Reproduction, run go build from the PR checkout:
root=$(mktemp -d)
checkout="$root/checkout"
metadata="$root/metadata/.git"
linked="$root/linked"
mkdir -p "$root/metadata"
git init --separate-git-dir="$metadata" --initial-branch=main "$checkout"
git -C "$checkout" -c user.name=Test -c [email protected] \
commit --allow-empty -m init
git -C "$checkout" worktree add -b source "$linked"
go build -o "$root/gt" .
(cd "$linked" && XDG_CONFIG_HOME="$root/config" "$root/gt" -x true feature)
git -C "$checkout" worktree list --porcelainObserved: gt switches to $root/metadata/.worktrees/feature and git worktree list shows the new worktree there.
Expected: this atypical layout should fall back to the current Git top-level, $root/linked/.worktrees/feature. Running the same reproduction against main creates that expected path.
The added separate-Git-directory tests cover execution from the main checkout, where gitDir == commonDir; they do not cover execution from a linked worktree, where the resolver enters the git worktree list path and accepts the metadata parent as the main worktree.
There was a problem hiding this comment.
Fixed in 02bbaaf, with the exact linked-worktree + separate-Git-directory shape covered by an integration test. I then simplified the resolver in e38dfa1 and made the policy deliberately conservative: only the conventional non-empty main checkout with a collocated .git directory uses the shared main-relative .worktrees folder. Any ambiguous or atypical layout—including this separate-Git-directory case—falls back to the current Git top-level; beyond that fallback behavior, no attempt is made to infer a canonical shared location. This removes five Git subprocess calls and 81 lines of detection logic. Full tests and vet pass.
There was a problem hiding this comment.
now address for real and encoded as integration test!
There was a problem hiding this comment.
The exact prior reproduction is fixed at e38dfa1, but the new directory-nonempty heuristic still misclassifies valid separate-Git-dir layouts.
I confirmed this locally by adding one unrelated entry beside the external Git directory:
mkdir "$metadata_parent/notes"With $metadata_parent/.git plus notes/, getMainWorktreePath treats $metadata_parent as a checkout because os.ReadDir finds a name other than .git. Running gt from either the separate checkout or a linked worktree then creates $metadata_parent/.worktrees/feature. It should fall back to the current Git top-level. The same smoke test without notes/ now passes.
Suggested fix shape:
- Resolve absolute
--git-dirand--git-common-dir. - If they identify the same directory, the current checkout is the main worktree; return
repoPath. This covers normal and separate-Git-dir main checkouts. - For a linked worktree, read the first porcelain worktree candidate and require
candidate/.gitto be the common Git directory usingos.SameFile. - Replace the arbitrary “contains anything besides
.git” test with checkout evidence from the index: rungit ls-files -zand require at least one tracked path to exist under the candidate. If that cannot be established, use the documented fallback to the currentrepoPath.
Please add a regression case that creates metadata/notes before resolving the worktree directory. The expected path should remain <current-checkout>/.worktrees/feature, never metadata/.worktrees/feature.
|
@melonamin Beyond the requested fixes, I added CLI E2E coverage for separate-Git-directory fallback, repository-root rejection, and the post-create hook working directory. I also simplified the creation flow so it returns the authoritative worktree path and reuses it for the hook/switch step, eliminating the duplicate path resolution. Full tests and vet pass. Could you please take another look? |
Summary
Tests