From 61815c10e7e901d31274706040c5c7598dcd0eca Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 27 Jun 2025 14:13:31 +0200 Subject: [PATCH 1/3] Add a test showing undesired branch icon at head commit The icon will appear when there's a tag with the same name as the current branch (that's what we're testing here), or even when there's a remote with the same name. I'm not adding a test for this latter case, but this was actually how I discovered the issue. --- ..._not_show_branch_marker_for_head_commit.go | 34 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 2 files changed, 35 insertions(+) create mode 100644 pkg/integration/tests/commit/do_not_show_branch_marker_for_head_commit.go diff --git a/pkg/integration/tests/commit/do_not_show_branch_marker_for_head_commit.go b/pkg/integration/tests/commit/do_not_show_branch_marker_for_head_commit.go new file mode 100644 index 000000000..70b1cb22e --- /dev/null +++ b/pkg/integration/tests/commit/do_not_show_branch_marker_for_head_commit.go @@ -0,0 +1,34 @@ +package commit + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var DoNotShowBranchMarkerForHeadCommit = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Verify that no branch heads are shown for the branch head if there is a tag with the same name as the branch", + ExtraCmdArgs: []string{}, + Skip: false, + GitVersion: AtLeast("2.38.0"), + SetupConfig: func(config *config.AppConfig) { + config.GetAppState().GitLogShowGraph = "never" + }, + SetupRepo: func(shell *Shell) { + shell.EmptyCommit("one") + shell.NewBranch("branch1") + shell.EmptyCommit("two") + shell.EmptyCommit("three") + shell.CreateLightweightTag("branch1", "master") + + shell.SetConfig("rebase.updateRefs", "true") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + // Check that the local commits view does show a branch marker for the head commit + t.Views().Commits(). + Lines( + Contains("CI * three"), // don't want the star here + Contains("CI two"), + Contains("CI branch1 one"), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index ef26f0223..e1dd36dbf 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -118,6 +118,7 @@ var tests = []*components.IntegrationTest{ commit.CreateTag, commit.DisableCopyCommitMessageBody, commit.DiscardOldFileChanges, + commit.DoNotShowBranchMarkerForHeadCommit, commit.FailHooksThenCommitNoHooks, commit.FindBaseCommitForFixup, commit.FindBaseCommitForFixupDisregardMainBranch, From f95ac6780f83b30d026f539fd1a4e41f26036ca0 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 27 Jun 2025 14:42:59 +0200 Subject: [PATCH 2/3] Cleanup: turn around error condition It is a common go convention for the "happy path" to be as little indented as possible. --- pkg/commands/git_commands/branch.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/commands/git_commands/branch.go b/pkg/commands/git_commands/branch.go index e634b27ec..6333d136e 100644 --- a/pkg/commands/git_commands/branch.go +++ b/pkg/commands/git_commands/branch.go @@ -111,10 +111,11 @@ func (self *BranchCommands) CurrentBranchName() (string, error) { ToArgv() output, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput() - if err == nil { - return strings.TrimSpace(output), nil + if err != nil { + return "", err } - return "", err + + return strings.TrimSpace(output), nil } // LocalDelete delete branch locally From 03abdfcfe2163e9e7f24b074056d2efe6893df60 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 27 Jun 2025 14:13:38 +0200 Subject: [PATCH 3/3] Fix the CurrentBranchName function The function would return "head/branchname" when there was either a tag or a remote with the same name. While fixing this we slightly change the semantic of the function (and of determineCheckedOutBranchName, which calls it): for a detached head it now returns an empty string rather than the commit hash. I actually think this is better. --- pkg/commands/git_commands/branch.go | 8 +++----- pkg/gui/controllers/helpers/refresh_helper.go | 2 +- .../commit/do_not_show_branch_marker_for_head_commit.go | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/pkg/commands/git_commands/branch.go b/pkg/commands/git_commands/branch.go index 6333d136e..c5c81e223 100644 --- a/pkg/commands/git_commands/branch.go +++ b/pkg/commands/git_commands/branch.go @@ -102,12 +102,10 @@ func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) { }, nil } -// CurrentBranchName get name of current branch +// CurrentBranchName get name of current branch. Returns empty string if HEAD is detached. func (self *BranchCommands) CurrentBranchName() (string, error) { - cmdArgs := NewGitCmd("rev-parse"). - Arg("--abbrev-ref"). - Arg("--verify"). - Arg("HEAD"). + cmdArgs := NewGitCmd("branch"). + Arg("--show-current"). ToArgv() output, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput() diff --git a/pkg/gui/controllers/helpers/refresh_helper.go b/pkg/gui/controllers/helpers/refresh_helper.go index 0e19149c1..2e0e157e3 100644 --- a/pkg/gui/controllers/helpers/refresh_helper.go +++ b/pkg/gui/controllers/helpers/refresh_helper.go @@ -308,7 +308,7 @@ func (self *RefreshHelper) determineCheckedOutBranchName() string { // In all other cases, get the branch name by asking git what branch is // checked out. Note that if we're on a detached head (for reasons other // than rebasing or bisecting, i.e. it was explicitly checked out), then - // this will return its hash. + // this will return an empty string. if branchName, err := self.c.Git().Branch.CurrentBranchName(); err == nil { return branchName } diff --git a/pkg/integration/tests/commit/do_not_show_branch_marker_for_head_commit.go b/pkg/integration/tests/commit/do_not_show_branch_marker_for_head_commit.go index 70b1cb22e..54b988ef4 100644 --- a/pkg/integration/tests/commit/do_not_show_branch_marker_for_head_commit.go +++ b/pkg/integration/tests/commit/do_not_show_branch_marker_for_head_commit.go @@ -26,7 +26,7 @@ var DoNotShowBranchMarkerForHeadCommit = NewIntegrationTest(NewIntegrationTestAr // Check that the local commits view does show a branch marker for the head commit t.Views().Commits(). Lines( - Contains("CI * three"), // don't want the star here + Contains("CI three"), Contains("CI two"), Contains("CI branch1 one"), )