Skip to content

Commit 2a166d1

Browse files
committed
Using TrimPrefix for getting current branch and improved tests
1 parent 3ef1693 commit 2a166d1

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

git/git.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,5 @@ func firstLine(output []byte) string {
294294

295295
func getBranchShortName(output []byte) string {
296296
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
297+
return strings.TrimPrefix(branch, "refs/heads/")
302298
}

git/git_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ func Test_CurrentBranch(t *testing.T) {
4545
}
4646
cases := []c{
4747
{
48-
Stub: "branch-name",
48+
Stub: "branch-name\n",
4949
Expected: "branch-name",
5050
},
5151
{
52-
Stub: "refs/heads/branch-name",
52+
Stub: "refs/heads/branch-name\n",
5353
Expected: "branch-name",
5454
},
55+
{
56+
Stub: "refs/heads/branch\u00A0with\u00A0non\u00A0breaking\u00A0space\n",
57+
Expected: "branch\u00A0with\u00A0non\u00A0breaking\u00A0space",
58+
},
5559
}
5660

5761
for _, v := range cases {

0 commit comments

Comments
 (0)