]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use relative paths in go fix, go fmt, go vet output
authorRuss Cox <rsc@golang.org>
Thu, 12 Jan 2012 23:28:52 +0000 (15:28 -0800)
committerRuss Cox <rsc@golang.org>
Thu, 12 Jan 2012 23:28:52 +0000 (15:28 -0800)
Fixes #2686.

R=golang-dev, bradfitz, r
CC=golang-dev
https://golang.org/cl/5528089

src/cmd/go/build.go
src/cmd/go/fix.go
src/cmd/go/fmt.go
src/cmd/go/vet.go

index 71b606d76e0c4b5eb81e5296898dba6db563f44f..4a046391db818071d3ad6e281e9251ea5513a882 100644 (file)
@@ -794,8 +794,13 @@ func (b *builder) showcmd(dir string, format string, args ...interface{}) {
 // showOutput also replaces references to the work directory with $WORK.
 //
 func (b *builder) showOutput(dir, desc, out string) {
-       prefix := "# " + desc + "\n"
-       suffix := relPaths(dir, out)
+       prefix := "# " + desc
+       suffix := "\n" + out
+       pwd, _ := os.Getwd()
+       if reldir, err := filepath.Rel(pwd, dir); err == nil && len(reldir) < len(dir) {
+               suffix = strings.Replace(suffix, " "+dir, " "+reldir, -1)
+               suffix = strings.Replace(suffix, "\n"+dir, "\n"+reldir, -1)
+       }
        suffix = strings.Replace(suffix, " "+b.work, " $WORK", -1)
 
        b.output.Lock()
@@ -803,16 +808,19 @@ func (b *builder) showOutput(dir, desc, out string) {
        fmt.Print(prefix, suffix)
 }
 
-// relPaths returns a copy of out with references to dir
-// made relative to the current directory if that would be shorter.
-func relPaths(dir, out string) string {
-       x := "\n" + out
+// relPaths returns a copy of paths with absolute paths
+// made relative to the current directory if they would be shorter.
+func relPaths(paths []string) []string {
+       var out []string
        pwd, _ := os.Getwd()
-       if reldir, err := filepath.Rel(pwd, dir); err == nil && len(reldir) < len(dir) {
-               x = strings.Replace(x, " "+dir, " "+reldir, -1)
-               x = strings.Replace(x, "\n"+dir, "\n"+reldir, -1)
+       for _, p := range paths {
+               rel, err := filepath.Rel(pwd, p)
+               if err == nil && len(rel) < len(p) {
+                       p = rel
+               }
+               out = append(out, p)
        }
-       return x[1:]
+       return out
 }
 
 // errPrintedOutput is a special error indicating that a command failed
index fdefe8db6ed91c717d902933a539970d44842815..bae9f5c9820595c424493dc0d483ee71e3b27c2a 100644 (file)
@@ -25,6 +25,6 @@ func runFix(cmd *Command, args []string) {
                // Use pkg.gofiles instead of pkg.Dir so that
                // the command only applies to this package,
                // not to packages in subdirectories.
-               run(stringList("gofix", pkg.gofiles))
+               run(stringList("gofix", relPaths(pkg.gofiles)))
        }
 }
index fb0b0911920dcb9e6babb9adfa66f3e3da8440e8..4a47e2ea2fa88c39b6fa5da783c88066010d0aa6 100644 (file)
@@ -26,7 +26,7 @@ func runFmt(cmd *Command, args []string) {
                // Use pkg.gofiles instead of pkg.Dir so that
                // the command only applies to this package,
                // not to packages in subdirectories.
-               run(stringList("gofmt", "-I", "w", pkg.gofiles))
+               run(stringList("gofmt", "-l", "-w", relPaths(pkg.gofiles)))
        }
 }
 
index c1e17dfd0cf213b47204b7efecd091bc6ae7ff91..52c3200325f0bb4b192e820d1a6f23c90398fdf1 100644 (file)
@@ -25,6 +25,6 @@ func runVet(cmd *Command, args []string) {
                // Use pkg.gofiles instead of pkg.Dir so that
                // the command only applies to this package,
                // not to packages in subdirectories.
-               run("govet", pkg.gofiles)
+               run("govet", relPaths(pkg.gofiles))
        }
 }