From a6879251ec3ba06d3848fecd6fdcaf9b825ec20e Mon Sep 17 00:00:00 2001 From: thesyncim Date: Sat, 31 Oct 2026 19:50:56 +0000 Subject: [PATCH] loopvec: label -d diffs with the file's path -d ran diff -u on two temporary files and replaced their names with the file's path, so each header still carried diff's timestamp. GNU diff prints it as "2026-10-31 18:49:17.123456789 +0000" and BSD diff, the one macOS ships, as "2026-10-31 18:49:17", which made flag_diff.txt fail on macOS, and the timestamp of a temporary file says nothing about the source. diff now labels both sides with the path (-L, which GNU and BSD diff both support), as gofmt -d does, so the headers are the same everywhere. --- main.go | 9 ++++----- testdata/flag_diff.txt | 8 ++++---- testdata/readme_go_tool.txt | 2 +- testdata/readme_patterns.txt | 8 ++++---- testdata/readme_workflow.txt | 2 +- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 2a7842d..61dd22f 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,6 @@ package main import ( - "bytes" "encoding/json" "flag" "fmt" @@ -321,7 +320,9 @@ func addBuildTag(src []byte, tag string) []byte { } // printDiff prints a unified diff between orig and rewritten for path, -// using the system diff command. +// using the system diff command. Both sides are labeled with path, so the +// header does not depend on the temporary files or on which diff is installed +// (GNU and BSD diff print their timestamps differently). func printDiff(path string, orig, rewritten []byte) error { origFile, err := os.CreateTemp("", "loopvec-orig-*.go") if err != nil { @@ -345,9 +346,7 @@ func printDiff(path string, orig, rewritten []byte) error { } _ = newFile.Close() - out, _ := exec.Command("diff", "-u", origFile.Name(), newFile.Name()).Output() - out = bytes.ReplaceAll(out, []byte(origFile.Name()), []byte(path)) - out = bytes.ReplaceAll(out, []byte(newFile.Name()), []byte(path)) + out, _ := exec.Command("diff", "-u", "-L", path, "-L", path, origFile.Name(), newFile.Name()).Output() _, err = os.Stdout.Write(out) return err } diff --git a/testdata/flag_diff.txt b/testdata/flag_diff.txt index bea082e..5d7bf4a 100644 --- a/testdata/flag_diff.txt +++ b/testdata/flag_diff.txt @@ -1,11 +1,11 @@ -# -d prints a unified diff (paths in the headers, timestamps aside) and leaves -# the source untouched. +# -d prints a unified diff, labeled with the file's path on both sides so the +# headers are the same with GNU and BSD diff, and leaves the source untouched. cp ops.go ops.go.orig loopvec -d . cmpenv stderr stderr.want -stdout -count=1 '^--- \S+/ops\.go\t\S+ \S+ [+-][0-9]{4}$' -stdout -count=1 '^\+\+\+ \S+/ops\.go\t\S+ \S+ [+-][0-9]{4}$' +stdout -count=1 '^--- \S+/ops\.go$' +stdout -count=1 '^\+\+\+ \S+/ops\.go$' stdout -count=1 '^@@ -1,7 \+1,43 @@$' stdout -count=1 '^\+//go:build goexperiment.simd$' stdout -count=1 '^\+import \($' diff --git a/testdata/readme_go_tool.txt b/testdata/readme_go_tool.txt index d4acc77..077772c 100644 --- a/testdata/readme_go_tool.txt +++ b/testdata/readme_go_tool.txt @@ -6,7 +6,7 @@ go work use $LOOPVEC_SRC exec go tool loopvec -d ./... cmpenv stderr stderr.want -stdout -count=1 '^\+\+\+ \S+/mypkg/ops\.go\t' +stdout -count=1 '^\+\+\+ \S+/mypkg/ops\.go$' exec go tool loopvec -split ./mypkg/... ! stdout . diff --git a/testdata/readme_patterns.txt b/testdata/readme_patterns.txt index f68ace1..fdc79f2 100644 --- a/testdata/readme_patterns.txt +++ b/testdata/readme_patterns.txt @@ -10,10 +10,10 @@ cmp b/ops.go b/ops.go.orig # unified diff, once per changed file loopvec -d ./... -stdout -count=1 '^--- \S+/a/ops\.go\t' -stdout -count=1 '^\+\+\+ \S+/a/ops\.go\t' -stdout -count=1 '^--- \S+/b/ops\.go\t' -stdout -count=1 '^\+\+\+ \S+/b/ops\.go\t' +stdout -count=1 '^--- \S+/a/ops\.go$' +stdout -count=1 '^\+\+\+ \S+/a/ops\.go$' +stdout -count=1 '^--- \S+/b/ops\.go$' +stdout -count=1 '^\+\+\+ \S+/b/ops\.go$' stdout -count=2 '^@@ -1,7 \+1,43 @@$' cmpenv stderr both.stderr cmp a/ops.go a/ops.go.orig diff --git a/testdata/readme_workflow.txt b/testdata/readme_workflow.txt index 37ceb56..2148adb 100644 --- a/testdata/readme_workflow.txt +++ b/testdata/readme_workflow.txt @@ -4,7 +4,7 @@ # 1. Preview changes loopvec -d ./mypkg/... cmpenv stderr stderr.want -stdout -count=1 '^\+\+\+ \S+/mypkg/ops\.go\t' +stdout -count=1 '^\+\+\+ \S+/mypkg/ops\.go$' ! exists mypkg/ops_simd.go # 2. Apply: creates ops_simd.go and guards ops.go