/
git.go
115 lines (98 loc) · 2.93 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Package git allows to interact with Git.
package git
import (
"os/exec"
"strings"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)
// GetCurrentBranchName retrieves the current branch name or
// empty string in case of detached state.
func GetCurrentBranchName(t testing.TestingT) string {
out, err := GetCurrentBranchNameE(t)
if err != nil {
t.Fatal(err)
}
return out
}
// 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 {
return GetCurrentBranchNameOldE(t)
}
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. 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()
if err != nil {
return "", err
}
name := strings.TrimSpace(string(bytes))
if name == "HEAD" {
return "", nil
}
return name, nil
}
// GetCurrentGitRef retrieves current branch name, lightweight (non-annotated) tag or
// if tag points to the commit exact tag value.
func GetCurrentGitRef(t testing.TestingT) string {
out, err := GetCurrentGitRefE(t)
if err != nil {
t.Fatal(err)
}
return out
}
// GetCurrentGitRefE retrieves current branch name, lightweight (non-annotated) tag or
// if tag points to the commit exact tag value.
func GetCurrentGitRefE(t testing.TestingT) (string, error) {
out, err := GetCurrentBranchNameE(t)
if err != nil {
return "", err
}
if out != "" {
return out, nil
}
out, err = GetTagE(t)
if err != nil {
return "", err
}
return out, nil
}
// GetTagE retrieves lightweight (non-annotated) tag or if tag points
// to the commit exact tag value.
func GetTagE(t testing.TestingT) (string, error) {
cmd := exec.Command("git", "describe", "--tags")
bytes, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(bytes)), nil
}
// GetRepoRoot retrieves the path to the root directory of the repo. This fails the test if there is an error.
func GetRepoRoot(t testing.TestingT) string {
out, err := GetRepoRootE(t)
require.NoError(t, err)
return out
}
// GetRepoRootE retrieves the path to the root directory of the repo.
func GetRepoRootE(t testing.TestingT) (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
bytes, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(bytes)), nil
}