Skip to content

Commit 0074c91

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7c61e25 commit 0074c91

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

combine-diff.c

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -344,38 +344,43 @@ struct combine_diff_state {
344344
struct sline *lost_bucket;
345345
};
346346

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)
348351
{
349352
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];
378374
}
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_;
379384
if (!state->lost_bucket)
380385
return; /* not in any hunk yet */
381386
switch (line[0]) {
@@ -419,8 +424,8 @@ static void combine_diff(const struct object_id *parent, unsigned int mode,
419424
state.num_parent = num_parent;
420425
state.n = n;
421426

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))
424429
die("unable to generate combined diff for %s",
425430
oid_to_hex(parent));
426431
free(parent_file.ptr);

0 commit comments

Comments
 (0)