-
Notifications
You must be signed in to change notification settings - Fork 9
/
git.go
234 lines (178 loc) · 5.75 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Package git provides basic git client functionality
package git
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"strings"
)
var (
repoParentDir string
username string
password string
gitName string
gitEmail string
)
type Output struct {
Buffer string
}
func (w *Output) Write(b []byte) (int, error) {
w.Buffer = w.Buffer + string(b)
return len(b), nil
}
func (o *Output) String() string {
return o.Buffer
}
func init() {
tmpDirOverride := os.Getenv("TMPDIR")
if len(tmpDirOverride) > 0 {
repoParentDir = tmpDirOverride
} else {
repoParentDir = os.TempDir()
}
username = os.Getenv("GITHUB_USERNAME")
password = os.Getenv("GITHUB_PASSWORD")
gitName = os.Getenv("GIT_USER")
gitEmail = os.Getenv("GIT_EMAIL")
if len(gitName) < 1 {
gitName = username
}
if len(gitEmail) < 1 {
gitEmail = fmt.Sprintf("%s@users.noreply.github.com", gitName)
}
}
func GetName() string {
return gitName
}
func GetEmail() string {
return gitEmail
}
func GenerateCloneURL(repositoryFullName string) string {
return fmt.Sprintf("https://%s:%s@github.com/%s.git", username, password, repositoryFullName)
}
func Exists(repositoryPath string) bool {
_, err := os.Stat(repositoryPath)
return !os.IsNotExist(err)
}
func GetRepositoryFilePath(name string) string {
return path.Join(repoParentDir, name)
}
// Clone executes a git clone command on the system. It returns the path to the repository on the system.
func Clone(repositoryUrl string) (string, error) {
orgName := extractOrgFromURL(repositoryUrl)
repoName := extractRepoNameFromURL(repositoryUrl)
repositoryPath := path.Join(repoParentDir, orgName, repoName)
log.Println("git.clone.started:", repositoryPath)
cmd := exec.Command("git", "clone", repositoryUrl, repositoryPath)
if err := cmd.Run(); err != nil {
log.Println("git.clone.failed:", repositoryPath, err.Error())
return "", err
}
log.Println("git.clone.finished:", repositoryPath)
return repositoryPath, nil
}
// Calls git fetch inside repository path
func Fetch(repositoryPath string) error {
log.Println("git.fetch.started:", repositoryPath)
cmd := exec.Command("git", "fetch", "origin")
cmd.Dir = path.Join(repositoryPath)
if err := cmd.Run(); err != nil {
log.Println("git.fetch.failed:", repositoryPath, err.Error())
return err
}
log.Println("git.fetch.finished:", repositoryPath)
return nil
}
// Checks out a given git ref inside repository path
func Checkout(repositoryPath, gitRef string) error {
log.Println("git.checkout.started:", repositoryPath, gitRef)
cmd := exec.Command("git", "checkout", gitRef)
cmd.Dir = path.Join(repositoryPath)
if err := cmd.Run(); err != nil {
log.Println("git.checkout.failed:", repositoryPath, err.Error())
return err
}
log.Println("git.checkout.finished:", repositoryPath, gitRef)
return nil
}
// Does hard reset inside repository path
func Reset(repositoryPath, branch string) error {
log.Println("git.reset.started:", repositoryPath, branch)
cmd := exec.Command("git", "reset", "--hard", branch)
cmd.Dir = path.Join(repositoryPath)
if err := cmd.Run(); err != nil {
log.Println("git.reset.failed:", repositoryPath, err.Error())
return err
}
log.Println("git.reset.finished:", repositoryPath, branch)
return nil
}
// Rebases branch with baseBranch inside repository path
func Rebase(repositoryPath, baseBranch string) error {
cmdOutput := &Output{Buffer: ""}
log.Println("git.rebase.started:", repositoryPath, baseBranch)
cmd := exec.Command("git", "rebase", baseBranch)
cmd.Dir = path.Join(repositoryPath)
cmd.Stderr = cmdOutput
cmd.Stdout = cmdOutput
if err := cmd.Run(); err != nil {
log.Printf("git.rebase.failed repo: %s, err: %s \n", repositoryPath, err.Error())
log.Printf("git.rebase.abort.started repo: %s, err: %s, stderr: %s \n", repositoryPath, err.Error(), cmdOutput.String())
abortCmd := exec.Command("git", "rebase", "--abort")
abortCmd.Dir = path.Join(repositoryPath)
if err := abortCmd.Run(); err != nil {
log.Println("git.rebase.abort.failed:", repositoryPath)
} else {
log.Println("git.rebase.abort.finished:", repositoryPath)
}
return err
}
log.Println("git.rebase.finished:", repositoryPath, baseBranch)
return nil
}
// Light wrapper around os/exec.Command + logging
func Prune(repositoryPath string) error {
log.Println("git.remote.prune.started:", repositoryPath)
cmd := exec.Command("git", "remote", "prune", "origin")
cmd.Dir = path.Join(repositoryPath)
if err := cmd.Run(); err != nil {
log.Println("git.remote.prune.failed:", repositoryPath, err.Error())
return err
}
log.Println("git.remote.prune.finished:", repositoryPath)
return nil
}
// Pushes branch back to origin
func Push(repositoryPath, branch string) error {
log.Println("git.push.started:", repositoryPath, branch)
cmd := exec.Command("git", "push", "--force", "origin", branch)
cmd.Dir = path.Join(repositoryPath)
if err := cmd.Run(); err != nil {
log.Println("git.push.failed:", repositoryPath, err.Error())
return err
}
log.Println("git.push.finished:", repositoryPath, branch)
return nil
}
func Config(repositoryPath, configKey, configValue string) error {
log.Printf("git.config.started: %s=%s\n", configKey, configValue)
cmd := exec.Command("git", "config", configKey, configValue)
cmd.Dir = path.Join(repositoryPath)
if err := cmd.Run(); err != nil {
log.Println("git.config.failed:", err.Error())
return err
}
log.Printf("git.config.finished: %s=%s\n", configKey, configValue)
return nil
}
func extractOrgFromURL(githubURL string) string {
splitBySlash := strings.Split(githubURL, "/")
return splitBySlash[len(splitBySlash)-2]
}
func extractRepoNameFromURL(githubURL string) string {
splitBySlash := strings.Split(githubURL, "/")
repoNameWithExt := splitBySlash[len(splitBySlash)-1]
return strings.Split(repoNameWithExt, ".")[0]
}