Skip to content

Commit 137c1f2

Browse files
committed
Merge branch 'js/rebase-am-options'
The way "git rebase" parses and forwards the command line options meant for underlying "git am" has been revamped, which fixed for options with parameters that were not passed correctly. * js/rebase-am-options: rebase: validate -C<n> and --whitespace=<mode> parameters early rebase: really just passthru the `git am` options
2 parents 7bdebaa + 04519d7 commit 137c1f2

File tree

2 files changed

+52
-63
lines changed

2 files changed

+52
-63
lines changed

builtin/rebase.c

Lines changed: 45 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct rebase_options {
8888
REBASE_FORCE = 1<<3,
8989
REBASE_INTERACTIVE_EXPLICIT = 1<<4,
9090
} flags;
91-
struct strbuf git_am_opt;
91+
struct argv_array git_am_opts;
9292
const char *action;
9393
int signoff;
9494
int allow_rerere_autoupdate;
@@ -340,7 +340,7 @@ N_("Resolve all conflicts manually, mark them as resolved with\n"
340340
static int run_specific_rebase(struct rebase_options *opts)
341341
{
342342
const char *argv[] = { NULL, NULL };
343-
struct strbuf script_snippet = STRBUF_INIT;
343+
struct strbuf script_snippet = STRBUF_INIT, buf = STRBUF_INIT;
344344
int status;
345345
const char *backend, *backend_func;
346346

@@ -434,7 +434,9 @@ static int run_specific_rebase(struct rebase_options *opts)
434434
oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
435435
add_var(&script_snippet, "GIT_QUIET",
436436
opts->flags & REBASE_NO_QUIET ? "" : "t");
437-
add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
437+
sq_quote_argv_pretty(&buf, opts->git_am_opts.argv);
438+
add_var(&script_snippet, "git_am_opt", buf.buf);
439+
strbuf_release(&buf);
438440
add_var(&script_snippet, "verbose",
439441
opts->flags & REBASE_VERBOSE ? "t" : "");
440442
add_var(&script_snippet, "diffstat",
@@ -775,7 +777,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
775777
struct rebase_options options = {
776778
.type = REBASE_UNSPECIFIED,
777779
.flags = REBASE_NO_QUIET,
778-
.git_am_opt = STRBUF_INIT,
780+
.git_am_opts = ARGV_ARRAY_INIT,
779781
.allow_rerere_autoupdate = -1,
780782
.allow_empty_message = 1,
781783
.git_format_patch_opt = STRBUF_INIT,
@@ -796,12 +798,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
796798
ACTION_EDIT_TODO,
797799
ACTION_SHOW_CURRENT_PATCH,
798800
} action = NO_ACTION;
799-
int committer_date_is_author_date = 0;
800-
int ignore_date = 0;
801-
int ignore_whitespace = 0;
802801
const char *gpg_sign = NULL;
803-
int opt_c = -1;
804-
struct string_list whitespace = STRING_LIST_INIT_NODUP;
805802
struct string_list exec = STRING_LIST_INIT_NODUP;
806803
const char *rebase_merges = NULL;
807804
int fork_point = -1;
@@ -823,15 +820,20 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
823820
{OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
824821
N_("do not show diffstat of what changed upstream"),
825822
PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
826-
OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
827-
N_("passed to 'git apply'")),
828823
OPT_BOOL(0, "signoff", &options.signoff,
829824
N_("add a Signed-off-by: line to each commit")),
830-
OPT_BOOL(0, "committer-date-is-author-date",
831-
&committer_date_is_author_date,
832-
N_("passed to 'git am'")),
833-
OPT_BOOL(0, "ignore-date", &ignore_date,
834-
N_("passed to 'git am'")),
825+
OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &options.git_am_opts,
826+
NULL, N_("passed to 'git am'"),
827+
PARSE_OPT_NOARG),
828+
OPT_PASSTHRU_ARGV(0, "committer-date-is-author-date",
829+
&options.git_am_opts, NULL,
830+
N_("passed to 'git am'"), PARSE_OPT_NOARG),
831+
OPT_PASSTHRU_ARGV(0, "ignore-date", &options.git_am_opts, NULL,
832+
N_("passed to 'git am'"), PARSE_OPT_NOARG),
833+
OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
834+
N_("passed to 'git apply'"), 0),
835+
OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
836+
N_("action"), N_("passed to 'git apply'"), 0),
835837
OPT_BIT('f', "force-rebase", &options.flags,
836838
N_("cherry-pick all commits, even if unchanged"),
837839
REBASE_FORCE),
@@ -875,10 +877,6 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
875877
{ OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
876878
N_("GPG-sign commits"),
877879
PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
878-
OPT_STRING_LIST(0, "whitespace", &whitespace,
879-
N_("whitespace"), N_("passed to 'git apply'")),
880-
OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"),
881-
REBASE_AM),
882880
OPT_BOOL(0, "autostash", &options.autostash,
883881
N_("automatically stash/stash pop before and after")),
884882
OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
@@ -903,6 +901,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
903901
N_("rebase all reachable commits up to the root(s)")),
904902
OPT_END(),
905903
};
904+
int i;
906905

