forked from cross-cpm/xrepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvs_git.go
105 lines (87 loc) · 1.97 KB
/
cvs_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
package main
import (
"os"
"strings"
)
type gitCvs struct {
url string
workdir string
info *RepoInfo
}
func NewGitCvs(workdir string, url string, info *RepoInfo) *gitCvs {
// TODO: check url branck ref invald
return &gitCvs{url, workdir, info}
}
func (e *gitCvs) Checkout() error {
//log.Println("workdir", e.workdir)
//log.Println("info", e.info)
if _, err := os.Stat(e.workdir); os.IsNotExist(err) {
return e.initial_checkout()
} else {
return e.update_checkout()
}
}
func (e *gitCvs) initial_checkout() error {
err := shell_run("git", "clone", e.url, e.workdir)
if err != nil {
return err
}
if e.info.Ref == "HEAD" {
err := shell_run_in_dir(e.workdir, "git", "checkout", e.info.Branch)
if err != nil {
return err
}
} else {
err := shell_run_in_dir(e.workdir, "git", "checkout", e.info.Ref)
if err != nil {
return err
}
}
return nil
}
func (e *gitCvs) update_checkout() error {
if e.info.Ref == "HEAD" {
err := shell_run_in_dir(e.workdir, "git", "checkout", e.info.Branch)
if err != nil {
return err
}
err = shell_run_in_dir(e.workdir, "git", "pull")
if err != nil {
return err
}
} else {
err := shell_run_in_dir(e.workdir, "git", "fetch")
if err != nil {
return err
}
err = shell_run_in_dir(e.workdir, "git", "checkout", e.info.Ref)
if err != nil {
return err
}
}
return nil
}
func (e *gitCvs) Pull() (string, error) {
err := shell_run_in_dir(e.workdir, "git", "checkout", e.info.Branch)
if err != nil {
return "", err
}
err = shell_run_in_dir(e.workdir, "git", "pull")
if err != nil {
return "", err
}
return e.Revision()
}
func (e *gitCvs) Revision() (string, error) {
rev, err := shell_get_in_dir(e.workdir, "git", "rev-parse", "HEAD")
if err != nil {
return "", err
}
return strings.Trim(rev, " \r\n"), err
}
func (e *gitCvs) Push() error {
return shell_run_in_dir(e.workdir, "git", "push")
}
func (e *gitCvs) Status() error {
return shell_run_in_dir(e.workdir, "git", "status")
}