diff --git a/pkg/commands/git_commands/branch.go b/pkg/commands/git_commands/branch.go index 92d69c06d..d36cd20ff 100644 --- a/pkg/commands/git_commands/branch.go +++ b/pkg/commands/git_commands/branch.go @@ -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 } } diff --git a/pkg/commands/git_commands/branch_loader.go b/pkg/commands/git_commands/branch_loader.go index 6cb61754f..0dd2178e0 100644 --- a/pkg/commands/git_commands/branch_loader.go +++ b/pkg/commands/git_commands/branch_loader.go @@ -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) diff --git a/pkg/commands/git_commands/branch_test.go b/pkg/commands/git_commands/branch_test.go index 97e5ba544..ae19ce159 100644 --- a/pkg/commands/git_commands/branch_test.go +++ b/pkg/commands/git_commands/branch_test.go @@ -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) diff --git a/pkg/commands/models/branch.go b/pkg/commands/models/branch.go index 176c8462d..ce9271430 100644 --- a/pkg/commands/models/branch.go +++ b/pkg/commands/models/branch.go @@ -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 diff --git a/pkg/gui/presentation/branches.go b/pkg/gui/presentation/branches.go index 75abd640f..016c2f71d 100644 --- a/pkg/gui/presentation/branches.go +++ b/pkg/gui/presentation/branches.go @@ -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 != "" { diff --git a/pkg/integration/tests/branch/detached_head_hash_and_subject.go b/pkg/integration/tests/branch/detached_head_hash_and_subject.go index e0c729b89..8600aae7d 100644 --- a/pkg/integration/tests/branch/detached_head_hash_and_subject.go +++ b/pkg/integration/tests/branch/detached_head_hash_and_subject.go @@ -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$`), ) },