Skip to content

Commit 9847a52

Browse files
committed
Merge branch 'js/diff-color-words'
* js/diff-color-words: Change the spelling of "wordregex". color-words: Support diff.wordregex config option color-words: make regex configurable via attributes color-words: expand docs with precise semantics color-words: enable REG_NEWLINE to help user color-words: take an optional regular expression describing words color-words: change algorithm to allow for 0-character word boundaries color-words: refactor word splitting and use ALLOC_GROW() Add color_fwrite_lines(), a function coloring each line individually
2 parents d64d483 + ae3b970 commit 9847a52

File tree

10 files changed

+492
-87
lines changed

10 files changed

+492
-87
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,12 @@ diff.suppressBlankEmpty::
639639
A boolean to inhibit the standard behavior of printing a space
640640
before each empty output line. Defaults to false.
641641

642+
diff.wordRegex::
643+
A POSIX Extended Regular Expression used to determine what is a "word"
644+
when performing word-by-word difference calculations. Character
645+
sequences that match the regular expression are "words", all other
646+
characters are *ignorable* whitespace.
647+
642648
fetch.unpackLimit::
643649
If the number of objects fetched over the git native
644650
transfer is below this

Documentation/diff-options.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,22 @@ endif::git-format-patch[]
9494
Turn off colored diff, even when the configuration file
9595
gives the default to color output.
9696

97-
--color-words::
98-
Show colored word diff, i.e. color words which have changed.
97+
--color-words[=<regex>]::
98+
Show colored word diff, i.e., color words which have changed.
99+
By default, words are separated by whitespace.
100+
+
101+
When a <regex> is specified, every non-overlapping match of the
102+
<regex> is considered a word. Anything between these matches is
103+
considered whitespace and ignored(!) for the purposes of finding
104+
differences. You may want to append `|[^[:space:]]` to your regular
105+
expression to make sure that it matches all non-whitespace characters.
106+
A match that contains a newline is silently truncated(!) at the
107+
newline.
108+
+
109+
The regex can also be set via a diff driver or configuration option, see
110+
linkgit:gitattributes[1] or linkgit:git-config[1]. Giving it explicitly
111+
overrides any diff driver or configuration setting. Diff drivers
112+
override configuration settings.
99113

100114
--no-renames::
101115
Turn off rename detection, even when the configuration

Documentation/gitattributes.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ patterns are available:
317317

318318
- `bibtex` suitable for files with BibTeX coded references.
319319

320+
- `cpp` suitable for source code in the C and C++ languages.
321+
320322
- `html` suitable for HTML/XHTML documents.
321323

322324
- `java` suitable for source code in the Java language.
@@ -334,6 +336,25 @@ patterns are available:
334336
- `tex` suitable for source code for LaTeX documents.
335337

336338

339+
Customizing word diff
340+
^^^^^^^^^^^^^^^^^^^^^
341+
342+
You can customize the rules that `git diff --color-words` uses to
343+
split words in a line, by specifying an appropriate regular expression
344+
in the "diff.*.wordRegex" configuration variable. For example, in TeX
345+
a backslash followed by a sequence of letters forms a command, but
346+
several such commands can be run together without intervening
347+
whitespace. To separate them, use a regular expression such as
348+
349+
------------------------
350+
[diff "tex"]
351+
wordRegex = "\\\\[a-zA-Z]+|[{}]|\\\\.|[^\\{}[:space:]]+"
352+
------------------------
353+
354+
A built-in pattern is provided for all languages listed in the
355+
previous section.
356+
357+
337358
Performing text diffs of binary files
338359
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
339360

color.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,31 @@ int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
202202
va_end(args);
203203
return r;
204204
}
205+
206+
/*
207+
* This function splits the buffer by newlines and colors the lines individually.
208+
*
209+
* Returns 0 on success.
210+
*/
211+
int color_fwrite_lines(FILE *fp, const char *color,
212+
size_t count, const char *buf)
213+
{
214+
if (!*color)
215+
return fwrite(buf, count, 1, fp) != 1;
216+
while (count) {
217+
char *p = memchr(buf, '\n', count);
218+
if (p != buf && (fputs(color, fp) < 0 ||
219+
fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
220+
fputs(COLOR_RESET, fp) < 0))
221+
return -1;
222+
if (!p)
223+
return 0;
224+
if (fputc('\n', fp) < 0)
225+
return -1;
226+
count -= p + 1 - buf;
227+
buf = p + 1;
228+
}
229+
return 0;
230+
}
231+
232+

color.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ void color_parse(const char *value, const char *var, char *dst);
2020
void color_parse_mem(const char *value, int len, const char *var, char *dst);
2121
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
2222
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
23+
int color_fwrite_lines(FILE *fp, const char *color, size_t count, const char *buf);
2324

2425
#endif /* COLOR_H */

0 commit comments

Comments
 (0)