@matheusbsbs / matheus-bagliano / commits / 7e2bfd3f99
Fork 0

revert: allow cherry-picking more than one commit

This makes it possible to pass many commits or ranges of commits to "git cherry-pick" and to "git revert" to process many commits instead of just one. In fact commits are now enumerated with an equivalent of git rev-list --no-walk "$@" so all the following are now possible: git cherry-pick master~2..master git cherry-pick ^master~2 master git cherry-pick master^ master The following should be possible but does not work: git cherry-pick -2 master because "git rev-list --no-walk -2 master" only outputs one commit as "--no-walk" seems to take over "-2". And there is currently no way to continue cherry-picking or reverting if there is a problem with one commit. It's also not possible to abort the whole process. Some future work should provide the --continue and --abort options to do just that. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Jun 2, 2010 at 07:58 UTC 7e2bfd3f99f8d6e89c7b855675919dd5404e47a2
1 file changed +41 -10
builtin/revert.c
+41 -10
@@ -39,7 +39,8 @@ static const char * const cherry_pick_usage[] = {
39 static int edit, no_replay, no_commit, mainline, signoff, allow_ff;
40 static enum { REVERT, CHERRY_PICK } action;
41 static struct commit *commit;
42 -static const char *commit_name;
42 +static int commit_argc;
43 +static const char **commit_argv;
44 static int allow_rerere_auto;
45
46 static const char *me;
@@ -53,7 +54,6 @@ static void parse_args(int argc, const char **argv)
54 {
55 const char * const * usage_str =
56 action == REVERT ? revert_usage : cherry_pick_usage;
56 - unsigned char sha1[20];
57 int noop;
58 struct option options[] = {
59 OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"),
@@ -78,15 +78,11 @@ static void parse_args(int argc, const char **argv)
78 die("program error");
79 }
80
81 - if (parse_options(argc, argv, NULL, options, usage_str, 0) != 1)
81 + commit_argc = parse_options(argc, argv, NULL, options, usage_str, 0);
82 + if (commit_argc < 1)
83 usage_with_options(usage_str, options);
84
84 - commit_name = argv[0];
85 - if (get_sha1(commit_name, sha1))
86 - die ("Cannot find '%s'", commit_name);
87 - commit = lookup_commit_reference(sha1);
88 - if (!commit)
89 - exit(1);
85 + commit_argv = argv;
86 }
87
88 struct commit_message {
@@ -527,8 +523,35 @@ static int do_pick_commit(void)
523 return 0;
524 }
525
526 +static void prepare_revs(struct rev_info *revs)
527 +{
528 + int argc = 0;
529 + int i;
530 + const char **argv = xmalloc((commit_argc + 4) * sizeof(*argv));
531 +
532 + argv[argc++] = NULL;
533 + argv[argc++] = "--no-walk";
534 + if (action != REVERT)
535 + argv[argc++] = "--reverse";
536 + for (i = 0; i < commit_argc; i++)
537 + argv[argc++] = commit_argv[i];
538 + argv[argc++] = NULL;
539 +
540 + init_revisions(revs, NULL);
541 + setup_revisions(argc - 1, argv, revs, NULL);
542 + if (prepare_revision_walk(revs))
543 + die("revision walk setup failed");
544 +
545 + if (!revs->commits)
546 + die("empty commit set passed");
547 +
548 + free(argv);
549 +}
550 +
551 static int revert_or_cherry_pick(int argc, const char **argv)
552 {
553 + struct rev_info revs;
554 +
555 git_config(git_default_config, NULL);
556 me = action == REVERT ? "revert" : "cherry-pick";
557 setenv(GIT_REFLOG_ACTION, me, 0);
@@ -548,7 +571,15 @@ static int revert_or_cherry_pick(int argc, const char **argv)
571 if (read_cache() < 0)
572 die("git %s: failed to read the index", me);
573
551 - return do_pick_commit();
574 + prepare_revs(&revs);
575 +
576 + while ((commit = get_revision(&revs))) {
577 + int res = do_pick_commit();
578 + if (res)
579 + return res;
580 + }
581 +
582 + return 0;
583 }
584
585 int cmd_revert(int argc, const char **argv, const char *prefix)