Using TrimPrefix for getting current branch and improved tests

This commit is contained in:
Martín Montes 2020-09-30 18:26:22 +02:00
parent 3ef1693881
commit 2a166d1dd7
2 changed files with 7 additions and 7 deletions

View file

@ -294,9 +294,5 @@ func firstLine(output []byte) string {
func getBranchShortName(output []byte) string {
branch := firstLine(output)
prefix := "refs/heads/"
if i := strings.Index(branch, prefix); i != -1 {
return branch[i+len(prefix):]
}
return branch
return strings.TrimPrefix(branch, "refs/heads/")
}

View file

@ -45,13 +45,17 @@ func Test_CurrentBranch(t *testing.T) {
}
cases := []c{
{
Stub: "branch-name",
Stub: "branch-name\n",
Expected: "branch-name",
},
{
Stub: "refs/heads/branch-name",
Stub: "refs/heads/branch-name\n",
Expected: "branch-name",
},
{
Stub: "refs/heads/branch\u00A0with\u00A0non\u00A0breaking\u00A0space\n",
Expected: "branch\u00A0with\u00A0non\u00A0breaking\u00A0space",
},
}
for _, v := range cases {