Skip to content

Commit 882749a

Browse files
trastgitster
authored andcommitted
diff: add --word-diff option that generalizes --color-words
This teaches the --color-words engine a more general interface that supports two new modes: * --word-diff=plain, inspired by the 'wdiff' utility (most similar to 'wdiff -n <old> <new>'): uses delimiters [-removed-] and {+added+} * --word-diff=porcelain, which generates an ad-hoc machine readable format: - each diff unit is prefixed by [-+ ] and terminated by newline as in unified diff - newlines in the input are output as a line consisting only of a tilde '~' Both of these formats still support color if it is enabled, using it to highlight the differences. --color-words becomes a synonym for --word-diff=color, which is the color-only format. Also adds some compatibility/convenience options. Thanks to Junio C Hamano and Miles Bader for good ideas. Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 6555b19 commit 882749a

File tree

7 files changed

+288
-54
lines changed

7 files changed

+288
-54
lines changed

Documentation/diff-options.txt

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,39 @@ any of those replacements occurred.
126126
gives the default to color output.
127127
Same as `--color=never`.
128128

129-
--color-words[=<regex>]::
130-
Show colored word diff, i.e., color words which have changed.
131-
By default, words are separated by whitespace.
129+
--word-diff[=<mode>]::
130+
Show a word diff, using the <mode> to delimit changed words.
131+
By default, words are delimited by whitespace; see
132+
`--word-diff-regex` below. The <mode> defaults to 'plain', and
133+
must be one of:
134+
+
135+
--
136+
color::
137+
Highlight changed words using only colors. Implies `--color`.
138+
plain::
139+
Show words as `[-removed-]` and `{+added+}`. Makes no
140+
attempts to escape the delimiters if they appear in the input,
141+
so the output may be ambiguous.
142+
porcelain::
143+
Use a special line-based format intended for script
144+
consumption. Added/removed/unchanged runs are printed in the
145+
usual unified diff format, starting with a `+`/`-`/` `
146+
character at the beginning of the line and extending to the
147+
end of the line. Newlines in the input are represented by a
148+
tilde `~` on a line of its own.
149+
none::
150+
Disable word diff again.
151+
--
152+
+
153+
Note that despite the name of the first mode, color is used to
154+
highlight the changed parts in all modes if enabled.
155+
156+
--word-diff-regex=<regex>::
157+
Use <regex> to decide what a word is, instead of considering
158+
runs of non-whitespace to be a word. Also implies
159+
`--word-diff` unless it was already enabled.
132160
+
133-
When a <regex> is specified, every non-overlapping match of the
161+
Every non-overlapping match of the
134162
<regex> is considered a word. Anything between these matches is
135163
considered whitespace and ignored(!) for the purposes of finding
136164
differences. You may want to append `|[^[:space:]]` to your regular
@@ -142,6 +170,10 @@ The regex can also be set via a diff driver or configuration option, see
142170
linkgit:gitattributes[1] or linkgit:git-config[1]. Giving it explicitly
143171
overrides any diff driver or configuration setting. Diff drivers
144172
override configuration settings.
173+
174+
--color-words[=<regex>]::
175+
Equivalent to `--word-diff=color` plus (if a regex was
176+
specified) `--word-diff-regex=<regex>`.
145177
endif::git-format-patch[]
146178

147179
--no-renames::

Documentation/gitattributes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ patterns are available:
360360
Customizing word diff
361361
^^^^^^^^^^^^^^^^^^^^^
362362

363-
You can customize the rules that `git diff --color-words` uses to
363+
You can customize the rules that `git diff --word-diff` uses to
364364
split words in a line, by specifying an appropriate regular expression
365365
in the "diff.*.wordRegex" configuration variable. For example, in TeX
366366
a backslash followed by a sequence of letters forms a command, but

