Skip to content

Commit 1bdd46c

Browse files
committed
Merge branch 'tr/word-diff'
* tr/word-diff: diff: add --word-diff option that generalizes --color-words Conflicts: diff.c
2 parents e22d62d + 882749a commit 1bdd46c

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
@@ -127,11 +127,39 @@ any of those replacements occurred.
127127
gives the default to color output.
128128
Same as `--color=never`.
129129

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

148180
--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
@@ -560,16 +560,68 @@ static void diff_words_append(char *line, unsigned long len,
560560
buffer->text.ptr[buffer->text.size] = '\0';
561561
}
562562

563+
struct diff_words_style_elem
564+
{
565+
const char *prefix;
566+
const char *suffix;
567+
const char *color; /* NULL; filled in by the setup code if
568+
* color is enabled */
569+
};
570+
571+
struct diff_words_style
572+
{
573+
enum diff_words_type type;
574+
struct diff_words_style_elem new, old, ctx;
575+
const char *newline;
576+
};
577+
578+
struct diff_words_style diff_words_styles[] = {
579+
{ DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
580+
{ DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
581+
{ DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
582+
};
583+
563584
struct diff_words_data {
564585
struct diff_words_buffer minus, plus;
565586
const char *current_plus;
566587
FILE *file;
567588
regex_t *word_regex;
589+
enum diff_words_type type;
590+
struct diff_words_style *style;
568591
};
569592

593+
static int fn_out_diff_words_write_helper(FILE *fp,
594+
struct diff_words_style_elem *st_el,
595+
const char *newline,
596+
size_t count, const char *buf)
597+
{
598+
while (count) {
599+
char *p = memchr(buf, '\n', count);
600+
if (p != buf) {
601+
if (st_el->color && fputs(st_el->color, fp) < 0)
602+
return -1;
603+
if (fputs(st_el->prefix, fp) < 0 ||
604+
fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
605+
fputs(st_el->suffix, fp) < 0)
606+
return -1;
607+
if (st_el->color && *st_el->color
608+
&& fputs(GIT_COLOR_RESET, fp) < 0)
609+
return -1;
610+
}
611+
if (!p)
612+
return 0;
613+
if (fputs(newline, fp) < 0)
614+
return -1;
615+
count -= p + 1 - buf;
616+
buf = p + 1;
617+
}
618+
return 0;
619+
}
620+
570621
static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
571622
{
572623
struct diff_words_data *diff_words = priv;
624+
struct diff_words_style *style = diff_words->style;
573625
int minus_first, minus_len, plus_first, plus_len;
574626
const char *minus_begin, *minus_end, *plus_begin, *plus_end;
575627

@@ -593,16 +645,17 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
593645
plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
594646

595647
if (diff_words->current_plus != plus_begin)
596-
fwrite(diff_words->current_plus,
597-
plus_begin - diff_words->current_plus, 1,
598-
diff_words->file);
648+
fn_out_diff_words_write_helper(diff_words->file,
649+
&style->ctx, style->newline,
650+
plus_begin - diff_words->current_plus,
651+
diff_words->current_plus);
599652
if (minus_begin != minus_end)
600-
color_fwrite_lines(diff_words->file,
601-
diff_get_color(1, DIFF_FILE_OLD),
653+
fn_out_diff_words_write_helper(diff_words->file,
654+
&style->old, style->newline,
602655
minus_end - minus_begin, minus_begin);
603656
if (plus_begin != plus_end)
604-
color_fwrite_lines(diff_words->file,
605-
diff_get_color(1, DIFF_FILE_NEW),
657+
fn_out_diff_words_write_helper(diff_words->file,
658+
&style->new, style->newline,
606659
plus_end - plus_begin, plus_begin);
607660

608661
diff_words->current_plus = plus_end;
@@ -684,11 +737,12 @@ static void diff_words_show(struct diff_words_data *diff_words)
684737
xpparam_t xpp;
685738
xdemitconf_t xecfg;
686739
mmfile_t minus, plus;
740+
struct diff_words_style *style = diff_words->style;
687741

688742
/* special case: only removal */
689743
if (!diff_words->plus.text.size) {
690-
color_fwrite_lines(diff_words->file,
691-
diff_get_color(1, DIFF_FILE_OLD),
744+
fn_out_diff_words_write_helper(diff_words->file,
745+
&style->old, style->newline,
692746
diff_words->minus.text.size, diff_words->minus.text.ptr);
693747
diff_words->minus.text.size = 0;
694748
return;
@@ -709,10 +763,10 @@ static void diff_words_show(struct diff_words_data *diff_words)
709763
free(plus.ptr);
710764
if (diff_words->current_plus != diff_words->plus.text.ptr +
711765
diff_words->plus.text.size)
712-
fwrite(diff_words->current_plus,
766+
fn_out_diff_words_write_helper(diff_words->file,
767+
&style->ctx, style->newline,
713768
diff_words->plus.text.ptr + diff_words->plus.text.size
714-
- diff_words->current_plus, 1,
715-
diff_words->file);
769+
- diff_words->current_plus, diff_words->current_plus);
716770
diff_words->minus.text.size = diff_words->plus.text.size = 0;
717771
}
718772

@@ -824,6 +878,9 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
824878

825879
if (len < 1) {
826880
emit_line(ecbdata->file, reset, reset, line, len);
881+
if (ecbdata->diff_words
882+
&& ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN)
883+
fputs("~\n", ecbdata->file);
827884
return;
828885
}
829886

@@ -838,9 +895,13 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
838895
return;
839896
}
840897
diff_words_flush(ecbdata);
841-
line++;
842-
len--;
843-
emit_line(ecbdata->file, plain, reset, line, len);
898+
if (ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN) {
899+
emit_line(ecbdata->file, plain, reset, line, len);
900+
fputs("~\n", ecbdata->file);
901+
} else {
902+
/* don't print the prefix character */
903+
emit_line(ecbdata->file, plain, reset, line+1, len-1);
904+
}
844905
return;
845906
}
846907

@@ -1740,10 +1801,13 @@ static void builtin_diff(const char *name_a,
17401801
xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
17411802
else if (!prefixcmp(diffopts, "-u"))
17421803
xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1743-
if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) {
1804+
if (o->word_diff) {
1805+
int i;
1806+
17441807
ecbdata.diff_words =
17451808
xcalloc(1, sizeof(struct diff_words_data));
17461809
ecbdata.diff_words->file = o->file;
1810+
ecbdata.diff_words->type = o->word_diff;
17471811
if (!o->word_regex)
17481812
o->word_regex = userdiff_word_regex(one);
17491813
if (!o->word_regex)
@@ -1759,10 +1823,23 @@ static void builtin_diff(const char *name_a,
17591823
die ("Invalid regular expression: %s",
17601824
o->word_regex);
17611825
}
1826+
for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
1827+
if (o->word_diff == diff_words_styles[i].type) {
1828+
ecbdata.diff_words->style =
1829+
&diff_words_styles[i];
1830+
break;
1831+
}
1832+
}
1833+
if (DIFF_OPT_TST(o, COLOR_DIFF)) {
1834+
struct diff_words_style *st = ecbdata.diff_words->style;
1835+
st->old.color = diff_get_color_opt(o, DIFF_FILE_OLD);
1836+
st->new.color = diff_get_color_opt(o, DIFF_FILE_NEW);
1837+
st->ctx.color = diff_get_color_opt(o, DIFF_PLAIN);
1838+
}
17621839
}
17631840
xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
17641841
&xpp, &xecfg);
1765-
if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))
1842+
if (o->word_diff)
17661843
free_diff_words_data(&ecbdata);
17671844
if (textconv_one)
17681845
free(mf1.ptr);
@@ -2829,13 +2906,37 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
28292906
DIFF_OPT_CLR(options, COLOR_DIFF);
28302907
else if (!strcmp(arg, "--color-words")) {
28312908
DIFF_OPT_SET(options, COLOR_DIFF);
2832-
DIFF_OPT_SET(options, COLOR_DIFF_WORDS);
2909+
options->word_diff = DIFF_WORDS_COLOR;
28332910
}
28342911
else if (!prefixcmp(arg, "--color-words=")) {
28352912
DIFF_OPT_SET(options, COLOR_DIFF);
2836-
DIFF_OPT_SET(options, COLOR_DIFF_WORDS);
2913+
options->word_diff = DIFF_WORDS_COLOR;
28372914
options->word_regex = arg + 14;
28382915
}
2916+
else if (!strcmp(arg, "--word-diff")) {
2917+
if (options->word_diff == DIFF_WORDS_NONE)
2918+
options->word_diff = DIFF_WORDS_PLAIN;
2919+
}
2920+
else if (!prefixcmp(arg, "--word-diff=")) {
2921+
const char *type = arg + 12;
2922+
if (!strcmp(type, "plain"))
2923+
options->word_diff = DIFF_WORDS_PLAIN;
2924+
else if (!strcmp(type, "color")) {
2925+
DIFF_OPT_SET(options, COLOR_DIFF);
2926+
options->word_diff = DIFF_WORDS_COLOR;
2927+
}
2928+
else if (!strcmp(type, "porcelain"))
2929+
options->word_diff = DIFF_WORDS_PORCELAIN;
2930+
else if (!strcmp(type, "none"))
2931+
options->word_diff = DIFF_WORDS_NONE;
2932+
else
2933+
die("bad --word-diff argument: %s", type);
2934+
}
2935+
else if (!prefixcmp(arg, "--word-diff-regex=")) {
2936+
if (options->word_diff == DIFF_WORDS_NONE)
2937+
options->word_diff = DIFF_WORDS_PLAIN;
2938+
options->word_regex = arg + 18;
2939+
}
28392940
else if (!strcmp(arg, "--exit-code"))
28402941
DIFF_OPT_SET(options, EXIT_WITH_STATUS);
28412942
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)