code: plan9front

Download patch

ref: 3c53420eaeeeb4a0f388d706488097e97f01632d
parent: 218f7a9ec7773484202d6fffb43b53f33524104c
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Apr 1 16:40:26 EDT 2023

diff: barf with explanation on binary diffs/merges

--- a/sys/src/cmd/diff/diff.h
+++ b/sys/src/cmd/diff/diff.h
@@ -27,6 +27,7 @@
 	Line *file[2], line;
 	int len[2];
 	int binary;
+	int bindiff;
 	Line *sfile[2];	/*shortened by pruning common prefix and suffix*/
 	int slen[2];
 	int pref, suff;	/*length of prefix and suffix*/
--- a/sys/src/cmd/diff/diffio.c
+++ b/sys/src/cmd/diff/diffio.c
@@ -109,6 +109,11 @@
 	int nbytes;
 	Rune r;
 
+	if (i == 0) {
+		d->file1 = orig;
+		d->firstchange = 0;
+	} else
+		d->file2 = orig;
 	bp = Bopen(arg, OREAD);
 	if (!bp) {
 		sysfatal("cannot open %s: %r", arg);
@@ -138,11 +143,6 @@
 	d->len[i] = j;
 	d->file[i] = p;
 	d->input[i] = bp;
-	if (i == 0) {
-		d->file1 = orig;
-		d->firstchange = 0;
-	} else
-		d->file2 = orig;
 	return bp;
 }
 
--- a/sys/src/cmd/diff/diffreg.c
+++ b/sys/src/cmd/diff/diffreg.c
@@ -315,8 +315,8 @@
 	}
 	if (d->binary){
 		// could use b0 and b1 but this is simpler.
-		if (cmp(b0, b1))
-			print("binary files %s %s differ\n", f, t);
+		if(cmp(b0, b1))
+			d->bindiff = 1;
 		Bterm(b0);
 		Bterm(b1);
 		return;
@@ -356,6 +356,10 @@
 {
 	int m, i0, i1, j0, j1;
 
+	if(d->bindiff){
+		print("binary files %s %s differ\n", d->file1, d->file2);
+		return;
+	}
 	m = d->len[0];
 	d->J[0] = 0;
 	d->J[m+1] = d->len[1]+1;
@@ -403,8 +407,10 @@
 void
 freediff(Diff *d)
 {
-	Bterm(d->input[0]);
-	Bterm(d->input[1]);
+	if(d->input[0] != nil)
+		Bterm(d->input[0]);
+	if(d->input[1] != nil)
+		Bterm(d->input[1]);
 	free(d->J);
 	free(d->ixold);
 	free(d->ixnew);
--- a/sys/src/cmd/diff/merge3.c
+++ b/sys/src/cmd/diff/merge3.c
@@ -162,6 +162,8 @@
 	memset(&r, 0, sizeof(r));
 	calcdiff(&l, argv[1], argv[1], argv[0], argv[0]);
 	calcdiff(&r, argv[1], argv[1], argv[2], argv[2]);
+	if(l.binary || r.binary)
+		sysfatal("cannot merge binaries");
 	x = merge(&l, &r);
 	freediff(&l);
 	freediff(&r);