]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: ignore dot and underscore files in fmt, fix, and get -fix
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 8 Jun 2017 22:38:15 +0000 (22:38 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 8 Jun 2017 23:54:27 +0000 (23:54 +0000)
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 <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/internal/base/path.go
src/cmd/go/internal/fix/fix.go
src/cmd/go/internal/fmtcmd/fmt.go
src/cmd/go/internal/get/get.go

index 7a51181c9736e9cdf8549a7192f9bdcf67f419ce..4f12fa8c28c316f24a558c9abfb01eb50948961f 100644 (file)
@@ -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 {
index 377cd037faf3c0e2a46aa8b3989fb953f893b4cb..788d49bcb6070c09a398458af35b1acfd078a704 100644 (file)
@@ -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))
        }
 }
index a4bf79e2657f89886ef7b2c240a45c1f3b0dc368..5b54bdc2578d7cd0a53b9d5df384f73ea63b58ed 100644 (file)
@@ -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))
        }
 }
 
index 1df7888d7db574705f7b4d0f4be18f28d8bcf0e1..45891bd34109c62ffbc9ced48828b8e65f38fe3b 100644 (file)
@@ -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)