diff: differentiate error handling in parse_color_moved_ws
PATCH 1/1 diff: differentiate error handling in parse_color_moved_ws
As we check command line options more strictly and allow configuration variables to be parsed more leniently, we need take different actions based on whether an unknown value is given on the command line or in the config.
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.
---
This is a fresh attempt to cleanup the sloppy part that was mentioned in https://public-inbox.org/git/[email protected]/
Another thing to follow up is to have color-moved-ws imply color-moved.
Thanks,
Stefan
diff.c | 21 ++++++++++++++------- diff.h | 3 ++- 2 files changed, 16 insertions(+), 8 deletions(-)
diff.c +14 -7
@@ -291,7 +291,7 @@ static int parse_color_moved(const char *arg)return error (_ ("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'" )); } -static int parse_color_moved_ws (const char *arg) +static unsigned parse_color_moved_ws (const char *arg) {int ret =0 ;struct string_list l =STRING_LIST_INIT_DUP ;
@@ -312,15 +312,19 @@ static int parse_color_moved_ws(const char *arg) ret |=XDF_IGNORE_WHITESPACE ; else if (!strcmp(sb.buf, "allow-indentation-change")) ret |=COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE ; - else + else { + ret |=COLOR_MOVED_WS_ERROR ;error (_ ("ignoring unknown color-moved-ws mode '%s'" ), sb.buf); + }strbuf_release (&sb); }if ((ret &COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE ) && - (ret &XDF_WHITESPACE_FLAGS )) -die (_ ("color-moved-ws: allow-indentation-change cannot be combined with other white space modes" )); + (ret &XDF_WHITESPACE_FLAGS )) { +error (_ ("color-moved-ws: allow-indentation-change cannot be combined with other white space modes" )); + ret |=COLOR_MOVED_WS_ERROR ; + }string_list_clear (&l,0 );
@@ -341,8 +345,8 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)return 0 ; }if (!strcmp (var,"diff.colormovedws" )) { -int cm =parse_color_moved_ws (value); -if (cm <0 ) +unsigned cm =parse_color_moved_ws (value); +if (cm &COLOR_MOVED_WS_ERROR )return -1 ; diff_color_moved_ws_default = cm;return 0 ;
@@ -5035,7 +5039,10 @@ int diff_opt_parse(struct diff_options *options,die ("bad --color-moved argument: %s" , arg); options->color_moved = cm; }else if (skip_prefix (arg,"--color-moved-ws=" , &arg)) { - options->color_moved_ws_handling =parse_color_moved_ws (arg); +unsigned cm =parse_color_moved_ws (arg); +if (cm &COLOR_MOVED_WS_ERROR ) +die ("bad --color-moved-ws argument: %s" , arg); + options->color_moved_ws_handling = cm; }else if (skip_to_optional_arg_default (arg,"--color-words" , &options->word_regex,NULL )) { options->use_color =1 ; options->word_diff =DIFF_WORDS_COLOR ;
diff.h +2 -1
@@ -225,7 +225,8 @@ struct diff_options {/* XDF_WHITESPACE_FLAGS regarding block detection are set at 2, 3, 4 */ #define COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE (1<<5) -int color_moved_ws_handling; +#define COLOR_MOVED_WS_ERROR (1<<0) +unsigned color_moved_ws_handling;struct repository *repo; };
Signature · 1 line
2.19.1.930.g4563a0d9d0-goog
Quoted 12 lines · Stefan Beller
diff.c +2
-2-static int parse_color_moved_ws (const char *arg) +static unsigned parse_color_moved_ws (const char *arg) {int ret =0 ;struct string_list l =STRING_LIST_INIT_DUP ;@@ -312,15 +312,19 @@ static int parse_color_moved_ws(const char *arg) ret |=XDF_IGNORE_WHITESPACE ; else if (!strcmp(sb.buf, "allow-indentation-change")) ret |=COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE ; - else + else {
diff.c static int parse_color_moved_ws(const char *arg)
Quoted 10 lines
diff.c +6
-1+ ret |=COLOR_MOVED_WS_ERROR ;error (_ ("ignoring unknown color-moved-ws mode '%s'" ), sb.buf); + } }else if (skip_prefix (arg,"--color-moved-ws=" , &arg)) { - options->color_moved_ws_handling =parse_color_moved_ws (arg); +unsigned cm =parse_color_moved_ws (arg); +if (cm &COLOR_MOVED_WS_ERROR ) +die ("bad --color-moved-ws argument: %s" , arg); + options->color_moved_ws_handling = cm;
Excellent.
Will queue. Perhaps a test or two can follow to ensure a bad value from config does not kill while a command line does?
Thanks.
Quoted 27 lines · Junio C Hamano
Quoted 12 lines · Stefan Beller
diff.c +2
-2-static int parse_color_moved_ws (const char *arg) +static unsigned parse_color_moved_ws (const char *arg) {int ret =0 ;struct string_list l =STRING_LIST_INIT_DUP ;@@ -312,15 +312,19 @@ static int parse_color_moved_ws(const char *arg) ret |=XDF_IGNORE_WHITESPACE ; else if (!strcmp(sb.buf, "allow-indentation-change")) ret |=COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE ; - else + else {diff.c static int parse_color_moved_ws(const char *arg)
Quoted 10 lines
diff.c +6
-1+ ret |=COLOR_MOVED_WS_ERROR ;error (_ ("ignoring unknown color-moved-ws mode '%s'" ), sb.buf); + } }else if (skip_prefix (arg,"--color-moved-ws=" , &arg)) { - options->color_moved_ws_handling =parse_color_moved_ws (arg); +unsigned cm =parse_color_moved_ws (arg); +if (cm &COLOR_MOVED_WS_ERROR ) +die ("bad --color-moved-ws argument: %s" , arg); + options->color_moved_ws_handling = cm;Excellent.
Quoted 2 lines
Will queue. Perhaps a test or two can follow to ensure a bad value from config does not kill while a command line does?
Wait. This does not fix
git -c diff.colormovedws=nonsense diff
that dies with an error message---it should ignore the config and at moat issue a warning.
The command line handling of
git diff --color-moved-ws=nonsense
does correctly die, but it first says "error: ignoring" before saying "fatal: bad argument", which is suboptimal.
So, not so excellent (yet) X-<.
Quoted 33 lines · Junio C Hamano
Quoted 27 lines · Junio C Hamano
Quoted 12 lines · Stefan Beller
diff.c +2
-2-static int parse_color_moved_ws (const char *arg) +static unsigned parse_color_moved_ws (const char *arg) {int ret =0 ;struct string_list l =STRING_LIST_INIT_DUP ;@@ -312,15 +312,19 @@ static int parse_color_moved_ws(const char *arg) ret |=XDF_IGNORE_WHITESPACE ; else if (!strcmp(sb.buf, "allow-indentation-change")) ret |=COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE ; - else + else {diff.c static int parse_color_moved_ws(const char *arg)
Quoted 10 lines
diff.c +6
-1+ ret |=COLOR_MOVED_WS_ERROR ;error (_ ("ignoring unknown color-moved-ws mode '%s'" ), sb.buf); + } }else if (skip_prefix (arg,"--color-moved-ws=" , &arg)) { - options->color_moved_ws_handling =parse_color_moved_ws (arg); +unsigned cm =parse_color_moved_ws (arg); +if (cm &COLOR_MOVED_WS_ERROR ) +die ("bad --color-moved-ws argument: %s" , arg); + options->color_moved_ws_handling = cm;Excellent.
Quoted 2 lines
Will queue. Perhaps a test or two can follow to ensure a bad value from config does not kill while a command line does?
Quoted 6 lines
Wait. This does not fix
git -c diff.colormovedws=nonsense diff
that dies with an error message---it should ignore the config and at moat issue a warning.
$ git -c core.abbrev=41 diff error: abbrev length out of range: 41 fatal: unable to parse 'core.abbrev' from command-line config $ ./git -c diff.colormovedws=nonsense diff HEAD error: ignoring unknown color-moved-ws mode 'nonsense' fatal: unable to parse 'diff.colormovedws' from command-line config
Ah, I see the issue there. We actually have to return 'success' to the config machinery after the warning claiming ignoring the setting or we'd have to reword the warning to state we're not ignoring the bogus setting.
Quoted 6 lines
The command line handling of
git diff --color-moved-ws=nonsensedoes correctly die, but it first says "error: ignoring" before saying "fatal: bad argument", which is suboptimal.
So to find the analogous here, maybe:
$ git diff --color=bogus error: option `color' expects "always", "auto", or "never"
Quoted 1 line
So, not so excellent (yet) X-<.
So to reach excellence, we'd want to reword the warning message and a test.
Thanks,
Stefan
PATCH 1/1 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.
---
c.f.
./git -c diff.colormovedws=bogus diff HEAD
error: unknown color-moved-ws mode 'bogus'
fatal: unable to parse 'diff.colormovedws' from command-line config ./git -c core.abbrev=41 diff
error: abbrev length out of range: 41
fatal: unable to parse 'core.abbrev' from command-line config
./git diff --color=bogus
error: option `color' expects "always", "auto", or "never" ./git -c diff.colormovedws=bogus diff HEAD
error: unknown color-moved-ws mode 'bogus'
fatal: unable to parse 'diff.colormovedws' from command-line config
diff.c | 25 ++++++++++++++++--------- diff.h | 3 ++- t/t4015-diff-whitespace.sh | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-)
diff.c +16 -9
@@ -291,7 +291,7 @@ static int parse_color_moved(const char *arg)return error (_ ("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'" )); } -static int parse_color_moved_ws (const char *arg) +static unsigned parse_color_moved_ws (const char *arg) {int ret =0 ;struct string_list l =STRING_LIST_INIT_DUP ;
@@ -312,15 +312,19 @@ static int parse_color_moved_ws(const char *arg) ret |=XDF_IGNORE_WHITESPACE ; else if (!strcmp(sb.buf, "allow-indentation-change")) ret |=COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE ; - else - error(_("ignoring unknown color-moved-ws mode '%s'"), sb.buf); + else { + ret |=COLOR_MOVED_WS_ERROR ; +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); + }strbuf_release (&sb); }if ((ret &COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE ) && - (ret &XDF_WHITESPACE_FLAGS )) -die (_ ("color-moved-ws: allow-indentation-change cannot be combined with other white space modes" )); + (ret &XDF_WHITESPACE_FLAGS )) { +error (_ ("color-moved-ws: allow-indentation-change cannot be combined with other white space modes" )); + ret |=COLOR_MOVED_WS_ERROR ; + }string_list_clear (&l,0 );
@@ -341,8 +345,8 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)return 0 ; }if (!strcmp (var,"diff.colormovedws" )) { -int cm =parse_color_moved_ws (value); -if (cm <0 ) +unsigned cm =parse_color_moved_ws (value); +if (cm &COLOR_MOVED_WS_ERROR )return -1 ; diff_color_moved_ws_default = cm;return 0 ;
@@ -5032,10 +5036,13 @@ int diff_opt_parse(struct diff_options *options,else if (skip_prefix (arg,"--color-moved=" , &arg)) {int cm =parse_color_moved (arg);if (cm <0 ) -die ("bad --color-moved argument: %s" , arg); +return error ("bad --color-moved argument: %s" , arg); options->color_moved = cm; }else if (skip_prefix (arg,"--color-moved-ws=" , &arg)) { - options->color_moved_ws_handling =parse_color_moved_ws (arg); +unsigned cm =parse_color_moved_ws (arg); +if (cm &COLOR_MOVED_WS_ERROR ) +return -1 ; + options->color_moved_ws_handling = cm; }else if (skip_to_optional_arg_default (arg,"--color-words" , &options->word_regex,NULL )) { options->use_color =1 ; options->word_diff =DIFF_WORDS_COLOR ;
diff.h +2 -1
@@ -225,7 +225,8 @@ struct diff_options {/* XDF_WHITESPACE_FLAGS regarding block detection are set at 2, 3, 4 */ #define COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE (1<<5) -int color_moved_ws_handling; +#define COLOR_MOVED_WS_ERROR (1<<0) +unsigned color_moved_ws_handling;struct repository *repo; };
t/t4015-diff-whitespace.sh +18 -0
@@ -1890,6 +1890,24 @@ test_expect_success 'compare whitespace delta across moved blocks' ' test_cmp expected actual ' +test_expect_success 'bogus settings in move detection erroring out' ' + test_must_fail git diff --color-moved=bogus 2>err && + test_i18ngrep "must be one of" err && + test_i18ngrep bogus err && + + test_must_fail git -c diff.colormoved=bogus diff 2>err && + test_i18ngrep "must be one of" err && + test_i18ngrep "from command-line config" err && + + test_must_fail git diff --color-moved-ws=bogus 2>err && + test_i18ngrep "possible values" err && + test_i18ngrep bogus err && + + test_must_fail git -c diff.colormovedws=bogus diff 2>err && + test_i18ngrep "possible values" err && + test_i18ngrep "from command-line config" err +' + test_expect_success 'compare whitespace delta incompatible with other space options' ' test_must_fail git diff \ --color-moved-ws=allow-indentation-change,ignore-all-space \
Signature · 1 line
2.19.1.1215.g8438c0b245-goog
Re: [PATCH] diff: align move detection error handling with other options
Quoted 1 line · Stefan Beller
Subject: Re: [PATCH] diff: align move detection error handling with other options
When sending an updated version of existing topic, please make sure you indicate as such with v$n etc. I will assume that this is to replace the patch queued on sb/diff-color-moved-config-option-fixup topic. Please do not assume that all messages on the References:
header are visible in the readers' MUA to show which thread it is a response to. At least a hint like v$n (or mentioning the name of the topic branch if the previous round is already in 'pu') would make the reader realize that the References: header can be used if the reader wants to find in what context the patch is relevant, especially when redoing a change whose previous round is more than a week old, as we see too many changes in a day already.
Quoted 9 lines
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 ofparse_color_moved_wsinto 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.
OK. That sound better than the original.
Quoted 8 lines
Signed-off-by Stefan Beller <[email protected]>---
c.f.
./git -c diff.colormovedws=bogus diff HEAD
error: unknown color-moved-ws mode 'bogus'
fatal: unable to parse 'diff.colormovedws' from command-line config
These double messages may be something we want to fix eventually, but I think that the issue is not specific to the config callback of diff API, but something the config API needs to support to allow its users produce a better single message (the first part is merely giving a more detailed explanation why Git was unable to parse, and should ideally be folded into the second part).
More generally, even if git_diff_ui_config() was called, as long as we do not do the "--color-moved" and "--color-moved-ws" operations, the user shouldn't even get an "unknown mode" message, let alone "fatal". The above (and other existing uses of "return -1"s in the same config callback) should actually become an example of what not to do, as "diff HEAD" does not *care* what value that variable has.
But again, fixing that anti-pattern is a much larger change. Once we move diff.c to the more modern git_config_get*() based interface, instead of the old style git_config() callback based interface, it would become easier to update it so that the complaint will be given (and kill the command) only when a variable whose value actually _matters_ is unparsable.
Thanks.