fix(pwd): find origin from inside a linked git worktree#54
Open
clawedassistant26 wants to merge 1 commit into
Open
fix(pwd): find origin from inside a linked git worktree#54clawedassistant26 wants to merge 1 commit into
clawedassistant26 wants to merge 1 commit into
Conversation
A linked worktree's git dir (.git/worktrees/<name>) carries only that worktree's own HEAD and index. The shared config, and therefore every remote, stays in the main repo's git dir, which git names in a `commondir` file. readOrigin() was pointed at the worktree git dir, so it read no config at all and `moshcode pwd` / `/pwd` silently dropped the `origin:` line — the same repo shows it from the main checkout. Resolve commondir (relative or absolute) before reading config. HEAD is still read from the worktree's own git dir, because the branch really is per-worktree. Plain repos and submodules have no commondir file, so they take the unchanged path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Inside a linked git worktree,
moshcode pwd(and the TUI/pwd) silently drops theorigin:line. The same repo prints it fine from the main checkout.Repro (real git, unmodified
main@ 5029919)Actual:
From
main-repo, same repo, same remote:Cause
locate()resolves the worktrees.gitfile to.git/worktrees/and then reads both HEAD andconfigfrom there. A linked worktrees git dir holds only its own HEAD, index and logs — there is noconfigfile, soreadOrigin()throws on the read, catches, and returns null. The shared config lives in the main repos git dir, which git names in acommondirfile next to that HEAD (normally the relative path../..`).Branch still resolves because HEAD genuinely is per-worktree, which is what makes the missing origin look like "this repo has no remote" rather than a lookup failure.
Fix
src/pwd.mjsonly. A smallcommonGitDir()helper readscommondirand resolves it against the worktree git dir (handles both the relative form git writes and an absolute one);readOrigin()is called with that.readBranch()is deliberately left on the worktree git dir.No behaviour change elsewhere: a plain checkout and a submodule (
.git/modules/<name>, which has its own config) have nocommondirfile, so the helper returns the git dir unchanged. Verified both against real git.Tests
Two added to
test/pwd.test.mjs, drivinglocate()over a git-shaped layout built withfs— nogiton PATH required, in keeping with this module staying zero-dependency. One covers the relativecommondirgit actually writes, one the absolute form.git stash push -- src/: both FAIL on unpatched src (3 pass / 2 fail), pass with the fix.node --test: 166 tests, 163 pass, 0 fail, 3 skipped (baseline onmainthis run: 164 / 161 / 0 fail / 3 skipped).