forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcs_repo.go
425 lines (365 loc) · 10.9 KB
/
vcs_repo.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
416
417
418
419
420
421
422
423
424
425
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gps
import (
"bytes"
"context"
"encoding/xml"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/Masterminds/vcs"
"github.com/pkg/errors"
)
type ctxRepo interface {
vcs.Repo
get(context.Context) error
fetch(context.Context) error
updateVersion(context.Context, string) error
//ping(context.Context) (bool, error)
}
// ensureCleaner is an optional extension of ctxRepo.
type ensureCleaner interface {
// ensureClean ensures a repository is clean and in working order,
// or returns an error if the adaptive recovery attempts fail.
ensureClean(context.Context) error
}
// original implementation of these methods come from
// https://github.com/Masterminds/vcs
type gitRepo struct {
*vcs.GitRepo
}
func newVcsRemoteErrorOr(err error, args []string, out, msg string) error {
if err == context.Canceled || err == context.DeadlineExceeded {
return err
}
return vcs.NewRemoteError(msg, errors.Wrapf(err, "command failed: %v", args), out)
}
func newVcsLocalErrorOr(err error, args []string, out, msg string) error {
if err == context.Canceled || err == context.DeadlineExceeded {
return err
}
return vcs.NewLocalError(msg, errors.Wrapf(err, "command failed: %v", args), out)
}
func (r *gitRepo) get(ctx context.Context) error {
cmd := commandContext(
ctx,
"git",
"clone",
"--recursive",
"-v",
"--progress",
r.Remote(),
r.LocalPath(),
)
// Ensure no prompting for PWs
cmd.SetEnv(append([]string{"GIT_ASKPASS=", "GIT_TERMINAL_PROMPT=0"}, os.Environ()...))
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to get repository")
}
return nil
}
func (r *gitRepo) fetch(ctx context.Context) error {
cmd := commandContext(
ctx,
"git",
"fetch",
"--tags",
"--prune",
r.RemoteLocation,
)
cmd.SetDir(r.LocalPath())
// Ensure no prompting for PWs
cmd.SetEnv(append([]string{"GIT_ASKPASS=", "GIT_TERMINAL_PROMPT=0"}, os.Environ()...))
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to update repository")
}
return nil
}
func (r *gitRepo) updateVersion(ctx context.Context, v string) error {
cmd := commandContext(ctx, "git", "checkout", v)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to update checked out version")
}
return r.defendAgainstSubmodules(ctx)
}
// defendAgainstSubmodules tries to keep repo state sane in the event of
// submodules. Or nested submodules. What a great idea, submodules.
func (r *gitRepo) defendAgainstSubmodules(ctx context.Context) error {
// First, update them to whatever they should be, if there should happen to be any.
{
cmd := commandContext(
ctx,
"git",
"submodule",
"update",
"--init",
"--recursive",
)
cmd.SetDir(r.LocalPath())
// Ensure no prompting for PWs
cmd.SetEnv(append([]string{"GIT_ASKPASS=", "GIT_TERMINAL_PROMPT=0"}, os.Environ()...))
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unexpected error while defensively updating submodules")
}
}
// Now, do a special extra-aggressive clean in case changing versions caused
// one or more submodules to go away.
{
cmd := commandContext(ctx, "git", "clean", "-x", "-d", "-f", "-f")
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unexpected error while defensively cleaning up after possible derelict submodule directories")
}
}
// Then, repeat just in case there are any nested submodules that went away.
{
cmd := commandContext(
ctx,
"git",
"submodule",
"foreach",
"--recursive",
"git",
"clean", "-x", "-d", "-f", "-f",
)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unexpected error while defensively cleaning up after possible derelict nested submodule directories")
}
}
return nil
}
func (r *gitRepo) ensureClean(ctx context.Context) error {
cmd := commandContext(
ctx,
"git",
"status",
"--porcelain",
)
cmd.SetDir(r.LocalPath())
out, err := cmd.CombinedOutput()
if err != nil {
// An error on simple git status indicates some aggressive repository
// corruption, outside of the purview that we can deal with here.
return err
}
if len(bytes.TrimSpace(out)) == 0 {
// No output from status indicates a clean tree, without any modified or
// untracked files - we're in good shape.
return nil
}
// We could be more parsimonious about this, but it's probably not worth it
// - it's a rare case to have to do any cleanup anyway, so when we do, we
// might as well just throw the kitchen sink at it.
cmd = commandContext(
ctx,
"git",
"reset",
"--hard",
)
cmd.SetDir(r.LocalPath())
_, err = cmd.CombinedOutput()
if err != nil {
return err
}
// We also need to git clean -df; just reuse defendAgainstSubmodules here,
// even though it's a bit layer-breaky.
err = r.defendAgainstSubmodules(ctx)
if err != nil {
return err
}
// Check status one last time. If it's still not clean, give up.
cmd = commandContext(
ctx,
"git",
"status",
"--porcelain",
)
cmd.SetDir(r.LocalPath())
out, err = cmd.CombinedOutput()
if err != nil {
return err
}
if len(bytes.TrimSpace(out)) != 0 {
return errors.Errorf("failed to clean up git repository at %s - dirty? corrupted? status output: \n%s", r.LocalPath(), string(out))
}
return nil
}
type bzrRepo struct {
*vcs.BzrRepo
}
func (r *bzrRepo) get(ctx context.Context) error {
basePath := filepath.Dir(filepath.FromSlash(r.LocalPath()))
if _, err := os.Stat(basePath); os.IsNotExist(err) {
err = os.MkdirAll(basePath, 0755)
if err != nil {
return newVcsLocalErrorOr(err, nil, "", "unable to create directory")
}
}
cmd := commandContext(ctx, "bzr", "branch", r.Remote(), r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to get repository")
}
return nil
}
func (r *bzrRepo) fetch(ctx context.Context) error {
cmd := commandContext(ctx, "bzr", "pull")
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to update repository")
}
return nil
}
func (r *bzrRepo) updateVersion(ctx context.Context, version string) error {
cmd := commandContext(ctx, "bzr", "update", "-r", version)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to update checked out version")
}
return nil
}
type hgRepo struct {
*vcs.HgRepo
}
func (r *hgRepo) get(ctx context.Context) error {
cmd := commandContext(ctx, "hg", "clone", r.Remote(), r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to get repository")
}
return nil
}
func (r *hgRepo) fetch(ctx context.Context) error {
cmd := commandContext(ctx, "hg", "pull")
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to fetch latest changes")
}
return nil
}
func (r *hgRepo) updateVersion(ctx context.Context, version string) error {
cmd := commandContext(ctx, "hg", "update", version)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to update checked out version")
}
return nil
}
type svnRepo struct {
*vcs.SvnRepo
}
func (r *svnRepo) get(ctx context.Context) error {
remote := r.Remote()
if strings.HasPrefix(remote, "/") {
remote = "file://" + remote
} else if runtime.GOOS == "windows" && filepath.VolumeName(remote) != "" {
remote = "file:///" + remote
}
cmd := commandContext(ctx, "svn", "checkout", remote, r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to get repository")
}
return nil
}
func (r *svnRepo) fetch(ctx context.Context) error {
cmd := commandContext(ctx, "svn", "update")
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to update repository")
}
return nil
}
func (r *svnRepo) updateVersion(ctx context.Context, version string) error {
cmd := commandContext(ctx, "svn", "update", "-r", version)
cmd.SetDir(r.LocalPath())
if out, err := cmd.CombinedOutput(); err != nil {
return newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to update checked out version")
}
return nil
}
func (r *svnRepo) CommitInfo(id string) (*vcs.CommitInfo, error) {
ctx := context.TODO()
// There are cases where Svn log doesn't return anything for HEAD or BASE.
// svn info does provide details for these but does not have elements like
// the commit message.
if id == "HEAD" || id == "BASE" {
type commit struct {
Revision string `xml:"revision,attr"`
}
type info struct {
Commit commit `xml:"entry>commit"`
}
cmd := commandContext(ctx, "svn", "info", "-r", id, "--xml")
cmd.SetDir(r.LocalPath())
out, err := cmd.CombinedOutput()
if err != nil {
return nil, newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to retrieve commit information")
}
infos := new(info)
if err := xml.Unmarshal(out, &infos); err != nil {
return nil, newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to retrieve commit information")
}
id = infos.Commit.Revision
if id == "" {
return nil, vcs.ErrRevisionUnavailable
}
}
cmd := commandContext(ctx, "svn", "log", "-r", id, "--xml")
cmd.SetDir(r.LocalPath())
out, err := cmd.CombinedOutput()
if err != nil {
return nil, newVcsRemoteErrorOr(err, cmd.Args(), string(out),
"unable to retrieve commit information")
}
type logentry struct {
Author string `xml:"author"`
Date string `xml:"date"`
Msg string `xml:"msg"`
}
type log struct {
XMLName xml.Name `xml:"log"`
Logs []logentry `xml:"logentry"`
}
logs := new(log)
if err := xml.Unmarshal(out, &logs); err != nil {
return nil, newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to retrieve commit information")
}
if len(logs.Logs) == 0 {
return nil, vcs.ErrRevisionUnavailable
}
ci := &vcs.CommitInfo{
Commit: id,
Author: logs.Logs[0].Author,
Message: logs.Logs[0].Msg,
}
if len(logs.Logs[0].Date) > 0 {
ci.Date, err = time.Parse(time.RFC3339Nano, logs.Logs[0].Date)
if err != nil {
return nil, newVcsLocalErrorOr(err, cmd.Args(), string(out),
"unable to retrieve commit information")
}
}
return ci, nil
}