This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
/
verify.go
96 lines (89 loc) · 2.83 KB
/
verify.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
package main
import (
"fmt"
"go/build"
"os"
"path/filepath"
"strings"
)
type mismatchedDepsError struct {
error
}
// Verify checks a project's cache directory against the dep list and returns an error if they do not match.
func Verify(root string, pinlist *Pinlist) error {
// Find all the repos containing Go packages in the cache.
cache := filepath.Join(root, projectDirName, cacheDirName, "src")
context := new(build.Context)
*context = build.Default
context.GOPATH = cache
var cachedRepos smap
for _, path := range FindDirsRecursively(cache) {
_, err := context.ImportDir(path, 0)
if err != nil {
if _, ok := err.(*build.NoGoError); ok {
continue
}
// NOTE: this is a special case for the import error that results from having more than one Go package
// in the directory at this path. Unfortunately, go/build does not provide a nice error to check against
// here, so I'm doing string matching for now. See issue 8288:
// https://code.google.com/p/go/issues/detail?id=8286
if strings.HasPrefix(err.Error(), "found packages ") {
continue
}
return err
}
importPath, err := filepath.Rel(cache, path)
if err != nil {
return err
}
repoRoot, err := RepoRootForImportPath(importPath, false)
if err != nil {
return err
}
cachedRepos.Add(repoRoot.Root)
}
// Verify that the package exists with the correct version for each package in the pinlist. Remove from the
// list of cached repos as we go.
for _, dep := range pinlist.Deps {
if err := verify(root, dep); err != nil {
return err
}
repoRoot, err := RepoRootForImportPath(dep.Name, false)
if err != nil {
return err
}
cachedRepos.Remove(repoRoot.Root)
}
// If there are any repos left in cachedRepos, they are not used by any packages in the pinlist. We need to
// remove these to avoid polluting the namespace and allowing untracked dependencies in the build.
for _, repo := range cachedRepos {
fmt.Printf("Removing cached repo not in the pinlist: %s\n", repo)
if err := os.RemoveAll(filepath.Join(cache, repo)); err != nil {
return err
}
}
return nil
}
func verify(root string, dep Dep) error {
repo, err := RepoRootForImportPath(dep.Name, false)
if err != nil {
return err
}
dir := filepath.Join(root, projectDirName, cacheDirName, "src", repo.Root)
// Check if the directory exists first just to provide a friendlier warning.
if _, err := os.Stat(dir); os.IsNotExist(err) {
return fmt.Errorf("Repo %s (for dependency %s) not cached. Run 'glp sync'.", repo.Repo, dep.Name)
}
rev, dirty, err := repo.VCS.GetRev(dir)
if err != nil {
return err
}
if rev != dep.Rev {
err := fmt.Errorf("Pinlist has version %s for %s, but found %s in cache.", dep.Rev, dep.Name, rev)
return mismatchedDepsError{err}
}
if dirty {
fmt.Fprintf(os.Stderr, "Warning: found dirty repo for dependency %s\n", dep.Name)
}
return nil
}