From 5d37407e3c98b41f64c20a3d03125d3ea6299a2d Mon Sep 17 00:00:00 2001 From: Ori Bernstein Date: Sat, 4 Jun 2022 02:21:19 +0000 Subject: diff: avoid empty hunks when there are no changes Currently, diff outputs a file header, even if there are no changes to the file. This is wonky. It means that the header chunks are ambiguous, since not all header chunks are followed by '@@ hunk', and '--- file', '+++ file' lines can be generated from file content. This changes the way that we decide to print the file header, so we only print it when outputting the first hunk on flushchanges. Flushchanges is called once per regular file, at the end of `diffreg`, so we output a hunk header once per file. --- sys/src/cmd/diff/diff.h | 1 - sys/src/cmd/diff/diffio.c | 19 ++++++++----------- sys/src/cmd/diff/diffreg.c | 1 - 3 files changed, 8 insertions(+), 13 deletions(-) (limited to 'sys/src/cmd/diff') diff --git a/sys/src/cmd/diff/diff.h b/sys/src/cmd/diff/diff.h index 8aca0439f..e1c8263d6 100644 --- a/sys/src/cmd/diff/diff.h +++ b/sys/src/cmd/diff/diff.h @@ -28,6 +28,5 @@ Biobuf *prepare(int, char *, char *); void panic(int, char *, ...); void check(Biobuf *, Biobuf *); void change(int, int, int, int); -void fileheader(void); void flushchanges(void); diff --git a/sys/src/cmd/diff/diffio.c b/sys/src/cmd/diff/diffio.c index b577b6d2a..1d4d4d6bb 100644 --- a/sys/src/cmd/diff/diffio.c +++ b/sys/src/cmd/diff/diffio.c @@ -329,24 +329,16 @@ changeset(int i) return nchanges; } -void -fileheader(void) -{ - if(mode != 'u') - return; - Bprint(&stdout, "--- %s\n", file1); - Bprint(&stdout, "+++ %s\n", file2); -} - void flushchanges(void) { - int a, b, c, d, at; + int a, b, c, d, at, hdr; int i, j; if(nchanges == 0) return; - + + hdr = 0; for(i=0; i