-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.go
415 lines (350 loc) · 8.79 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) 2017-2019 The Fonero developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package gitbe
import (
"bufio"
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
// gitError contains all the components of a git invocation.
type gitError struct {
cmd []string
stdout []string
stderr []string
}
// log pretty prints a gitError.
func (e gitError) log() {
var cmd string
for _, v := range e.cmd {
cmd += v + " "
}
log.Infof("Git command : %v", cmd)
s := "Git stdout :"
for _, v := range e.stdout {
log.Infof("%v %v", s, v)
s = ""
}
s = "Git stderr :"
for _, v := range e.stderr {
log.Infof("%v %v", s, v)
s = ""
}
}
func outputReader(r io.Reader) ([]string, error) {
rv := make([]string, 0, 128)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
rv = append(rv, scanner.Text())
}
return rv, scanner.Err()
}
// git excutes the git command using the provided arguments. If the path
// argument is set it'll be copied to the GIT_DIR environment variable.
func (g *gitBackEnd) git(path string, args ...string) ([]string, error) {
if len(args) == 0 {
return nil, fmt.Errorf("git requires arguments")
}
// Setup gitError
ge := gitError{
cmd: make([]string, 0, len(args)+1),
stdout: make([]string, 0, 128),
stderr: make([]string, 0, 128),
}
ge.cmd = append(ge.cmd, g.gitPath)
ge.cmd = append(ge.cmd, args...)
if g.gitTrace {
defer func() { ge.log() }()
}
// Execute git command
var stdout, stderr bytes.Buffer
cmd := exec.Command(g.gitPath, args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// Determine if we need to set GIT_DIR
if path != "" {
cmd.Dir = path
}
doneError := cmd.Run()
// Prepare output
var err error
ge.stdout, err = outputReader(bytes.NewReader(stdout.Bytes()))
if err != nil {
log.Errorf("git stdout scanner error: %v", err)
}
ge.stderr, err = outputReader(bytes.NewReader(stderr.Bytes()))
if err != nil {
log.Errorf("git stderr scanner error: %v", err)
}
return ge.stdout, doneError
}
// gitVersion returns the version of git.
func (g *gitBackEnd) gitVersion() (string, error) {
out, err := g.git("", "version")
if err != nil {
return "", err
}
if len(out) != 1 {
return "", fmt.Errorf("unexpected git output")
}
return out[0], nil
}
func (g *gitBackEnd) gitHasChanges(path string) (rv bool) {
if _, err := g.git(path, "diff", "--quiet"); err != nil {
rv = true
} else if _, err := g.git(path, "diff", "--cached",
"--quiet"); err != nil {
rv = true
}
return rv
}
func (g *gitBackEnd) gitDiff(path string) ([]string, error) {
return g.git(path, "diff")
}
func (g *gitBackEnd) gitStash(path string) error {
_, err := g.git(path, "stash")
return err
}
func (g *gitBackEnd) gitStashDrop(path string) error {
_, err := g.git(path, "stash", "drop")
return err
}
func (g *gitBackEnd) gitRm(path, filename string, force bool) error {
var err error
if force {
_, err = g.git(path, "rm", "-f", filename)
} else {
_, err = g.git(path, "rm", filename)
}
return err
}
func (g *gitBackEnd) gitAdd(path, filename string) error {
_, err := g.git(path, "add", filename)
return err
}
func (g *gitBackEnd) gitCommit(path, message string) error {
_, err := g.git(path, "commit", "-m", message)
return err
}
func (g *gitBackEnd) gitCheckout(path, branch string) error {
_, err := g.git(path, "checkout", branch)
return err
}
func (g *gitBackEnd) gitBranchDelete(path, branch string) error {
_, err := g.git(path, "branch", "-D", branch)
return err
}
func (g *gitBackEnd) gitClean(path string) error {
_, err := g.git(path, "clean", "-xdf")
return err
}
func (g *gitBackEnd) gitBranches(path string) ([]string, error) {
branches, err := g.git(path, "branch")
if err != nil {
return nil, err
}
b := make([]string, 0, len(branches))
for _, v := range branches {
b = append(b, strings.Trim(v, " *\t\n"))
}
return b, nil
}
func (g *gitBackEnd) gitBranchNow(path string) (string, error) {
branches, err := g.git(path, "branch")
if err != nil {
return "", err
}
for _, v := range branches {
if strings.Contains(v, "*") {
return strings.Trim(v, " *\t\n"), nil
}
}
return "", fmt.Errorf("unexpected git output")
}
func (g *gitBackEnd) gitPull(path string, fastForward bool) error {
var err error
if fastForward {
_, err = g.git(path, "pull", "--ff-only", "--rebase")
} else {
_, err = g.git(path, "pull")
}
if err != nil {
return err
}
return nil
}
func (g *gitBackEnd) gitRebase(path, branch string) error {
_, err := g.git(path, "rebase", branch)
return err
}
func (g *gitBackEnd) gitPush(path, remote, branch string, upstream bool) error {
var err error
if upstream {
_, err = g.git(path, "push", "--set-upstream", remote, branch)
} else {
_, err = g.git(path, "push")
}
if err != nil {
return err
}
return nil
}
func (g *gitBackEnd) gitUnwind(path string) error {
_, err := g.git(path, "checkout", "-f")
if err != nil {
return err
}
return g.gitClean(path)
}
func (g *gitBackEnd) gitUnwindBranch(path, branch string) error {
err := g.gitUnwind(path)
if err != nil {
return err
}
err = g.gitCheckout(path, "master")
if err != nil {
return err
}
return g.gitBranchDelete(path, branch)
}
func (g *gitBackEnd) gitNewBranch(path, branch string) error {
_, err := g.git(path, "checkout", "-b", branch)
return err
}
func (g *gitBackEnd) gitLastDigest(path string) ([]byte, error) {
out, err := g.git(path, "log", "--pretty=oneline", "-n 1")
if err != nil {
return nil, err
}
if len(out) == 0 {
return nil, fmt.Errorf("invalid git output")
}
// Returned data is "<digest> <commit message>"
ds := strings.SplitN(out[0], " ", 2)
if len(ds) == 0 {
return nil, fmt.Errorf("invalid log")
}
d, err := hex.DecodeString(ds[0])
if err != nil {
return nil, err
}
if len(d) != sha1.Size {
return nil, fmt.Errorf("invalid sha1 size")
}
return d, nil
}
func (g *gitBackEnd) gitLog(path string) ([]string, error) {
out, err := g.git(path, "log")
if err != nil {
return nil, err
}
return out, nil
}
func (g *gitBackEnd) gitFsck(path string) ([]string, error) {
out, err := g.git(path, "fsck", "--full", "--strict")
if err != nil {
return nil, err
}
return out, nil
}
// gitConfig sets a config value for the provided repo.
func (g *gitBackEnd) gitConfig(path, name, value string) error {
_, err := g.git(path, "config", name, value)
return err
}
// gitClone clones a git repository. This functions exits without an error
// if the directory is already a git repo.
func (g *gitBackEnd) gitClone(from, to string, repoConfig map[string]string) error {
_, err := os.Stat(filepath.Join(from, ".git"))
if os.IsNotExist(err) {
return fmt.Errorf("source repo does not exist")
}
_, err = os.Stat(filepath.Join(to, ".git"))
if !os.IsNotExist(err) {
return err
}
log.Infof("Cloning git repo %v to %v", from, to)
// Clone the repo (with config, if applicable).
args := []string{"clone", from, to}
for k, v := range repoConfig {
args = append(args, "-c", k+"="+v)
}
_, err = g.git("", args...)
return err
}
// gitInit initializes a new repository. If the repository exists
// it does not reinit it; it reutns failure instead.
func (g *gitBackEnd) gitInit(path string) (string, error) {
out, err := g.git("", "init", path)
if err != nil {
return "", err
}
if len(out) != 1 {
return "", fmt.Errorf("unexpected git output")
}
return out[0], nil
}
// gitInitRepo initializes a directory as a git repo. This functions exits
// without an error if the directory is already a git repo. The git repo is
// initialized with a .gitignore file so that a) have a master and b) always
// ignore the lock file.
func (g *gitBackEnd) gitInitRepo(path string, repoConfig map[string]string) error {
_, err := os.Stat(filepath.Join(path, ".git"))
// This test is unreadable but correct.
if !os.IsNotExist(err) {
return err
}
// Containing directory
log.Infof("Initializing git repo: %v", path)
err = os.MkdirAll(path, 0755)
if err != nil {
return err
}
// Initialize git repo
_, err = g.gitInit(path)
if err != nil {
return err
}
// Apply repo config
for k, v := range repoConfig {
err = g.gitConfig(path, k, v)
if err != nil {
return err
}
}
// Add empty .gitignore
// This makes the repo ready to go and we'll always use this as the
// initial commit.
err = ioutil.WriteFile(filepath.Join(path, ".gitignore"), []byte{},
0664)
if err != nil {
return err
}
err = g.gitAdd(path, ".gitignore")
if err != nil {
return err
}
err = g.gitCommit(path, "Add .gitignore")
if err != nil {
return err
}
// Add README.md
err = ioutil.WriteFile(filepath.Join(path, "README.md"),
[]byte(defaultReadme), 0644)
if err != nil {
return err
}
err = g.gitAdd(path, "README.md")
if err != nil {
return err
}
return g.gitCommit(path, "Add README.md")
}