Skip to content

Fix relative worktree dir from linked worktrees - #11

Merged
melonamin merged 7 commits into
melonamin:mainfrom
mishamsk:fix/worktree-dir-main-root
Jul 12, 2026
Merged

melonamin merged 7 commits into
melonamin:mainfrom
mishamsk:fix/worktree-dir-main-root

Conversation

@mishamsk

@mishamsk mishamsk commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Resolve relative worktree_dir values from the main worktree for normal non-bare repos with a real .git directory
  • Fall back to the current Git top-level when that typical layout cannot be identified
  • Keep branch creation based on the active worktree HEAD and add e2e coverage for linked-worktree launch behavior

Tests

  • go test ./...

@melonamin melonamin left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Two path-resolution regressions need fixes before merge.

Comment thread main.go

func relPathWithin(basePath, targetPath string) (string, bool) {
relPath, err := filepath.Rel(basePath, targetPath)
if err != nil || relPath == "" || relPath == "." || filepath.IsAbs(relPath) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread main.go Outdated
commonDir := filepath.Clean(strings.TrimSpace(string(output)))
if filepath.Base(commonDir) != ".git" {
return "", false
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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 --porcelain

Observed: 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

now address for real and encoded as integration test!

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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:

  1. Resolve absolute --git-dir and --git-common-dir.
  2. If they identify the same directory, the current checkout is the main worktree; return repoPath. This covers normal and separate-Git-dir main checkouts.
  3. For a linked worktree, read the first porcelain worktree candidate and require candidate/.git to be the common Git directory using os.SameFile.
  4. Replace the arbitrary “contains anything besides .git” test with checkout evidence from the index: run git ls-files -z and require at least one tracked path to exist under the candidate. If that cannot be established, use the documented fallback to the current repoPath.

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.

@mishamsk

Copy link
Copy Markdown
Contributor Author

@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?

@mishamsk
mishamsk requested a review from melonamin July 11, 2026 16:07

@melonamin melonamin left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Reviewed at e38dfa1 with green CI. Accepting the documented separate-Git-directory edge case.

@melonamin
melonamin merged commit 461f54d into melonamin:main Jul 12, 2026
5 checks passed
@mishamsk
mishamsk deleted the fix/worktree-dir-main-root branch July 12, 2026 19:53
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