9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] diff(1): fix -u when comparing identical files
@ 2021-11-05 17:51 Kyle Milz
  0 siblings, 0 replies; only message in thread
From: Kyle Milz @ 2021-11-05 17:51 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: diff_u_identical.diff --]
[-- Type: text/plain, Size: 2359 bytes --]

hi,

i notice when comparing identical files with diff -u the header
is always printed:

	% diff -u /dev/null /dev/null
	--- /tmp/diff100000021006
	+++ /tmp/diff200000021006

i dont think this is correct and is not consistent with other diff
formats like -c. i think the header should be printed only when
there is at least one change (diff hunk), and only before the
first change in a given file.

it look like change() in diffio.c is only called when there is
at least one change, and it already keeps track if the
first change has been seen already.

diff below moves the -u header printing into change() under the
'firstchange' condition. ive been testing this for a few days
under different conditions and it has been working well:

	% diff -u /dev/null /dev/null
	%

indeed the diff -u below was created with the diff below applied.


kyle


diff e2e4a46f26ff7268a696a45d82414fb992b648d5 uncommitted
--- a/sys/src/cmd/diff/diff.h
+++ b/sys/src/cmd/diff/diff.h
@@ -22,6 +22,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
@@ -267,16 +267,22 @@
 	if (a > b && c > d)
 		return;
 	anychange = 1;
-	if (mflag && firstchange == 0) {
-		if(mode) {
-			buf[0] = '-';
-			buf[1] = mode;
-			buf[2] = ' ';
-			buf[3] = '\0';
-		} else {
-			buf[0] = '\0';
+	if (firstchange == 0) {
+		if (mflag) {
+			if(mode) {
+				buf[0] = '-';
+				buf[1] = mode;
+				buf[2] = ' ';
+				buf[3] = '\0';
+			} else {
+				buf[0] = '\0';
+			}
+			Bprint(&stdout, "diff %s%s %s\n", buf, file1, file2);
 		}
-		Bprint(&stdout, "diff %s%s %s\n", buf, file1, file2);
+		if (mode == 'u') {
+			Bprint(&stdout, "--- %s\n", file1);
+			Bprint(&stdout, "+++ %s\n", file2);
+		}
 		firstchange = 1;
 	}
 	verb = a > b ? 'a': c > d ? 'd': 'c';
@@ -337,15 +343,6 @@
 	if(i<nchanges)
 		return i+1;
 	return nchanges;
-}
-
-void
-fileheader(void)
-{
-	if(mode != 'u')
-		return;
-	Bprint(&stdout, "--- %s\n", file1);
-	Bprint(&stdout, "+++ %s\n", file2);
 }
 
 void
--- a/sys/src/cmd/diff/diffreg.c
+++ b/sys/src/cmd/diff/diffreg.c
@@ -285,7 +285,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:[~2021-11-05 19:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-05 17:51 [9front] diff(1): fix -u when comparing identical files Kyle Milz

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).