X Tutup
Skip to content

Commit 0e2e97a

Browse files
committed
Using git branch --show-current for getting the current branch. Fallback to symbolic-ref
1 parent b987cfb commit 0e2e97a

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

git/git.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,16 @@ func ShowRefs(ref ...string) ([]Ref, error) {
5656

5757
// CurrentBranch reads the checked-out branch for the git repository
5858
func CurrentBranch() (string, error) {
59-
refCmd := GitCommand("symbolic-ref", "--quiet", "--short", "HEAD")
59+
// Available from Git 2.22 and above
60+
branchCmd := GitCommand("branch", "--show-current")
61+
output, err := run.PrepareCmd(branchCmd).Output()
62+
63+
// Fallback to symbolic-ref
64+
if err != nil {
65+
refCmd := GitCommand("symbolic-ref", "--quiet", "--short", "HEAD")
66+
output, err = run.PrepareCmd(refCmd).Output()
67+
}
6068

61-
output, err := run.PrepareCmd(refCmd).Output()
6269
if err == nil {
6370
// Found the branch name
6471
return firstLine(output), nil

git/git_test.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,31 @@ func Test_CurrentBranch(t *testing.T) {
5858
}
5959
}
6060

61+
func Test_CurrentBranch_show_current_error(t *testing.T) {
62+
cs, teardown := test.InitCmdStubber()
63+
defer teardown()
64+
65+
cs.StubError("")
66+
expected := "branch-name"
67+
cs.Stub(expected)
68+
69+
result, err := CurrentBranch()
70+
if err != nil {
71+
t.Errorf("got unexpected error: %w", err)
72+
}
73+
if len(cs.Calls) != 2 {
74+
t.Errorf("expected 2 git calls, saw %d", len(cs.Calls))
75+
}
76+
if result != expected {
77+
t.Errorf("unexpected branch name: %s instead of %s", result, expected)
78+
}
79+
}
80+
6181
func Test_CurrentBranch_detached_head(t *testing.T) {
6282
cs, teardown := test.InitCmdStubber()
6383
defer teardown()
6484

85+
cs.StubError("")
6586
cs.StubError("")
6687

6788
_, err := CurrentBranch()
@@ -71,15 +92,16 @@ func Test_CurrentBranch_detached_head(t *testing.T) {
7192
if err != ErrNotOnAnyBranch {
7293
t.Errorf("got unexpected error: %s instead of %s", err, ErrNotOnAnyBranch)
7394
}
74-
if len(cs.Calls) != 1 {
75-
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
95+
if len(cs.Calls) != 2 {
96+
t.Errorf("expected 2 git calls, saw %d", len(cs.Calls))
7697
}
7798
}
7899

79100
func Test_CurrentBranch_unexpected_error(t *testing.T) {
80101
cs, teardown := test.InitCmdStubber()
81102
defer teardown()
82103

104+
cs.StubError("lol")
83105
cs.StubError("lol")
84106

85107
expectedError := "lol\nstub: lol"
@@ -91,8 +113,8 @@ func Test_CurrentBranch_unexpected_error(t *testing.T) {
91113
if err.Error() != expectedError {
92114
t.Errorf("got unexpected error: %s instead of %s", err.Error(), expectedError)
93115
}
94-
if len(cs.Calls) != 1 {
95-
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
116+
if len(cs.Calls) != 2 {
117+
t.Errorf("expected 2 git calls, saw %d", len(cs.Calls))
96118
}
97119
}
98120

0 commit comments

Comments
 (0)
X Tutup