git: 7c03df6855f4 - main - diff3: allow diff3 ed scripts to generate deletions

From: Tom Jones <thj_at_FreeBSD.org>
Date: Fri, 15 Apr 2022 14:01:32 UTC
The branch main has been updated by thj:

URL: https://cgit.FreeBSD.org/src/commit/?id=7c03df6855f451749501748b98019c8e2d9d9600

commit 7c03df6855f451749501748b98019c8e2d9d9600
Author:     Tom Jones <thj@FreeBSD.org>
AuthorDate: 2022-04-15 13:59:14 +0000
Commit:     Tom Jones <thj@FreeBSD.org>
CommitDate: 2022-04-15 14:00:59 +0000

    diff3: allow diff3 ed scripts to generate deletions
    
    diff3 with the -e (ed script flag) can generate line deletions, add
    support for deletions and add a test case to exercise this behaviour.
    This functionality was unearthed through comparison of bsd diff3 and gnu
    diff3 output.
    
    Reviewed by:    pstef
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D34912
---
 usr.bin/diff3/diff3.c             | 22 ++++++++++++++--------
 usr.bin/diff3/tests/Makefile      |  7 +++++--
 usr.bin/diff3/tests/diff3_test.sh | 14 ++++++++------
 usr.bin/diff3/tests/long-ed.out   |  7 +++++++
 usr.bin/diff3/tests/long-m.txt    | 26 ++++++++++++++++++++++++++
 usr.bin/diff3/tests/long-o.txt    | 27 +++++++++++++++++++++++++++
 usr.bin/diff3/tests/long-y.txt    | 26 ++++++++++++++++++++++++++
 7 files changed, 113 insertions(+), 16 deletions(-)

diff --git a/usr.bin/diff3/diff3.c b/usr.bin/diff3/diff3.c
index 122243a9448f..37aa599eaab7 100644
--- a/usr.bin/diff3/diff3.c
+++ b/usr.bin/diff3/diff3.c
@@ -139,7 +139,7 @@ static int skip(int, int, const char *);
 static void change(int, struct range *, bool);
 static void keep(int, struct range *);
 static void merge(int, int);
-static void prange(struct range *);
+static void prange(struct range *, bool);
 static void repos(int);
 static void edscript(int) __dead2;
 static void increase(void);
@@ -382,7 +382,7 @@ change(int i, struct range *rold, bool dup)
 
 	printf("%d:", i);
 	last[i] = rold->to;
-	prange(rold);
+	prange(rold, false);
 	if (dup)
 		return;
 	i--;
@@ -395,7 +395,7 @@ change(int i, struct range *rold, bool dup)
  * n1.
  */
 static void
-prange(struct range *rold)
+prange(struct range *rold, bool delete)
 {
 
 	if (rold->to <= rold->from)
@@ -404,7 +404,10 @@ prange(struct range *rold)
 		printf("%d", rold->from);
 		if (rold->to > rold->from + 1)
 			printf(",%d", rold->to - 1);
-		printf("c\n");
+		if (delete)
+			printf("d\n");
+		else
+			printf("c\n");
 	}
 }
 
