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