Skip to content

Commit d173e79

Browse files
stefanbellergitster
authored andcommitted
diff: align move detection error handling with other options
This changes the error handling for the options --color-moved-ws and --color-moved-ws to be like the rest of the options. Move the die() call out of parse_color_moved_ws into the parsing of command line options. As the function returns a bit field, change its signature to return an unsigned instead of an int; add a new bit to signal errors. Once the error is signaled, we discard the other bits, such that it doesn't matter if the error bit overlaps with any other bit. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cd69ec8 commit d173e79

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

diff.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static int parse_color_moved(const char *arg)
291291
return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
292292
}
293293

294-
static int parse_color_moved_ws(const char *arg)
294+
static unsigned parse_color_moved_ws(const char *arg)
295295
{
296296
int ret = 0;
297297
struct string_list l = STRING_LIST_INIT_DUP;
@@ -312,15 +312,19 @@ static int parse_color_moved_ws(const char *arg)
312312
ret |= XDF_IGNORE_WHITESPACE;
313313
else if (!strcmp(sb.buf, "allow-indentation-change"))
314314
ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE;
315-
else
316-
error(_("ignoring unknown color-moved-ws mode '%s'"), sb.buf);
315+
else {
316+
ret |= COLOR_MOVED_WS_ERROR;
317+
error(_("unknown color-moved-ws mode '%s', possible values are 'ignore-space-change', 'ignore-space-at-eol', 'ignore-all-space', 'allow-indentation-change'"), sb.buf);
318+
}
317319

318320
strbuf_release(&sb);
319321
}
320322

321323
if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) &&
322-
(ret & XDF_WHITESPACE_FLAGS))
323-
die(_("color-moved-ws: allow-indentation-change cannot be combined with other white space modes"));
324+
(ret & XDF_WHITESPACE_FLAGS)) {
325+
error(_("color-moved-ws: allow-indentation-change cannot be combined with other white space modes"));
326+
ret |= COLOR_MOVED_WS_ERROR;
327+
}
324328

325329
string_list_clear(&l, 0);
326330

@@ -341,8 +345,8 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
341345
return 0;
342346
}
343347
if (!strcmp(var, "diff.colormovedws")) {
344-
int cm = parse_color_moved_ws(value);
345-
if (cm < 0)
348+
unsigned cm = parse_color_moved_ws(value);
349+
if (cm & COLOR_MOVED_WS_ERROR)
346350
return -1;
347351
diff_color_moved_ws_default = cm;
348352
return 0;
@@ -5032,10 +5036,13 @@ int diff_opt_parse(struct diff_options *options,
50325036
else if (skip_prefix(arg, "--color-moved=", &arg)) {
50335037
int cm = parse_color_moved(arg);
50345038
if (cm < 0)
5035-
die("bad --color-moved argument: %s", arg);
5039+
return error("bad --color-moved argument: %s", arg);
50365040
options->color_moved = cm;
50375041
} else if (skip_prefix(arg, "--color-moved-ws=", &arg)) {
5038-
options->color_moved_ws_handling = parse_color_moved_ws(arg);
5042+
unsigned cm = parse_color_moved_ws(arg);
5043+
if (cm & COLOR_MOVED_WS_ERROR)
5044+
return -1;
5045+
options->color_moved_ws_handling = cm;
50395046
} else if (skip_to_optional_arg_default(arg, "--color-words", &options->word_regex, NULL)) {
50405047
options->use_color = 1;
50415048
options->word_diff = DIFF_WORDS_COLOR;

diff.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ struct diff_options {
225225

226226
/* XDF_WHITESPACE_FLAGS regarding block detection are set at 2, 3, 4 */
227227
#define COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE (1<<5)
228-
int color_moved_ws_handling;
228+
#define COLOR_MOVED_WS_ERROR (1<<0)
229+
unsigned color_moved_ws_handling;
229230

230231
struct repository *repo;
231232
};

t/t4015-diff-whitespace.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,24 @@ test_expect_success 'compare whitespace delta across moved blocks' '
18901890
test_cmp expected actual
18911891
'
18921892

1893+
test_expect_success 'bogus settings in move detection erroring out' '
1894+
test_must_fail git diff --color-moved=bogus 2>err &&
1895+
test_i18ngrep "must be one of" err &&
1896+
test_i18ngrep bogus err &&
1897+
1898+
test_must_fail git -c diff.colormoved=bogus diff 2>err &&
1899+
test_i18ngrep "must be one of" err &&
1900+
test_i18ngrep "from command-line config" err &&
1901+
1902+
test_must_fail git diff --color-moved-ws=bogus 2>err &&
1903+
test_i18ngrep "possible values" err &&
1904+
test_i18ngrep bogus err &&
1905+
1906+
test_must_fail git -c diff.colormovedws=bogus diff 2>err &&
1907+
test_i18ngrep "possible values" err &&
1908+
test_i18ngrep "from command-line config" err
1909+
'
1910+
18931911
test_expect_success 'compare whitespace delta incompatible with other space options' '
18941912
test_must_fail git diff \
18951913
--color-moved-ws=allow-indentation-change,ignore-all-space \

0 commit comments

Comments
 (0)