Skip to content
This repository was archived by the owner on Aug 25, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand All @@ -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)
}
Expand Down
10 changes: 7 additions & 3 deletions fileutils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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() {
Expand Down
26 changes: 25 additions & 1 deletion fileutils/fileutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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)
}
}
}
4 changes: 2 additions & 2 deletions gbvendor/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ 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 {
if err != nil {
return err
}

if fileutils.ShouldSkip(p, info, tests, all) {
if fileutils.ShouldSkip(p, info, tests, all, makefiles) {
if info.IsDir() {
return filepath.SkipDir
}
Expand Down
3 changes: 3 additions & 0 deletions gbvendor/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down