Skip to content

Commit 7c61e25

Browse files
peffgitster
authored andcommitted
diff: use hunk callback for word-diff
Our word-diff does not look at the -/+ lines generated by xdiff at all (because they are not real lines to show the user, but just the tokenized words split into lines). Instead we use the line numbers from the hunk headers to index our own data structure. As a result, our xdi_diff_outf() callback throws away all lines except hunk headers. We can instead use a hunk callback, which has two benefits: 1. We don't have to re-parse the generated hunk header line, but can use the passed parameters directly. 2. By setting our line callback to NULL, we can tell xdiff-interface that it does not even need to bother generating the other lines, saving a small amount of work. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b135739 commit 7c61e25

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

diff.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,19 +1883,17 @@ static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
18831883
}
18841884
}
18851885

1886-
static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
1886+
static void fn_out_diff_words_aux(void *priv,
1887+
long minus_first, long minus_len,
1888+
long plus_first, long plus_len,
1889+
const char *func, long funclen)
18871890
{
18881891
struct diff_words_data *diff_words = priv;
18891892
struct diff_words_style *style = diff_words->style;
1890-
int minus_first, minus_len, plus_first, plus_len;
18911893
const char *minus_begin, *minus_end, *plus_begin, *plus_end;
18921894
struct diff_options *opt = diff_words->opt;
18931895
const char *line_prefix;
18941896

1895-
if (line[0] != '@' || parse_hunk_header(line, len,
1896-
&minus_first, &minus_len, &plus_first, &plus_len))
1897-
return;
1898-
18991897
assert(opt);
19001898
line_prefix = diff_line_prefix(opt);
19011899

@@ -2045,7 +2043,7 @@ static void diff_words_show(struct diff_words_data *diff_words)
20452043
xpp.flags = 0;
20462044
/* as only the hunk header will be parsed, we need a 0-context */
20472045
xecfg.ctxlen = 0;
2048-
if (xdi_diff_outf(&minus, &plus, NULL, fn_out_diff_words_aux,
2046+
if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL,
20492047
diff_words, &xpp, &xecfg))
20502048
die("unable to generate word diff");
20512049
free(minus.ptr);

xdiff-interface.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
9595
struct xdiff_emit_state *priv = priv_;
9696
int i;
9797

98+
if (!priv->line_fn)
99+
return 0;
100+
98101
for (i = 0; i < nbuf; i++) {
99102
if (mb[i].ptr[mb[i].size-1] != '\n') {
100103
/* Incomplete line */

0 commit comments

Comments
 (0)