Skip to content

Commit e5272d3

Browse files
committed
Merge branch 'jc/ws-error-highlight'
"git diff/log --ws-error-highlight=<kind>" lacked the corresponding configuration variable to set it by default. * jc/ws-error-highlight: diff: introduce diff.wsErrorHighlight option diff.c: move ws-error-highlight parsing helpers up diff.c: refactor parse_ws_error_highlight() t4015: split out the "setup" part of ws-error-highlight test
2 parents c334eff + a17505f commit e5272d3

File tree

4 files changed

+119
-52
lines changed

4 files changed

+119
-52
lines changed

Documentation/diff-config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,9 @@ diff.algorithm::
193193
low-occurrence common elements".
194194
--
195195
+
196+
197+
diff.wsErrorHighlight::
198+
A comma separated list of `old`, `new`, `context`, that
199+
specifies how whitespace errors on lines are highlighted
200+
with `color.diff.whitespace`. Can be overridden by the
201+
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
@@ -308,6 +308,8 @@ ifndef::git-format-patch[]
308308
lines are highlighted. E.g. `--ws-error-highlight=new,old`
309309
highlights whitespace errors on both deleted and added lines.
310310
`all` can be used as a short-hand for `old,new,context`.
311+
The `diff.wsErrorHighlight` configuration variable can be
312+
used to specify the default behaviour.
311313

312314
endif::git-format-patch[]
313315

diff.c

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static int diff_stat_graph_width;
4343
static int diff_dirstat_permille_default = 30;
4444
static struct diff_options default_diff_options;
4545
static long diff_algorithm;
46+
static unsigned ws_error_highlight_default = WSEH_NEW;
4647

4748
static char diff_colors[][COLOR_MAXLEN] = {
4849
GIT_COLOR_RESET,
@@ -172,6 +173,43 @@ long parse_algorithm_value(const char *value)
172173
return -1;
173174
}
174175

176+
static int parse_one_token(const char **arg, const char *token)
177+
{
178+
const char *rest;
179+
if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
180+
*arg = rest;
181+
return 1;
182+
}
183+
return 0;
184+
}
185+
186+
static int parse_ws_error_highlight(const char *arg)
187+
{
188+
const char *orig_arg = arg;
189+
unsigned val = 0;
190+
191+
while (*arg) {
192+
if (parse_one_token(&arg, "none"))
193+
val = 0;
194+
else if (parse_one_token(&arg, "default"))
195+
val = WSEH_NEW;
196+
else if (parse_one_token(&arg, "all"))
197+
val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
198+
else if (parse_one_token(&arg, "new"))
199+
val |= WSEH_NEW;
200+
else if (parse_one_token(&arg, "old"))
201+
val |= WSEH_OLD;
202+
else if (parse_one_token(&arg, "context"))
203+
val |= WSEH_CONTEXT;
204+
else {
205+
return -1 - (int)(arg - orig_arg);
206+
}
207+
if (*arg)
208+
arg++;
209+
}
210+
return val;
211+
}
212+
175213
/*
176214
* These are to give UI layer defaults.
177215
* The core-level commands such as git-diff-files should
@@ -256,6 +294,15 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
256294

257295
if (git_diff_heuristic_config(var, value, cb) < 0)
258296
return -1;
297+
298+
if (!strcmp(var, "diff.wserrorhighlight")) {
299+
int val = parse_ws_error_highlight(value);
300+
if (val < 0)
301+
return -1;
302+
ws_error_highlight_default = val;
303+
return 0;
304+
}
305+
259306
if (git_color_config(var, value, cb) < 0)
260307
return -1;
261308

@@ -3307,7 +3354,7 @@ void diff_setup(struct diff_options *options)
33073354
options->rename_limit = -1;
33083355
options->dirstat_permille = diff_dirstat_permille_default;
33093356
options->context = diff_context_default;
3310-
options->ws_error_highlight = WSEH_NEW;
3357+
options->ws_error_highlight = ws_error_highlight_default;
33113358
DIFF_OPT_SET(options, RENAME_EMPTY);
33123359

33133360
/* pathchange left =NULL by default */
@@ -3698,40 +3745,14 @@ static void enable_patch_output(int *fmt) {
36983745
*fmt |= DIFF_FORMAT_PATCH;
36993746
}
37003747

3701-
static int parse_one_token(const char **arg, const char *token)
3748+
static int parse_ws_error_highlight_opt(struct diff_options *opt, const char *arg)
37023749
{
3703-
const char *rest;
3704-
if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
3705-
*arg = rest;
3706-
return 1;
3707-
}
3708-
return 0;
3709-
}
3750+
int val = parse_ws_error_highlight(arg);
37103751

