rebase: validate -C<n> and --whitespace=<mode> parameters early

It is a good idea to error out early upon seeing, say, `-Cbad`, rather than starting the rebase only to have the `--am` backend complain later. Let's do this. The only options accepting parameters which we pass through to `git am` (which may, or may not, forward them to `git apply`) are `-C` and `--whitespace`. The other options we pass through do not accept parameters, so we do not have to validate them here. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Nov 14, 2018 at 08:25 UTC 04519d72011478e17b2dbb177820ef2c886b8e5f
2 files changed +18 -1
builtin/rebase.c
+11 -1
@@ -1064,12 +1064,22 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1064 }
1065
1066 for (i = 0; i < options.git_am_opts.argc; i++) {
1067 - const char *option = options.git_am_opts.argv[i];
1067 + const char *option = options.git_am_opts.argv[i], *p;
1068 if (!strcmp(option, "--committer-date-is-author-date") ||
1069 !strcmp(option, "--ignore-date") ||
1070 !strcmp(option, "--whitespace=fix") ||
1071 !strcmp(option, "--whitespace=strip"))
1072 options.flags |= REBASE_FORCE;
1073 + else if (skip_prefix(option, "-C", &p)) {
1074 + while (*p)
1075 + if (!isdigit(*(p++)))
1076 + die(_("switch `C' expects a "
1077 + "numerical value"));
1078 + } else if (skip_prefix(option, "--whitespace=", &p)) {
1079 + if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
1080 + strcmp(p, "error") && strcmp(p, "error-all"))
1081 + die("Invalid whitespace option: '%s'", p);
1082 + }
1083 }
1084
1085 if (!(options.flags & REBASE_NO_QUIET))
t/t3406-rebase-message.sh
+7
@@ -84,4 +84,11 @@ test_expect_success 'rebase --onto outputs the invalid ref' '
84 test_i18ngrep "invalid-ref" err
85 '
86
87 +test_expect_success 'error out early upon -C<n> or --whitespace=<bad>' '
88 + test_must_fail git rebase -Cnot-a-number HEAD 2>err &&
89 + test_i18ngrep "numerical value" err &&
90 + test_must_fail git rebase --whitespace=bad HEAD 2>err &&
91 + test_i18ngrep "Invalid whitespace option" err
92 +'
93 +
94 test_done