Show the hash and subject of a detached HEAD in the branches panel

In the branches panel, the row of a detached HEAD has an empty hash
column if gui.showBranchCommitHash is on. In half or full screen mode,
it also has no subject. The branch loader creates this row without the
hash and subject of the commit.

Ask git for the subject too. Show the Name of the row in the hash
column; for a detached HEAD, the Name is the full hash. Leave the
row's CommitHash empty. Otherwise, code that looks for branches at a
commit would take the detached HEAD for a branch. For example, the
checkout menu of the HEAD commit would offer to check it out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Stefan HallerandClaude Opus 5.5 committed 2026-11-14 15:34:44 +01:00
1 parent 8fd0684c56
commit 9dcef62ff2
6 files changed
+19 -14

No files matched your search

+4 -2
View File
@@ -78,7 +78,7 @@ func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) {
}
output, err := self.cmd.New(
NewGitCmd("branch").
Arg("--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)").
Arg("--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)%00%(contents:subject)").
ToArgv(),
).DontLog().RunWithOutput()
if err != nil {
@@ -86,13 +86,15 @@ func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) {
}
for _, line := range utils.SplitLines(output) {
split := strings.Split(strings.TrimRight(line, "\r\n"), "\x00")
if len(split) == 3 && split[0] == "*" {
if len(split) == 4 && split[0] == "*" {
hash := split[1]
displayName := split[2]
subject := split[3]
return BranchInfo{
RefName: hash,
DisplayName: displayName,
DetachedHead: true,
Subject: subject,
}, nil
}
}
+3 -1
View File
@@ -36,6 +36,8 @@ type BranchInfo struct {
RefName string
DisplayName string // e.g. '(HEAD detached at 123asdf)'
DetachedHead bool
// The subject of the commit that a detached HEAD points at
Subject string
}
// BranchLoader returns a list of Branch objects for the current repo
@@ -129,7 +131,7 @@ func (self *BranchLoader) Load(reflogCommits []*models.Commit,
if err != nil {
return nil, err
}
branches = utils.Prepend(branches, &models.Branch{Name: info.RefName, DisplayName: info.DisplayName, Head: true, DetachedHead: info.DetachedHead, Recency: " *"})
branches = utils.Prepend(branches, &models.Branch{Name: info.RefName, DisplayName: info.DisplayName, Head: true, DetachedHead: info.DetachedHead, Subject: info.Subject, Recency: " *"})
}
configBranches := self.config.Branches(self.cmd)
+8 -6
View File
@@ -271,13 +271,14 @@ func TestBranchCurrentBranchInfo(t *testing.T) {
"falls back to git `git branch --points-at=HEAD` if symbolic-ref fails",
oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"symbolic-ref", "--short", "HEAD"}, "", errors.New("error")).
ExpectGitArgs([]string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)"},
"*\x006f71c57a8d4bd6c11399c3f55f42c815527a73a4\x00(HEAD detached at 6f71c57a)\n", nil),
ExpectGitArgs([]string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)%00%(contents:subject)"},
"*\x006f71c57a8d4bd6c11399c3f55f42c815527a73a4\x00(HEAD detached at 6f71c57a)\x00Some subject\n", nil),
func(info BranchInfo, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "6f71c57a8d4bd6c11399c3f55f42c815527a73a4", info.RefName)
assert.EqualValues(t, "(HEAD detached at 6f71c57a)", info.DisplayName)
assert.True(t, info.DetachedHead)
assert.EqualValues(t, "Some subject", info.Subject)
},
},
{
@@ -285,22 +286,23 @@ func TestBranchCurrentBranchInfo(t *testing.T) {
oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"symbolic-ref", "--short", "HEAD"}, "", errors.New("error")).
ExpectGitArgs(
[]string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)"},
"*\x00679b0456f3db7c505b398def84e7d023e5b55a8d\x00(头指针在 679b0456 分离)\n"+
" \x00679b0456f3db7c505b398def84e7d023e5b55a8d\x00refs/heads/master\n",
[]string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)%00%(contents:subject)"},
"*\x00679b0456f3db7c505b398def84e7d023e5b55a8d\x00(头指针在 679b0456 分离)\x00一些主题\n"+
" \x00679b0456f3db7c505b398def84e7d023e5b55a8d\x00refs/heads/master\x00一些主题\n",
nil),
func(info BranchInfo, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "679b0456f3db7c505b398def84e7d023e5b55a8d", info.RefName)
assert.EqualValues(t, "(头指针在 679b0456 分离)", info.DisplayName)
assert.True(t, info.DetachedHead)
assert.EqualValues(t, "一些主题", info.Subject)
},
},
{
"bubbles up error if there is one",
oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"symbolic-ref", "--short", "HEAD"}, "", errors.New("error")).
ExpectGitArgs([]string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)"}, "", errors.New("error")),
ExpectGitArgs([]string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)%00%(contents:subject)"}, "", errors.New("error")),
func(info BranchInfo, err error) {
assert.Error(t, err)
assert.EqualValues(t, "", info.RefName)
+2 -1
View File
@@ -40,7 +40,8 @@ type Branch struct {
PushBranch string
// subject line in commit message
Subject string
// commit hash
// commit hash; empty for a detached HEAD, whose Name is the hash. Code that
// looks for branches at a commit relies on this.
CommitHash string
// How far we have fallen behind our base branch. 0 means either not
+2 -1
View File
@@ -162,7 +162,8 @@ func getBranchDisplayStrings(
res = append(res, coloredPrIcon)
if showCommitHash {
res = append(res, utils.ShortHash(b.CommitHash))
// The CommitHash of a detached HEAD is empty, but its Name is the hash
res = append(res, utils.ShortHash(lo.Ternary(b.DetachedHead, b.Name, b.CommitHash)))
}
if divergence != "" {
@@ -20,10 +20,7 @@ var DetachedHeadHashAndSubject = NewIntegrationTest(NewIntegrationTestArgs{
Focus().
Press(keys.Universal.NextScreenMode).
Lines(
/* EXPECTED:
MatchesRegexp(`^ \* [0-9a-f]{8} \(HEAD detached at [0-9a-f]+\) {3}commit-02$`).IsSelected(),
ACTUAL: */
MatchesRegexp(`^ \* {10}\(HEAD detached at [0-9a-f]+\) {3}$`).IsSelected(),
MatchesRegexp(`[0-9a-f]{8} master +commit-03$`),
)
},