Skip to content

Commit 7e2bfd3

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4b20956 commit 7e2bfd3

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

builtin/revert.c

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ static const char * const cherry_pick_usage[] = {
3939
static int edit, no_replay, no_commit, mainline, signoff, allow_ff;
4040
static enum { REVERT, CHERRY_PICK } action;
4141
static struct commit *commit;
42-
static const char *commit_name;
42+
static int commit_argc;
43+
static const char **commit_argv;
4344
static int allow_rerere_auto;
4445

4546
static const char *me;
@@ -53,7 +54,6 @@ static void parse_args(int argc, const char **argv)
5354
{
5455
const char * const * usage_str =
5556
action == REVERT ? revert_usage : cherry_pick_usage;
56-
unsigned char sha1[20];
5757
int noop;
5858
struct option options[] = {
5959
OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"),
@@ -78,15 +78,11 @@ static void parse_args(int argc, const char **argv)
7878
die("program error");
7979
}
8080

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)
8283
usage_with_options(usage_str, options);
8384

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;
9086
}
9187

9288
struct commit_message {
@@ -527,8 +523,35 @@ static int do_pick_commit(void)
527523
return 0;
528524
}
529525

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+
530551
static int revert_or_cherry_pick(int argc, const char **argv)
531552
{
553+
struct rev_info revs;
554+
532555
git_config(git_default_config, NULL);
533556
me = action == REVERT ? "revert" : "cherry-pick";
534557
setenv(GIT_REFLOG_ACTION, me, 0);
@@ -548,7 +571,15 @@ static int revert_or_cherry_pick(int argc, const char **argv)
548571
if (read_cache() < 0)
549572
die("git %s: failed to read the index", me);
550573

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;
552583
}
553584

554585
int cmd_revert(int argc, const char **argv, const char *prefix)

0 commit comments

Comments
 (0)