Skip to content

Fix: Enable HEAD resolution in git worktrees#741

Merged
meekrosoft merged 2 commits intomainfrom
fix-worktree-head-resolution
Mar 30, 2026
Merged

Fix: Enable HEAD resolution in git worktrees#741
meekrosoft merged 2 commits intomainfrom
fix-worktree-head-resolution

Conversation

@meekrosoft
Copy link
Copy Markdown
Contributor

Summary

Enable EnableDotGitCommonDir option in gitview.New() to properly resolve HEAD references in git worktrees.

Changes

  • Modified gitView.New() to use git.PlainOpenWithOptions() with EnableDotGitCommonDir: true
  • Added comprehensive test TestNewGitViewFromWorktree() that validates:
    • Creating GitView from a worktree path succeeds
    • BranchName() returns correct branch in worktree context
    • GetCommitInfoFromCommitSHA("HEAD", ...) resolves correctly in worktree
  • Updated TODO.md marking all items in the worktree fix slice as complete

Impact

Users can now use gitview operations from git worktree paths without errors when resolving HEAD references.

@claude
Copy link
Copy Markdown
Contributor

claude bot commented Mar 27, 2026

Claude finished @meekrosoft's task in 2m 19s —— View job


PR Review: Fix HEAD resolution in git worktrees

  • Phase 1: Static analysis (go build, go vet, go mod tidy)
  • Phase 2: Code review
  • Phase 3: Post review feedback

Static Analysis

All checks pass: go build ./... ✓, go vet ./... ✓, go mod tidy ✓ (no diff).

Code Review

Production change (internal/gitview/gitView.go): The one-line change from git.PlainOpen to git.PlainOpenWithOptions with EnableDotGitCommonDir: true is the correct fix. This option tells go-git to follow the .git file in worktrees to the shared commondir, allowing proper HEAD resolution. It's backward-compatible — for non-worktree repos this option is a no-op since there's no .git file pointing to a commondir.

Test (internal/gitview/gitView_test.go): Well-structured test that:

  • Creates a real git repo, then a worktree via git worktree add
  • Validates three key operations: New(), BranchName(), and GetCommitInfoFromCommitSHA("HEAD", ...)
  • Uses CombinedOutput() to capture error details on failure
  • Cleanup is handled by the existing AfterTest() which removes the entire tmpDir (both repo and worktree live under it)

TODO.md: Follows the repo's slice-tracking convention correctly.

Minor Observations

  1. The test shells out to git worktree add via exec.Command, which is reasonable — go-git doesn't have a native worktree-creation API, and using the real git command is the most reliable way to set up this fixture.
  2. No cleanup of the git worktree metadata (git worktree remove) before os.RemoveAll — this is fine for test purposes since we're deleting the entire temp directory, but worth noting that in long-running processes stale worktree entries could appear in git worktree list for the parent repo. Not a concern here since the parent repo is also temp.

Summary

Clean, minimal, well-tested fix. The change is backward-compatible, the test covers the important code paths, and the PR follows the repo's TDD and slice conventions. Looks good to merge. 👍

meekrosoft and others added 2 commits March 30, 2026 11:03
Enable EnableDotGitCommonDir in go-git's PlainOpenWithOptions so that
ref resolution follows the commondir chain. Without this, HEAD in a
linked worktree points to a branch whose ref lives in the main repo's
.git/refs, and go-git fails with "reference not found".

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@meekrosoft meekrosoft force-pushed the fix-worktree-head-resolution branch from 0b6c5d4 to 4b865da Compare March 30, 2026 09:04
@tooky
Copy link
Copy Markdown
Contributor

tooky commented Mar 30, 2026

I'd love to hear more about why this is useful? and what was happening without it?

@meekrosoft
Copy link
Copy Markdown
Contributor Author

Reply to @tooky 's comment: the make demo command doesn't work inside the worktree - the repo_root() command in the shell scripts that make the dummy data fail.

@meekrosoft meekrosoft requested a review from tooky March 30, 2026 11:53
@meekrosoft meekrosoft merged commit 6b94d14 into main Mar 30, 2026
17 checks passed
@meekrosoft meekrosoft deleted the fix-worktree-head-resolution branch March 30, 2026 14:48
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