revert: Don't create invalid replay_opts in parse_args

The "--ff" command-line option cannot be used with some other command-line options. However, parse_args still parses these incompatible options into a replay_opts structure for use by the rest of the program. Although pick_commits, the current gatekeeper to the cherry-pick machinery, checks the validity of the replay_opts structure before before starting its operation, there will be multiple entry points to the cherry-pick machinery in future. To futureproof the code and catch these errors in one place, make sure that an invalid replay_opts structure is not created by parse_args in the first place. We still check the replay_opts structure for validity in pick_commits, but this is an assert() now to emphasize that it's the caller's responsibility to get it right. Inspired-by: Christian Couder <chriscool@tuxfamily.org> Mentored-by: Jonathan Nieder <jrnieder@gmail.com> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed Aug 4, 2011 at 16:09 UTC 9044143ff150a4ccc834523ae2f9ee11c2c961c3
1 file changed +27 -11
builtin/revert.c
+27 -11
@@ -86,9 +86,26 @@ static int option_parse_x(const struct option *opt,
86 return 0;
87 }
88
89 +static void verify_opt_compatible(const char *me, const char *base_opt, ...)
90 +{
91 + const char *this_opt;
92 + va_list ap;
93 +
94 + va_start(ap, base_opt);
95 + while ((this_opt = va_arg(ap, const char *))) {
96 + if (va_arg(ap, int))
97 + break;
98 + }
99 + va_end(ap);
100 +
101 + if (this_opt)
102 + die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
103 +}
104 +
105 static void parse_args(int argc, const char **argv, struct replay_opts *opts)
106 {
107 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
108 + const char *me = action_name(opts);
109 int noop;
110 struct option options[] = {
111 OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"),
@@ -122,6 +139,13 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
139 if (opts->commit_argc < 2)
140 usage_with_options(usage_str, options);
141
142 + if (opts->allow_ff)
143 + verify_opt_compatible(me, "--ff",
144 + "--signoff", opts->signoff,
145 + "--no-commit", opts->no_commit,
146 + "-x", opts->record_origin,
147 + "--edit", opts->edit,
148 + NULL);
149 opts->commit_argv = argv;
150 }
151
@@ -569,17 +593,9 @@ static int pick_commits(struct replay_opts *opts)
593 struct commit *commit;
594
595 setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
572 - if (opts->allow_ff) {
573 - if (opts->signoff)
574 - die(_("cherry-pick --ff cannot be used with --signoff"));
575 - if (opts->no_commit)
576 - die(_("cherry-pick --ff cannot be used with --no-commit"));
577 - if (opts->record_origin)
578 - die(_("cherry-pick --ff cannot be used with -x"));
579 - if (opts->edit)
580 - die(_("cherry-pick --ff cannot be used with --edit"));
581 - }
582 -
596 + if (opts->allow_ff)
597 + assert(!(opts->signoff || opts->no_commit ||
598 + opts->record_origin || opts->edit));
599 read_and_refresh_cache(opts);
600
601 prepare_revs(&revs, opts);