@matheusbsbs / matheus-bagliano / commits / 0d21efa51c
Fork 0

Teach diff about -b and -w flags

This adds -b (--ignore-space-change) and -w (--ignore-all-space) flags to diff. The main part of the patch is teaching libxdiff about it. [jc: renamed xdl_line_match() to xdl_recmatch() since the former is used for different purposes in xpatchi.c which is in the parts of the upstream source we do not use.] Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jun 14, 2006 at 17:40 UTC 0d21efa51cc7de5250d5da46bceacda78ba35373
9 files changed +81 -20
diff.c
+9 -4
@@ -678,7 +678,7 @@ static void builtin_diff(const char *name_a,
678 memset(&ecbdata, 0, sizeof(ecbdata));
679 ecbdata.label_path = lbl;
680 ecbdata.color_diff = o->color_diff;
681 - xpp.flags = XDF_NEED_MINIMAL;
681 + xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
682 xecfg.ctxlen = o->context;
683 xecfg.flags = XDL_EMIT_FUNCNAMES;
684 if (!diffopts)
@@ -703,6 +703,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
703 struct diff_filespec *one,
704 struct diff_filespec *two,
705 struct diffstat_t *diffstat,
706 + struct diff_options *o,
707 int complete_rewrite)
708 {
709 mmfile_t mf1, mf2;
@@ -732,7 +733,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
733 xdemitconf_t xecfg;
734 xdemitcb_t ecb;
735
735 - xpp.flags = XDF_NEED_MINIMAL;
736 + xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
737 xecfg.ctxlen = 0;
738 xecfg.flags = 0;
739 ecb.outf = xdiff_outf;
@@ -1317,7 +1318,7 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1318
1319 if (DIFF_PAIR_UNMERGED(p)) {
1320 /* unmerged */
1320 - builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, 0);
1321 + builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1322 return;
1323 }
1324
@@ -1329,7 +1330,7 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1330
1331 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1332 complete_rewrite = 1;
1332 - builtin_diffstat(name, other, p->one, p->two, diffstat, complete_rewrite);
1333 + builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1334 }
1335
1336 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
@@ -1534,6 +1535,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1535 }
1536 else if (!strcmp(arg, "--color"))
1537 options->color_diff = 1;
1538 + else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
1539 + options->xdl_opts |= XDF_IGNORE_WHITESPACE;
1540 + else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
1541 + options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1542 else
1543 return 0;
1544 return 1;
diff.h
+1
@@ -46,6 +46,7 @@ struct diff_options {
46 int setup;
47 int abbrev;
48 const char *stat_sep;
49 + long xdl_opts;
50
51 int nr_paths;
52 const char **paths;
xdiff/xdiff.h
+3
@@ -29,6 +29,9 @@ extern "C" {
29
30
31 #define XDF_NEED_MINIMAL (1 << 1)
32 +#define XDF_IGNORE_WHITESPACE (1 << 2)
33 +#define XDF_IGNORE_WHITESPACE_CHANGE (1 << 3)
34 +#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)
35
36 #define XDL_PATCH_NORMAL '-'
37 #define XDL_PATCH_REVERSE '+'
xdiff/xdiffi.c
+6 -6
@@ -45,7 +45,7 @@ static long xdl_split(unsigned long const *ha1, long off1, long lim1,
45 long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
46 xdalgoenv_t *xenv);
47 static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2);
48 -static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo);
48 +static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags);
49
50
51
@@ -397,7 +397,7 @@ static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1,
397 }
398
399
400 -static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo) {
400 +static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
401 long ix, ixo, ixs, ixref, grpsiz, nrec = xdf->nrec;
402 char *rchg = xdf->rchg, *rchgo = xdfo->rchg;
403 xrecord_t **recs = xdf->recs;
@@ -440,7 +440,7 @@ static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo) {
440 * the group.
441 */
442 while (ixs > 0 && recs[ixs - 1]->ha == recs[ix - 1]->ha &&
443 - XDL_RECMATCH(recs[ixs - 1], recs[ix - 1])) {
443 + xdl_recmatch(recs[ixs - 1]->ptr, recs[ixs - 1]->size, recs[ix - 1]->ptr, recs[ix - 1]->size, flags)) {
444 rchg[--ixs] = 1;
445 rchg[--ix] = 0;
446
@@ -468,7 +468,7 @@ static int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo) {
468 * the group.
469 */
470 while (ix < nrec && recs[ixs]->ha == recs[ix]->ha &&
471 - XDL_RECMATCH(recs[ixs], recs[ix])) {
471 + xdl_recmatch(recs[ixs]->ptr, recs[ixs]->size, recs[ix]->ptr, recs[ix]->size, flags)) {
472 rchg[ixs++] = 0;
473 rchg[ix++] = 1;
474
@@ -546,8 +546,8 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
546
547 return -1;
548 }
549 - if (xdl_change_compact(&xe.xdf1, &xe.xdf2) < 0 ||
550 - xdl_change_compact(&xe.xdf2, &xe.xdf1) < 0 ||
549 + if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 ||
550 + xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 ||
551 xdl_build_script(&xe, &xscr) < 0) {
552
553 xdl_free_env(&xe);
xdiff/xdiffi.h
-1
@@ -55,6 +55,5 @@ void xdl_free_script(xdchange_t *xscr);
55 int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
56 xdemitconf_t const *xecfg);
57
58 -
58 #endif /* #if !defined(XDIFFI_H) */
59
xdiff/xmacros.h
-1
@@ -33,7 +33,6 @@
33 #define XDL_ISDIGIT(c) ((c) >= '0' && (c) <= '9')
34 #define XDL_HASHLONG(v, b) (((unsigned long)(v) * GR_PRIME) >> ((CHAR_BIT * sizeof(unsigned long)) - (b)))
35 #define XDL_PTRFREE(p) do { if (p) { xdl_free(p); (p) = NULL; } } while (0)
36 -#define XDL_RECMATCH(r1, r2) ((r1)->size == (r2)->size && memcmp((r1)->ptr, (r2)->ptr, (r1)->size) == 0)
36 #define XDL_LE32_PUT(p, v) \
37 do { \
38 unsigned char *__p = (unsigned char *) (p); \
xdiff/xprepare.c
+10 -6
@@ -43,12 +43,13 @@ typedef struct s_xdlclassifier {
43 xdlclass_t **rchash;
44 chastore_t ncha;
45 long count;
46 + long flags;
47 } xdlclassifier_t;
48
49
50
51
51 -static int xdl_init_classifier(xdlclassifier_t *cf, long size);
52 +static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
53 static void xdl_free_classifier(xdlclassifier_t *cf);
54 static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
55 xrecord_t *rec);
@@ -63,9 +64,11 @@ static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2);
64
65
66
66 -static int xdl_init_classifier(xdlclassifier_t *cf, long size) {
67 +static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
68 long i;
69
70 + cf->flags = flags;
71 +
72 cf->hbits = xdl_hashbits((unsigned int) size);
73 cf->hsize = 1 << cf->hbits;
74
@@ -103,8 +106,9 @@ static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned
106 line = rec->ptr;
107 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
108 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
106 - if (rcrec->ha == rec->ha && rcrec->size == rec->size &&
107 - !memcmp(line, rcrec->line, rec->size))
109 + if (rcrec->ha == rec->ha &&
110 + xdl_recmatch(rcrec->line, rcrec->size,
111 + rec->ptr, rec->size, cf->flags))
112 break;
113
114 if (!rcrec) {
@@ -173,7 +177,7 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
177 top = blk + bsize;
178 }
179 prev = cur;
176 - hav = xdl_hash_record(&cur, top);
180 + hav = xdl_hash_record(&cur, top, xpp->flags);
181 if (nrec >= narec) {
182 narec *= 2;
183 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
@@ -268,7 +272,7 @@ int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
272 enl1 = xdl_guess_lines(mf1) + 1;
273 enl2 = xdl_guess_lines(mf2) + 1;
274
271 - if (xdl_init_classifier(&cf, enl1 + enl2 + 1) < 0) {
275 + if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) {
276
277 return -1;
278 }
xdiff/xutils.c
+50 -1
@@ -186,12 +186,61 @@ long xdl_guess_lines(mmfile_t *mf) {
186 return nl + 1;
187 }
188
189 +int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
190 +{
191 + int i1, i2;
192 +
193 + if (flags & XDF_IGNORE_WHITESPACE) {
194 + for (i1 = i2 = 0; i1 < s1 && i2 < s2; i1++, i2++) {
195 + if (isspace(l1[i1]))
196 + while (isspace(l1[i1]) && i1 < s1)
197 + i1++;
198 + else if (isspace(l2[i2]))
199 + while (isspace(l2[i2]) && i2 < s2)
200 + i2++;
201 + else if (l1[i1] != l2[i2])
202 + return l2[i2] - l1[i1];
203 + }
204 + if (i1 >= s1)
205 + return 1;
206 + else if (i2 >= s2)
207 + return -1;
208 + } else if (flags & XDF_IGNORE_WHITESPACE_CHANGE) {
209 + for (i1 = i2 = 0; i1 < s1 && i2 < s2; i1++, i2++) {
210 + if (isspace(l1[i1])) {
211 + if (!isspace(l2[i2]))
212 + return -1;
213 + while (isspace(l1[i1]) && i1 < s1)
214 + i1++;
215 + while (isspace(l2[i2]) && i2 < s2)
216 + i2++;
217 + } else if (l1[i1] != l2[i2])
218 + return l2[i2] - l1[i1];
219 + }
220 + if (i1 >= s1)
221 + return 1;
222 + else if (i2 >= s2)
223 + return -1;
224 + } else
225 + return s1 == s2 && !memcmp(l1, l2, s1);
226 +
227 + return 0;
228 +}
229
190 -unsigned long xdl_hash_record(char const **data, char const *top) {
230 +unsigned long xdl_hash_record(char const **data, char const *top, long flags) {
231 unsigned long ha = 5381;
232 char const *ptr = *data;
233
234 for (; ptr < top && *ptr != '\n'; ptr++) {
235 + if (isspace(*ptr) && (flags & XDF_WHITESPACE_FLAGS)) {
236 + while (ptr < top && isspace(*ptr) && ptr[1] != '\n')
237 + ptr++;
238 + if (flags & XDF_IGNORE_WHITESPACE_CHANGE) {
239 + ha += (ha << 5);
240 + ha ^= (unsigned long) ' ';
241 + }
242 + continue;
243 + }
244 ha += (ha << 5);
245 ha ^= (unsigned long) *ptr;
246 }
xdiff/xutils.h
+2 -1
@@ -34,7 +34,8 @@ void *xdl_cha_alloc(chastore_t *cha);
34 void *xdl_cha_first(chastore_t *cha);
35 void *xdl_cha_next(chastore_t *cha);
36 long xdl_guess_lines(mmfile_t *mf);
37 -unsigned long xdl_hash_record(char const **data, char const *top);
37 +int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags);
38 +unsigned long xdl_hash_record(char const **data, char const *top, long flags);
39 unsigned int xdl_hashbits(unsigned int size);
40 int xdl_num_out(char *out, long val);
41 long xdl_atol(char const *str, char const **next);