Skip to content

Commit 0e66f87

Browse files
committed
Merge branch 'jk/cherry-pick-root-with-resolve' into maint
* jk/cherry-pick-root-with-resolve: t3503: test cherry picking and reverting root commits revert: allow reverting a root commit cherry-pick: handle root commits with external strategies
2 parents 93446aa + e9fe74c commit 0e66f87

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

builtin/merge.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,14 @@ static void write_tree_trivial(unsigned char *sha1)
599599
die(_("git write-tree failed to write a tree"));
600600
}
601601

602+
static const char *merge_argument(struct commit *commit)
603+
{
604+
if (commit)
605+
return sha1_to_hex(commit->object.sha1);
606+
else
607+
return EMPTY_TREE_SHA1_HEX;
608+
}
609+
602610
int try_merge_command(const char *strategy, size_t xopts_nr,
603611
const char **xopts, struct commit_list *common,
604612
const char *head_arg, struct commit_list *remotes)
@@ -619,11 +627,11 @@ int try_merge_command(const char *strategy, size_t xopts_nr,
619627
args[i++] = s;
620628
}
621629
for (j = common; j; j = j->next)
622-
args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
630+
args[i++] = xstrdup(merge_argument(j->item));
623631
args[i++] = "--";
624632
args[i++] = head_arg;
625633
for (j = remotes; j; j = j->next)
626-
args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
634+
args[i++] = xstrdup(merge_argument(j->item));
627635
args[i] = NULL;
628636
ret = run_command_v_opt(args, RUN_GIT_CMD);
629637
strbuf_release(&buf);

builtin/revert.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,6 @@ static int do_pick_commit(void)
408408
discard_cache();
409409

410410
if (!commit->parents) {
411-
if (action == REVERT)
412-
die (_("Cannot revert a root commit"));
413411
parent = NULL;
414412
}
415413
else if (commit->parents->next) {
@@ -467,7 +465,7 @@ static int do_pick_commit(void)
467465
strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit ");
468466
strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
469467

470-
if (commit->parents->next) {
468+
if (commit->parents && commit->parents->next) {
471469
strbuf_addstr(&msgbuf, ", reversing\nchanges made to ");
472470
strbuf_addstr(&msgbuf, sha1_to_hex(parent->object.sha1));
473471
}

t/t3503-cherry-pick-root.sh

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
test_description='test cherry-picking a root commit'
3+
test_description='test cherry-picking (and reverting) a root commit'
44

55
. ./test-lib.sh
66

@@ -23,7 +23,30 @@ test_expect_success setup '
2323
test_expect_success 'cherry-pick a root commit' '
2424
2525
git cherry-pick master &&
26-
test first = $(cat file1)
26+
echo first >expect &&
27+
test_cmp expect file1
28+
29+
'
30+
31+
test_expect_success 'revert a root commit' '
32+
33+
git revert master &&
34+
test_path_is_missing file1
35+
36+
'
37+
38+
test_expect_success 'cherry-pick a root commit with an external strategy' '
39+
40+
git cherry-pick --strategy=resolve master &&
41+
echo first >expect &&
42+
test_cmp expect file1
43+
44+
'
45+
46+
test_expect_success 'revert a root commit with an external strategy' '
47+
48+
git revert --strategy=resolve master &&
49+
test_path_is_missing file1
2750
2851
'
2952

0 commit comments

Comments
 (0)