summaryrefslogtreecommitdiff
path: root/sys/src/cmd/diff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2022-06-04 02:21:19 +0000
committerOri Bernstein <ori@eigenstate.org>2022-06-04 02:21:19 +0000
commit5d37407e3c98b41f64c20a3d03125d3ea6299a2d (patch)
treec8d787050c91a96e667e25f140c43674a2b7dc64 /sys/src/cmd/diff
parent9eb9c9e56064d41f45f2db22a8cc8a1a797f4a53 (diff)
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.
Diffstat (limited to 'sys/src/cmd/diff')
-rw-r--r--sys/src/cmd/diff/diff.h1
-rw-r--r--sys/src/cmd/diff/diffio.c19
-rw-r--r--sys/src/cmd/diff/diffreg.c1
3 files changed, 8 insertions, 13 deletions
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
@@ -330,23 +330,15 @@ changeset(int i)
}
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<nchanges; ){
j = changeset(i);
a = changes[i].a-Lines;
@@ -369,6 +361,11 @@ flushchanges(void)
j = nchanges;
}
if(mode == 'u'){
+ if(!hdr){
+ Bprint(&stdout, "--- %s\n", file1);
+ Bprint(&stdout, "+++ %s\n", file2);
+ hdr = 1;
+ }
Bprint(&stdout, "@@ -%d,%d +%d,%d @@\n", a, b-a+1, c, d-c+1);
}else{
Bprint(&stdout, "%s:", file1);
diff --git a/sys/src/cmd/diff/diffreg.c b/sys/src/cmd/diff/diffreg.c
index f773a4edf..e37ffa7a3 100644
--- a/sys/src/cmd/diff/diffreg.c
+++ b/sys/src/cmd/diff/diffreg.c
@@ -284,7 +284,6 @@ output(void)
m = len[0];
J[0] = 0;
J[m+1] = len[1]+1;
- fileheader();
if (mode != 'e') {
for (i0 = 1; i0 <= m; i0 = i1+1) {
while (i0 <= m && J[i0] == J[i0-1]+1)