Show the hash and subject of a detached HEAD in the branches panel (#6220)

For a detached HEAD, the row in the branches panel was missing
information. Its hash column was empty if `gui.showBranchCommitHash` was
on or the panel was in half or full screen mode. In half or full screen
mode, the row also had no commit subject. Both are shown now.

Pressing `n` on that row opened a prompt titled "New branch name (branch
is off of '<40-character hash>')". The prompt now shows the short hash
and subject instead, the same as when you press `n` on the commit in the
commits panel.
This commit is contained in:
Stefan Haller authored and GitHub committed 2026-11-14 15:49:47 +01:00
commit c63f9b024c
10 files changed
+62 -15

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)
+3 -2
View File
@@ -9,7 +9,7 @@ import (
// duplicating this for now
type Branch struct {
Name string
// the displayname is something like '(HEAD detached at 123asdf)', whereas in that case the name would be '123asdf'
// the displayname is something like '(HEAD detached at 123asdf)', whereas in that case the name is the full hash of the commit
DisplayName string
// indicator of when the branch was last checked out e.g. '2d', '3m'
Recency string
@@ -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
+7 -1
View File
@@ -151,7 +151,13 @@ func (c *Commit) ID() string {
}
func (c *Commit) Description() string {
return fmt.Sprintf("%s %s", c.Hash()[:7], c.Name)
return CommitDescription(c.Hash(), c.Name)
}
// CommitDescription returns the short hash and the subject of a commit, e.g.
// "6f71c57 Fix the thing".
func CommitDescription(hash string, subject string) string {
return fmt.Sprintf("%s %s", hash[:min(7, len(hash))], subject)
}
func (c *Commit) IsMerge() bool {
+6 -1
View File
@@ -746,7 +746,12 @@ func (self *BranchesController) rename(branch *models.Branch) error {
}
func (self *BranchesController) newBranch(selectedBranch *models.Branch) error {
return self.c.Helpers().Refs.NewBranch(selectedBranch.FullRefName(), selectedBranch.RefName(), "")
fromName := selectedBranch.RefName()
if selectedBranch.DetachedHead {
// The RefName of a detached HEAD is the full hash of its commit
fromName = models.CommitDescription(selectedBranch.Name, selectedBranch.Subject)
}
return self.c.Helpers().Refs.NewBranch(selectedBranch.FullRefName(), fromName, "")
}
func (self *BranchesController) createPullRequestMenu(selectedBranch *models.Branch, checkedOutBranch *models.Branch) error {
+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 != "" {
@@ -25,7 +25,7 @@ var DetachedHead = NewIntegrationTest(NewIntegrationTestArgs{
Press(keys.Universal.New)
t.ExpectPopup().Prompt().
Title(MatchesRegexp(`^New branch name \(branch is off of '[0-9a-f]+'\)$`)).
Title(MatchesRegexp(`^New branch name \(branch is off of '[0-9a-f]{7} commit-09'\)$`)).
Type("new-branch").
Confirm()
@@ -0,0 +1,27 @@
package branch
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var DetachedHeadHashAndSubject = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Show the commit hash and subject of a detached HEAD in the branches panel",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.
CreateNCommits(3).
Checkout("HEAD^")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Branches().
Focus().
Press(keys.Universal.NextScreenMode).
Lines(
MatchesRegexp(`^ \* [0-9a-f]{8} \(HEAD detached at [0-9a-f]+\) {3}commit-02$`).IsSelected(),
MatchesRegexp(`[0-9a-f]{8} master +commit-03$`),
)
},
})
+1
View File
@@ -70,6 +70,7 @@ var tests = []*components.IntegrationTest{
branch.DeleteRemoteBranchWithDifferentName,
branch.DeleteWhileFiltering,
branch.DetachedHead,
branch.DetachedHeadHashAndSubject,
branch.MergeFastForward,
branch.MergeIntoDetachedHead,
branch.MergeNonFastForward,