Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75d0b81

Browse files
ethomsoncarlosmn
authored andcommitted
xdiff: upgrade to core git 2.4.5
Upgrade xdiff to version used in core git 2.4.5 (0df0541). Corrects an issue where an LF is added at EOF while applying an unrelated change (ba31180), cleans up some unused code (be89977 and e5b0662), and provides an improved callback to avoid leaking internal (to xdiff) structures (467d348). This also adds some additional functionality that we do not yet take advantage of, namely the ability to ignore changes whose lines are all blank (36617af).
1 parent ba6b288 commit 75d0b81

12 files changed

+137
-76
lines changed

src/blame_git.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -304,21 +304,16 @@ static void blame_chunk(
304304
}
305305

306306
static int my_emit(
307-
xdfenv_t *xe,
308-
xdchange_t *xscr,
309-
xdemitcb_t *ecb,
310-
xdemitconf_t const *xecfg)
307+
long start_a, long count_a,
308+
long start_b, long count_b,
309+
void *cb_data)
311310
{
312-
xdchange_t *xch = xscr;
313-
GIT_UNUSED(xe);
314-
GIT_UNUSED(xecfg);
315-
while (xch) {
316-
blame_chunk_cb_data *d = ecb->priv;
317-
blame_chunk(d->blame, d->tlno, d->plno, xch->i2, d->target, d->parent);
318-
d->plno = xch->i1 + xch->chg1;
319-
d->tlno = xch->i2 + xch->chg2;
320-
xch = xch->next;
321-
}
311+
blame_chunk_cb_data *d = (blame_chunk_cb_data *)cb_data;
312+
313+
blame_chunk(d->blame, d->tlno, d->plno, start_b, d->target, d->parent);
314+
d->plno = start_a + count_a;
315+
d->tlno = start_b + count_b;
316+
322317
return 0;
323318
}
324319

@@ -352,7 +347,7 @@ static int diff_hunks(mmfile_t file_a, mmfile_t file_b, void *cb_data)
352347
xdemitconf_t xecfg = {0};
353348
xdemitcb_t ecb = {0};
354349

355-
xecfg.emit_func = (void(*)(void))my_emit;
350+
xecfg.hunk_func = my_emit;
356351
ecb.priv = cb_data;
357352

358353
trim_common_tail(&file_a, &file_b, 0);

src/xdiff/xdiff.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ extern "C" {
3232
#define XDF_IGNORE_WHITESPACE (1 << 2)
3333
#define XDF_IGNORE_WHITESPACE_CHANGE (1 << 3)
3434
#define XDF_IGNORE_WHITESPACE_AT_EOL (1 << 4)
35+
#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE | XDF_IGNORE_WHITESPACE_AT_EOL)
36+
3537
#define XDF_PATIENCE_DIFF (1 << 5)
3638
#define XDF_HISTOGRAM_DIFF (1 << 6)
37-
#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE | XDF_IGNORE_WHITESPACE_AT_EOL)
39+
#define XDF_DIFF_ALGORITHM_MASK (XDF_PATIENCE_DIFF | XDF_HISTOGRAM_DIFF)
40+
#define XDF_DIFF_ALG(x) ((x) & XDF_DIFF_ALGORITHM_MASK)
3841

39-
#define XDL_PATCH_NORMAL '-'
40-
#define XDL_PATCH_REVERSE '+'
41-
#define XDL_PATCH_MODEMASK ((1 << 8) - 1)
42-
#define XDL_PATCH_IGNOREBSPACE (1 << 8)
42+
#define XDF_IGNORE_BLANK_LINES (1 << 7)
4343

4444
#define XDL_EMIT_FUNCNAMES (1 << 0)
4545
#define XDL_EMIT_COMMON (1 << 1)
@@ -88,13 +88,17 @@ typedef struct s_xdemitcb {
8888

8989
typedef long (*find_func_t)(const char *line, long line_len, char *buffer, long buffer_size, void *priv);
9090

91+
typedef int (*xdl_emit_hunk_consume_func_t)(long start_a, long count_a,
92+
long start_b, long count_b,
93+
void *cb_data);
94+
9195
typedef struct s_xdemitconf {
9296
long ctxlen;
9397
long interhunkctxlen;
9498
unsigned long flags;
9599
find_func_t find_func;
96100
void *find_func_priv;
97-
void (*emit_func)(void);
101+
xdl_emit_hunk_consume_func_t hunk_func;
98102
} xdemitconf_t;
99103

100104
typedef struct s_bdiffparam {

src/xdiff/xdiffi.c

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
328328
xdalgoenv_t xenv;
329329
diffdata_t dd1, dd2;
330330

331-
if (xpp->flags & XDF_PATIENCE_DIFF)
331+
if (XDF_DIFF_ALG(xpp->flags) == XDF_PATIENCE_DIFF)
332332
return xdl_do_patience_diff(mf1, mf2, xpp, xe);
333333

334-
if (xpp->flags & XDF_HISTOGRAM_DIFF)
334+
if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF)
335335
return xdl_do_histogram_diff(mf1, mf2, xpp, xe);
336336

337337
if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) {
@@ -394,6 +394,7 @@ static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1,
394394
xch->i2 = i2;
395395
xch->chg1 = chg1;
396396
xch->chg2 = chg2;
397+
xch->ignore = 0;
397398

398399
return xch;
399400
}
@@ -538,13 +539,49 @@ void xdl_free_script(xdchange_t *xscr) {
538539
}
539540
}
540541