color.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -211,31 +211,3 @@ int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
211211
va_end(args);
212212
return r;
213213
}
214-
215-
/*
216-
* This function splits the buffer by newlines and colors the lines individually.
217-
*
218-
* Returns 0 on success.
219-
*/
220-
int color_fwrite_lines(FILE *fp, const char *color,
221-
size_t count, const char *buf)
222-
{
223-
if (!*color)
224-
return fwrite(buf, count, 1, fp) != 1;
225-
while (count) {
226-
char *p = memchr(buf, '\n', count);
227-
if (p != buf && (fputs(color, fp) < 0 ||
228-
fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
229-
fputs(GIT_COLOR_RESET, fp) < 0))
230-
return -1;
231-
if (!p)
232-
return 0;
233-
if (fputc('\n', fp) < 0)
234-
return -1;
235-
count -= p + 1 - buf;
236-
buf = p + 1;
237-
}
238-
return 0;
239-
}
240-
241-

color.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,5 @@ __attribute__((format (printf, 3, 4)))
6161
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
6262
__attribute__((format (printf, 3, 4)))
6363
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
64-
int color_fwrite_lines(FILE *fp, const char *color, size_t count, const char *buf);
6564

6665
#endif /* COLOR_H */

diff.c

Lines changed: 120 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -572,16 +572,68 @@ static void diff_words_append(char *line, unsigned long len,
572572
buffer->text.ptr[buffer->text.size] = '\0';
573573
}
574574

