cherry-pick: handle root commits with external strategies

The merge-recursive strategy already handles root commits; it cherry-picks the difference between the empty tree and the root commit's tree. However, for external strategies, we dereference NULL and segfault while building the argument list. Instead, let's handle this by passing the empty tree sha1 to the merge script. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 12, 2011 at 07:09 UTC 161807349aa1ee853880a5c6e39c53f55b10077d
1 file changed +10 -2
builtin/merge.c
+10 -2
@@ -590,6 +590,14 @@ static void write_tree_trivial(unsigned char *sha1)
590 die(_("git write-tree failed to write a tree"));
591 }
592
593 +static const char *merge_argument(struct commit *commit)
594 +{
595 + if (commit)
596 + return sha1_to_hex(commit->object.sha1);
597 + else
598 + return EMPTY_TREE_SHA1_HEX;
599 +}
600 +
601 int try_merge_command(const char *strategy, size_t xopts_nr,
602 const char **xopts, struct commit_list *common,
603 const char *head_arg, struct commit_list *remotes)
@@ -610,11 +618,11 @@ int try_merge_command(const char *strategy, size_t xopts_nr,
618 args[i++] = s;
619 }
620 for (j = common; j; j = j->next)
613 - args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
621 + args[i++] = xstrdup(merge_argument(j->item));
622 args[i++] = "--";
623 args[i++] = head_arg;
624 for (j = remotes; j; j = j->next)
617 - args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
625 + args[i++] = xstrdup(merge_argument(j->item));
626 args[i] = NULL;
627 ret = run_command_v_opt(args, RUN_GIT_CMD);
628 strbuf_release(&buf);