542+
static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
543+
xdemitconf_t const *xecfg)
544+
{
545+
xdchange_t *xch, *xche;
546+
547+
for (xch = xscr; xch; xch = xche->next) {
548+
xche = xdl_get_hunk(&xch, xecfg);
549+
if (!xch)
550+
break;
551+
if (xecfg->hunk_func(xch->i1, xche->i1 + xche->chg1 - xch->i1,
552+
xch->i2, xche->i2 + xche->chg2 - xch->i2,
553+
ecb->priv) < 0)
554+
return -1;
555+
}
556+
return 0;
557+
}
558+
559+
static void xdl_mark_ignorable(xdchange_t *xscr, xdfenv_t *xe, long flags)
560+
{
561+
xdchange_t *xch;
562+
563+
for (xch = xscr; xch; xch = xch->next) {
564+
int ignore = 1;
565+
xrecord_t **rec;
566+
long i;
567+
568+
rec = &xe->xdf1.recs[xch->i1];
569+
for (i = 0; i < xch->chg1 && ignore; i++)
570+
ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags);
571+
572+
rec = &xe->xdf2.recs[xch->i2];
573+
for (i = 0; i < xch->chg2 && ignore; i++)
574+
ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags);
575+
576+
xch->ignore = ignore;
577+
}
578+
}
541579

