Skip to content

Commit 03abdfc

Browse files
committed
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.
1 parent f95ac67 commit 03abdfc

File tree

3 files changed

+5
-7
lines changed

3 files changed

+5
-7
lines changed

pkg/commands/git_commands/branch.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,10 @@ 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()

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
}

pkg/integration/tests/commit/do_not_show_branch_marker_for_head_commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var DoNotShowBranchMarkerForHeadCommit = NewIntegrationTest(NewIntegrationTestAr
2626
// Check that the local commits view does show a branch marker for the head commit
2727
t.Views().Commits().
2828
Lines(
29-
Contains("CI * three"), // don't want the star here
29+
Contains("CI three"),
3030
Contains("CI two"),
3131
Contains("CI branch1 one"),
3232
)

0 commit comments

Comments
 (0)