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 b01e5a7ef..ce9271430 100644 --- a/pkg/commands/models/branch.go +++ b/pkg/commands/models/branch.go @@ -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 diff --git a/pkg/commands/models/commit.go b/pkg/commands/models/commit.go index c5b72bf55..3e4201fb6 100644 --- a/pkg/commands/models/commit.go +++ b/pkg/commands/models/commit.go @@ -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 { diff --git a/pkg/gui/controllers/branches_controller.go b/pkg/gui/controllers/branches_controller.go index 37ac90261..a51666ba1 100644 --- a/pkg/gui/controllers/branches_controller.go +++ b/pkg/gui/controllers/branches_controller.go @@ -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 { 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.go b/pkg/integration/tests/branch/detached_head.go index 9a419540b..368087e75 100644 --- a/pkg/integration/tests/branch/detached_head.go +++ b/pkg/integration/tests/branch/detached_head.go @@ -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() diff --git a/pkg/integration/tests/branch/detached_head_hash_and_subject.go b/pkg/integration/tests/branch/detached_head_hash_and_subject.go new file mode 100644 index 000000000..8600aae7d --- /dev/null +++ b/pkg/integration/tests/branch/detached_head_hash_and_subject.go @@ -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$`), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 550d6bae7..7286c20a8 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -70,6 +70,7 @@ var tests = []*components.IntegrationTest{ branch.DeleteRemoteBranchWithDifferentName, branch.DeleteWhileFiltering, branch.DetachedHead, + branch.DetachedHeadHashAndSubject, branch.MergeFastForward, branch.MergeIntoDetachedHead, branch.MergeNonFastForward,