From 88d5072466de2e0ba256a283eaaa6a79e31735a5 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sat, 6 Jun 2015 19:46:07 +0800 Subject: 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 Signed-off-by: Junio C Hamano --- t/t4151-am-abort.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 't/t4151-am-abort.sh') diff --git a/t/t4151-am-abort.sh b/t/t4151-am-abort.sh index 1176bcccf3..5ac741a1ac 100755 --- a/t/t4151-am-abort.sh +++ b/t/t4151-am-abort.sh @@ -63,6 +63,17 @@ do done +test_expect_success 'am -3 --skip removes otherfile-4' ' + git reset --hard initial && + test_must_fail git am -3 0003-*.patch && + test 3 -eq $(git ls-files -u | wc -l) && + test 4 = "$(cat otherfile-4)" && + git am --skip && + test_cmp_rev initial HEAD && + test -z $(git ls-files -u) && + test_path_is_missing otherfile-4 +' + test_expect_success 'am --abort will keep the local commits intact' ' test_must_fail git am 0004-*.patch && test_commit unrelated && -- cgit v1.2.3 From 2c970c9ec3af9e162951a1b22d42c1a4dd9a8fed Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sat, 6 Jun 2015 19:46:08 +0800 Subject: 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 Signed-off-by: Junio C Hamano --- t/t4151-am-abort.sh | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 't/t4151-am-abort.sh') diff --git a/t/t4151-am-abort.sh b/t/t4151-am-abort.sh index 5ac741a1ac..2683ffaacd 100755 --- a/t/t4151-am-abort.sh +++ b/t/t4151-am-abort.sh @@ -83,4 +83,13 @@ test_expect_success 'am --abort will keep the local commits intact' ' test_cmp expect actual ' +test_expect_success 'am -3 stops on conflict on unborn branch' ' + git checkout -f --orphan orphan && + git reset && + rm -f otherfile-4 && + test_must_fail git am -3 0003-*.patch && + test 2 -eq $(git ls-files -u | wc -l) && + test 4 = "$(cat otherfile-4)" +' + test_done -- cgit v1.2.3 From f8da6801e2fb3e46a42031b860c6411ef76a0335 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sat, 6 Jun 2015 19:46:09 +0800 Subject: am --skip: support skipping while on unborn branch When git am --skip is run, git am will copy HEAD's tree entries to the index with "git reset HEAD". However, on an unborn branch, HEAD does not point to a tree, so "git reset HEAD" will fail. Fix this by treating HEAD as en empty tree when we are on an unborn branch. Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano --- t/t4151-am-abort.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 't/t4151-am-abort.sh') diff --git a/t/t4151-am-abort.sh b/t/t4151-am-abort.sh index 2683ffaacd..2a7c5247f9 100755 --- a/t/t4151-am-abort.sh +++ b/t/t4151-am-abort.sh @@ -92,4 +92,14 @@ test_expect_success 'am -3 stops on conflict on unborn branch' ' test 4 = "$(cat otherfile-4)" ' +test_expect_success 'am -3 --skip clears index on unborn branch' ' + test_path_is_dir .git/rebase-apply && + echo tmpfile >tmpfile && + git add tmpfile && + git am --skip && + test -z "$(git ls-files)" && + test_path_is_missing otherfile-4 && + test_path_is_missing tmpfile +' + test_done -- cgit v1.2.3 From 20c3fe762105a29150fd21e3e0a340bca7890848 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sat, 6 Jun 2015 19:46:10 +0800 Subject: 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 Signed-off-by: Junio C Hamano --- t/t4151-am-abort.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 't/t4151-am-abort.sh') diff --git a/t/t4151-am-abort.sh b/t/t4151-am-abort.sh index 2a7c5247f9..e61be63114 100755 --- a/t/t4151-am-abort.sh +++ b/t/t4151-am-abort.sh @@ -70,6 +70,17 @@ test_expect_success 'am -3 --skip removes otherfile-4' ' test 4 = "$(cat otherfile-4)" && git am --skip && test_cmp_rev initial HEAD && + test -z "$(git ls-files -u)" && + test_path_is_missing otherfile-4 +' + +test_expect_success 'am -3 --abort removes otherfile-4' ' + git reset --hard initial && + test_must_fail git am -3 0003-*.patch && + test 3 -eq $(git ls-files -u | wc -l) && + test 4 = "$(cat otherfile-4)" && + git am --abort && + test_cmp_rev initial HEAD && test -z $(git ls-files -u) && test_path_is_missing otherfile-4 ' @@ -102,4 +113,16 @@ test_expect_success 'am -3 --skip clears index on unborn branch' ' test_path_is_missing tmpfile ' +test_expect_success 'am -3 --abort removes otherfile-4 on unborn branch' ' + git checkout -f --orphan orphan && + git reset && + rm -f otherfile-4 file-1 && + test_must_fail git am -3 0003-*.patch && + test 2 -eq $(git ls-files -u | wc -l) && + test 4 = "$(cat otherfile-4)" && + git am --abort && + test -z "$(git ls-files -u)" && + test_path_is_missing otherfile-4 +' + test_done -- cgit v1.2.3 From e06764c8ebf87a80737dc0a6198a013799f18e32 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sat, 6 Jun 2015 19:46:11 +0800 Subject: am --abort: support aborting to unborn branch When git-am is first run on an unborn branch, no ORIG_HEAD is created. As such, any applied commits will remain even after a git am --abort. To be consistent with the behavior of git am --abort when it is not run from an unborn branch, we empty the index, and then destroy the branch pointed to by HEAD if there is no ORIG_HEAD. Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano --- t/t4151-am-abort.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 't/t4151-am-abort.sh') diff --git a/t/t4151-am-abort.sh b/t/t4151-am-abort.sh index e61be63114..dc6b3b0c7c 100755 --- a/t/t4151-am-abort.sh +++ b/t/t4151-am-abort.sh @@ -14,6 +14,7 @@ test_expect_success setup ' git add file-1 file-2 && git commit -m initial && git tag initial && + git format-patch --stdout --root initial >initial.patch && for i in 2 3 4 5 6 do echo $i >>file-1 && @@ -125,4 +126,20 @@ test_expect_success 'am -3 --abort removes otherfile-4 on unborn branch' ' test_path_is_missing otherfile-4 ' +test_expect_success 'am -3 --abort on unborn branch removes applied commits' ' + git checkout -f --orphan orphan && + git reset && + rm -f otherfile-4 otherfile-2 file-1 file-2 && + test_must_fail git am -3 initial.patch 0003-*.patch && + test 3 -eq $(git ls-files -u | wc -l) && + test 4 = "$(cat otherfile-4)" && + git am --abort && + test -z "$(git ls-files -u)" && + test_path_is_missing otherfile-4 && + test_path_is_missing file-1 && + test_path_is_missing file-2 && + test 0 -eq $(git log --oneline 2>/dev/null | wc -l) && + test refs/heads/orphan = "$(git symbolic-ref HEAD)" +' + test_done -- cgit v1.2.3 From 6ea3b67b4e3f4a09561a26ca42af1492b3b48c95 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Sat, 6 Jun 2015 19:46:12 +0800 Subject: am --abort: keep unrelated commits on unborn branch Since 7b3b7e3 (am --abort: keep unrelated commits since the last failure and warn, 2010-12-21), git-am would refuse to rewind HEAD if commits were made since the last git-am failure. This check was implemented in safe_to_abort(), which checked to see if HEAD's hash matched the abort-safety file. However, this check was skipped if the abort-safety file was empty, which can happen if git-am failed while on an unborn branch. As such, if any commits were made since then, they would be discarded. Fix this by carrying on the abort safety check even if the abort-safety file is empty. Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano --- t/t4151-am-abort.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 't/t4151-am-abort.sh') diff --git a/t/t4151-am-abort.sh b/t/t4151-am-abort.sh index dc6b3b0c7c..1274f75a60 100755 --- a/t/t4151-am-abort.sh +++ b/t/t4151-am-abort.sh @@ -142,4 +142,15 @@ test_expect_success 'am -3 --abort on unborn branch removes applied commits' ' test refs/heads/orphan = "$(git symbolic-ref HEAD)" ' +test_expect_success 'am --abort on unborn branch will keep local commits intact' ' + git checkout -f --orphan orphan && + git reset && + test_must_fail git am 0004-*.patch && + test_commit unrelated2 && + git rev-parse HEAD >expect && + git am --abort && + git rev-parse HEAD >actual && + test_cmp expect actual +' + test_done -- cgit v1.2.3