am --skip: 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 succesfully merged files (such as a new file). These changes to the index will not be reverted with a "git read-tree --reset -u HEAD HEAD", as git read-tree will not be aware of how the current index differs from HEAD. To fix this, we first reset any conflicting entries from the index. The resulting index will contain the results of successfully merged files. We write the index to a tree, then use git read-tree -m to fast-forward the "index tree" back to HEAD, thus undoing all the changes from the failed merge. 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 88d5072466de2e0ba256a283eaaa6a79e31735a5
2 files changed +17 -1
git-am.sh
+6 -1
@@ -68,6 +68,8 @@ then
68 cmdline="$cmdline -3"
69 fi
70
71 +empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
72 +
73 sq () {
74 git rev-parse --sq-quote "$@"
75 }
@@ -492,7 +494,10 @@ then
494 ;;
495 t,)
496 git rerere clear
495 - git read-tree --reset -u HEAD HEAD
497 + head_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree) &&
498 + git read-tree --reset -u $head_tree $head_tree &&
499 + index_tree=$(git write-tree) &&
500 + git read-tree -m -u $index_tree $head_tree
501 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
502 git reset HEAD
503 git update-ref ORIG_HEAD $orig_head
t/t4151-am-abort.sh
+11
@@ -63,6 +63,17 @@ do
63
64 done
65
66 +test_expect_success 'am -3 --skip removes otherfile-4' '
67 + git reset --hard initial &&
68 + test_must_fail git am -3 0003-*.patch &&
69 + test 3 -eq $(git ls-files -u | wc -l) &&
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 --abort will keep the local commits intact' '
78 test_must_fail git am 0004-*.patch &&
79 test_commit unrelated &&