/
vcs.go
494 lines (445 loc) · 12.3 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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package repo
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
cp "github.com/Masterminds/glide/cache"
"github.com/Masterminds/glide/cfg"
"github.com/Masterminds/glide/msg"
gpath "github.com/Masterminds/glide/path"
"github.com/Masterminds/semver"
v "github.com/Masterminds/vcs"
)
// VcsUpdate updates to a particular checkout based on the VCS setting.
func VcsUpdate(dep *cfg.Dependency, force bool, updated *UpdateTracker) error {
// If the dependency has already been pinned we can skip it. This is a
// faster path so we don't need to resolve it again.
if dep.Pin != "" {
msg.Debug("Dependency %s has already been pinned. Fetching updates skipped", dep.Name)
return nil
}
if updated.Check(dep.Name) {
msg.Debug("%s was already updated, skipping", dep.Name)
return nil
}
updated.Add(dep.Name)
if filterArchOs(dep) {
msg.Info("%s is not used for %s/%s.\n", dep.Name, runtime.GOOS, runtime.GOARCH)
return nil
}
key, err := cp.Key(dep.Remote())
if err != nil {
msg.Die("Cache key generation error: %s", err)
}
location := cp.Location()
dest := filepath.Join(location, "src", key)
// If destination doesn't exist we need to perform an initial checkout.
if _, err := os.Stat(dest); os.IsNotExist(err) {
msg.Info("--> Fetching %s", dep.Name)
if err = VcsGet(dep); err != nil {
msg.Warn("Unable to checkout %s\n", dep.Name)
return err
}
} else {
// At this point we have a directory for the package.
msg.Info("--> Fetching updates for %s", dep.Name)
// When the directory is not empty and has no VCS directory it's
// a vendored files situation.
empty, err := gpath.IsDirectoryEmpty(dest)
if err != nil {
return err
}
_, err = v.DetectVcsFromFS(dest)
if empty == true && err == v.ErrCannotDetectVCS {
msg.Warn("Cached version of %s is an empty directory. Fetching a new copy of the dependency", dep.Name)
msg.Debug("Removing empty directory %s", dest)
err := os.RemoveAll(dest)
if err != nil {
return err
}
if err = VcsGet(dep); err != nil {
msg.Warn("Unable to checkout %s\n", dep.Name)
return err
}
} else {
repo, err := dep.GetRepo(dest)
// Tried to checkout a repo to a path that does not work. Either the
// type or endpoint has changed. Force is being passed in so the old
// location can be removed and replaced with the new one.
// Warning, any changes in the old location will be deleted.
// TODO: Put dirty checking in on the existing local checkout.
if (err == v.ErrWrongVCS || err == v.ErrWrongRemote) && force == true {
newRemote := dep.Remote()
msg.Warn("Replacing %s with contents from %s\n", dep.Name, newRemote)
rerr := os.RemoveAll(dest)
if rerr != nil {
return rerr
}
if err = VcsGet(dep); err != nil {
msg.Warn("Unable to checkout %s\n", dep.Name)
return err
}
repo, err = dep.GetRepo(dest)
if err != nil {
return err
}
} else if err != nil {
return err
} else if repo.IsDirty() {
return fmt.Errorf("%s contains uncommitted changes. Skipping update", dep.Name)
}
ver := dep.Reference
if ver == "" {
ver = defaultBranch(repo)
}
// Check if the current version is a tag or commit id. If it is
// and that version is already checked out we can skip updating
// which is faster than going out to the Internet to perform
// an update.
if ver != "" {
version, err := repo.Version()
if err != nil {
return err
}
ib, err := isBranch(ver, repo)
if err != nil {
return err
}
// If the current version equals the ref and it's not a
// branch it's a tag or commit id so we can skip
// performing an update.
if version == ver && !ib {
msg.Debug("%s is already set to version %s. Skipping update", dep.Name, dep.Reference)
return nil
}
}
if err := repo.Update(); err != nil {
msg.Warn("Download failed.\n")
return err
}
}
}
return nil
}
// VcsVersion set the VCS version for a checkout.
func VcsVersion(dep *cfg.Dependency) error {
// If the dependency has already been pinned we can skip it. This is a
// faster path so we don't need to resolve it again.
if dep.Pin != "" {
msg.Debug("Dependency %s has already been pinned. Setting version skipped", dep.Name)
return nil
}
key, err := cp.Key(dep.Remote())
if err != nil {
msg.Die("Cache key generation error: %s", err)
}
location := cp.Location()
cwd := filepath.Join(location, "src", key)
// If there is no reference configured there is nothing to set.
if dep.Reference == "" {
// Before exiting update the pinned version
repo, err := dep.GetRepo(cwd)
if err != nil {
return err
}
dep.Pin, err = repo.Version()
if err != nil {
return err
}
return nil
}
// When the directory is not empty and has no VCS directory it's
// a vendored files situation.
empty, err := gpath.IsDirectoryEmpty(cwd)
if err != nil {
return err
}
_, err = v.DetectVcsFromFS(cwd)
if empty == false && err == v.ErrCannotDetectVCS {
return fmt.Errorf("Cache directory missing VCS information for %s", dep.Name)
}
repo, err := dep.GetRepo(cwd)
if err != nil {
return err
}
ver := dep.Reference
// References in Git can begin with a ^ which is similar to semver.
// If there is a ^ prefix we assume it's a semver constraint rather than
// part of the git/VCS commit id.
if repo.IsReference(ver) && !strings.HasPrefix(ver, "^") {
msg.Info("--> Setting version for %s to %s.\n", dep.Name, ver)
} else {
// Create the constraint first to make sure it's valid before
// working on the repo.
constraint, err := semver.NewConstraint(ver)
// Make sure the constriant is valid. At this point it's not a valid
// reference so if it's not a valid constrint we can exit early.
if err != nil {
msg.Warn("The reference '%s' is not valid\n", ver)
return err
}
// Get the tags and branches (in that order)
refs, err := getAllVcsRefs(repo)
if err != nil {
return err
}
// Convert and filter the list to semver.Version instances
semvers := getSemVers(refs)
// Sort semver list
sort.Sort(sort.Reverse(semver.Collection(semvers)))
found := false
for _, v := range semvers {
if constraint.Check(v) {
found = true
// If the constrint passes get the original reference
ver = v.Original()
break
}
}
if found {
msg.Info("--> Detected semantic version. Setting version for %s to %s", dep.Name, ver)
} else {
msg.Warn("--> Unable to find semantic version for constraint %s %s", dep.Name, ver)
}
}
if err := repo.UpdateVersion(ver); err != nil {
return err
}
dep.Pin, err = repo.Version()
if err != nil {
return err
}
return nil
}
// VcsGet figures out how to fetch a dependency, and then gets it.
//
// VcsGet installs into the cache.
func VcsGet(dep *cfg.Dependency) error {
key, err := cp.Key(dep.Remote())
if err != nil {
msg.Die("Cache key generation error: %s", err)
}
location := cp.Location()
d := filepath.Join(location, "src", key)
repo, err := dep.GetRepo(d)
if err != nil {
return err
}
// If the directory does not exist this is a first cache.
if _, err = os.Stat(d); os.IsNotExist(err) {
msg.Debug("Adding %s to the cache for the first time", dep.Name)
err = repo.Get()
if err != nil {
return err
}
branch := findCurrentBranch(repo)
if branch != "" {
msg.Debug("Saving default branch for %s", repo.Remote())
c := cp.RepoInfo{DefaultBranch: branch}
err = cp.SaveRepoData(key, c)
if err == cp.ErrCacheDisabled {
msg.Debug("Unable to cache default branch because caching is disabled")
} else if err != nil {
msg.Debug("Error saving %s to cache. Error: %s", repo.Remote(), err)
}
}
} else {
msg.Debug("Updating %s in the cache", dep.Name)
err = repo.Update()
if err != nil {
return err
}
}
return nil
}
// filterArchOs indicates a dependency should be filtered out because it is
// the wrong GOOS or GOARCH.
//
// FIXME: Should this be moved to the dependency package?
func filterArchOs(dep *cfg.Dependency) bool {
found := false
if len(dep.Arch) > 0 {
for _, a := range dep.Arch {
if a == runtime.GOARCH {
found = true
}
}
// If it's not found, it should be filtered out.
if !found {
return true
}
}
found = false
if len(dep.Os) > 0 {
for _, o := range dep.Os {
if o == runtime.GOOS {
found = true
}
}
if !found {
return true
}
}
return false
}
// isBranch returns true if the given string is a branch in VCS.
func isBranch(branch string, repo v.Repo) (bool, error) {
branches, err := repo.Branches()
if err != nil {
return false, err
}
for _, b := range branches {
if b == branch {
return true, nil
}
}
return false, nil
}
// defaultBranch tries to ascertain the default branch for the given repo.
// Some repos will have multiple branches in them (e.g. Git) while others
// (e.g. Svn) will not.
func defaultBranch(repo v.Repo) string {
// Svn and Bzr use different locations (paths or entire locations)
// for branches so we won't have a default branch.
if repo.Vcs() == v.Svn || repo.Vcs() == v.Bzr {
return ""
}
// Check the cache for a value.
key, kerr := cp.Key(repo.Remote())
var d cp.RepoInfo
if kerr == nil {
d, err := cp.RepoData(key)
if err == nil {
if d.DefaultBranch != "" {
return d.DefaultBranch
}
}
}
// If we don't have it in the store try some APIs
r := repo.Remote()
u, err := url.Parse(r)
if err != nil {
return ""
}
if u.Scheme == "" {
// Where there is no scheme we try urls like git@github.com:foo/bar
r = strings.Replace(r, ":", "/", -1)
r = "ssh://" + r
u, err = url.Parse(r)
if err != nil {
return ""
}
u.Scheme = ""
}
if u.Host == "github.com" {
parts := strings.Split(u.Path, "/")
if len(parts) != 2 {
return ""
}
api := fmt.Sprintf("https://api.github.com/repos/%s/%s", parts[0], parts[1])
resp, err := http.Get(api)
if err != nil {
return ""
}
defer resp.Body.Close()
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
return ""
}
body, err := ioutil.ReadAll(resp.Body)
var data interface{}
err = json.Unmarshal(body, &data)
if err != nil {
return ""
}
gh := data.(map[string]interface{})
db := gh["default_branch"].(string)
if kerr == nil {
d.DefaultBranch = db
err := cp.SaveRepoData(key, d)
if err == cp.ErrCacheDisabled {
msg.Debug("Unable to cache default branch because caching is disabled")
} else if err != nil {
msg.Debug("Error saving %s to cache. Error: %s", repo.Remote(), err)
}
}
return db
}
if u.Host == "bitbucket.org" {
parts := strings.Split(u.Path, "/")
if len(parts) != 2 {
return ""
}
api := fmt.Sprintf("https://bitbucket.org/api/1.0/repositories/%s/%s/main-branch/", parts[0], parts[1])
resp, err := http.Get(api)
if err != nil {
return ""
}
defer resp.Body.Close()
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
return ""
}
body, err := ioutil.ReadAll(resp.Body)
var data interface{}
err = json.Unmarshal(body, &data)
if err != nil {
return ""
}
bb := data.(map[string]interface{})
db := bb["name"].(string)
if kerr == nil {
d.DefaultBranch = db
err := cp.SaveRepoData(key, d)
if err == cp.ErrCacheDisabled {
msg.Debug("Unable to cache default branch because caching is disabled")
} else if err != nil {
msg.Debug("Error saving %s to cache. Error: %s", repo.Remote(), err)
}
}
return db
}
return ""
}
// From a local repo find out the current branch name if there is one.
// Note, this should only be used right after a fresh clone to get accurate
// information.
func findCurrentBranch(repo v.Repo) string {
msg.Debug("Attempting to find current branch for %s", repo.Remote())
// Svn and Bzr don't have default branches.
if repo.Vcs() == v.Svn || repo.Vcs() == v.Bzr {
return ""
}
if repo.Vcs() == v.Git || repo.Vcs() == v.Hg {
ver, err := repo.Current()
if err != nil {
msg.Debug("Unable to find current branch for %s, error: %s", repo.Remote(), err)
return ""
}
return ver
}
return ""
}
func envForDir(dir string) []string {
env := os.Environ()
return mergeEnvLists([]string{"PWD=" + dir}, env)
}
func mergeEnvLists(in, out []string) []string {
NextVar:
for _, inkv := range in {
k := strings.SplitAfterN(inkv, "=", 2)[0]
for i, outkv := range out {
if strings.HasPrefix(outkv, k) {
out[i] = inkv
continue NextVar
}
}
out = append(out, inkv)
}
return out
}