Skip to content
This repository was archived by the owner on Aug 25, 2018. It is now read-only.

Commit 0572fac

Browse files
committed
Don't check ignored files for imports - closes #63
1 parent 02e2422 commit 0572fac

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-48
lines changed

fetch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func fetchRecursive(m *vendor.Manifest, fullPath string, level int) error {
229229
return fmt.Errorf("unable to derive the root repo import path")
230230
}
231231
rootRepoPath := strings.TrimRight(strings.TrimSuffix(dep.Importpath, dep.Path), "/")
232-
deps, err := vendor.ParseImports(src, wc.Dir(), rootRepoPath)
232+
deps, err := vendor.ParseImports(src, wc.Dir(), rootRepoPath, tests, all)
233233
if err != nil {
234234
return fmt.Errorf("failed to parse imports: %s", err)
235235
}

fileutils/fileutils.go

+48-42
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,53 @@ var licenseFiles = []string{
2626
"LICENSE", "LICENCE", "UNLICENSE", "COPYING", "COPYRIGHT",
2727
}
2828

29+
func ShouldSkip(path string, info os.FileInfo, tests, all bool) bool {
30+
name := filepath.Base(path)
31+
32+
relevantFile := false
33+
for _, ext := range goFileTypes {
34+
if strings.HasSuffix(name, ext) {
35+
relevantFile = true
36+
break
37+
}
38+
}
39+
40+
testdata := false
41+
for _, n := range strings.Split(filepath.Dir(path), string(filepath.Separator)) {
42+
if n == "testdata" || n == "_testdata" {
43+
testdata = true
44+
}
45+
}
46+
47+
skip := false
48+
switch {
49+
case all && !(name == ".git" && info.IsDir()) && name != ".bzr" && name != ".hg":
50+
skip = false
51+
52+
// Include all files in a testdata folder
53+
case tests && testdata:
54+
skip = false
55+
56+
// https://golang.org/cmd/go/#hdr-Description_of_package_lists
57+
case strings.HasPrefix(name, "."):
58+
skip = true
59+
case strings.HasPrefix(name, "_") && name != "_testdata":
60+
skip = true
61+
62+
case !tests && name == "_testdata" && info.IsDir():
63+
skip = true
64+
case !tests && name == "testdata" && info.IsDir():
65+
skip = true
66+
case !tests && strings.HasSuffix(name, "_test.go") && !info.IsDir():
67+
skip = true
68+
69+
case !relevantFile && !info.IsDir():
70+
skip = true
71+
}
72+
73+
return skip
74+
}
75+
2976
// Copypath copies the contents of src to dst, excluding any file that is not
3077
// relevant to the Go compiler.
3178
func Copypath(dst string, src string, tests, all bool) error {
@@ -34,48 +81,7 @@ func Copypath(dst string, src string, tests, all bool) error {
3481
return err
3582
}
3683

37-
name := filepath.Base(path)
38-
39-
relevantFile := false
40-
for _, ext := range goFileTypes {
41-
if strings.HasSuffix(name, ext) {
42-
relevantFile = true
43-
break
44-
}
45-
}
46-
47-
testdata := false
48-
for _, n := range strings.Split(filepath.Dir(path), string(filepath.Separator)) {
49-
if n == "testdata" || n == "_testdata" {
50-
testdata = true
51-
}
52-
}
53-
54-
skip := false
55-
switch {
56-
case all && !(name == ".git" && info.IsDir()) && name != ".bzr" && name != ".hg":
57-
skip = false
58-
59-
// Include all files in a testdata folder
60-
case tests && testdata:
61-
skip = false
62-
63-
// https://golang.org/cmd/go/#hdr-Description_of_package_lists
64-
case strings.HasPrefix(name, "."):
65-
skip = true
66-
case strings.HasPrefix(name, "_") && name != "_testdata":
67-
skip = true
68-
69-
case !tests && name == "_testdata" && info.IsDir():
70-
skip = true
71-
case !tests && name == "testdata" && info.IsDir():
72-
skip = true
73-
case !tests && strings.HasSuffix(name, "_test.go") && !info.IsDir():
74-
skip = true
75-
76-
case !relevantFile && !info.IsDir():
77-
skip = true
78-
}
84+
skip := ShouldSkip(path, info, tests, all)
7985

8086
if skip {
8187
if info.IsDir() {

gbvendor/imports.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,29 @@ import (
1212
"path"
1313
"path/filepath"
1414
"strings"
15+
16+
"github.com/FiloSottile/gvt/fileutils"
1517
)
1618

1719
// ParseImports parses Go packages from a specific root returning a set of import paths.
1820
// vendorRoot is how deep to go looking for vendor folders, usually the repo root.
1921
// vendorPrefix is the vendorRoot import path.
20-
func ParseImports(root, vendorRoot, vendorPrefix string) (map[string]bool, error) {
22+
func ParseImports(root, vendorRoot, vendorPrefix string, tests, all bool) (map[string]bool, error) {
2123
pkgs := make(map[string]bool)
2224

2325
var walkFn = func(p string, info os.FileInfo, err error) error {
24-
if info.IsDir() {
25-
name := info.Name()
26-
if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") || name == "testdata" {
26+
if err != nil {
27+
return err
28+
}
29+
30+
if fileutils.ShouldSkip(p, info, tests, all) {
31+
if info.IsDir() {
2732
return filepath.SkipDir
2833
}
2934
return nil
3035
}
31-
if filepath.Ext(p) != ".go" { // Parse only go source files
36+
37+
if info.IsDir() || filepath.Ext(p) != ".go" {
3238
return nil
3339
}
3440

0 commit comments

Comments
 (0)