combine-diff: use an xdiff hunk callback

A combined diff has to line up the hunks for all of the individual pairwise diffs, and thus needs to know their line numbers and sizes. We get that now by parsing the hunk header line that xdiff generates. However, now that xdiff supports a hunk callback, we can just use the values directly. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 2, 2018 at 02:38 UTC 0074c9110db6b8cddc9950f94f69d143379b25ed
1 file changed +36 -31
combine-diff.c
+36 -31
@@ -344,38 +344,43 @@ struct combine_diff_state {
344 struct sline *lost_bucket;
345 };
346
347 -static void consume_line(void *state_, char *line, unsigned long len)
347 +static void consume_hunk(void *state_,
348 + long ob, long on,
349 + long nb, long nn,
350 + const char *funcline, long funclen)
351 {
352 struct combine_diff_state *state = state_;
350 - if (5 < len && !memcmp("@@ -", line, 4)) {
351 - if (parse_hunk_header(line, len,
352 - &state->ob, &state->on,
353 - &state->nb, &state->nn))
354 - return;
355 - state->lno = state->nb;
356 - if (state->nn == 0) {
357 - /* @@ -X,Y +N,0 @@ removed Y lines
358 - * that would have come *after* line N
359 - * in the result. Our lost buckets hang
360 - * to the line after the removed lines,
361 - *
362 - * Note that this is correct even when N == 0,
363 - * in which case the hunk removes the first
364 - * line in the file.
365 - */
366 - state->lost_bucket = &state->sline[state->nb];
367 - if (!state->nb)
368 - state->nb = 1;
369 - } else {
370 - state->lost_bucket = &state->sline[state->nb-1];
371 - }
372 - if (!state->sline[state->nb-1].p_lno)
373 - state->sline[state->nb-1].p_lno =
374 - xcalloc(state->num_parent,
375 - sizeof(unsigned long));
376 - state->sline[state->nb-1].p_lno[state->n] = state->ob;
377 - return;
353 +
354 + state->ob = ob;
355 + state->on = on;
356 + state->nb = nb;
357 + state->nn = nn;
358 + state->lno = state->nb;
359 + if (state->nn == 0) {
360 + /* @@ -X,Y +N,0 @@ removed Y lines
361 + * that would have come *after* line N
362 + * in the result. Our lost buckets hang
363 + * to the line after the removed lines,
364 + *
365 + * Note that this is correct even when N == 0,
366 + * in which case the hunk removes the first
367 + * line in the file.
368 + */
369 + state->lost_bucket = &state->sline[state->nb];
370 + if (!state->nb)
371 + state->nb = 1;
372 + } else {
373 + state->lost_bucket = &state->sline[state->nb-1];
374 }
375 + if (!state->sline[state->nb-1].p_lno)
376 + state->sline[state->nb-1].p_lno =
377 + xcalloc(state->num_parent, sizeof(unsigned long));
378 + state->sline[state->nb-1].p_lno[state->n] = state->ob;
379 +}
380 +
381 +static void consume_line(void *state_, char *line, unsigned long len)
382 +{
383 + struct combine_diff_state *state = state_;
384 if (!state->lost_bucket)
385 return; /* not in any hunk yet */
386 switch (line[0]) {
@@ -419,8 +424,8 @@ static void combine_diff(const struct object_id *parent, unsigned int mode,
424 state.num_parent = num_parent;
425 state.n = n;
426
422 - if (xdi_diff_outf(&parent_file, result_file, NULL, consume_line,
423 - &state, &xpp, &xecfg))
427 + if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
428 + consume_line, &state, &xpp, &xecfg))
429 die("unable to generate combined diff for %s",
430 oid_to_hex(parent));
431 free(parent_file.ptr);