[PATCH 09/14] revert: Don't create invalid replay_opts in parse_args
From: Ramkumar Ramachandra <hidden>
Date: 2016-06-15 22:51:33
Subsystem:
the rest · Maintainer:
Linus Torvalds
The "--ff" command-line option cannot be used with four other command-line options. However, when these options are specified with "--ff" on the command-line, parse_args will still parse these incompatible options into a replay_opts structure for use by the rest of the program. Although pick_commits checks the validity of the replay_opts strucutre before before starting its operation, this is inelegant design; pick_commits is currently the gatekeeper to the cherry-pick machinery, but this will change 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. Also ensure that regressions in maintaining this invariant are caught in the future by adding an assertion in pick_commits. Inspired-by: Christian Couder [off-list ref] Mentored-by: Jonathan Nieder [off-list ref] Helped-by: Junio C Hamano [off-list ref] Signed-off-by: Ramkumar Ramachandra <redacted> --- builtin/revert.c | 37 ++++++++++++++++++++++++++----------- 1 files changed, 26 insertions(+), 11 deletions(-)
diff --git a/builtin/revert.c b/builtin/revert.c
index e3a7c9e..27dc538 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c@@ -82,6 +82,22 @@ static int option_parse_x(const struct option *opt, return 0; } +static void verify_opt_compatible(const char *me, const char *base_opt, ...) +{ + const char *this_opt; + va_list ap; + int set; + + va_start(ap, base_opt); + while ((this_opt = va_arg(ap, const char *))) { + set = va_arg(ap, int); + if (set) + die(_("%s: %s cannot be used with %s"), + me, this_opt, base_opt); + } + va_end(ap); +} + static void parse_args(int argc, const char **argv, struct replay_opts *opts) { const char * const * usage_str = revert_or_cherry_pick_usage(opts);
@@ -118,6 +134,13 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts) if (opts->commit_argc < 2) usage_with_options(usage_str, options); + if (opts->allow_ff) + verify_opt_compatible(me, "--ff", + "--signoff", opts->signoff, + "--no-commit", opts->no_commit, + "-x", opts->record_origin, + "--edit", opts->edit, + NULL); opts->commit_argv = argv; }
@@ -561,17 +584,9 @@ static int pick_commits(struct replay_opts *opts) struct commit *commit; setenv(GIT_REFLOG_ACTION, me, 0); - if (opts->allow_ff) { - if (opts->signoff) - die(_("cherry-pick --ff cannot be used with --signoff")); - if (opts->no_commit) - die(_("cherry-pick --ff cannot be used with --no-commit")); - if (opts->record_origin) - die(_("cherry-pick --ff cannot be used with -x")); - if (opts->edit) - die(_("cherry-pick --ff cannot be used with --edit")); - } - + if (opts->allow_ff) + assert(!(opts->signoff || opts->no_commit || + opts->record_origin || opts->edit)); read_and_refresh_cache(me, opts); prepare_revs(&revs, opts);
--
1.7.5.GIT