Skip to content

Commit a73e22e

Browse files
martinvonzgitster
authored andcommitted
cherry-pick/revert: respect order of revisions to pick
When giving multiple individual revisions to cherry-pick or revert, as in 'git cherry-pick A B' or 'git revert B A', one would expect them to be picked/reverted in the order given on the command line. They are instead ordered by their commit timestamp -- in chronological order for "cherry-pick" and in reverse chronological order for "revert". This matches the order in which one would usually give them on the command line, making this bug somewhat hard to notice. Still, it has been reported at least once before [1]. It seems like the chronological sorting happened by accident because the revision walker has traditionally always sorted commits in reverse chronological order when rev_info.no_walk was enabled. In the case of 'git revert B A' where B is newer than A, this sorting is a no-op. For 'git cherry-pick A B', the sorting would reverse the arguments, but because the sequencer also flips the rev_info.reverse flag when picking (as opposed to reverting), the end result is a chronological order. The rev_info.reverse flag was probably flipped so that the revision walker emits B before C in 'git cherry-pick A..C'; that it happened to effectively undo the unexpected sorting done when not walking, was probably a coincidence that allowed this bug to happen at all. Fix the bug by telling the revision walker not to sort the commits when not walking. The only case we want to reverse the order is now when cherry-picking and walking revisions (rev_info.no_walk = 0). [1] http://thread.gmane.org/gmane.comp.version-control.git/164794 Signed-off-by: Martin von Zweigbergk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d023c24 commit a73e22e

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

builtin/revert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
193193
struct setup_revision_opt s_r_opt;
194194
opts->revs = xmalloc(sizeof(*opts->revs));
195195
init_revisions(opts->revs, NULL);
196-
opts->revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
196+
opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
197197
if (argc < 2)
198198
usage_with_options(usage_str, options);
199199
memset(&s_r_opt, 0, sizeof(s_r_opt));

sequencer.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,11 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
543543

544544
static void prepare_revs(struct replay_opts *opts)
545545
{
546-
if (opts->action != REPLAY_REVERT)
546+
/*
547+
* picking (but not reverting) ranges (but not individual revisions)
548+
* should be done in reverse
549+
*/
550+
if (opts->action == REPLAY_PICK && !opts->revs->no_walk)
547551
opts->revs->reverse ^= 1;
548552

549553
if (prepare_revision_walk(opts->revs))

t/t3508-cherry-pick-many-commits.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ test_expect_success 'cherry-pick first..fourth works' '
4444
check_head_differs_from fourth
4545
'
4646

47-
test_expect_failure 'cherry-pick three one two works' '
47+
test_expect_success 'cherry-pick three one two works' '
4848
git checkout -f first &&
4949
test_commit one &&
5050
test_commit two &&

0 commit comments

Comments
 (0)