9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Kyle Milz <krwmilz@gmail.com>
To: 9front@9front.org
Subject: [9front] diff(1): fix -u when comparing identical files
Date: Fri, 5 Nov 2021 10:51:15 -0700	[thread overview]
Message-ID: <YYVvE55KkpuIjeet@imac.0x30.net> (raw)

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

                 reply	other threads:[~2021-11-05 19:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YYVvE55KkpuIjeet@imac.0x30.net \
    --to=krwmilz@gmail.com \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).