am -3: support 3way merge on unborn branch

While on an unborn branch, git am -3 will fail to do a threeway merge as it references HEAD as "our tree", but HEAD does not point to a valid tree. Fix this by using an empty tree as "our tree" when we are on an unborn branch. Signed-off-by: Paul Tan <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paul Tan committed Jun 6, 2015 at 19:46 UTC 2c970c9ec3af9e162951a1b22d42c1a4dd9a8fed
2 files changed +11 -1
git-am.sh
+2 -1
@@ -178,7 +178,8 @@ It does not apply to blobs recorded in its index.")"
178 then
179 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
180 fi
181 - git-merge-recursive $orig_tree -- HEAD $his_tree || {
181 + our_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree)
182 + git-merge-recursive $orig_tree -- $our_tree $his_tree || {
183 git rerere $allow_rerere_autoupdate
184 die "$(gettext "Failed to merge in the changes.")"
185 }
t/t4151-am-abort.sh
+9
@@ -83,4 +83,13 @@ test_expect_success 'am --abort will keep the local commits intact' '
83 test_cmp expect actual
84 '
85
86 +test_expect_success 'am -3 stops on conflict on unborn branch' '
87 + git checkout -f --orphan orphan &&
88 + git reset &&
89 + rm -f otherfile-4 &&
90 + test_must_fail git am -3 0003-*.patch &&
91 + test 2 -eq $(git ls-files -u | wc -l) &&
92 + test 4 = "$(cat otherfile-4)"
93 +'
94 +
95 test_done