Commit c2ffd9d0 authored by Russ Cox's avatar Russ Cox

cmd/go: use relative paths in go fix, go fmt, go vet output

Fixes #2686.

R=golang-dev, bradfitz, r
CC=golang-dev
https://golang.org/cl/5528089
parent 4505ae38
...@@ -794,8 +794,13 @@ func (b *builder) showcmd(dir string, format string, args ...interface{}) { ...@@ -794,8 +794,13 @@ func (b *builder) showcmd(dir string, format string, args ...interface{}) {
// showOutput also replaces references to the work directory with $WORK. // showOutput also replaces references to the work directory with $WORK.
// //
func (b *builder) showOutput(dir, desc, out string) { func (b *builder) showOutput(dir, desc, out string) {
prefix := "# " + desc + "\n" prefix := "# " + desc
suffix := relPaths(dir, out) 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) suffix = strings.Replace(suffix, " "+b.work, " $WORK", -1)
b.output.Lock() b.output.Lock()
...@@ -803,16 +808,19 @@ func (b *builder) showOutput(dir, desc, out string) { ...@@ -803,16 +808,19 @@ func (b *builder) showOutput(dir, desc, out string) {
fmt.Print(prefix, suffix) fmt.Print(prefix, suffix)
} }
// relPaths returns a copy of out with references to dir // relPaths returns a copy of paths with absolute paths
// made relative to the current directory if that would be shorter. // made relative to the current directory if they would be shorter.
func relPaths(dir, out string) string { func relPaths(paths []string) []string {
x := "\n" + out var out []string
pwd, _ := os.Getwd() pwd, _ := os.Getwd()
if reldir, err := filepath.Rel(pwd, dir); err == nil && len(reldir) < len(dir) { for _, p := range paths {
x = strings.Replace(x, " "+dir, " "+reldir, -1) rel, err := filepath.Rel(pwd, p)
x = strings.Replace(x, "\n"+dir, "\n"+reldir, -1) 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 // errPrintedOutput is a special error indicating that a command failed
......
...@@ -25,6 +25,6 @@ func runFix(cmd *Command, args []string) { ...@@ -25,6 +25,6 @@ func runFix(cmd *Command, args []string) {
// Use pkg.gofiles instead of pkg.Dir so that // Use pkg.gofiles instead of pkg.Dir so that
// the command only applies to this package, // the command only applies to this package,
// not to packages in subdirectories. // not to packages in subdirectories.
run(stringList("gofix", pkg.gofiles)) run(stringList("gofix", relPaths(pkg.gofiles)))
} }
} }
...@@ -26,7 +26,7 @@ func runFmt(cmd *Command, args []string) { ...@@ -26,7 +26,7 @@ func runFmt(cmd *Command, args []string) {
// Use pkg.gofiles instead of pkg.Dir so that // Use pkg.gofiles instead of pkg.Dir so that
// the command only applies to this package, // the command only applies to this package,
// not to packages in subdirectories. // not to packages in subdirectories.
run(stringList("gofmt", "-I", "w", pkg.gofiles)) run(stringList("gofmt", "-l", "-w", relPaths(pkg.gofiles)))
} }
} }
......
...@@ -25,6 +25,6 @@ func runVet(cmd *Command, args []string) { ...@@ -25,6 +25,6 @@ func runVet(cmd *Command, args []string) {
// Use pkg.gofiles instead of pkg.Dir so that // Use pkg.gofiles instead of pkg.Dir so that
// the command only applies to this package, // the command only applies to this package,
// not to packages in subdirectories. // not to packages in subdirectories.
run("govet", pkg.gofiles) run("govet", relPaths(pkg.gofiles))
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment