From f5076d8b041bc7d0e1497cacbec370237a5ccaf7 Mon Sep 17 00:00:00 2001 From: Christian Halstrick Date: Wed, 3 Nov 2010 13:32:54 +0100 Subject: [PATCH] Add a test for merging deleted files The JGit merge algorithm or the Merge Command may have problems with handling deletions always correctly. Therefore one additional test is added to check this. Change-Id: Id6aa49136996b29047c340994fe7faba68858e8c Signed-off-by: Christian Halstrick --- .../eclipse/jgit/api/MergeCommandTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java index 02d4b0e15..7efe7c948 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java @@ -313,6 +313,58 @@ public class MergeCommandTest extends RepositoryTestCase { assertEquals(RepositoryState.SAFE, db.getRepositoryState()); } + public void testSingleDeletion() throws Exception { + Git git = new Git(db); + + writeTrashFile("a", "1\na\n3\n"); + writeTrashFile("b", "1\nb\n3\n"); + writeTrashFile("d", "1\nd\n3\n"); + writeTrashFile("c/c/c", "1\nc\n3\n"); + git.add().addFilepattern("a").addFilepattern("b") + .addFilepattern("c/c/c").addFilepattern("d").call(); + RevCommit initialCommit = git.commit().setMessage("initial").call(); + + createBranch(initialCommit, "refs/heads/side"); + checkoutBranch("refs/heads/side"); + + assertTrue(new File(db.getWorkTree(), "b").delete()); + git.add().addFilepattern("b").setUpdate(true).call(); + RevCommit secondCommit = git.commit().setMessage("side").call(); + + assertFalse(new File(db.getWorkTree(), "b").exists()); + checkoutBranch("refs/heads/master"); + assertTrue(new File(db.getWorkTree(), "b").exists()); + + writeTrashFile("a", "1\na\n3(main)\n"); + writeTrashFile("c/c/c", "1\nc(main)\n3\n"); + git.add().addFilepattern("a").addFilepattern("c/c/c").call(); + RevCommit thirdCommit = git.commit().setMessage("main").call(); + + // We are merging a deletion into our branch + MergeResult result = git.merge().include(secondCommit.getId()) + .setStrategy(MergeStrategy.RESOLVE).call(); + assertEquals(MergeStatus.MERGED, result.getMergeStatus()); + + assertEquals("1\na\n3(main)\n", read(new File(db.getWorkTree(), "a"))); + assertFalse(new File(db.getWorkTree(), "b").exists()); + assertEquals("1\nc(main)\n3\n", + read(new File(db.getWorkTree(), "c/c/c"))); + + // Do the opposite, be on a branch where we have deleted a file and + // merge in a old commit where this file was not deleted + checkoutBranch("refs/heads/side"); + assertFalse(new File(db.getWorkTree(), "b").exists()); + + result = git.merge().include(thirdCommit.getId()) + .setStrategy(MergeStrategy.RESOLVE).call(); + assertEquals(MergeStatus.MERGED, result.getMergeStatus()); + + assertEquals("1\na\n3(main)\n", read(new File(db.getWorkTree(), "a"))); + assertFalse(new File(db.getWorkTree(), "b").exists()); + assertEquals("1\nc(main)\n3\n", + read(new File(db.getWorkTree(), "c/c/c"))); + } + public void testMergeFailingWithDirtyWorkingTree() throws Exception { Git git = new Git(db);