shithub: riscv

Download patch

ref: 5d37407e3c98b41f64c20a3d03125d3ea6299a2d
parent: 9eb9c9e56064d41f45f2db22a8cc8a1a797f4a53
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Jun 3 22:21:19 EDT 2022

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.

--- a/sys/src/cmd/diff/diff.h
+++ b/sys/src/cmd/diff/diff.h
@@ -28,6 +28,5 @@
 void panic(int, char *, ...);
 void check(Biobuf *, Biobuf *);
 void change(int, int, int, int);
-void fileheader(void);
 void flushchanges(void);
 
--- a/sys/src/cmd/diff/diffio.c
+++ b/sys/src/cmd/diff/diffio.c
@@ -330,23 +330,15 @@
 }
 
 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 @@
 			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);
--- a/sys/src/cmd/diff/diffreg.c
+++ b/sys/src/cmd/diff/diffreg.c
@@ -284,7 +284,6 @@
 	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)