3711-
static int parse_ws_error_highlight(struct diff_options *opt, const char *arg)
3712-
{
3713-
const char *orig_arg = arg;
3714-
unsigned val = 0;
3715-
while (*arg) {
3716-
if (parse_one_token(&arg, "none"))
3717-
val = 0;
3718-
else if (parse_one_token(&arg, "default"))
3719-
val = WSEH_NEW;
3720-
else if (parse_one_token(&arg, "all"))
3721-
val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
3722-
else if (parse_one_token(&arg, "new"))
3723-
val |= WSEH_NEW;
3724-
else if (parse_one_token(&arg, "old"))
3725-
val |= WSEH_OLD;
3726-
else if (parse_one_token(&arg, "context"))
3727-
val |= WSEH_CONTEXT;
3728-
else {
3729-
error("unknown value after ws-error-highlight=%.*s",
3730-
(int)(arg - orig_arg), orig_arg);
3731-
return 0;
3732-
}
3733-
if (*arg)
3734-
arg++;
3752+
if (val < 0) {
3753+
error("unknown value after ws-error-highlight=%.*s",
3754+
-1 - val, arg);
3755+
return 0;
37353756
}
37363757
opt->ws_error_highlight = val;
37373758
return 1;
@@ -3950,7 +3971,7 @@ int diff_opt_parse(struct diff_options *options,
39503971
else if (skip_prefix(arg, "--submodule=", &arg))
39513972
return parse_submodule_opt(options, arg);
39523973
else if (skip_prefix(arg, "--ws-error-highlight=", &arg))
3953-
return parse_ws_error_highlight(options, arg);
3974+
return parse_ws_error_highlight_opt(options, arg);
39543975

39553976
/* misc options */
39563977
else if (!strcmp(arg, "-z"))

t/t4015-diff-whitespace.sh

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,8 @@ test_expect_success 'diff that introduces and removes ws breakages' '
869869
test_cmp expected current
870870
'
871871

872-
test_expect_success 'the same with --ws-error-highlight' '
872+
test_expect_success 'ws-error-highlight test setup' '
873+
873874
git reset --hard &&
874875
{
875876
echo "0. blank-at-eol " &&
@@ -882,10 +883,7 @@ test_expect_success 'the same with --ws-error-highlight' '
882883
echo "2. and a new line "
883884
} >x &&
884885
885-
git -c color.diff=always diff --ws-error-highlight=default,old |
886-
test_decode_color >current &&
887-
888-
cat >expected <<-\EOF &&
886+
cat >expect.default-old <<-\EOF &&
889887
<BOLD>diff --git a/x b/x<RESET>
890888
<BOLD>index d0233a2..700886e 100644<RESET>
891889
<BOLD>--- a/x<RESET>
@@ -897,12 +895,7 @@ test_expect_success 'the same with --ws-error-highlight' '
897895
<GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
898896
EOF
899897
900-
test_cmp expected current &&
901-
902-
git -c color.diff=always diff --ws-error-highlight=all |
903-
test_decode_color >current &&
904-
905-
cat >expected <<-\EOF &&
898+
cat >expect.all <<-\EOF &&
906899
<BOLD>diff --git a/x b/x<RESET>
907900
<BOLD>index d0233a2..700886e 100644<RESET>
908901
<BOLD>--- a/x<RESET>
@@ -914,12 +907,7 @@ test_expect_success 'the same with --ws-error-highlight' '
914907
<GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
915908
EOF
916909
917-
test_cmp expected current &&
918-
919-
git -c color.diff=always diff --ws-error-highlight=none |
920-
test_decode_color >current &&
921-
922-
cat >expected <<-\EOF &&
910+
cat >expect.none <<-\EOF
923911
<BOLD>diff --git a/x b/x<RESET>
924912
<BOLD>index d0233a2..700886e 100644<RESET>
925913
<BOLD>--- a/x<RESET>
@@ -931,7 +919,57 @@ test_expect_success 'the same with --ws-error-highlight' '
931919
<GREEN>+2. and a new line <RESET>
932920
EOF
933921
934-
test_cmp expected current
922+
'
923+
924+
test_expect_success 'test --ws-error-highlight option' '
925+
926+
git -c color.diff=always diff --ws-error-highlight=default,old |
927+
test_decode_color >current &&
928+
test_cmp expect.default-old current &&
929+
930+
git -c color.diff=always diff --ws-error-highlight=all |
931+
test_decode_color >current &&
932+
test_cmp expect.all current &&
933+
934+
git -c color.diff=always diff --ws-error-highlight=none |
935+
test_decode_color >current &&
936+
test_cmp expect.none current
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+
935973
'
936974

937975
test_done

0 commit comments

Comments
 (0)