575+
struct diff_words_style_elem
576+
{
577+
const char *prefix;
578+
const char *suffix;
579+
const char *color; /* NULL; filled in by the setup code if
580+
* color is enabled */
581+
};
582+
583+
struct diff_words_style
584+
{
585+
enum diff_words_type type;
586+
struct diff_words_style_elem new, old, ctx;
587+
const char *newline;
588+
};
589+
590+
struct diff_words_style diff_words_styles[] = {
591+
{ DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
592+
{ DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
593+
{ DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
594+
};
595+
575596
struct diff_words_data {
576597
struct diff_words_buffer minus, plus;
577598
const char *current_plus;
578599
FILE *file;
579600
regex_t *word_regex;
601+
enum diff_words_type type;
602+
struct diff_words_style *style;
580603
};
581604

605+
static int fn_out_diff_words_write_helper(FILE *fp,
606+
struct diff_words_style_elem *st_el,
607+
const char *newline,
608+
size_t count, const char *buf)
609+
{
610+
while (count) {
611+
char *p = memchr(buf, '\n', count);
612+
if (p != buf) {
613+
if (st_el->color && fputs(st_el->color, fp) < 0)
614+
return -1;
615+
if (fputs(st_el->prefix, fp) < 0 ||
616+
fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
617+
fputs(st_el->suffix, fp) < 0)
618+
return -1;
619+
if (st_el->color && *st_el->color
620+
&& fputs(GIT_COLOR_RESET, fp) < 0)
621+
return -1;
622+
}
623+
if (!p)
624+
return 0;
625+
if (fputs(newline, fp) < 0)
626+
return -1;
627+
count -= p + 1 - buf;
628+
buf = p + 1;
629+
}
630+
return 0;
631+
}
632+
582633
static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
583634
{
584635
struct diff_words_data *diff_words = priv;
636+
struct diff_words_style *style = diff_words->style;
585637
int minus_first, minus_len, plus_first, plus_len;
586638
const char *minus_begin, *minus_end, *plus_begin, *plus_end;
587639

@@ -605,16 +657,17 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
605657
plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
606658

607659
if (diff_words->current_plus != plus_begin)
608-
fwrite(diff_words->current_plus,
609-
plus_begin - diff_words->current_plus, 1,
610-
diff_words->file);
660+
fn_out_diff_words_write_helper(diff_words->file,
661+
&style->ctx, style->newline,
662+
plus_begin - diff_words->current_plus,
663+
diff_words->current_plus);
611664
if (minus_begin != minus_end)
612-
color_fwrite_lines(diff_words->file,
613-
diff_get_color(1, DIFF_FILE_OLD),
665+
fn_out_diff_words_write_helper(diff_words->file,
666+
&style->old, style->newline,
614667
minus_end - minus_begin, minus_begin);
615668
if (plus_begin != plus_end)
616-
color_fwrite_lines(diff_words->file,
617-
diff_get_color(1, DIFF_FILE_NEW),
669+
fn_out_diff_words_write_helper(diff_words->file,
670+
&style->new, style->newline,
618671
plus_end - plus_begin, plus_begin);
619672

620673
diff_words->current_plus = plus_end;
@@ -697,11 +750,12 @@ static void diff_words_show(struct diff_words_data *diff_words)
697750
xdemitconf_t xecfg;
698751
xdemitcb_t ecb;
699752
mmfile_t minus, plus;
753+
struct diff_words_style *style = diff_words->style;
700754

701755
/* special case: only removal */
702756
if (!diff_words->plus.text.size) {
703-
color_fwrite_lines(diff_words->file,
704-
diff_get_color(1, DIFF_FILE_OLD),
757+
fn_out_diff_words_write_helper(diff_words->file,
758+
&style->old, style->newline,
705759
diff_words->minus.text.size, diff_words->minus.text.ptr);
706760
diff_words->minus.text.size = 0;
707761
return;
@@ -722,10 +776,10 @@ static void diff_words_show(struct diff_words_data *diff_words)
722776
free(plus.ptr);
723777
if (diff_words->current_plus != diff_words->plus.text.ptr +
724778
diff_words->plus.text.size)
725-
fwrite(diff_words->current_plus,
779+
fn_out_diff_words_write_helper(diff_words->file,
780+
&style->ctx, style->newline,
726781
diff_words->plus.text.ptr + diff_words->plus.text.size
727-
- diff_words->current_plus, 1,
728-
diff_words->file);
782+
- diff_words->current_plus, diff_words->current_plus);
729783
diff_words->minus.text.size = diff_words->plus.text.size = 0;
730784
}
731785

@@ -837,6 +891,9 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
837891

838892
if (len < 1) {
839893
emit_line(ecbdata->file, reset, reset, line, len);
894+
if (ecbdata->diff_words
895+
&& ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN)
896+
fputs("~\n", ecbdata->file);
840897
return;
841898
}
842899

@@ -851,9 +908,13 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
851908
return;
852909
}
853910
diff_words_flush(ecbdata);
854-
line++;
855-
len--;
856-
emit_line(ecbdata->file, plain, reset, line, len);
911+
if (ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN) {
912+
emit_line(ecbdata->file, plain, reset, line, len);
913+
fputs("~\n", ecbdata->file);
914+
} else {
915+
/* don't print the prefix character */
916+
emit_line(ecbdata->file, plain, reset, line+1, len-1);
917+
}
857918
return;
858919
}
859920

@@ -1755,10 +1816,13 @@ static void builtin_diff(const char *name_a,
17551816
xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
17561817
else if (!prefixcmp(diffopts, "-u"))
17571818
xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1758-
if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) {
1819+
if (o->word_diff) {
1820+
int i;
1821+
17591822
ecbdata.diff_words =
17601823
xcalloc(1, sizeof(struct diff_words_data));
17611824
ecbdata.diff_words->file = o->file;
1825+
ecbdata.diff_words->type = o->word_diff;
17621826
if (!o->word_regex)
17631827
o->word_regex = userdiff_word_regex(one);
17641828
if (!o->word_regex)
@@ -1774,10 +1838,23 @@ static void builtin_diff(const char *name_a,
17741838
die ("Invalid regular expression: %s",
17751839
o->word_regex);
17761840
}
1841+
for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
1842+
if (o->word_diff == diff_words_styles[i].type) {
1843+
ecbdata.diff_words->style =
1844+
&diff_words_styles[i];
1845+
break;
1846+
}
1847+
}
1848+
if (DIFF_OPT_TST(o, COLOR_DIFF)) {
1849+
struct diff_words_style *st = ecbdata.diff_words->style;
1850+
st->old.color = diff_get_color_opt(o, DIFF_FILE_OLD);
1851+
st->new.color = diff_get_color_opt(o, DIFF_FILE_NEW);
1852+
st->ctx.color = diff_get_color_opt(o, DIFF_PLAIN);
1853+
}
17771854
}
17781855
xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
17791856
&xpp, &xecfg, &ecb);
1780-
if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))
1857+
if (o->word_diff)
17811858
free_diff_words_data(&ecbdata);
17821859
if (textconv_one)
17831860
free(mf1.ptr);
@@ -2845,13 +2922,37 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
28452922
DIFF_OPT_CLR(options, COLOR_DIFF);
28462923
else if (!strcmp(arg, "--color-words")) {
28472924
DIFF_OPT_SET(options, COLOR_DIFF);
2848-
DIFF_OPT_SET(options, COLOR_DIFF_WORDS);
2925+
options->word_diff = DIFF_WORDS_COLOR;
28492926
}
28502927
else if (!prefixcmp(arg, "--color-words=")) {
28512928
DIFF_OPT_SET(options, COLOR_DIFF);
2852-
DIFF_OPT_SET(options, COLOR_DIFF_WORDS);
2929+
options->word_diff = DIFF_WORDS_COLOR;
28532930
options->word_regex = arg + 14;
28542931
}
2932+
else if (!strcmp(arg, "--word-diff")) {
2933+
if (options->word_diff == DIFF_WORDS_NONE)
2934+
options->word_diff = DIFF_WORDS_PLAIN;
2935+
}
2936+
else if (!prefixcmp(arg, "--word-diff=")) {
2937+
const char *type = arg + 12;
2938+
if (!strcmp(type, "plain"))
2939+
options->word_diff = DIFF_WORDS_PLAIN;
2940+
else if (!strcmp(type, "color")) {
2941+
DIFF_OPT_SET(options, COLOR_DIFF);
2942+
options->word_diff = DIFF_WORDS_COLOR;
2943+
}
2944+
else if (!strcmp(type, "porcelain"))
2945+
options->word_diff = DIFF_WORDS_PORCELAIN;
2946+
else if (!strcmp(type, "none"))
2947+
options->word_diff = DIFF_WORDS_NONE;
2948+
else
2949+
die("bad --word-diff argument: %s", type);
2950+
}
2951+
else if (!prefixcmp(arg, "--word-diff-regex=")) {
2952+
if (options->word_diff == DIFF_WORDS_NONE)
2953+
options->word_diff = DIFF_WORDS_PLAIN;
2954+
options->word_regex = arg + 18;
2955+
}
28552956
else if (!strcmp(arg, "--exit-code"))
28562957
DIFF_OPT_SET(options, EXIT_WITH_STATUS);
28572958
else if (!strcmp(arg, "--quiet"))

diff.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
5454
#define DIFF_OPT_FIND_COPIES_HARDER (1 << 6)
5555
#define DIFF_OPT_FOLLOW_RENAMES (1 << 7)
5656
#define DIFF_OPT_COLOR_DIFF (1 << 8)
57-
#define DIFF_OPT_COLOR_DIFF_WORDS (1 << 9)
57+
/* (1 << 9) unused */
5858
#define DIFF_OPT_HAS_CHANGES (1 << 10)
5959
#define DIFF_OPT_QUICK (1 << 11)
6060
#define DIFF_OPT_NO_INDEX (1 << 12)
@@ -79,6 +79,13 @@ typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
7979
#define DIFF_XDL_SET(opts, flag) ((opts)->xdl_opts |= XDF_##flag)
8080
#define DIFF_XDL_CLR(opts, flag) ((opts)->xdl_opts &= ~XDF_##flag)
8181

82+
enum diff_words_type {
83+
DIFF_WORDS_NONE = 0,
84+
DIFF_WORDS_PORCELAIN,
85+
DIFF_WORDS_PLAIN,
86+
DIFF_WORDS_COLOR
87+
};
88+
8289
struct diff_options {
8390
const char *filter;
8491
const char *orderfile;
@@ -108,6 +115,7 @@ struct diff_options {
108115
int stat_width;
109116
int stat_name_width;
110117
const char *word_regex;
118+
enum diff_words_type word_diff;
111119

112120
/* this is set by diffcore for DIFF_FORMAT_PATCH */
113121
int found_changes;

0 commit comments

Comments
 (0)