diff --git a/fetch.go b/fetch.go index 08af2b1..f04e3f6 100644 --- a/fetch.go +++ b/fetch.go @@ -14,13 +14,14 @@ import ( ) var ( - branch string - revision string // revision (commit) - tag string - noRecurse bool - insecure bool // Allow the use of insecure protocols - tests bool - all bool + branch string + revision string // revision (commit) + tag string + noRecurse bool + insecure bool // Allow the use of insecure protocols + tests bool + all bool + noMakefiles bool ) func addFetchFlags(fs *flag.FlagSet) { @@ -31,6 +32,7 @@ func addFetchFlags(fs *flag.FlagSet) { fs.BoolVar(&insecure, "precaire", false, "allow the use of insecure protocols") fs.BoolVar(&tests, "t", false, "fetch _test.go files and testdata") fs.BoolVar(&all, "a", false, "fetch all files and subfolders") + fs.BoolVar(&noMakefiles, "no-makefiles", false, "do not fetch makefiles") } var cmdFetch = &Command{ @@ -50,6 +52,8 @@ from private repositories that cannot be probed. Flags: -t fetch also _test.go files and testdata. + -no-makefiles + do not fetch *.mak files (makefiles) -a fetch all files and subfolders, ignoring ONLY .git, .hg and .bzr. -branch branch @@ -196,6 +200,7 @@ func fetchRecursive(m *vendor.Manifest, fullPath string, level int) error { Path: extra, NoTests: !tests, AllFiles: all, + Makefiles: !noMakefiles, } if err := m.AddDependency(dep); err != nil { @@ -207,7 +212,7 @@ func fetchRecursive(m *vendor.Manifest, fullPath string, level int) error { dst := filepath.Join(vendorDir, dep.Importpath) src := filepath.Join(wc.Dir(), dep.Path) - if err := fileutils.Copypath(dst, src, !dep.NoTests, dep.AllFiles); err != nil { + if err := fileutils.Copypath(dst, src, !dep.NoTests, dep.AllFiles, !noMakefiles || dep.Makefiles); err != nil { return err } @@ -230,7 +235,7 @@ func fetchRecursive(m *vendor.Manifest, fullPath string, level int) error { return fmt.Errorf("unable to derive the root repo import path") } rootRepoPath := strings.TrimRight(strings.TrimSuffix(dep.Importpath, dep.Path), "/") - deps, err := vendor.ParseImports(src, wc.Dir(), rootRepoPath, tests, all) + deps, err := vendor.ParseImports(src, wc.Dir(), rootRepoPath, tests, all, !noMakefiles) if err != nil { return fmt.Errorf("failed to parse imports: %s", err) } diff --git a/fileutils/fileutils.go b/fileutils/fileutils.go index da0db9e..0438f84 100644 --- a/fileutils/fileutils.go +++ b/fileutils/fileutils.go @@ -26,7 +26,7 @@ var licenseFiles = []string{ "LICENSE", "LICENCE", "UNLICENSE", "COPYING", "COPYRIGHT", } -func ShouldSkip(path string, info os.FileInfo, tests, all bool) bool { +func ShouldSkip(path string, info os.FileInfo, tests, all, makefiles bool) bool { name := filepath.Base(path) relevantFile := false @@ -49,6 +49,10 @@ func ShouldSkip(path string, info os.FileInfo, tests, all bool) bool { case all && !(name == ".git" && info.IsDir()) && name != ".bzr" && name != ".hg": skip = false + // Include all makefiles + case makefiles && strings.HasSuffix(name, ".mak"): + skip = false + // Include all files in a testdata folder case tests && testdata: skip = false @@ -75,13 +79,13 @@ func ShouldSkip(path string, info os.FileInfo, tests, all bool) bool { // Copypath copies the contents of src to dst, excluding any file that is not // relevant to the Go compiler. -func Copypath(dst string, src string, tests, all bool) error { +func Copypath(dst string, src string, tests, all, makefiles bool) error { err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error { if err != nil { return err } - skip := ShouldSkip(path, info, tests, all) + skip := ShouldSkip(path, info, tests, all, makefiles) if skip { if info.IsDir() { diff --git a/fileutils/fileutils_test.go b/fileutils/fileutils_test.go index 2be29c8..a89e0b3 100644 --- a/fileutils/fileutils_test.go +++ b/fileutils/fileutils_test.go @@ -15,7 +15,7 @@ func TestCopypathSymlinks(t *testing.T) { dst := mktemp(t) defer RemoveAll(dst) src := filepath.Join("_testdata", "copyfile") - if err := Copypath(dst, src, true, false); err != nil { + if err := Copypath(dst, src, true, false, false); err != nil { t.Fatalf("copypath(%s, %s): %v", dst, src, err) } res, err := os.Readlink(filepath.Join(dst, "a", "rick")) @@ -34,3 +34,27 @@ func mktemp(t *testing.T) string { } return s } + +func TestShouldSkip(t *testing.T) { + _, filename, _, _ := runtime.Caller(1) + stat, _ := os.Stat(filename) + + expectations := [][]interface{}{ + []interface{}{"a.go", stat, false, false, false, false}, // default: go files are ok + []interface{}{"a_test.go", stat, false, false, false, true}, // default: test files are not ok + []interface{}{"a.mak", stat, false, false, false, true}, // default: makefiles are not ok + []interface{}{"a.rand", stat, false, false, false, true}, // default: all files are not ok + + []interface{}{"a_test.go", stat, true, false, false, false}, // Allow test files + []interface{}{"a.mak", stat, false, false, true, false}, // Allow makefiles + []interface{}{"a.rand", stat, false, true, false, false}, // Allow all files + } + + for _, e := range expectations { + result := ShouldSkip(e[0].(string), e[1].(os.FileInfo), e[2].(bool), e[3].(bool), e[4].(bool)) + + if result != e[5].(bool) { + t.Fatalf("wrong result expected(%v) got(%v)", e[5].(bool), result) + } + } +} diff --git a/gbvendor/imports.go b/gbvendor/imports.go index 299bf6e..6cc2ec1 100644 --- a/gbvendor/imports.go +++ b/gbvendor/imports.go @@ -19,7 +19,7 @@ import ( // ParseImports parses Go packages from a specific root returning a set of import paths. // vendorRoot is how deep to go looking for vendor folders, usually the repo root. // vendorPrefix is the vendorRoot import path. -func ParseImports(root, vendorRoot, vendorPrefix string, tests, all bool) (map[string]bool, error) { +func ParseImports(root, vendorRoot, vendorPrefix string, tests, all, makefiles bool) (map[string]bool, error) { pkgs := make(map[string]bool) var walkFn = func(p string, info os.FileInfo, err error) error { @@ -27,7 +27,7 @@ func ParseImports(root, vendorRoot, vendorPrefix string, tests, all bool) (map[s return err } - if fileutils.ShouldSkip(p, info, tests, all) { + if fileutils.ShouldSkip(p, info, tests, all, makefiles) { if info.IsDir() { return filepath.SkipDir } diff --git a/gbvendor/manifest.go b/gbvendor/manifest.go index 54f304a..eb48e45 100644 --- a/gbvendor/manifest.go +++ b/gbvendor/manifest.go @@ -117,6 +117,9 @@ type Dependency struct { // AllFiles indicates that no files were ignored. AllFiles bool `json:"allfiles,omitempty"` + + // Makefiles indicates that makefiles would be included. + Makefiles bool `json:"makefiles,omitempty"` } // WriteManifest writes a Manifest to the path. If the manifest does diff --git a/restore.go b/restore.go index dc0b1e3..523d7ab 100644 --- a/restore.go +++ b/restore.go @@ -123,7 +123,7 @@ func downloadDependency(dep vendor.Dependency, errors *uint32, vendorDir string, } } - if err := fileutils.Copypath(dst, src, !dep.NoTests, dep.AllFiles); err != nil { + if err := fileutils.Copypath(dst, src, !dep.NoTests, dep.AllFiles, dep.Makefiles); err != nil { return err } diff --git a/update.go b/update.go index 9c73a49..c7fc99d 100644 --- a/update.go +++ b/update.go @@ -99,6 +99,7 @@ Flags: Path: d.Path, NoTests: d.NoTests, AllFiles: d.AllFiles, + Makefiles: d.Makefiles, } if err := fileutils.RemoveAll(filepath.Join(vendorDir, filepath.FromSlash(d.Importpath))); err != nil { @@ -109,7 +110,7 @@ Flags: dst := filepath.Join(vendorDir, filepath.FromSlash(dep.Importpath)) src := filepath.Join(wc.Dir(), dep.Path) - if err := fileutils.Copypath(dst, src, !d.NoTests, d.AllFiles); err != nil { + if err := fileutils.Copypath(dst, src, !d.NoTests, d.AllFiles, d.Makefiles); err != nil { return err }