am --abort: revert changes introduced by failed 3way merge

Even when a merge conflict occurs with am --3way, the index will be modified with the results of any successfully merged files. These changes to the index will not be reverted with a "git read-tree --reset -u HEAD ORIG_HEAD", as git read-tree will not be aware of how the current index differs from HEAD or ORIG_HEAD. To fix this, we first reset any conflicting entries in the index. The resulting index will contain the results of successfully merged files introduced by the failed merge. We write this index to a tree, and then use git read-tree to fast-forward this "index tree" back to ORIG_HEAD, thus undoing all the changes from the failed merge. When we are on an unborn branch, HEAD and ORIG_HEAD will not point to valid trees. In this case, use an empty tree. 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 20c3fe762105a29150fd21e3e0a340bca7890848
2 files changed +28 -1
git-am.sh
+5 -1
@@ -509,7 +509,11 @@ then
509 git rerere clear
510 if safe_to_abort
511 then
512 - git read-tree --reset -u HEAD ORIG_HEAD
512 + head_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree) &&
513 + git read-tree --reset -u $head_tree $head_tree &&
514 + index_tree=$(git write-tree) &&
515 + orig_head=$(git rev-parse --verify -q ORIG_HEAD || echo $empty_tree) &&
516 + git read-tree -m -u $index_tree $orig_head
517 git reset ORIG_HEAD
518 fi
519 rm -fr "$dotest"
t/t4151-am-abort.sh
+23
@@ -70,6 +70,17 @@ test_expect_success 'am -3 --skip removes otherfile-4' '
70 test 4 = "$(cat otherfile-4)" &&
71 git am --skip &&
72 test_cmp_rev initial HEAD &&
73 + test -z "$(git ls-files -u)" &&
74 + test_path_is_missing otherfile-4
75 +'
76 +
77 +test_expect_success 'am -3 --abort removes otherfile-4' '
78 + git reset --hard initial &&
79 + test_must_fail git am -3 0003-*.patch &&
80 + test 3 -eq $(git ls-files -u | wc -l) &&
81 + test 4 = "$(cat otherfile-4)" &&
82 + git am --abort &&
83 + test_cmp_rev initial HEAD &&
84 test -z $(git ls-files -u) &&
85 test_path_is_missing otherfile-4
86 '
@@ -102,4 +113,16 @@ test_expect_success 'am -3 --skip clears index on unborn branch' '
113 test_path_is_missing tmpfile
114 '
115
116 +test_expect_success 'am -3 --abort removes otherfile-4 on unborn branch' '
117 + git checkout -f --orphan orphan &&
118 + git reset &&
119 + rm -f otherfile-4 file-1 &&
120 + test_must_fail git am -3 0003-*.patch &&
121 + test 2 -eq $(git ls-files -u | wc -l) &&
122 + test 4 = "$(cat otherfile-4)" &&
123 + git am --abort &&
124 + test -z "$(git ls-files -u)" &&
125 + test_path_is_missing otherfile-4
126 +'
127 +
128 test_done