Skip to content

Commit 3ef1693

Browse files
committed
Using symbolic-ref for getting branch and parsing its output
1 parent 0e2e97a commit 3ef1693

File tree

2 files changed

+42
-48
lines changed

2 files changed

+42
-48
lines changed

git/git.go

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

5757
// CurrentBranch reads the checked-out branch for the git repository
5858
func CurrentBranch() (string, error) {
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-
}
59+
refCmd := GitCommand("symbolic-ref", "--quiet", "HEAD")
6860

61+
output, err := run.PrepareCmd(refCmd).Output()
6962
if err == nil {
7063
// Found the branch name
71-
return firstLine(output), nil
64+
return getBranchShortName(output), nil
7265
}
7366

7467
var cmdErr *run.CmdError
@@ -298,3 +291,12 @@ func firstLine(output []byte) string {
298291
}
299292
return string(output)
300293
}
294+
295+
func getBranchShortName(output []byte) string {
296+
branch := firstLine(output)
297+
prefix := "refs/heads/"
298+
if i := strings.Index(branch, prefix); i != -1 {
299+
return branch[i+len(prefix):]
300+
}
301+
return branch
302+
}

git/git_test.go

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,50 +39,43 @@ func Test_UncommittedChangeCount(t *testing.T) {
3939
}
4040

4141
func Test_CurrentBranch(t *testing.T) {
42-
cs, teardown := test.InitCmdStubber()
43-
defer teardown()
44-
45-
expected := "branch-name"
46-
47-
cs.Stub(expected)
48-
49-
result, err := CurrentBranch()
50-
if err != nil {
51-
t.Errorf("got unexpected error: %w", err)
52-
}
53-
if len(cs.Calls) != 1 {
54-
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
42+
type c struct {
43+
Stub string
44+
Expected string
5545
}
56-
if result != expected {
57-
t.Errorf("unexpected branch name: %s instead of %s", result, expected)
46+
cases := []c{
47+
{
48+
Stub: "branch-name",
49+
Expected: "branch-name",
50+
},
51+
{
52+
Stub: "refs/heads/branch-name",
53+
Expected: "branch-name",
54+
},
5855
}
59-
}
6056

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)
57+
for _, v := range cases {
58+
cs, teardown := test.InitCmdStubber()
59+
cs.Stub(v.Stub)
6860

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)
61+
result, err := CurrentBranch()
62+
if err != nil {
63+
t.Errorf("got unexpected error: %w", err)
64+
}
65+
if len(cs.Calls) != 1 {
66+
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
67+
}
68+
if result != v.Expected {
69+
t.Errorf("unexpected branch name: %s instead of %s", result, v.Expected)
70+
}
71+
teardown()
7872
}
7973
}
8074

8175
func Test_CurrentBranch_detached_head(t *testing.T) {
8276
cs, teardown := test.InitCmdStubber()
8377
defer teardown()
8478

85-
cs.StubError("")
8679
cs.StubError("")
8780

8881
_, err := CurrentBranch()
@@ -92,16 +85,15 @@ func Test_CurrentBranch_detached_head(t *testing.T) {
9285
if err != ErrNotOnAnyBranch {
9386
t.Errorf("got unexpected error: %s instead of %s", err, ErrNotOnAnyBranch)
9487
}
95-
if len(cs.Calls) != 2 {
96-
t.Errorf("expected 2 git calls, saw %d", len(cs.Calls))
88+
if len(cs.Calls) != 1 {
89+
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
9790
}
9891
}
9992

10093
func Test_CurrentBranch_unexpected_error(t *testing.T) {
10194
cs, teardown := test.InitCmdStubber()
10295
defer teardown()
10396

104-
cs.StubError("lol")
10597
cs.StubError("lol")
10698

10799
expectedError := "lol\nstub: lol"
@@ -113,8 +105,8 @@ func Test_CurrentBranch_unexpected_error(t *testing.T) {
113105
if err.Error() != expectedError {
114106
t.Errorf("got unexpected error: %s instead of %s", err.Error(), expectedError)
115107
}
116-
if len(cs.Calls) != 2 {
117-
t.Errorf("expected 2 git calls, saw %d", len(cs.Calls))
108+
if len(cs.Calls) != 1 {
109+
t.Errorf("expected 1 git call, saw %d", len(cs.Calls))
118110
}
119111
}
120112

0 commit comments

Comments
 (0)