diff options
author | Mikhail Fesenko <proggga@gmail.com> | 2019-10-28 21:51:00 +0000 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2019-10-28 23:59:10 +0000 |
commit | fd1e60f6e3bd42075e335a90ad36719ffed0eb1a (patch) | |
tree | 1f7534f05f914e138735e895b396d5e7de63c353 /src/cmd/gofmt | |
parent | 449b6abbacc464443a7faf166bf4db3df3e0f8da (diff) | |
download | go-fd1e60f6e3bd42075e335a90ad36719ffed0eb1a.tar.gz go-fd1e60f6e3bd42075e335a90ad36719ffed0eb1a.zip |
cmd/fix, cmd/go, cmd/gofmt: refactor common code into new internal diff package
Change-Id: Idac8473d1752059bf2f617fd7a781000ee2c3af4
GitHub-Last-Rev: 02a3aa1a3241d3ed4422518f1c954cd54bbe858e
GitHub-Pull-Request: golang/go#35141
Reviewed-on: https://go-review.googlesource.com/c/go/+/203218
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/cmd/gofmt')
-rw-r--r-- | src/cmd/gofmt/gofmt.go | 46 | ||||
-rw-r--r-- | src/cmd/gofmt/gofmt_test.go | 4 |
2 files changed, 8 insertions, 42 deletions
diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go index d7a77a9682..9e472b2d51 100644 --- a/src/cmd/gofmt/gofmt.go +++ b/src/cmd/gofmt/gofmt.go @@ -16,11 +16,12 @@ import ( "io" "io/ioutil" "os" - "os/exec" "path/filepath" "runtime" "runtime/pprof" "strings" + + "cmd/internal/diff" ) var ( @@ -141,7 +142,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error } } if *doDiff { - data, err := diff(src, res, filename) + data, err := diffWithReplaceTempFile(src, res, filename) if err != nil { return fmt.Errorf("computing diff: %s", err) } @@ -227,47 +228,12 @@ func gofmtMain() { } } -func writeTempFile(dir, prefix string, data []byte) (string, error) { - file, err := ioutil.TempFile(dir, prefix) - if err != nil { - return "", err - } - _, err = file.Write(data) - if err1 := file.Close(); err == nil { - err = err1 - } - if err != nil { - os.Remove(file.Name()) - return "", err - } - return file.Name(), nil -} - -func diff(b1, b2 []byte, filename string) (data []byte, err error) { - f1, err := writeTempFile("", "gofmt", b1) - if err != nil { - return - } - defer os.Remove(f1) - - f2, err := writeTempFile("", "gofmt", b2) - if err != nil { - return - } - defer os.Remove(f2) - - cmd := "diff" - if runtime.GOOS == "plan9" { - cmd = "/bin/ape/diff" - } - - data, err = exec.Command(cmd, "-u", f1, f2).CombinedOutput() +func diffWithReplaceTempFile(b1, b2 []byte, filename string) ([]byte, error) { + data, err := diff.Diff("gofmt", b1, b2) if len(data) > 0 { - // diff exits with a non-zero status when the files don't match. - // Ignore that failure as long as we get output. return replaceTempFilename(data, filename) } - return + return data, err } // replaceTempFilename replaces temporary filenames in diff with actual one. diff --git a/src/cmd/gofmt/gofmt_test.go b/src/cmd/gofmt/gofmt_test.go index 3008365cd2..98d3eb7eb2 100644 --- a/src/cmd/gofmt/gofmt_test.go +++ b/src/cmd/gofmt/gofmt_test.go @@ -112,7 +112,7 @@ func runTest(t *testing.T, in, out string) { } t.Errorf("(gofmt %s) != %s (see %s.gofmt)", in, out, in) - d, err := diff(expected, got, in) + d, err := diffWithReplaceTempFile(expected, got, in) if err == nil { t.Errorf("%s", d) } @@ -194,7 +194,7 @@ func TestDiff(t *testing.T) { in := []byte("first\nsecond\n") out := []byte("first\nthird\n") filename := "difftest.txt" - b, err := diff(in, out, filename) + b, err := diffWithReplaceTempFile(in, out, filename) if err != nil { t.Fatal(err) } |