revert: pass around rev-list args in already-parsed form

Since 7e2bfd3f (revert: allow cherry-picking more than one commit, 2010-07-02), the pick/revert machinery has kept track of the set of commits to be cherry-picked or reverted using commit_argc and commit_argv variables, storing the corresponding command-line parameters. Future callers as other commands are built in (am, rebase, sequencer) may find it easier to pass rev-list options to this machinery in already-parsed form. Teach cmd_cherry_pick and cmd_revert to parse the rev-list arguments in advance and pass the commit set to pick_revisions() as a rev_info structure. Original patch by Jonathan, tweaks and test from Ram. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Improved-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Aug 13, 2011 at 12:06 UTC 7f13334e074bb053eccd14787e416306bc4b413a
2 files changed +34 -24
builtin/revert.c
+29 -24
@@ -60,13 +60,14 @@ struct replay_opts {
60 int allow_rerere_auto;
61
62 int mainline;
63 - int commit_argc;
64 - const char **commit_argv;
63
64 /* Merge strategy */
65 const char *strategy;
66 const char **xopts;
67 size_t xopts_nr, xopts_alloc;
68 +
69 + /* Only used by REPLAY_NONE */
70 + struct rev_info *revs;
71 };
72
73 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
@@ -169,9 +170,9 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
170 die(_("program error"));
171 }
172
172 - opts->commit_argc = parse_options(argc, argv, NULL, options, usage_str,
173 - PARSE_OPT_KEEP_ARGV0 |
174 - PARSE_OPT_KEEP_UNKNOWN);
173 + argc = parse_options(argc, argv, NULL, options, usage_str,
174 + PARSE_OPT_KEEP_ARGV0 |
175 + PARSE_OPT_KEEP_UNKNOWN);
176
177 /* Check for incompatible subcommands */
178 verify_opt_mutually_compatible(me,
@@ -213,9 +214,6 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
214 NULL);
215 }
216
216 - else if (opts->commit_argc < 2)
217 - usage_with_options(usage_str, options);
218 -
217 if (opts->allow_ff)
218 verify_opt_compatible(me, "--ff",
219 "--signoff", opts->signoff,
@@ -223,7 +221,20 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
221 "-x", opts->record_origin,
222 "--edit", opts->edit,
223 NULL);
226 - opts->commit_argv = argv;
224 +
225 + if (opts->subcommand != REPLAY_NONE) {
226 + opts->revs = NULL;
227 + } else {
228 + opts->revs = xmalloc(sizeof(*opts->revs));
229 + init_revisions(opts->revs, NULL);
230 + opts->revs->no_walk = 1;
231 + if (argc < 2)
232 + usage_with_options(usage_str, options);
233 + argc = setup_revisions(argc, argv, opts->revs, NULL);
234 + }
235 +
236 + if (argc > 1)
237 + usage_with_options(usage_str, options);
238 }
239
240 struct commit_message {
@@ -631,23 +642,15 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
642 return res;
643 }
644
634 -static void prepare_revs(struct rev_info *revs, struct replay_opts *opts)
645 +static void prepare_revs(struct replay_opts *opts)
646 {
636 - int argc;
637 -
638 - init_revisions(revs, NULL);
639 - revs->no_walk = 1;
647 if (opts->action != REVERT)
641 - revs->reverse = 1;
648 + opts->revs->reverse ^= 1;
649
643 - argc = setup_revisions(opts->commit_argc, opts->commit_argv, revs, NULL);
644 - if (argc > 1)
645 - usage(*revert_or_cherry_pick_usage(opts));
646 -
647 - if (prepare_revision_walk(revs))
650 + if (prepare_revision_walk(opts->revs))
651 die(_("revision walk setup failed"));
652
650 - if (!revs->commits)
653 + if (!opts->revs->commits)
654 die(_("empty commit set passed"));
655 }
656
@@ -844,14 +847,13 @@ static void read_populate_opts(struct replay_opts **opts_ptr)
847 static void walk_revs_populate_todo(struct commit_list **todo_list,
848 struct replay_opts *opts)
849 {
847 - struct rev_info revs;
850 struct commit *commit;
851 struct commit_list **next;
852
851 - prepare_revs(&revs, opts);
853 + prepare_revs(opts);
854
855 next = todo_list;
854 - while ((commit = get_revision(&revs)))
856 + while ((commit = get_revision(opts->revs)))
857 next = commit_list_append(commit, next);
858 }
859
@@ -1075,6 +1077,9 @@ static int pick_revisions(struct replay_opts *opts)
1077 struct commit_list *todo_list = NULL;
1078 unsigned char sha1[20];
1079
1080 + if (opts->subcommand == REPLAY_NONE)
1081 + assert(opts->revs);
1082 +
1083 read_and_refresh_cache(opts);
1084
1085 /*
t/t3510-cherry-pick-sequence.sh
+5
@@ -461,4 +461,9 @@ test_expect_success 'malformed instruction sheet 2' '
461 test_must_fail git cherry-pick --continue
462 '
463
464 +test_expect_success 'empty commit set' '
465 + pristine_detach initial &&
466 + test_expect_code 128 git cherry-pick base..base
467 +'
468 +
469 test_done