forked from Sixt/protoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd-git.go
44 lines (36 loc) · 839 Bytes
/
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
//+build !gogit
package main
import (
"fmt"
)
type gitRepo struct {
url string
dir string
}
func gitCmd(args ...string) error {
code := execute("git", args...)
if code != 0 {
return fmt.Errorf("git failed: exit code %d", code)
}
return 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 {
if err := gitCmd("-C", r.dir, "checkout", "master"); err != nil {
return err
}
if rev == "" || rev == latestRev {
rev = "HEAD"
}
return gitCmd("-C", r.dir, "checkout", "-q", rev)
}
func (r *gitRepo) Fetch() error {
return gitCmd("-C", r.dir, "pull")
}