summaryrefslogtreecommitdiff
path: root/builtin/revert.c
diff options
context:
space:
mode:
authorRamkumar Ramachandra <artagnon@gmail.com>2011-08-04 10:39:07 (GMT)
committerJunio C Hamano <gitster@pobox.com>2011-08-04 22:40:43 (GMT)
commit9044143ff150a4ccc834523ae2f9ee11c2c961c3 (patch)
treed5430d126f887285122b72fc9856b48a6bc478e7 /builtin/revert.c
parent89641472aafc28469448688a4ba610cf082921fa (diff)
downloadgit-9044143ff150a4ccc834523ae2f9ee11c2c961c3.zip
git-9044143ff150a4ccc834523ae2f9ee11c2c961c3.tar.gz
git-9044143ff150a4ccc834523ae2f9ee11c2c961c3.tar.bz2
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>
Diffstat (limited to 'builtin/revert.c')
-rw-r--r--builtin/revert.c38
1 files changed, 27 insertions, 11 deletions
diff --git a/builtin/revert.c b/builtin/revert.c
index 90fe2ef..f75c9cb 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -86,9 +86,26 @@ 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;
+
+ va_start(ap, base_opt);
+ while ((this_opt = va_arg(ap, const char *))) {
+ if (va_arg(ap, int))
+ break;
+ }
+ va_end(ap);
+
+ if (this_opt)
+ die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
+}
+
static void parse_args(int argc, const char **argv, struct replay_opts *opts)
{
const char * const * usage_str = revert_or_cherry_pick_usage(opts);
+ const char *me = action_name(opts);
int noop;
struct option options[] = {
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)
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;
}
@@ -569,17 +593,9 @@ static int pick_commits(struct replay_opts *opts)
struct commit *commit;
setenv(GIT_REFLOG_ACTION, action_name(opts), 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(opts);
prepare_revs(&revs, opts);