-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd-git.go
61 lines (51 loc) · 1.27 KB
/
cmd-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
//go:build !gogit
package main
import (
"fmt"
"regexp"
)
type gitRepo struct {
url string
dir string
}
var re = regexp.MustCompile(`.*HEAD branch: (.*)\n`) // regex to extract default branch name of a repo
func gitCmd(args ...string) (string, error) {
output, code := execute("git", args...)
if code != 0 {
return "", fmt.Errorf("git failed: exit code %d", code)
}
return output, nil
}
func gitOpenDir(url, dir string) (repo, error) {
_, err := gitCmd("-C", dir, "rev-parse")
return &gitRepo{url: url, dir: dir}, err
}
func gitCloneDir(url, dir string) (repo, error) {
_, err := gitCmd("clone", "https://"+url, dir)
return &gitRepo{url: url, dir: dir}, err
}
func (r *gitRepo) Checkout(rev string) error {
output, err := gitCmd("-C", r.dir, "remote", "show", "origin")
if err != nil {
return err
}
defaultBranch := extractBranch(output)
if _, err := gitCmd("-C", r.dir, "checkout", defaultBranch); err != nil {
return err
}
if rev == "" || rev == latestRev {
rev = "HEAD"
}
_, err = gitCmd("-C", r.dir, "checkout", "-q", rev)
return err
}
func (r *gitRepo) Fetch() error {
_, err := gitCmd("-C", r.dir, "pull")
return err
}
func extractBranch(output string) string {
if !re.MatchString(output) {
return "master"
}
return re.FindStringSubmatch(output)[1]
}