From 2a166d1dd7635dccac8365ef7ac3286ea1420b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Montes?= Date: Wed, 30 Sep 2020 18:26:22 +0200 Subject: [PATCH] Using TrimPrefix for getting current branch and improved tests --- git/git.go | 6 +----- git/git_test.go | 8 ++++++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/git/git.go b/git/git.go index 99d5880c7..212648095 100644 --- a/git/git.go +++ b/git/git.go @@ -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/") } diff --git a/git/git_test.go b/git/git_test.go index 02b1133d6..34cc9c7cb 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -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 {