From 7747c33a41491be74da65b116718f4df7a2f8337 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Sat, 29 Jan 2022 16:13:12 -0500 Subject: internal/diff: add, replacing cmd/internal/diff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Run-TryBot: Russ Cox TryBot-Result: Gopher Robot Reviewed-by: Peter Weinberger Trust: Peter Weinberger --- src/cmd/internal/diff/diff.go | 78 ------------------------------------------- 1 file changed, 78 deletions(-) delete mode 100644 src/cmd/internal/diff/diff.go (limited to 'src/cmd/internal/diff/diff.go') diff --git a/src/cmd/internal/diff/diff.go b/src/cmd/internal/diff/diff.go deleted file mode 100644 index 0ec2d7f8f9..0000000000 --- a/src/cmd/internal/diff/diff.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package diff implements a Diff function that compare two inputs -// using the 'diff' tool. -package diff - -import ( - "bytes" - exec "internal/execabs" - "io/ioutil" - "os" - "runtime" -) - -// Returns diff of two arrays of bytes in diff tool format. -func Diff(prefix string, b1, b2 []byte) ([]byte, error) { - f1, err := writeTempFile(prefix, b1) - if err != nil { - return nil, err - } - defer os.Remove(f1) - - f2, err := writeTempFile(prefix, b2) - if err != nil { - return nil, err - } - defer os.Remove(f2) - - cmd := "diff" - if runtime.GOOS == "plan9" { - cmd = "/bin/ape/diff" - } - - data, err := exec.Command(cmd, "-u", f1, f2).CombinedOutput() - 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. - err = nil - } - - // If we are on Windows and the diff is Cygwin diff, - // machines can get into a state where every Cygwin - // command works fine but prints a useless message like: - // - // Cygwin WARNING: - // Couldn't compute FAST_CWD pointer. This typically occurs if you're using - // an older Cygwin version on a newer Windows. Please update to the latest - // available Cygwin version from https://cygwin.com/. If the problem persists, - // please see https://cygwin.com/problems.html - // - // Skip over that message and just return the actual diff. - if len(data) > 0 && !bytes.HasPrefix(data, []byte("--- ")) { - i := bytes.Index(data, []byte("\n--- ")) - if i >= 0 && i < 80*10 && bytes.Contains(data[:i], []byte("://cygwin.com/")) { - data = data[i+1:] - } - } - - return data, err -} - -func writeTempFile(prefix string, data []byte) (string, error) { - file, err := ioutil.TempFile("", 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 -} -- cgit v1.2.1