From 6eb75978e8c97faa9bb8e75704e34a946a9c04f1 Mon Sep 17 00:00:00 2001 From: rho song Date: Tue, 16 Feb 2021 10:48:38 -0800 Subject: [PATCH 1/3] Use git branch --show-current because it works when there are no commits. --- modules/git/git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/git.go b/modules/git/git.go index 6e0c1e5d4..df0b97e94 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -21,7 +21,7 @@ func GetCurrentBranchName(t testing.TestingT) string { // GetCurrentBranchNameE retrieves the current branch name or // empty string in case of detached state. func GetCurrentBranchNameE(t testing.TestingT) (string, error) { - cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") + cmd := exec.Command("git", "branch", "--show-current") bytes, err := cmd.Output() if err != nil { return "", err From 4503be48c193e21c035e39dc87716512a606b954 Mon Sep 17 00:00:00 2001 From: rho song Date: Fri, 19 Feb 2021 08:55:47 -0800 Subject: [PATCH 2/3] Implement a fallback. --- modules/git/git.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/modules/git/git.go b/modules/git/git.go index df0b97e94..d5c444a7c 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -20,9 +20,38 @@ func GetCurrentBranchName(t testing.TestingT) string { // GetCurrentBranchNameE retrieves the current branch name or // empty string in case of detached state. +// Uses branch --show-current, which was introduced in git v2.22. +// Falls back to rev-parse for users of the older version, like Ubuntu 18.04. func GetCurrentBranchNameE(t testing.TestingT) (string, error) { cmd := exec.Command("git", "branch", "--show-current") bytes, err := cmd.Output() + if err != nil { + cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") + bytes, err := cmd.Output() + if err != nil { + return "", err + } + name := strings.TrimSpace(string(bytes)) + if name == "HEAD" { + return "", nil + } + + return name, nil + } + + name := strings.TrimSpace(string(bytes)) + if name == "HEAD" { + return "", nil + } + + return name, nil +} + +// GetCurrentBranchNameOldE retrieves the current branch name or +// empty string in case of detached state. +func GetCurrentBranchNameOldE(t testing.TestingT) (string, error) { + cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") + bytes, err := cmd.Output() if err != nil { return "", err } From d2a5f73256ac1b859742c09392869d3f64e5ee1d Mon Sep 17 00:00:00 2001 From: rho song Date: Thu, 18 Mar 2021 08:46:44 -0700 Subject: [PATCH 3/3] DRY the logic --- modules/git/git.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/modules/git/git.go b/modules/git/git.go index d5c444a7c..d13594756 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -26,17 +26,7 @@ func GetCurrentBranchNameE(t testing.TestingT) (string, error) { cmd := exec.Command("git", "branch", "--show-current") bytes, err := cmd.Output() if err != nil { - cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") - bytes, err := cmd.Output() - if err != nil { - return "", err - } - name := strings.TrimSpace(string(bytes)) - if name == "HEAD" { - return "", nil - } - - return name, nil + return GetCurrentBranchNameOldE(t) } name := strings.TrimSpace(string(bytes)) @@ -48,7 +38,8 @@ func GetCurrentBranchNameE(t testing.TestingT) (string, error) { } // GetCurrentBranchNameOldE retrieves the current branch name or -// empty string in case of detached state. +// empty string in case of detached state. This uses the older pattern +// of `git rev-parse` rather than `git branch --show-current`. func GetCurrentBranchNameOldE(t testing.TestingT) (string, error) { cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") bytes, err := cmd.Output()