From 12abbeabb581be75ff058d31caa1fc9dc27634b2 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Mon, 14 Jan 2019 15:34:53 +0300 Subject: [PATCH] IDEA-196361 git: fix error on committing "path.txt -> path.txt/file.txt" rename --- .../checkin/GitCheckinEnvironment.java | 8 +-- .../tests/git4idea/test/GitTestAssertions.kt | 2 +- .../tests/git4idea/tests/GitCommitTest.kt | 63 +++++++++++++++++++ 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/plugins/git4idea/src/git4idea/checkin/GitCheckinEnvironment.java b/plugins/git4idea/src/git4idea/checkin/GitCheckinEnvironment.java index b8ebc8debbc9..285a9b41cc45 100644 --- a/plugins/git4idea/src/git4idea/checkin/GitCheckinEnvironment.java +++ b/plugins/git4idea/src/git4idea/checkin/GitCheckinEnvironment.java @@ -923,18 +923,18 @@ public class GitCheckinEnvironment implements CheckinEnvironment { final Collection removed, final List exceptions) { boolean rc = true; - if (!added.isEmpty()) { + if (!removed.isEmpty()) { try { - GitFileUtils.addPathsForce(project, root, added); + GitFileUtils.deletePaths(project, root, removed, "--ignore-unmatch", "--cached", "-r"); } catch (VcsException ex) { exceptions.add(ex); rc = false; } } - if (!removed.isEmpty()) { + if (!added.isEmpty()) { try { - GitFileUtils.deletePaths(project, root, removed, "--ignore-unmatch", "--cached"); + GitFileUtils.addPathsForce(project, root, added); } catch (VcsException ex) { exceptions.add(ex); diff --git a/plugins/git4idea/tests/git4idea/test/GitTestAssertions.kt b/plugins/git4idea/tests/git4idea/test/GitTestAssertions.kt index 35b6d4514994..41e7659405ba 100644 --- a/plugins/git4idea/tests/git4idea/test/GitTestAssertions.kt +++ b/plugins/git4idea/tests/git4idea/test/GitTestAssertions.kt @@ -183,7 +183,7 @@ class ChangesBuilder { fun rename(from: String, to: String) { PlatformTestCase.assertTrue(changes.add(AChange(FileStatus.MODIFIED, from, to) { - it.isRenamed && from == it.beforeRevision.relativePath && to == it.afterRevision.relativePath + (it.isRenamed || it.isMoved) && from == it.beforeRevision.relativePath && to == it.afterRevision.relativePath })) } } diff --git a/plugins/git4idea/tests/git4idea/tests/GitCommitTest.kt b/plugins/git4idea/tests/git4idea/tests/GitCommitTest.kt index 46ecf6bb8e17..e0d9abcb077c 100644 --- a/plugins/git4idea/tests/git4idea/tests/GitCommitTest.kt +++ b/plugins/git4idea/tests/git4idea/tests/GitCommitTest.kt @@ -823,6 +823,69 @@ abstract class GitCommitTest(private val useStagingArea: Boolean) : GitSingleRep } } + fun `test file to directory renames`() { + assumeTrue(Registry.`is`("git.force.commit.using.staging.area")) // known bug in "--only" implementation + + tac("a_path", "file content 1") + tac("b_path", "file content 2") + + rm("a_path") + rm("b_path") + touch("a_path/file1.txt", "file content 1") + touch("b_path/file2.txt", "file content 2") + git("add -A .") + + val changes = assertChanges { + rename("a_path", "a_path/file1.txt") + rename("b_path", "b_path/file2.txt") + } + + commit(listOf(changes[0])) + + assertChanges { + rename("b_path", "b_path/file2.txt") + } + repo.assertStagedChanges { + rename("b_path", "b_path/file2.txt") + } + assertMessage("comment", repo.message("HEAD")) + repo.assertCommitted { + rename("a_path", "a_path/file1.txt") + } + } + + fun `test directory to file renames`() { + tac("a_path/file1.txt", "file content 1") + tac("b_path/file2.txt", "file content 2") + + rm("a_path/file1.txt") + rm("b_path/file2.txt") + rm("a_path") + rm("b_path") + touch("a_path", "file content 1") + touch("b_path", "file content 2") + git("add -A .") + + val changes = assertChanges { + rename("a_path/file1.txt", "a_path") + rename("b_path/file2.txt", "b_path") + } + + commit(listOf(changes[0])) + + assertChanges { + rename("b_path/file2.txt", "b_path") + } + repo.assertStagedChanges { + rename("b_path/file2.txt", "b_path") + } + assertMessage("comment", repo.message("HEAD")) + repo.assertCommitted { + rename("a_path/file1.txt", "a_path") + } + } + + private fun `assume version where git reset returns 0 exit code on success `() { assumeTrue("Not testing: git reset returns 1 and fails the commit process in ${vcs.version}", vcs.version.isLaterOrEqual(GitVersion(1, 8, 2, 0)))