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>

Junio C Hamano committed Oct 4, 2016 at 15:26 UTC a17505f262b62e429bb0e188c5ed73ac749e25b8
4 files changed +53 -1
Documentation/diff-config.txt
+6
@@ -182,3 +182,9 @@ diff.algorithm::
182 low-occurrence common elements".
183 --
184 +
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
+2
@@ -303,6 +303,8 @@ ifndef::git-format-patch[]
303 lines are highlighted. E.g. `--ws-error-highlight=new,old`
304 highlights whitespace errors on both deleted and added lines.
305 `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.
308
309 endif::git-format-patch[]
310
diff.c
+10 -1
@@ -41,6 +41,7 @@ static int diff_stat_graph_width;
41 static int diff_dirstat_permille_default = 30;
42 static struct diff_options default_diff_options;
43 static long diff_algorithm;
44 +static unsigned ws_error_highlight_default = WSEH_NEW;
45
46 static char diff_colors[][COLOR_MAXLEN] = {
47 GIT_COLOR_RESET,
@@ -262,6 +263,14 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
263 return 0;
264 }
265
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 +
274 if (git_color_config(var, value, cb) < 0)
275 return -1;
276
@@ -3306,7 +3315,7 @@ void diff_setup(struct diff_options *options)
3315 options->rename_limit = -1;
3316 options->dirstat_permille = diff_dirstat_permille_default;
3317 options->context = diff_context_default;
3309 - options->ws_error_highlight = WSEH_NEW;
3318 + options->ws_error_highlight = ws_error_highlight_default;
3319 DIFF_OPT_SET(options, RENAME_EMPTY);
3320
3321 /* pathchange left =NULL by default */
t/t4015-diff-whitespace.sh
+35
@@ -937,4 +937,39 @@ test_expect_success 'test --ws-error-highlight option' '
937
938 '
939
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 +
975 test_done