From: Brad Fitzpatrick Date: Thu, 8 Jun 2017 22:38:15 +0000 (+0000) Subject: cmd/go: ignore dot and underscore files in fmt, fix, and get -fix X-Git-Tag: go1.9beta1~61 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=760636d55a87fe7348e9e14ef1474df2a58439de;p=gostls13.git cmd/go: ignore dot and underscore files in fmt, fix, and get -fix No test because as far as I can tell, there aren't existing tests for these. Fixes #18383 Change-Id: I06eaef05777a1474886167e3797c5bcd93189d1b Reviewed-on: https://go-review.googlesource.com/45156 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- diff --git a/src/cmd/go/internal/base/path.go b/src/cmd/go/internal/base/path.go index 7a51181c97..4f12fa8c28 100644 --- a/src/cmd/go/internal/base/path.go +++ b/src/cmd/go/internal/base/path.go @@ -44,6 +44,28 @@ func RelPaths(paths []string) []string { return out } +// FilterDotUnderscoreFiles returns a slice containing all elements +// of path whose base name doesn't begin with "." or "_". +func FilterDotUnderscoreFiles(path []string) []string { + var out []string // lazily initialized + for i, p := range path { + base := filepath.Base(p) + if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") { + if out == nil { + out = append(make([]string, 0, len(path)), path[:i]...) + } + continue + } + if out != nil { + out = append(out, p) + } + } + if out == nil { + return path + } + return out +} + // IsTestFile reports whether the source file is a set of tests and should therefore // be excluded from coverage analysis. func IsTestFile(file string) bool { diff --git a/src/cmd/go/internal/fix/fix.go b/src/cmd/go/internal/fix/fix.go index 377cd037fa..788d49bcb6 100644 --- a/src/cmd/go/internal/fix/fix.go +++ b/src/cmd/go/internal/fix/fix.go @@ -33,6 +33,7 @@ func runFix(cmd *base.Command, args []string) { // Use pkg.gofiles instead of pkg.Dir so that // the command only applies to this package, // not to packages in subdirectories. - base.Run(str.StringList(cfg.BuildToolexec, base.Tool("fix"), base.RelPaths(pkg.Internal.AllGoFiles))) + files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles)) + base.Run(str.StringList(cfg.BuildToolexec, base.Tool("fix"), files)) } } diff --git a/src/cmd/go/internal/fmtcmd/fmt.go b/src/cmd/go/internal/fmtcmd/fmt.go index a4bf79e265..5b54bdc257 100644 --- a/src/cmd/go/internal/fmtcmd/fmt.go +++ b/src/cmd/go/internal/fmtcmd/fmt.go @@ -45,7 +45,8 @@ func runFmt(cmd *base.Command, args []string) { // Use pkg.gofiles instead of pkg.Dir so that // the command only applies to this package, // not to packages in subdirectories. - base.Run(str.StringList(gofmt, "-l", "-w", base.RelPaths(pkg.Internal.AllGoFiles))) + files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles)) + base.Run(str.StringList(gofmt, "-l", "-w", files)) } } diff --git a/src/cmd/go/internal/get/get.go b/src/cmd/go/internal/get/get.go index 1df7888d7d..45891bd341 100644 --- a/src/cmd/go/internal/get/get.go +++ b/src/cmd/go/internal/get/get.go @@ -298,7 +298,8 @@ func download(arg string, parent *load.Package, stk *load.ImportStack, mode int) // due to wildcard expansion. for _, p := range pkgs { if *getFix { - base.Run(cfg.BuildToolexec, str.StringList(base.Tool("fix"), base.RelPaths(p.Internal.AllGoFiles))) + files := base.FilterDotUnderscoreFiles(base.RelPaths(p.Internal.AllGoFiles)) + base.Run(cfg.BuildToolexec, str.StringList(base.Tool("fix"), files)) // The imports might have changed, so reload again. p = load.ReloadPackage(arg, stk)