forked from robfig/glock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
save.go
347 lines (297 loc) · 9.21 KB
/
save.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
package main
import (
"bufio"
"errors"
"fmt"
"go/build"
"io"
"log"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"golang.org/x/tools/go/buildutil"
)
var cmdSave = &Command{
UsageLine: "save [import path]",
Short: "save a GLOCKFILE for the given package's dependencies",
Long: `save is used to record the current revisions of a package's dependencies
It writes this state to a file in the root of the package called "GLOCKFILE".
Options:
-n print to stdout instead of writing to file.
`,
}
var saveN = cmdSave.Flag.Bool("n", false, "Don't save the file, just print to stdout")
func init() {
cmdSave.Run = runSave // break init loop
}
func runSave(cmd *Command, args []string) {
if len(args) == 0 {
cmdSave.Usage()
return
}
// Read cmd lines from GLOCKFILE and calculate required dependencies.
var (
importPath = args[0]
cmds = readCmds(importPath)
depRoots = calcDepRoots(importPath, cmds)
)
output := glockfileWriter(importPath, *saveN)
outputCmds(output, cmds)
outputDeps(output, depRoots)
output.Close()
}
func outputCmds(w io.Writer, cmds []string) {
sort.Strings(cmds)
var prev string
for _, cmd := range cmds {
if cmd != prev {
fmt.Fprintln(w, "cmd", cmd)
}
prev = cmd
}
}
func outputDeps(w io.Writer, depRoots []*repoRoot) {
for _, repoRoot := range depRoots {
revision, err := repoRoot.vcs.head(repoRoot.path, repoRoot.repo)
if err != nil {
perror(err)
}
revision = strings.TrimSpace(revision)
fmt.Fprintln(w, repoRoot.root, revision)
}
}
// calcDepRoots discovers all dependencies of the given importPath and returns
// them as a list of the repo roots that cover all dependent packages. for
// example, github.com/robfig/soy and github.com/robfig/soy/data are two
// dependent packages but only one repo. the returned repos are ordered
// alphabetically by import path.
func calcDepRoots(importPath string, cmds []string) []*repoRoot {
var attempts = 1
GetAllDeps:
var depRoots = map[string]*repoRoot{}
var missingPackages []string
for _, importPath := range getAllDeps(importPath, cmds) {
// Convert from packages to repo roots.
// TODO: Filter out any packages that have prefixes also included in the list.
// e.g. pkg/foo/bar , pkg/foo/baz , pkg/foo
// That would skip the relatively slow (redundant) determining of repo root for each.
var repoRoot, err = glockRepoRootForImportPath(importPath)
if err != nil {
perror(err)
}
// Ensure we have the package locally. if not, we don't have all the possible deps.
_, err = build.Import(repoRoot.root, "", build.FindOnly)
if err != nil {
missingPackages = append(missingPackages, repoRoot.root)
}
depRoots[repoRoot.root] = repoRoot
}
// If there were missing packages, try again.
if len(missingPackages) > 0 {
if attempts > 3 {
perror(fmt.Errorf("failed to fetch missing packages: %v", missingPackages))
}
fmt.Fprintln(os.Stderr, "go", "get", "-d", strings.Join(missingPackages, " "))
run("go", append([]string{"get", "-d"}, missingPackages...)...)
attempts++
goto GetAllDeps
}
// Remove any dependencies to packages within the target repo
delete(depRoots, importPath)
var repos []*repoRoot
for _, repo := range depRoots {
repos = append(repos, repo)
}
sort.Sort(byImportPath(repos))
return repos
}
type byImportPath []*repoRoot
func (p byImportPath) Len() int { return len(p) }
func (p byImportPath) Less(i, j int) bool { return p[i].root < p[j].root }
func (p byImportPath) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
var majorVersionComponent = regexp.MustCompile(`v[\d]+`)
// pathWithoutMajorVersion returns path with the 1st major version /component
// (if any) stripped out. If one was found, the 2nd return value is true.
func pathWithoutMajorVersion(path string) (string, bool) {
parts := strings.Split(path, "/")
for idx, part := range strings.Split(path, "/") {
if majorVersionComponent.MatchString(part) {
return strings.Join(append(parts[:idx], parts[idx+1:]...), "/"), true
}
}
return path, false
}
// tryImport attempts to import the path as-is and, if it fails to be found and
// path contains a major module version, reattempts with the version removed.
func tryImport(ctx build.Context, path, srcDir string, mode build.ImportMode) (*build.Package, error) {
pkg, err := ctx.Import(path, srcDir, mode)
if err != nil && strings.HasPrefix(err.Error(), "cannot find package ") {
if versionlessPath, ok := pathWithoutMajorVersion(path); ok {
return ctx.Import(versionlessPath, srcDir, mode)
}
}
return pkg, err
}
// getAllDeps returns a slice of package import paths for all dependencies
// (including test dependencies) of the given import path (and subpackages) and commands.
func getAllDeps(importPath string, cmds []string) []string {
subpackagePrefix := importPath + "/"
var depsSlice []string
for _, useAllFiles := range []bool{false, true} {
printLoadingError := func(path string, err error) {
if err != nil && !useAllFiles {
// Lots of errors because of UseAllFiles.
log.Printf("error loading package %s: %s", path, err)
}
}
deps := map[string]struct{}{}
roots := map[string]struct{}{
importPath: {},
}
// Add the command packages. Note that external command packages are
// considered dependencies.
for _, pkg := range cmds {
roots[pkg] = struct{}{}
if !strings.HasPrefix(pkg, subpackagePrefix) {
deps[pkg] = struct{}{}
}
}
buildContext := build.Default
buildContext.CgoEnabled = true
buildContext.UseAllFiles = useAllFiles
// Add the subpackages.
for path := range buildutil.ExpandPatterns(&buildContext, []string{subpackagePrefix + "..."}) {
_, err := tryImport(buildContext, path, "", 0)
if _, ok := err.(*build.NoGoError); ok {
continue
}
printLoadingError(path, err)
roots[path] = struct{}{}
}
var addTransitiveClosure func(string)
addTransitiveClosure = func(path string) {
pkg, err := tryImport(buildContext, path, "", 0)
printLoadingError(path, err)
importPaths := append([]string(nil), pkg.Imports...)
if _, ok := roots[path]; ok {
importPaths = append(importPaths, pkg.TestImports...)
importPaths = append(importPaths, pkg.XTestImports...)
}
for _, path := range importPaths {
if path == "C" {
continue // "C" is fake
}
// Resolve the import path relative to the importing package.
if bp2, _ := tryImport(buildContext, path, pkg.Dir, build.FindOnly); bp2 != nil {
path = bp2.ImportPath
}
// Exclude our roots. Note that commands are special-cased above.
if _, ok := roots[path]; ok {
continue
}
slash := strings.IndexByte(path, '/')
stdLib := slash == -1 || strings.IndexByte(path[:slash], '.') == -1
// Exclude the standard library.
if stdLib {
continue
}
if _, ok := deps[path]; !ok {
deps[path] = struct{}{}
addTransitiveClosure(path)
}
}
}
for path := range roots {
addTransitiveClosure(path)
}
addTransitiveClosure(importPath)
depsSlice = append(depsSlice, setToSlice(deps)...)
}
return depsSlice
}
func run(name string, args ...string) ([]byte, error) {
if buildV {
fmt.Println(name, args)
}
var cmd = exec.Command(name, args...)
return cmd.CombinedOutput()
}
func setToSlice(set map[string]struct{}) []string {
var slice []string
for k := range set {
slice = append(slice, k)
}
return slice
}
// readCmds returns the list of cmds declared in the given glockfile.
// They must appear at the top of the file, with the syntax:
// cmd code.google.com/p/go.tools/cmd/godoc
// cmd github.com/robfig/glock
func readCmds(importPath string) []string {
var (
glockfile io.ReadCloser
err error
)
for _, gopath := range gopaths() {
glockfile, err = os.Open(glockFilename(gopath, importPath))
if err == nil {
break
}
}
if err != nil {
if os.IsNotExist(err) {
return nil
}
perror(err)
}
var cmds []string
var scanner = bufio.NewScanner(glockfile)
for scanner.Scan() {
var fields = strings.Fields(scanner.Text())
if len(fields) != 2 || fields[0] != "cmd" {
return cmds
}
cmds = append(cmds, fields[1])
}
if err := scanner.Err(); err != nil {
perror(err)
}
return cmds
}
// Keep edits to vcs.go separate from the stock version.
var headCmds = map[string]string{
"git": "rev-parse HEAD", // 2bebebd91805dbb931317f7a4057e4e8de9d9781
"hg": "id", // 19114a3ee7d5+ tip
"bzr": "log -r-1 --line", // 50: Dimiter Naydenov 2014-02-12 [merge] ec2: Added (Un)AssignPrivateIPAddresses APIs
}
var (
revisionSeparator = regexp.MustCompile(`[ :+]+`)
validRevision = regexp.MustCompile(`^[\d\w]+$`)
)
func (v *vcsCmd) head(dir, repo string) (string, error) {
var output, err = v.runOutput(dir, headCmds[v.cmd], "dir", dir, "repo", repo)
if err != nil {
return "", err
}
return parseHEAD(output)
}
func parseHEAD(output []byte) (string, error) {
// Handle a case where HG returns success but prints an error, causing our
// parsing of the revision id to break.
var str = strings.TrimSpace(string(output))
for strings.HasPrefix(str, "*** ") {
var i = strings.Index(str, "\n")
if i == -1 {
break
}
str = str[i+1:]
}
var head = revisionSeparator.Split(str, -1)[0]
if !validRevision.MatchString(head) {
fmt.Fprintln(os.Stderr, string(output))
return "", errors.New("error getting head revision")
}
return head, nil
}