Skip to content

Commit a17505f

Browse files
committed
diff: introduce diff.wsErrorHighlight option
With the preparatory steps, it has become trivial to teach the system a new diff.wsErrorHighlight configuration that gives the default value for --ws-error-highlight command line option. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 0b4b42e commit a17505f

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

Documentation/diff-config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,9 @@ diff.algorithm::
182182
low-occurrence common elements".
183183
--
184184
+
185+
186+
diff.wsErrorHighlight::
187+
A comma separated list of `old`, `new`, `context`, that
188+
specifies how whitespace errors on lines are highlighted
189+
with `color.diff.whitespace`. Can be overridden by the
190+
command line option `--ws-error-highlight=<kind>`

Documentation/diff-options.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ ifndef::git-format-patch[]
303303
lines are highlighted. E.g. `--ws-error-highlight=new,old`
304304
highlights whitespace errors on both deleted and added lines.
305305
`all` can be used as a short-hand for `old,new,context`.
306+
The `diff.wsErrorHighlight` configuration variable can be
307+
used to specify the default behaviour.
306308

307309
endif::git-format-patch[]
308310

diff.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static int diff_stat_graph_width;
4141
static int diff_dirstat_permille_default = 30;
4242
static struct diff_options default_diff_options;
4343
static long diff_algorithm;
44+
static unsigned ws_error_highlight_default = WSEH_NEW;
4445

4546
static char diff_colors[][COLOR_MAXLEN] = {
4647
GIT_COLOR_RESET,
@@ -262,6 +263,14 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
262263
return 0;
263264
}
264265

266+
if (!strcmp(var, "diff.wserrorhighlight")) {
267+
int val = parse_ws_error_highlight(value);
268+
if (val < 0)
269+
return -1;
270+
ws_error_highlight_default = val;
271+
return 0;
272+
}
273+
265274
if (git_color_config(var, value, cb) < 0)
266275
return -1;
267276

@@ -3306,7 +3315,7 @@ void diff_setup(struct diff_options *options)
33063315
options->rename_limit = -1;
33073316
options->dirstat_permille = diff_dirstat_permille_default;
33083317
options->context = diff_context_default;
3309-
options->ws_error_highlight = WSEH_NEW;
3318+
options->ws_error_highlight = ws_error_highlight_default;
33103319
DIFF_OPT_SET(options, RENAME_EMPTY);
33113320

33123321
/* pathchange left =NULL by default */

t/t4015-diff-whitespace.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,4 +937,39 @@ test_expect_success 'test --ws-error-highlight option' '
937937
938938
'
939939

940+
test_expect_success 'test diff.wsErrorHighlight config' '
941+
942+
git -c color.diff=always -c diff.wsErrorHighlight=default,old diff |
943+
test_decode_color >current &&
944+
test_cmp expect.default-old current &&
945+
946+
git -c color.diff=always -c diff.wsErrorHighlight=all diff |
947+
test_decode_color >current &&
948+
test_cmp expect.all current &&
949+
950+
git -c color.diff=always -c diff.wsErrorHighlight=none diff |
951+
test_decode_color >current &&
952+
test_cmp expect.none current
953+
954+
'
955+
956+
test_expect_success 'option overrides diff.wsErrorHighlight' '
957+
958+
git -c color.diff=always -c diff.wsErrorHighlight=none \
959+
diff --ws-error-highlight=default,old |
960+
test_decode_color >current &&
961+
test_cmp expect.default-old current &&
962+
963+
git -c color.diff=always -c diff.wsErrorHighlight=default \
964+
diff --ws-error-highlight=all |
965+
test_decode_color >current &&
966+
test_cmp expect.all current &&
967+
968+
git -c color.diff=always -c diff.wsErrorHighlight=all \
969+
diff --ws-error-highlight=none |
970+
test_decode_color >current &&
971+
test_cmp expect.none current
972+
973+
'
974+
940975
test_done

0 commit comments

Comments
 (0)