-
Notifications
You must be signed in to change notification settings - Fork 23
/
vcs.go
213 lines (183 loc) · 4.66 KB
/
vcs.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
package main
import (
"encoding/hex"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
var kindToVCS = map[string]VCS{
"bzr": bzrVCS{},
"hg": hgVCS{},
"git": gitVCS{},
}
type VCS interface {
Kind() string
Info(dir string) (VCSInfo, error)
Update(dir string, isTag bool, revid string) error
Clean(dir string) error
Create(repo, rootDir string) error
Fetch(dir string) error
}
type VCSInfo struct {
revid string
revno string // optional
clean bool
}
type gitVCS struct{}
func (gitVCS) Kind() string {
return "git"
}
func (gitVCS) Info(dir string) (VCSInfo, error) {
out, err := runCmd(dir, "git", "log", "-n", "1", "--pretty=format:%H %ct", "HEAD")
if err != nil {
return VCSInfo{}, err
}
fields := strings.Fields(out)
if len(fields) != 2 {
return VCSInfo{}, fmt.Errorf("unexpected git log output %q", out)
}
revid := fields[0]
// validate the revision hash
revhash, err := hex.DecodeString(revid)
if err != nil || len(revhash) == 0 {
return VCSInfo{},
fmt.Errorf("git rev-parse provided invalid revision %q", revid)
}
unixTime, err := strconv.ParseInt(fields[1], 10, 64)
if err != nil {
return VCSInfo{},
fmt.Errorf("git rev-parse provided invalid time %q", fields[1])
}
// `git status --porcelain` outputs one line per changed or untracked file.
out, err = runCmd(dir, "git", "status", "--porcelain")
if err != nil {
return VCSInfo{}, err
}
return VCSInfo{
revid: revid,
// Empty output (with rc=0) indicates no changes in working copy.
clean: out == "",
revno: time.Unix(unixTime, 0).UTC().Format(time.RFC3339),
}, nil
}
func (gitVCS) Create(repo, rootDir string) error {
_, err := runUpdateCmd("", "git", "clone", repo, rootDir)
return err
}
func (gitVCS) Update(dir string, isTag bool, revid string) error {
_, err := runUpdateCmd(dir, "git", "checkout", revid)
return err
}
func (gitVCS) Clean(dir string) error {
_, err := runUpdateCmd(dir, "git", "reset", "--hard", "HEAD")
return err
}
func (gitVCS) Fetch(dir string) error {
_, err := runCmd(dir, "git", "fetch")
return err
}
type bzrVCS struct{}
func (bzrVCS) Kind() string {
return "bzr"
}
var validBzrInfo = regexp.MustCompile(`^([0-9.]+) ([^ \t]+)$`)
var shelveLine = regexp.MustCompile(`^[0-9]+ (shelves exist|shelf exists)\.`)
func (bzrVCS) Info(dir string) (VCSInfo, error) {
out, err := runCmd(dir, "bzr", "revision-info", "--tree")
if err != nil {
return VCSInfo{}, err
}
m := validBzrInfo.FindStringSubmatch(strings.TrimSpace(out))
if m == nil {
return VCSInfo{}, fmt.Errorf("bzr revision-info has unexpected result %q", out)
}
out, err = runCmd(dir, "bzr", "status", "-S")
if err != nil {
return VCSInfo{}, err
}
clean := true
statusLines := strings.Split(out, "\n")
for _, line := range statusLines {
if line == "" || shelveLine.MatchString(line) {
continue
}
clean = false
break
}
return VCSInfo{
revid: m[2],
revno: m[1],
clean: clean,
}, nil
}
func (bzrVCS) Create(repo, rootDir string) error {
_, err := runUpdateCmd("", "bzr", "branch", repo, rootDir)
return err
}
func (bzrVCS) Clean(dir string) error {
_, err := runUpdateCmd(dir, "bzr", "revert")
return err
}
func (bzrVCS) Update(dir string, isTag bool, to string) error {
if isTag {
to = "tag:" + to
} else {
to = "revid:" + to
}
_, err := runUpdateCmd(dir, "bzr", "update", "-r", to)
return err
}
func (bzrVCS) Fetch(dir string) error {
_, err := runCmd(dir, "bzr", "pull")
return err
}
var validHgInfo = regexp.MustCompile(`^([a-f0-9]+) ([0-9]+)$`)
type hgVCS struct{}
func (hgVCS) Info(dir string) (VCSInfo, error) {
out, err := runCmd(dir, "hg", "log", "-l", "1", "-r", ".", "--template", "{node} {rev}")
if err != nil {
return VCSInfo{}, err
}
m := validHgInfo.FindStringSubmatch(strings.TrimSpace(out))
if m == nil {
return VCSInfo{}, fmt.Errorf("hg identify has unexpected result %q", out)
}
out, err = runCmd(dir, "hg", "status")
if err != nil {
return VCSInfo{}, err
}
// TODO(rog) check that tree is clean
return VCSInfo{
revid: m[1],
revno: m[2],
clean: out == "",
}, nil
}
func (hgVCS) Kind() string {
return "hg"
}
func (hgVCS) Create(repo, rootDir string) error {
_, err := runUpdateCmd("", "hg", "clone", "-U", repo, rootDir)
return err
}
func (hgVCS) Clean(dir string) error {
_, err := runUpdateCmd(dir, "hg", "revert", "--all")
return err
}
func (hgVCS) Update(dir string, isTag bool, revid string) error {
_, err := runUpdateCmd(dir, "hg", "update", revid)
return err
}
func (hgVCS) Fetch(dir string) error {
_, err := runCmd(dir, "hg", "pull")
return err
}
func runUpdateCmd(dir string, name string, args ...string) (string, error) {
if *dryRun {
printShellCommand(dir, name, args)
return "", nil
}
return runCmd(dir, name, args...)
}