@@ -514,12 +517,14 @@ static void
 edscript(int n)
 {
 	int k;
+	bool delete;
 	size_t j;
 	char block[BUFSIZ];
 
 	for (; n > 0; n--) {
+		delete = (de[n].new.from == de[n].new.to);
 		if (!oflag || !overlap[n]) {
-			prange(&de[n].old);
+			prange(&de[n].old, delete);
 		} else {
 			printf("%da\n", de[n].old.to - 1);
 			if (Aflag) {
@@ -550,9 +555,10 @@ edscript(int n)
 				j = r;
 			(void)fwrite(block, 1, j, stdout);
 		}
-		if (!oflag || !overlap[n])
-			printf(".\n");
-		else {
+		if (!oflag || !overlap[n]) {
+			if (!delete)
+				printf(".\n");
+		} else {
 			printf("%s\n.\n", f3mark);
 			printf("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
 		}
diff --git a/usr.bin/diff3/tests/Makefile b/usr.bin/diff3/tests/Makefile
index b8bea154944b..91af4d1a6237 100644
--- a/usr.bin/diff3/tests/Makefile
+++ b/usr.bin/diff3/tests/Makefile
@@ -3,12 +3,14 @@
 PACKAGE=	tests
 
 ATF_TESTS_SH=	diff3_test
-
 ${PACKAGE}FILES+=	\
 	1.txt \
 	1cr.txt \
 	2.txt \
 	3.txt \
+	long-m.txt \
+	long-o.txt \
+	long-y.txt \
 	1.out \
 	1t.out \
 	2.out \
@@ -18,6 +20,7 @@ ${PACKAGE}FILES+=	\
 	6.out \
 	7.out \
 	8.out \
-	9.out
+	9.out \
+	long-ed.out
 
 .include <bsd.test.mk>
diff --git a/usr.bin/diff3/tests/diff3_test.sh b/usr.bin/diff3/tests/diff3_test.sh
index 64031b023187..0eb08f46a803 100755
--- a/usr.bin/diff3/tests/diff3_test.sh
+++ b/usr.bin/diff3/tests/diff3_test.sh
@@ -2,6 +2,7 @@
 
 atf_test_case diff3
 atf_test_case diff3_lesssimple
+atf_test_case diff3_ed
 
 diff3_body()
 {
@@ -31,12 +32,6 @@ diff3_body()
 
 	atf_check -o file:$(atf_get_srcdir)/7.out \
 		diff3 -i $(atf_get_srcdir)/1.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt
-
-#	atf_check -o file:$(atf_get_srcdir)/8.out \
-#		diff3 -A -L 1 -L 2 -L 3 $(atf_get_srcdir)/1.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt
-
-#	atf_check -s exit:1 -o file:$(atf_get_srcdir)/9.out \
-#		diff3 -m -L 1 -L 2 -L 3 $(atf_get_srcdir)/1.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt
 }
 
 diff3_lesssimple_body()
@@ -45,8 +40,15 @@ diff3_lesssimple_body()
 		diff3 -m -L 1 -L 2 -L 3 $(atf_get_srcdir)/4.txt $(atf_get_srcdir)/5.txt $(atf_get_srcdir)/6.txt
 }
 
+diff3_ed_body()
+{
+	atf_check -s exit:0 -o file:$(atf_get_srcdir)/long-ed.out \
+		diff3 -e $(atf_get_srcdir)/long-m.txt $(atf_get_srcdir)/long-o.txt $(atf_get_srcdir)/long-y.txt
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case diff3
 #	atf_add_test_case diff3_lesssimple
+	atf_add_test_case diff3_ed
 }
diff --git a/usr.bin/diff3/tests/long-ed.out b/usr.bin/diff3/tests/long-ed.out
new file mode 100644
index 000000000000..8814a3518d60
--- /dev/null
+++ b/usr.bin/diff3/tests/long-ed.out
@@ -0,0 +1,7 @@
+24d
+16c
+This line is different in yours and mine, but the best in yours
+.
+8c
+This line is different in yours, much butter
+.
diff --git a/usr.bin/diff3/tests/long-m.txt b/usr.bin/diff3/tests/long-m.txt
new file mode 100644
index 000000000000..327bf8c225f7
--- /dev/null
+++ b/usr.bin/diff3/tests/long-m.txt
@@ -0,0 +1,26 @@
+This is a long file
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in mine, not better
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in yours
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in yours and mine, the same is in both
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in yours and mine, best change in mine
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+This line is deleted in yours
+These lines are the same in all three files
+These lines are the same in all three files
diff --git a/usr.bin/diff3/tests/long-o.txt b/usr.bin/diff3/tests/long-o.txt
new file mode 100644
index 000000000000..b4085d1d98c0
--- /dev/null
+++ b/usr.bin/diff3/tests/long-o.txt
@@ -0,0 +1,27 @@
+This is a long file
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in mine
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in yours
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in yours and mine, but the same
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in yours and mine, but the different in each
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+This line is deleted in mine
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+This line is deleted in yours
+These lines are the same in all three files
+These lines are the same in all three files
diff --git a/usr.bin/diff3/tests/long-y.txt b/usr.bin/diff3/tests/long-y.txt
new file mode 100644
index 000000000000..1bb25004924e
--- /dev/null
+++ b/usr.bin/diff3/tests/long-y.txt
@@ -0,0 +1,26 @@
+This is a long file
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in mine
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in yours, much butter
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in yours and mine, the same is in both
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+This line is different in yours and mine, but the best in yours
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+This line is deleted in mine
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files
+These lines are the same in all three files