-
Notifications
You must be signed in to change notification settings - Fork 785
/
testhelpers.go
121 lines (104 loc) · 3.74 KB
/
testhelpers.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
116
117
118
119
120
121
package testhelpers
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
)
// WriteFile creates a file with the specified name underneath the gitRepo directory adding the specified content.
// The file name can be path as well and intermediate directories are created.
func WriteFile(fail func(string, ...int), repoDir string, name string, contents string) {
path := filepath.Join(repoDir, name)
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
log.Logger().Error(err.Error())
fail("unable to create directory")
}
b := []byte(contents)
err = ioutil.WriteFile(path, b, 0644)
if err != nil {
log.Logger().Error(err.Error())
fail("unable to write file content")
}
}
// HeadSha returns the commit SHA of the current HEAD commit within the specified git directory
func HeadSha(fail func(string, ...int), repoDir string) string {
data, err := ioutil.ReadFile(filepath.Join(repoDir, ".git", "HEAD"))
if err != nil {
log.Logger().Error(err.Error())
fail("unable to read file")
}
var sha string
if strings.HasPrefix(string(data), "ref:") {
headRef := strings.TrimPrefix(string(data), "ref: ")
headRef = strings.Trim(headRef, "\n")
sha = ReadRef(fail, repoDir, headRef)
} else {
sha = string(data)
}
return sha
}
// ReadRef reads the commit SHA of the specified ref. Needs to be of the form /refs/heads/<name>.
func ReadRef(fail func(string, ...int), repoDir string, name string) string {
data, err := ioutil.ReadFile(filepath.Join(repoDir, ".git", name))
if err != nil {
log.Logger().Error(err.Error())
fail("unable to read file")
}
return strings.Trim(string(data), "\n")
}
// Add adds all unstaged changes to the index.
func Add(fail func(string, ...int), repoDir string) {
GitCmd(fail, repoDir, "add", ".")
}
// Commit commits all staged changes with the specified commit message.
func Commit(fail func(string, ...int), repoDir string, message string) string {
GitCmd(fail, repoDir, "commit", "-m", message, "--no-gpg-sign")
return HeadSha(fail, repoDir)
}
// Tag creates an annotated tag.
func Tag(fail func(string, ...int), repoDir string, tag string, message string) string {
GitCmd(fail, repoDir, "tag", "-a", "-m", message, tag)
return ReadRef(fail, repoDir, fmt.Sprintf("/refs/tags/%s", tag))
}
// Checkout switches to the specified branch.
func Checkout(fail func(string, ...int), repoDir string, branch string) {
GitCmd(fail, repoDir, "checkout", branch)
}
// Branch creates a new branch with the specified name.
func Branch(fail func(string, ...int), repoDir string, branch string) {
GitCmd(fail, repoDir, "checkout", "-b", branch)
}
// DetachHead puts the repository in a detached head mode.
func DetachHead(fail func(string, ...int), repoDir string) {
head := HeadSha(fail, repoDir)
GitCmd(fail, repoDir, "checkout", head)
}
// Merge merges the specified commits into the current branch
func Merge(fail func(string, ...int), repoDir string, commits ...string) {
args := []string{"merge", "--no-gpg-sign"}
args = append(args, commits...)
GitCmd(fail, repoDir, args...)
}
// Revlist lists commits that are reachable by following the parent links from the given commit
func Revlist(fail func(string, ...int), repoDir string, maxCount int, commit string) string {
args := []string{"rev-list", fmt.Sprintf("--max-count=%d", maxCount), commit}
return GitCmd(fail, repoDir, args...)
}
// GitCmd runs a git command with arguments in the specified git repository
func GitCmd(fail func(string, ...int), repoDir string, args ...string) string {
cmd := util.Command{
Dir: repoDir,
Name: "git",
Args: args,
}
out, err := cmd.RunWithoutRetry()
if err != nil {
log.Logger().Error(err.Error())
fail("unable to write file content")
}
return out
}