1
0
mirror of https://github.com/golang/go synced 2024-11-21 17:54:39 -07:00

gofmt: add -diff

Some code duplication with gofix.

R=rsc, gri, bradfitzgo, r2, adg, peterGo, r, brainman
CC=golang-dev
https://golang.org/cl/4430054
This commit is contained in:
David Crawshaw 2011-04-20 10:07:56 -07:00 committed by Robert Griesemer
parent aaa6ce2301
commit 123e89fef1

View File

@ -6,6 +6,7 @@ package main
import (
"bytes"
"exec"
"flag"
"fmt"
"go/ast"
@ -28,6 +29,7 @@ var (
write = flag.Bool("w", false, "write result to (source) file instead of stdout")
rewriteRule = flag.String("r", "", "rewrite rule (e.g., 'α[β:len(α)] -> α[β:]')")
simplifyAST = flag.Bool("s", false, "simplify code")
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
// layout control
comments = flag.Bool("comments", true, "print comments")
@ -134,9 +136,17 @@ func processFile(filename string, in io.Reader, out io.Writer) os.Error {
return err
}
}
if *doDiff {
data, err := diff(src, res)
if err != nil {
return fmt.Errorf("computing diff: %s", err)
}
fmt.Printf("diff %s fixed/%s\n", filename, filename)
out.Write(data)
}
}
if !*list && !*write {
if !*list && !*write && !*doDiff {
_, err = out.Write(res)
}
@ -230,3 +240,37 @@ func gofmtMain() {
}
}
}
func diff(b1, b2 []byte) (data []byte, err os.Error) {
f1, err := ioutil.TempFile("", "gofmt")
if err != nil {
return nil, err
}
defer os.Remove(f1.Name())
defer f1.Close()
f2, err := ioutil.TempFile("", "gofmt")
if err != nil {
return nil, err
}
defer os.Remove(f2.Name())
defer f2.Close()
f1.Write(b1)
f2.Write(b2)
diffcmd, err := exec.LookPath("diff")
if err != nil {
return nil, err
}
c, err := exec.Run(diffcmd, []string{"diff", "-u", f1.Name(), f2.Name()},
nil, "", exec.DevNull, exec.Pipe, exec.MergeWithStdout)
if err != nil {
return nil, err
}
defer c.Close()
return ioutil.ReadAll(c.Stdout)
}