diff options
| author | Russ Cox <rsc@golang.org> | 2022-01-29 16:13:12 -0500 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2022-03-18 16:56:29 +0000 |
| commit | 7747c33a41491be74da65b116718f4df7a2f8337 (patch) | |
| tree | 5dc2a332865fe954703f5e05dc62e670a7732d4c /src/cmd/fix | |
| parent | 8ff42d1bb1919b38e0d852618168f18d33db866b (diff) | |
| download | go-7747c33a41491be74da65b116718f4df7a2f8337.tar.xz | |
internal/diff: add, replacing cmd/internal/diff
This is an in-process (non-exec'ing) replacement for cmd/internal/diff.
It uses an O(n log n) algorithm instead of the O(n²) algorithm
in standard diff binaries. It does not produce the absolute
shortest diffs, but the results are often more meaningful
than the standard diff, because it doesn't try to align
random blank lines or other noise.
Adding so that tests inside std (especially go/printer)
can print diffs.
Replacing cmd/internal/diff because we don't need two.
Change-Id: I9155dd925e4a813f5bfa84a8ad3dec8ffdbf8550
Reviewed-on: https://go-review.googlesource.com/c/go/+/384255
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Peter Weinberger <pjw@google.com>
Trust: Peter Weinberger <pjw@google.com>
Diffstat (limited to 'src/cmd/fix')
| -rw-r--r-- | src/cmd/fix/main.go | 10 | ||||
| -rw-r--r-- | src/cmd/fix/main_test.go | 18 |
2 files changed, 8 insertions, 20 deletions
diff --git a/src/cmd/fix/main.go b/src/cmd/fix/main.go index 3229b71ec4..4e5c08731b 100644 --- a/src/cmd/fix/main.go +++ b/src/cmd/fix/main.go @@ -13,6 +13,7 @@ import ( "go/parser" "go/scanner" "go/token" + "internal/diff" "io" "io/fs" "os" @@ -20,8 +21,6 @@ import ( "sort" "strconv" "strings" - - "cmd/internal/diff" ) var ( @@ -228,12 +227,7 @@ func processFile(filename string, useStdin bool) error { } if *doDiff { - data, err := diff.Diff("go-fix", src, newSrc) - if err != nil { - return fmt.Errorf("computing diff: %s", err) - } - fmt.Printf("diff %s fixed/%s\n", filename, filename) - os.Stdout.Write(data) + os.Stdout.Write(diff.Diff(filename, src, "fixed/"+filename, newSrc)) return nil } diff --git a/src/cmd/fix/main_test.go b/src/cmd/fix/main_test.go index 1baa95c545..755007bc0d 100644 --- a/src/cmd/fix/main_test.go +++ b/src/cmd/fix/main_test.go @@ -7,10 +7,9 @@ package main import ( "go/ast" "go/parser" + "internal/diff" "strings" "testing" - - "cmd/internal/diff" ) type testCase struct { @@ -52,7 +51,7 @@ func parseFixPrint(t *testing.T, fn func(*ast.File) bool, desc, in string, mustB if s := string(outb); in != s && mustBeGofmt { t.Errorf("not gofmt-formatted.\n--- %s\n%s\n--- %s | gofmt\n%s", desc, in, desc, s) - tdiff(t, in, s) + tdiff(t, "want", in, "have", s) return } @@ -109,7 +108,7 @@ func TestRewrite(t *testing.T) { if !strings.HasPrefix(tt.Name, "testdata/") { t.Errorf("--- have\n%s\n--- want\n%s", out, tt.Out) } - tdiff(t, out, tt.Out) + tdiff(t, "have", out, "want", tt.Out) return } @@ -132,17 +131,12 @@ func TestRewrite(t *testing.T) { if out2 != out { t.Errorf("changed output after second round of fixes.\n--- output after first round\n%s\n--- output after second round\n%s", out, out2) - tdiff(t, out, out2) + tdiff(t, "first", out, "second", out2) } }) } } -func tdiff(t *testing.T, a, b string) { - data, err := diff.Diff("go-fix-test", []byte(a), []byte(b)) - if err != nil { - t.Error(err) - return - } - t.Error(string(data)) +func tdiff(t *testing.T, aname, a, bname, b string) { + t.Errorf("%s", diff.Diff(aname, []byte(a), bname, []byte(b))) } |
