Skip to content

Read refs and objects from linked worktrees#196

Merged
andrew merged 2 commits into
mainfrom
worktree-commondir
May 9, 2026
Merged

Read refs and objects from linked worktrees#196
andrew merged 2 commits into
mainfrom
worktree-commondir

Conversation

@andrew
Copy link
Copy Markdown
Contributor

@andrew andrew commented May 9, 2026

In a linked worktree .git is a file pointing at <main>/.git/worktrees/<name>, and that directory has a commondir file pointing back to the main .git where refs and objects actually live. go-git's PlainOpen roots its storer at the per-worktree dir, which has no objects/ and no refs/heads/, so even resolving HEAD fails with reference not found.

go-git has PlainOpenOptions.EnableDotGitCommonDir for this, but in v5 it opens commondir without closing it, which on Windows blocks t.TempDir() cleanup and would leak an fd per open in any long-running caller. The fix is on go-git's main but not in any v5 tag including v5.19.0.

We already shell out to git rev-parse --git-common-dir for DatabasePath(), and #194 already rebuilds the storage to set AlternatesFS, so this wraps the dotgit fs in dotgit.NewRepositoryFilesystem with the common dir during that same rebuild. go-git never opens commondir itself.

This is separate from #194. That one is about objects/info/alternates (git clone --shared / --reference), which is a different mechanism with the same error message. The two compose: a worktree of a --shared clone needs both, and the RepositoryFilesystem wrapper routes objects/info/alternates to the common dir where it lives.

Extended the existing worktree test to actually read refs and objects rather than just checking path resolution.

Fixes #195

@andrew andrew force-pushed the worktree-commondir branch from 4dc6eee to 238a9c4 Compare May 9, 2026 10:46
@skagedal
Copy link
Copy Markdown

skagedal commented May 9, 2026

Tested, worked great! 🎉

@andrew andrew merged commit 7cfaa99 into main May 9, 2026
7 checks passed
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.

git pkgs diff does not work in worktrees

2 participants