9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] diff: avoid empty hunks when there are no changes
@ 2022-05-28 22:15 ori
  0 siblings, 0 replies; only message in thread
From: ori @ 2022-05-28 22:15 UTC (permalink / raw)
  To: 9front

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.

diff b75e549126641108880a24a4ff0b38171eb1a856 uncommitted
--- 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,17 @@
 }
 
 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 +363,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)


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-05-28 22:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-28 22:15 [9front] diff: avoid empty hunks when there are no changes ori

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).