907906
/*
908907
* NEEDSWORK: Once the builtin rebase has been tested enough
@@ -1087,22 +1086,27 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10871086
state_dir_base, cmd_live_rebase, buf.buf);
10881087
}
10891088

1090-
if (!(options.flags & REBASE_NO_QUIET))
1091-
strbuf_addstr(&options.git_am_opt, " -q");
1092-
1093-
if (committer_date_is_author_date) {
1094-
strbuf_addstr(&options.git_am_opt,
1095-
" --committer-date-is-author-date");
1096-
options.flags |= REBASE_FORCE;
1089+
for (i = 0; i < options.git_am_opts.argc; i++) {
1090+
const char *option = options.git_am_opts.argv[i], *p;
1091+
if (!strcmp(option, "--committer-date-is-author-date") ||
1092+
!strcmp(option, "--ignore-date") ||
1093+
!strcmp(option, "--whitespace=fix") ||
1094+
!strcmp(option, "--whitespace=strip"))
1095+
options.flags |= REBASE_FORCE;
1096+
else if (skip_prefix(option, "-C", &p)) {
1097+
while (*p)
1098+
if (!isdigit(*(p++)))
1099+
die(_("switch `C' expects a "
1100+
"numerical value"));
1101+
} else if (skip_prefix(option, "--whitespace=", &p)) {
1102+
if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
1103+
strcmp(p, "error") && strcmp(p, "error-all"))
1104+
die("Invalid whitespace option: '%s'", p);
1105+
}
10971106
}
10981107

1099-
if (ignore_whitespace)
1100-
strbuf_addstr(&options.git_am_opt, " --ignore-whitespace");
1101-
1102-
if (ignore_date) {
1103-
strbuf_addstr(&options.git_am_opt, " --ignore-date");
1104-
options.flags |= REBASE_FORCE;
1105-
}
1108+
if (!(options.flags & REBASE_NO_QUIET))
1109+
argv_array_push(&options.git_am_opts, "-q");
11061110

11071111
if (options.keep_empty)
11081112
imply_interactive(&options, "--keep-empty");
@@ -1112,23 +1116,6 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
11121116
options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
11131117
}
11141118

1115-
if (opt_c >= 0)
1116-
strbuf_addf(&options.git_am_opt, " -C%d", opt_c);
1117-
1118-
if (whitespace.nr) {
1119-
int i;
1120-
1121-
for (i = 0; i < whitespace.nr; i++) {
1122-
const char *item = whitespace.items[i].string;
1123-
1124-
strbuf_addf(&options.git_am_opt, " --whitespace=%s",
1125-
item);
1126-
1127-
if ((!strcmp(item, "fix")) || (!strcmp(item, "strip")))
1128-
options.flags |= REBASE_FORCE;
1129-
}
1130-
}
1131-
11321119
if (exec.nr) {
11331120
int i;
11341121

@@ -1204,23 +1191,18 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
12041191
break;
12051192
}
12061193

1207-
if (options.git_am_opt.len) {
1208-
const char *p;
1209-
1194+
if (options.git_am_opts.argc) {
12101195
/* all am options except -q are compatible only with --am */
1211-
strbuf_reset(&buf);
1212-
strbuf_addbuf(&buf, &options.git_am_opt);
1213-
strbuf_addch(&buf, ' ');
1214-
while ((p = strstr(buf.buf, " -q ")))
1215-
strbuf_splice(&buf, p - buf.buf, 4, " ", 1);
1216-
strbuf_trim(&buf);
1196+
for (i = options.git_am_opts.argc - 1; i >= 0; i--)
1197+
if (strcmp(options.git_am_opts.argv[i], "-q"))
1198+
break;
12171199

1218-
if (is_interactive(&options) && buf.len)
1200+
if (is_interactive(&options) && i >= 0)
12191201
die(_("error: cannot combine interactive options "
12201202
"(--interactive, --exec, --rebase-merges, "
12211203
"--preserve-merges, --keep-empty, --root + "
12221204
"--onto) with am options (%s)"), buf.buf);
1223-
if (options.type == REBASE_MERGE && buf.len)
1205+
if (options.type == REBASE_MERGE && i >= 0)
12241206
die(_("error: cannot combine merge options (--merge, "
12251207
"--strategy, --strategy-option) with am options "
12261208
"(%s)"), buf.buf);
@@ -1230,7 +1212,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
12301212
if (options.type == REBASE_PRESERVE_MERGES)
12311213
die("cannot combine '--signoff' with "
12321214
"'--preserve-merges'");
1233-
strbuf_addstr(&options.git_am_opt, " --signoff");
1215+
argv_array_push(&options.git_am_opts, "--signoff");
12341216
options.flags |= REBASE_FORCE;
12351217
}
12361218

t/t3406-rebase-message.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,11 @@ test_expect_success 'rebase --onto outputs the invalid ref' '
8484
test_i18ngrep "invalid-ref" err
8585
'
8686

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+
8794
test_done

0 commit comments

Comments
 (0)