forked from dpw/vendetta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
720 lines (595 loc) · 14.4 KB
/
main.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
package main
import (
"bufio"
"flag"
"fmt"
"go/build"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"regexp"
"sort"
"strings"
)
// TODO:
//
// check the directory is a git repo, and error if not
//
// option to run in any directory of git repo. Needs to figure out
// path from repo root.
//
// verbose option to print git commands being run
//
// do import path checking, as described at https://golang.org/cmd/go/
//
// popen should include command in errors
//
// Deal with git being fussy when a submodule is removed then re-added
//
// warn when it looks like a package ought to be present at the
// particular path, but it's not. E.g. when resolving an import of
// github.com/foo/bar/baz, we find github.com/foo.
//
// check that declared package names match dirs
//
// infer project name from GOPATH
//
// use type aliases for packages and paths?
type config struct {
rootDir string
projectName string
update bool
prune bool
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [ <project directory> ]\n",
os.Args[0])
flag.PrintDefaults()
}
var cf config
flag.StringVar(&cf.projectName, "n", "",
"base package name for the project, e.g. github.com/user/proj")
flag.BoolVar(&cf.update, "u", false,
"update dependency submodules from their remote repos")
flag.BoolVar(&cf.prune, "p", false,
"prune unused dependency submodules")
flag.Parse()
cf.rootDir = "."
switch {
case flag.NArg() == 1:
cf.rootDir = flag.Arg(1)
case flag.NArg() > 1:
flag.Usage()
os.Exit(2)
}
if err := run(&cf); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
type vendetta struct {
*config
goPath
goPaths map[string]*goPath
processedDirs map[string]struct{}
submodules []submodule
trace []trace
}
type trace struct {
dir string
pkg string
}
type submodule struct {
dir string
updated bool
used bool
}
// Conceptually, a gopath element. These are arranged into linked
// lists, representing the gopath applicable for a particular
// directory (produced by getGoPath and memoized in the goPaths map).
type goPath struct {
dir string
prefixes map[string]struct{}
next *goPath
}
func run(cf *config) error {
v := vendetta{
config: cf,
goPaths: make(map[string]*goPath),
processedDirs: make(map[string]struct{}),
}
v.goPaths[""] = &goPath{dir: "vendor", next: &v.goPath}
v.prefixes = make(map[string]struct{})
if cf.projectName != "" {
v.prefixes[cf.projectName] = struct{}{}
} else {
if err := v.inferProjectNameFromGit(); err != nil {
return err
}
if len(v.prefixes) == 0 {
return fmt.Errorf("Unable to infer project name; specify it explicitly with the '-p' option.")
}
}
if err := v.checkSubmodules(); err != nil {
return err
}
if err := v.populateSubmodules(); err != nil {
return err
}
if err := v.processRecursive("", true); err != nil {
return err
}
return v.pruneSubmodules()
}
var remoteUrlRE = regexp.MustCompile(`^(?:https://github\.com/|git@github\.com:)(.*\.?)$`)
func (v *vendetta) inferProjectNameFromGit() error {
remotes, err := popen("git", "-C", v.rootDir, "remote", "-v")
if err != nil {
return err
}
defer remotes.close()
for remotes.Scan() {
fields := splitWS(remotes.Text())
if len(fields) < 2 {
return fmt.Errorf("could not parse 'git remote' output")
}
m := remoteUrlRE.FindStringSubmatch(fields[1])
if m != nil {
name := m[1]
if strings.HasSuffix(name, ".git") {
name = name[:len(name)-4]
}
name = "github.com/" + name
if _, found := v.prefixes[name]; !found {
fmt.Println("Inferred package name", name, "from git remote")
v.prefixes[name] = struct{}{}
}
}
}
if err := remotes.close(); err != nil {
return err
}
return nil
}
// Check for submodules that seem to be missing in the working tree.
func (v *vendetta) checkSubmodules() error {
var err2 error
if err := v.querySubmodules(func(path string) bool {
err2 = v.checkSubmodule(path)
return err2 == nil
}, "--recursive"); err != nil {
return err
}
return err2
}
func (v *vendetta) checkSubmodule(dir string) error {
foundSomething := false
if err := readDir(dir, func(fi os.FileInfo) bool {
foundSomething = true
return false
}); err != nil && !os.IsNotExist(err) {
return err
}
if !foundSomething {
return fmt.Errorf("The submodule '%s' doesn't seem to the present in the working tree. Maybe you forgot to update with 'git submodule update --init --recursive'?", dir)
}
return nil
}
func (v *vendetta) querySubmodules(f func(string) bool, args ...string) error {
args = append([]string{"-C", v.rootDir, "submodule", "status"}, args...)
status, err := popen("git", args...)
if err != nil {
return err
}
defer status.close()
for status.Scan() {
fields := splitWS(strings.TrimSpace(status.Text()))
if len(fields) < 2 {
return fmt.Errorf("could not parse 'git submodule status' output")
}
path := fields[1]
if !f(path) {
return nil
}
}
return status.close()
}
func (v *vendetta) populateSubmodules() error {
var submodules []string
if err := v.querySubmodules(func(path string) bool {
submodules = append(submodules, path)
return true
}); err != nil {
return err
}
sort.Strings(submodules)
v.submodules = make([]submodule, 0, len(submodules))
for _, p := range submodules {
v.submodules = append(v.submodules, submodule{dir: p})
}
return nil
}
func (v *vendetta) pathInSubmodule(path string) *submodule {
i := sort.Search(len(v.submodules), func(i int) bool {
return v.submodules[i].dir >= path
})
if i < len(v.submodules) && v.submodules[i].dir == path {
return &v.submodules[i]
}
if i > 0 && isSubpath(path, v.submodules[i-1].dir) {
return &v.submodules[i-1]
}
return nil
}
func (v *vendetta) addSubmodule(dir string) {
i := sort.Search(len(v.submodules), func(i int) bool {
return v.submodules[i].dir >= dir
})
submodules := make([]submodule, len(v.submodules)+1)
copy(submodules, v.submodules[:i])
submodules[i] = submodule{dir: dir, updated: true, used: true}
copy(submodules[i+1:], v.submodules[i:])
v.submodules = submodules
}
func isSubpath(path, dir string) bool {
return path == dir ||
(strings.HasPrefix(path, dir) && path[len(dir)] == os.PathSeparator)
}
func (v *vendetta) updateSubmodule(sm *submodule) error {
if sm.updated {
return nil
}
sm.updated = true
fmt.Fprintf(os.Stderr, "Updating submodule %s from remote\n", sm.dir)
if err := v.git("submodule", "update", "--remote", "--recursive", sm.dir); err != nil {
return err
}
// If we don't put the updated submodule into the index, a
// subsequent "git submodule update" will revert it, which can
// lead to surprises.
return v.git("add", sm.dir)
}
func (v *vendetta) pruneSubmodules() error {
for _, sm := range v.submodules {
if sm.used || !isSubpath(sm.dir, "vendor") {
continue
}
if v.prune {
fmt.Fprintf(os.Stderr, "Removing unused submodule %s\n",
sm.dir)
if err := v.git("rm", "-f", sm.dir); err != nil {
return err
}
if err := v.removeEmptyDirsAbove(sm.dir); err != nil {
return err
}
} else {
fmt.Fprintf(os.Stderr, "Unused submodule %s (use -p option to prune)\n", sm.dir)
}
}
return nil
}
func (v *vendetta) removeEmptyDirsAbove(dir string) error {
for {
dir = parentDir(dir)
if dir == "" {
return nil
}
empty := true
if err := readDir(v.realDir(dir), func(_ os.FileInfo) bool {
empty = false
return false
}); err != nil {
return err
}
if !empty {
return nil
}
if err := os.Remove(v.realDir(dir)); err != nil {
return err
}
}
}
// Get the directory name from a path. path.Dir doesn't
// do what we want in the case where there is no path
// separator:
func parentDir(path string) string {
slash := strings.LastIndexByte(path, os.PathSeparator)
dir := ""
if slash >= 0 {
dir = path[:slash]
}
return dir
}
var wsRE = regexp.MustCompile(`[ \t]+`)
func splitWS(s string) []string {
return wsRE.Split(s, -1)
}
func (v *vendetta) gitSubmoduleAdd(url, dir string) error {
fmt.Fprintf(os.Stderr, "Adding %s at %s\n", url, dir)
err := v.git("submodule", "add", url, dir)
if err != nil {
return err
}
v.addSubmodule(dir)
return nil
}
func (v *vendetta) git(args ...string) error {
return system("git", append([]string{"-C", v.rootDir}, args...)...)
}
func system(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err == nil {
err = cmd.Wait()
if err == nil {
return nil
}
}
return fmt.Errorf("Command failed: %s %s (%s)",
name, strings.Join(args, " "), err)
}
type popenLines struct {
cmd *exec.Cmd
stdout io.ReadCloser
*bufio.Scanner
}
func popen(name string, args ...string) (popenLines, error) {
cmd := exec.Command(name, args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return popenLines{}, err
}
cmd.Stderr = os.Stderr
p := popenLines{cmd: cmd, stdout: stdout}
if err := cmd.Start(); err != nil {
return popenLines{}, err
}
p.Scanner = bufio.NewScanner(stdout)
return p, nil
}
func (p popenLines) close() error {
res := p.Scanner.Err()
setRes := func(err error) {
if res == nil {
res = err
}
}
if p.stdout != nil {
_, err := io.Copy(ioutil.Discard, p.stdout)
p.stdout = nil
if err != nil {
setRes(err)
p.cmd.Process.Kill()
}
}
if p.cmd != nil {
setRes(p.cmd.Wait())
p.cmd = nil
}
return res
}
func (v *vendetta) realDir(dir string) string {
return path.Join(v.rootDir, dir)
}
func (v *vendetta) processRecursive(dir string, root bool) error {
if err := v.process(dir, true, false); err != nil {
return err
}
var subdirs []string
if err := readDir(v.realDir(dir), func(fi os.FileInfo) bool {
if fi.IsDir() {
subdirs = append(subdirs, fi.Name())
}
return true
}); err != nil {
return err
}
for _, subdir := range subdirs {
switch subdir {
case "vendor":
if root {
continue
}
case "testdata":
continue
}
err := v.processRecursive(path.Join(dir, subdir), false)
if err != nil {
return err
}
}
return nil
}
func (v *vendetta) process(dir string, testsToo bool, strict bool) error {
if _, found := v.processedDirs[dir]; found {
return nil
}
v.processedDirs[dir] = struct{}{}
pkg, err := build.Default.ImportDir(v.realDir(dir), 0)
if err != nil {
if _, ok := err.(*build.NoGoError); ok && !strict {
return nil
}
return fmt.Errorf("gathering imports in %s: %s",
v.realDir(dir), err)
}
deps := func(imports []string) error {
for _, imp := range imports {
v.trace = append(v.trace, trace{dir, imp})
if err := v.dependency(dir, imp); err != nil {
return err
}
v.trace = v.trace[:len(v.trace)-1]
}
return nil
}
if err := deps(pkg.Imports); err != nil {
return err
}
if testsToo {
if err := deps(pkg.TestImports); err != nil {
return err
}
}
return nil
}
func (v *vendetta) dependency(dir string, pkg string) error {
found, pkgdir, err := v.searchGoPath(dir, pkg)
switch {
case err != nil:
return err
case found:
// Does it fall within an existing submodule
// args..under vendor/ ?
if sm := v.pathInSubmodule(pkgdir); sm != nil {
sm.used = true
if v.update {
if err := v.updateSubmodule(sm); err != nil {
return err
}
}
}
return v.process(pkgdir, false, true)
}
// Figure out how to obtain the package. This is a rough
// approximation of what golang's vcs.go does:
bits := strings.Split(pkg, "/")
// Exclude golang standard packages
if !strings.Contains(bits[0], ".") {
return nil
}
var rootPkg, url string
if bits[0] == "github.com" {
if len(bits) < 3 {
return fmt.Errorf("github.com package name %s seems to be truncated", pkg)
}
rootPkg = strings.Join(bits[:3], "/")
url = "https://" + rootPkg
} else {
rr, err := queryRepoRoot(pkg, secure)
if err != nil {
return err
}
if rr.vcs != "git" {
return fmt.Errorf("Package %s does not live in a git repo")
}
rootPkg = rr.root
url = rr.repo
}
projDir := path.Join("vendor", packageToPath(rootPkg))
if err := v.gitSubmoduleAdd(url, projDir); err != nil {
return err
}
return v.process(path.Join("vendor", packageToPath(pkg)), false, true)
}
// Search the gopath for the given dir to find an existing package
func (v *vendetta) searchGoPath(dir, pkg string) (bool, string, error) {
gp, err := v.getGoPath(dir)
if err != nil {
return false, "", err
}
for gp != nil {
found, pkgdir, err := gp.provides(pkg, v)
if err != nil {
return false, "", err
}
if found {
return found, pkgdir, nil
}
gp = gp.next
}
return false, "", nil
}
func (v *vendetta) getGoPath(dir string) (*goPath, error) {
gp := v.goPaths[dir]
if gp != nil {
return gp, nil
}
gp, err := v.getGoPath(parentDir(dir))
if err != nil {
return nil, err
}
// If there's a vendor/ dir here, we need to put it on the
// front of the gopath
vendorDir := path.Join(dir, "vendor")
fi, err := os.Stat(v.realDir(vendorDir))
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
} else if fi.IsDir() {
gp = &goPath{dir: vendorDir, next: gp}
}
v.goPaths[dir] = gp
return gp, nil
}
func (gp *goPath) provides(pkg string, v *vendetta) (bool, string, error) {
matched, pkg := gp.removePrefix(pkg)
if !matched {
return false, "", nil
}
foundGoSrc := false
pkgdir := path.Join(gp.dir, packageToPath(pkg))
if err := readDir(v.realDir(pkgdir), func(fi os.FileInfo) bool {
// Should check for symlinks here?
if fi.Mode().IsRegular() && strings.HasSuffix(fi.Name(), ".go") {
foundGoSrc = true
return false
}
return true
}); err != nil {
if os.IsNotExist(err) {
err = nil
}
return false, "", err
}
return foundGoSrc, pkgdir, nil
}
func (gp *goPath) removePrefix(pkg string) (bool, string) {
if gp.prefixes == nil {
return true, pkg
}
for prefix := range gp.prefixes {
if pkg == prefix {
return true, ""
} else if isSubpath(pkg, prefix) {
return true, pkg[len(prefix)+1:]
}
}
return false, ""
}
// Convert a package name to a filesystem path
func packageToPath(name string) string {
return strings.Replace(name, "/", string(os.PathSeparator), -1)
}
// Convert a filesystem path to a package name
func pathToPackage(path string) string {
return strings.Replace(path, string(os.PathSeparator), "/", -1)
}
func readDir(dir string, f func(os.FileInfo) bool) error {
dh, err := os.Open(dir)
if err != nil {
return err
}
defer dh.Close()
for {
fis, err := dh.Readdir(100)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
for _, fi := range fis {
if !f(fi) {
return nil
}
}
}
}