Resolve a relative gitdir against the repo it belongs to

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Stefan Haller 2026-09-14 10:27:46 +02:00
parent 00c0c4c061
commit 93713b5401
3 changed files with 10 additions and 12 deletions

View file

@ -88,7 +88,16 @@ func gitDirOfRepo(repoPath string) (string, bool) {
if err != nil {
return "", false
}
return strings.CutPrefix(strings.TrimSpace(string(content)), "gitdir: ")
gitDir, ok := strings.CutPrefix(strings.TrimSpace(string(content)), "gitdir: ")
if !ok {
return "", false
}
// A relative name is relative to the repo. Git writes one for a submodule,
// and for a worktree created with --relative-paths.
if !filepath.IsAbs(gitDir) {
gitDir = filepath.Join(repoPath, gitDir)
}
return gitDir, true
}
// readHeadInfo reads the HEAD file of the repo at repoPath to find out what it

View file

@ -52,13 +52,9 @@ func TestReadHeadInfo(t *testing.T) {
"repo/.git/worktrees/wt/HEAD": "ref: refs/heads/mybranch\n",
"wt/.git": "gitdir: ../repo/.git/worktrees/wt\n",
},
/* EXPECTED:
repoPath: "wt",
expected: headInfo{branch: "mybranch"},
expectedOk: true,
ACTUAL: */
repoPath: "wt",
expectedOk: false,
},
{
name: "submodule, whose .git file always names the git dir relatively",
@ -66,13 +62,9 @@ func TestReadHeadInfo(t *testing.T) {
"repo/.git/modules/sub/HEAD": "ref: refs/heads/mybranch\n",
"repo/sub/.git": "gitdir: ../.git/modules/sub\n",
},
/* EXPECTED:
repoPath: "repo/sub",
expected: headInfo{branch: "mybranch"},
expectedOk: true,
ACTUAL: */
repoPath: "repo/sub",
expectedOk: false,
},
{
name: "directory without a .git entry",

View file

@ -38,10 +38,7 @@ var RecentReposBranchColumn = NewIntegrationTest(NewIntegrationTestArgs{
Lines(
Contains("on-branch").Contains("master").IsSelected(),
Contains("detached").MatchesRegexp(`HEAD detached at [0-9a-f]{8}`),
/* EXPECTED:
Contains("sub").MatchesRegexp(`HEAD detached at [0-9a-f]{8}`),
ACTUAL: */
Contains("sub").Contains("Branch unknown"),
Contains("Cancel"),
).
Cancel()