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
This commit is contained in:
Tom Jones 2022-04-15 14:59:14 +01:00
parent 362ff9867e
commit 7c03df6855
7 changed files with 113 additions and 16 deletions

View file

@ -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);
}

View file

@ -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>

View file

@ -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
}

View file

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

View file

@ -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

View file

@ -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

View file

@ -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