Skip to content

Commit 718cbdb

Browse files
authored
Fix branch head icon appearing at head commit when a remote or tag exists with the same name as the current branch (#4669)
- **PR Description** Fix the problem that if the `rebase.updateRefs` git config is on, a branch icon would appear in the commits list for the head commit as soon as a remote or tag exists whose name is the same as the name of the current branch.
2 parents 7a89372 + 03abdfc commit 718cbdb

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

pkg/commands/git_commands/branch.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,18 @@ func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) {
102102
}, nil
103103
}
104104

105-
// CurrentBranchName get name of current branch
105+
// CurrentBranchName get name of current branch. Returns empty string if HEAD is detached.
106106
func (self *BranchCommands) CurrentBranchName() (string, error) {
107-
cmdArgs := NewGitCmd("rev-parse").
108-
Arg("--abbrev-ref").
109-
Arg("--verify").
110-
Arg("HEAD").
107+
cmdArgs := NewGitCmd("branch").
108+
Arg("--show-current").
111109
ToArgv()
112110

113111
output, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
114-
if err == nil {
115-
return strings.TrimSpace(output), nil
112+
if err != nil {
113+
return "", err
116114
}
117-
return "", err
115+
116+
return strings.TrimSpace(output), nil
118117
}
119118

120119
// LocalDelete delete branch locally

pkg/gui/controllers/helpers/refresh_helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func (self *RefreshHelper) determineCheckedOutBranchName() string {
308308
// In all other cases, get the branch name by asking git what branch is
309309
// checked out. Note that if we're on a detached head (for reasons other
310310
// than rebasing or bisecting, i.e. it was explicitly checked out), then
311-
// this will return its hash.
311+
// this will return an empty string.
312312
if branchName, err := self.c.Git().Branch.CurrentBranchName(); err == nil {
313313
return branchName
314314
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package commit
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var DoNotShowBranchMarkerForHeadCommit = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Verify that no branch heads are shown for the branch head if there is a tag with the same name as the branch",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
GitVersion: AtLeast("2.38.0"),
13+
SetupConfig: func(config *config.AppConfig) {
14+
config.GetAppState().GitLogShowGraph = "never"
15+
},
16+
SetupRepo: func(shell *Shell) {
17+
shell.EmptyCommit("one")
18+
shell.NewBranch("branch1")
19+
shell.EmptyCommit("two")
20+
shell.EmptyCommit("three")
21+
shell.CreateLightweightTag("branch1", "master")
22+
23+
shell.SetConfig("rebase.updateRefs", "true")
24+
},
25+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
26+
// Check that the local commits view does show a branch marker for the head commit
27+
t.Views().Commits().
28+
Lines(
29+
Contains("CI three"),
30+
Contains("CI two"),
31+
Contains("CI branch1 one"),
32+
)
33+
},
34+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ var tests = []*components.IntegrationTest{
118118
commit.CreateTag,
119119
commit.DisableCopyCommitMessageBody,
120120
commit.DiscardOldFileChanges,
121+
commit.DoNotShowBranchMarkerForHeadCommit,
121122
commit.FailHooksThenCommitNoHooks,
122123
commit.FindBaseCommitForFixup,
123124
commit.FindBaseCommitForFixupDisregardMainBranch,

0 commit comments

Comments
 (0)