542580
int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
543581
xdemitconf_t const *xecfg, xdemitcb_t *ecb) {
544582
xdchange_t *xscr;
545583
xdfenv_t xe;
546-
emit_func_t ef = xecfg->emit_func ?
547-
(emit_func_t)xecfg->emit_func : xdl_emit_diff;
584+
emit_func_t ef = xecfg->hunk_func ? xdl_call_hunk_func : xdl_emit_diff;
548585

549586
if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) {
550587

@@ -558,6 +595,9 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
558595
return -1;
559596
}
560597
if (xscr) {
598+
if (xpp->flags & XDF_IGNORE_BLANK_LINES)
599+
xdl_mark_ignorable(xscr, &xe, xpp->flags);
600+
561601
if (ef(&xe, xscr, ecb, xecfg) < 0) {
562602

563603
xdl_free_script(xscr);

src/xdiff/xdiffi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ typedef struct s_xdchange {
4141
struct s_xdchange *next;
4242
long i1, i2;
4343
long chg1, chg2;
44+
int ignore;
4445
} xdchange_t;
4546

4647

src/xdiff/xemit.c

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,51 @@ static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *
5656
/*
5757
* Starting at the passed change atom, find the latest change atom to be included
5858
* inside the differential hunk according to the specified configuration.
59+
* Also advance xscr if the first changes must be discarded.
5960
*/
60-
xdchange_t *xdl_get_hunk(xdchange_t *xscr, xdemitconf_t const *xecfg) {
61-
xdchange_t *xch, *xchp;
61+
xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg)
62+
{
63+
xdchange_t *xch, *xchp, *lxch;
6264
long max_common = 2 * xecfg->ctxlen + xecfg->interhunkctxlen;
65+
long max_ignorable = xecfg->ctxlen;
66+
unsigned long ignored = 0; /* number of ignored blank lines */
67+
68+
/* remove ignorable changes that are too far before other changes */
69+
for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) {
70+
xch = xchp->next;
71+
72+
if (xch == NULL ||
73+
xch->i1 - (xchp->i1 + xchp->chg1) >= max_ignorable)
74+
*xscr = xch;
75+
}
76+
77+
if (*xscr == NULL)
78+
return NULL;
79+
80+
lxch = *xscr;
6381

64-
for (xchp = xscr, xch = xscr->next; xch; xchp = xch, xch = xch->next)
65-
if (xch->i1 - (xchp->i1 + xchp->chg1) > max_common)
82+
for (xchp = *xscr, xch = xchp->next; xch; xchp = xch, xch = xch->next) {
83+
long distance = xch->i1 - (xchp->i1 + xchp->chg1);
84+
if (distance > max_common)
6685
break;
6786

68-
return xchp;
87+
if (distance < max_ignorable && (!xch->ignore || lxch == xchp)) {
88+
lxch = xch;
89+
ignored = 0;
90+
} else if (distance < max_ignorable && xch->ignore) {
91+
ignored += xch->chg2;
92+
} else if (lxch != xchp &&
93+
xch->i1 + ignored - (lxch->i1 + lxch->chg1) > max_common) {
94+
break;
95+
} else if (!xch->ignore) {
96+
lxch = xch;
97+
ignored = 0;
98+
} else {
99+
ignored += xch->chg2;
100+
}
101+
}
102+
103+
return lxch;
69104
}
70105

71106

@@ -144,7 +179,9 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
144179
return xdl_emit_common(xe, xscr, ecb, xecfg);
145180

146181
for (xch = xscr; xch; xch = xche->next) {
147-
xche = xdl_get_hunk(xch, xecfg);
182+
xche = xdl_get_hunk(&xch, xecfg);
183+
if (!xch)
184+
break;
148185

149186
s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0);
150187
s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0);

src/xdiff/xemit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
typedef int (*emit_func_t)(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
2828
xdemitconf_t const *xecfg);
2929

30-
xdchange_t *xdl_get_hunk(xdchange_t *xscr, xdemitconf_t const *xecfg);
30+
xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg);
3131
int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
3232
xdemitconf_t const *xecfg);
3333

src/xdiff/xhistogram.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ static int fall_back_to_classic_diff(struct histindex *index,
258258
int line1, int count1, int line2, int count2)
259259
{
260260
xpparam_t xpp;
261-
xpp.flags = index->xpp->flags & ~XDF_HISTOGRAM_DIFF;
261+
xpp.flags = index->xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
262262

263263
return xdl_fall_back_diff(index->env, &xpp,
264264
line1, count1, line2, count2);

src/xdiff/xmerge.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,11 @@ static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
245245
dest ? dest + size : NULL);
246246
/* Postimage from side #1 */
247247
if (m->mode & 1)
248-
size += xdl_recs_copy(xe1, m->i1, m->chg1, 1,
248+
size += xdl_recs_copy(xe1, m->i1, m->chg1, (m->mode & 2),
249249
dest ? dest + size : NULL);
250250
/* Postimage from side #2 */
251251
if (m->mode & 2)
252-
size += xdl_recs_copy(xe2, m->i2, m->chg2, 1,
252+
size += xdl_recs_copy(xe2, m->i2, m->chg2, 0,
253253
dest ? dest + size : NULL);
254254
} else
255255
continue;

src/xdiff/xpatience.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ static int fall_back_to_classic_diff(struct hashmap *map,
288288
int line1, int count1, int line2, int count2)
289289
{
290290
xpparam_t xpp;
291-
xpp.flags = map->xpp->flags & ~XDF_PATIENCE_DIFF;
291+
xpp.flags = map->xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
292292

293293
return xdl_fall_back_diff(map->env, &xpp,
294294
line1, count1, line2, count2);

src/xdiff/xprepare.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
181181
if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *))))
182182
goto abort;
183183

