diff3: make the eflag logic easier to follow

Discussed with:	dim
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D35324
This commit is contained in:
Tom Jones 2022-07-05 12:33:55 +01:00
parent d80d734937
commit d458eb8de1

View File

@ -112,6 +112,11 @@ struct diff {
struct range new; struct range new;
}; };
#define EFLAG_NONE 0
#define EFLAG_OVERLAP 1
#define EFLAG_NOOVERLAP 2
#define EFLAG_UNMERGED 3
static size_t szchanges; static size_t szchanges;
static struct diff *d13; static struct diff *d13;
@ -313,7 +318,7 @@ merge(int m1, int m2)
/* first file is different from the others */ /* first file is different from the others */
if (!t2 || (t1 && d1->new.to < d2->new.from)) { if (!t2 || (t1 && d1->new.to < d2->new.from)) {
/* stuff peculiar to 1st file */ /* stuff peculiar to 1st file */
if (eflag == 0) { if (eflag == EFLAG_NONE) {
printf("====1\n"); printf("====1\n");
change(1, &d1->old, false); change(1, &d1->old, false);
keep(2, &d1->new); keep(2, &d1->new);
@ -324,7 +329,7 @@ merge(int m1, int m2)
} }
/* second file is different from others */ /* second file is different from others */
if (!t1 || (t2 && d2->new.to < d1->new.from)) { if (!t1 || (t2 && d2->new.to < d1->new.from)) {
if (eflag == 0) { if (eflag == EFLAG_NONE) {
printf("====2\n"); printf("====2\n");
keep(1, &d2->new); keep(1, &d2->new);
change(3, &d2->new, false); change(3, &d2->new, false);
@ -361,7 +366,7 @@ merge(int m1, int m2)
* dup = 0 means all files differ * dup = 0 means all files differ
* dup = 1 means files 1 and 2 identical * dup = 1 means files 1 and 2 identical
*/ */
if (eflag == 0) { if (eflag == EFLAG_NONE) {
printf("====%s\n", dup ? "3" : ""); printf("====%s\n", dup ? "3" : "");
change(1, &d1->old, dup); change(1, &d1->old, dup);
change(2, &d2->old, false); change(2, &d2->old, false);
@ -530,9 +535,11 @@ repos(int nchar)
static int static int
edit(struct diff *diff, bool dup, int j, int difftype) edit(struct diff *diff, bool dup, int j, int difftype)
{ {
if (!(eflag == EFLAG_UNMERGED ||
if (((dup + 1) & eflag) == 0) (!dup && eflag == EFLAG_OVERLAP ) ||
(dup && eflag == EFLAG_NOOVERLAP))) {
return (j); return (j);
}
j++; j++;
overlap[j] = !dup; overlap[j] = !dup;
if (!dup) if (!dup)
@ -607,7 +614,7 @@ edscript(int n)
if (iflag) if (iflag)
printf("w\nq\n"); printf("w\nq\n");
exit(eflag == 0 ? overlapcnt : 0); exit(eflag == EFLAG_NONE ? overlapcnt : 0);
} }
/* /*
@ -825,13 +832,13 @@ main(int argc, char **argv)
struct kevent *e; struct kevent *e;
nblabels = 0; nblabels = 0;
eflag = 0; eflag = EFLAG_NONE;
oflag = 0; oflag = 0;
diffargv[diffargc++] = __DECONST(char *, diffprog); diffargv[diffargc++] = __DECONST(char *, diffprog);
while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
switch (ch) { switch (ch) {
case '3': case '3':
eflag = 2; eflag = EFLAG_NOOVERLAP;
break; break;
case 'a': case 'a':
diffargv[diffargc++] = __DECONST(char *, "-a"); diffargv[diffargc++] = __DECONST(char *, "-a");
@ -840,10 +847,10 @@ main(int argc, char **argv)
Aflag = 1; Aflag = 1;
break; break;
case 'e': case 'e':
eflag = 3; eflag = EFLAG_UNMERGED;
break; break;
case 'E': case 'E':
eflag = 3; eflag = EFLAG_UNMERGED;
oflag = 1; oflag = 1;
break; break;
case 'i': case 'i':
@ -864,11 +871,11 @@ main(int argc, char **argv)
Tflag = 1; Tflag = 1;
break; break;
case 'x': case 'x':
eflag = 1; eflag = EFLAG_OVERLAP;
break; break;
case 'X': case 'X':
oflag = 1; oflag = 1;
eflag = 1; eflag = EFLAG_OVERLAP;
break; break;
case DIFFPROG_OPT: case DIFFPROG_OPT:
diffprog = optarg; diffprog = optarg;
@ -889,7 +896,7 @@ main(int argc, char **argv)
argv += optind; argv += optind;
if (Aflag) { if (Aflag) {
eflag = 3; eflag = EFLAG_UNMERGED;
oflag = 1; oflag = 1;
} }