184-
if (xpp->flags & XDF_HISTOGRAM_DIFF)
184+
if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF)
185185
hbits = hsize = 0;
186186
else {
187187
hbits = xdl_hashbits((unsigned int) narec);
@@ -209,8 +209,8 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
209209
crec->ha = hav;
210210
recs[nrec++] = crec;
211211

212-
if (!(xpp->flags & XDF_HISTOGRAM_DIFF) &&
213-
xdl_classify_record(pass, cf, rhash, hbits, crec) < 0)
212+
if ((XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) &&
213+
xdl_classify_record(pass, cf, rhash, hbits, crec) < 0)
214214
goto abort;
215215
}
216216
}
@@ -273,16 +273,15 @@ int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
273273
* (nrecs) will be updated correctly anyway by
274274
* xdl_prepare_ctx().
275275
*/
276-
sample = xpp->flags & XDF_HISTOGRAM_DIFF ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1;
276+
sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF
277+
? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1);
277278

278279
enl1 = xdl_guess_lines(mf1, sample) + 1;
279280
enl2 = xdl_guess_lines(mf2, sample) + 1;
280281

281-
if (!(xpp->flags & XDF_HISTOGRAM_DIFF) &&
282-
xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) {
283-
282+
if (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF &&
283+
xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0)
284284
return -1;
285-
}
286285

287286
if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
288287

@@ -296,9 +295,9 @@ int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
296295
return -1;
297296
}
298297

299-
if (!(xpp->flags & XDF_PATIENCE_DIFF) &&
300-
!(xpp->flags & XDF_HISTOGRAM_DIFF) &&
301-
xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
298+
if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
299+
(XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) &&
300+
xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
302301

303302
xdl_free_ctx(&xe->xdf2);
304303
xdl_free_ctx(&xe->xdf1);

src/xdiff/xutils.c

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -120,35 +120,6 @@ void *xdl_cha_alloc(chastore_t *cha) {
120120
return data;
121121
}
122122

123-
124-
void *xdl_cha_first(chastore_t *cha) {
125-
chanode_t *sncur;
126-
127-
if (!(cha->sncur = sncur = cha->head))
128-
return NULL;
129-
130-
cha->scurr = 0;
131-
132-
return (char *) sncur + sizeof(chanode_t) + cha->scurr;
133-
}
134-
135-
136-
void *xdl_cha_next(chastore_t *cha) {
137-
chanode_t *sncur;
138-
139-
if (!(sncur = cha->sncur))
140-
return NULL;
141-
cha->scurr += cha->isize;
142-
if (cha->scurr == sncur->icurr) {
143-
if (!(sncur = cha->sncur = sncur->next))
144-
return NULL;
145-
cha->scurr = 0;
146-
}
147-
148-
return (char *) sncur + sizeof(chanode_t) + cha->scurr;
149-
}
150-
151-
152123
long xdl_guess_lines(mmfile_t *mf, long sample) {
153124
long nl = 0, size, tsize = 0;
154125
char const *data, *cur, *top;
@@ -170,6 +141,19 @@ long xdl_guess_lines(mmfile_t *mf, long sample) {
170141
return nl + 1;
171142
}
172143

144+
int xdl_blankline(const char *line, long size, long flags)
145+
{
146+
long i;
147+
148+
if (!(flags & XDF_WHITESPACE_FLAGS))
149+
return (size <= 1);
150+
151+
for (i = 0; i < size && XDL_ISSPACE(line[i]); i++)
152+
;
153+
154+
return (i == size);
155+
}
156+
173157
int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
174158
{
175159
int i1, i2;

src/xdiff/xutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void *xdl_cha_alloc(chastore_t *cha);
3434
void *xdl_cha_first(chastore_t *cha);
3535
void *xdl_cha_next(chastore_t *cha);
3636
long xdl_guess_lines(mmfile_t *mf, long sample);
37+
int xdl_blankline(const char *line, long size, long flags);
3738
int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags);
3839
unsigned long xdl_hash_record(char const **data, char const *top, long flags);
3940
unsigned int xdl_hashbits(unsigned int size);

0 commit comments

Comments
 (0)