From 1fd150d565c10da901784071c2d82319a3fd8be4 Mon Sep 17 00:00:00 2001 From: Laurent Goubet Date: Mon, 28 Jul 2014 14:27:05 +0200 Subject: [PATCH] Add IndexDiff tests for merge conflict state BOTH_ADDED JGit handled this case improperly which these tests demonstrate. Fixed by I25915880f304090fe90584c79bddf021231227a2. Bug: 440537 Change-Id: Ia29c1d6cf8c0ce724cc3ff5ed9e0b396949b44bf Signed-off-by: Laurent Goubet Signed-off-by: Matthias Sohn --- .../org/eclipse/jgit/lib/IndexDiffTest.java | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java index a1e16e4cf..a85198f01 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java @@ -578,6 +578,122 @@ public class IndexDiffTest extends RepositoryTestCase { assertTrue(StageState.BOTH_ADDED.hasTheirs()); } + @Test + public void testStageState_mergeAndReset_bug() throws Exception { + Git git = new Git(db); + + writeTrashFile("a", "content"); + git.add().addFilepattern("a").call(); + RevCommit initialCommit = git.commit().setMessage("initial commit") + .call(); + + // create branch and add a new file + final String branchName = Constants.R_HEADS + "branch"; + createBranch(initialCommit, branchName); + checkoutBranch(branchName); + writeTrashFile("b", "second file content - branch"); + git.add().addFilepattern("b").call(); + RevCommit branchCommit = git.commit().setMessage("branch commit") + .call(); + + // checkout master and add the same new file + checkoutBranch(Constants.R_HEADS + Constants.MASTER); + writeTrashFile("b", "second file content - master"); + git.add().addFilepattern("b").call(); + git.commit().setMessage("master commit").call(); + + // try and merge + MergeResult result = git.merge().include(branchCommit).call(); + assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); + + FileTreeIterator iterator = new FileTreeIterator(db); + IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator); + diff.diff(); + + assertTrue(diff.getChanged().isEmpty()); + assertTrue(diff.getAdded().isEmpty()); + assertTrue(diff.getRemoved().isEmpty()); + assertTrue(diff.getMissing().isEmpty()); + assertTrue(diff.getModified().isEmpty()); + assertEquals(1, diff.getConflicting().size()); + assertTrue(diff.getConflicting().contains("b")); + assertEquals(StageState.BOTH_ADDED, diff.getConflictingStageStates() + .get("b")); + assertTrue(diff.getUntrackedFolders().isEmpty()); + + // reset file b to its master state without altering the index + writeTrashFile("b", "second file content - master"); + + // we should have the same result + iterator = new FileTreeIterator(db); + diff = new IndexDiff(db, Constants.HEAD, iterator); + diff.diff(); + + assertTrue(diff.getChanged().isEmpty()); + assertTrue(diff.getAdded().isEmpty()); + assertTrue(diff.getRemoved().isEmpty()); + assertTrue(diff.getMissing().isEmpty()); + assertTrue(diff.getModified().isEmpty()); + assertEquals(1, diff.getConflicting().size()); + assertTrue(diff.getConflicting().contains("b")); + assertEquals(StageState.BOTH_ADDED, diff.getConflictingStageStates() + .get("b")); + assertTrue(diff.getUntrackedFolders().isEmpty()); + } + + @Test + public void testStageState_simulated_bug() throws Exception { + Git git = new Git(db); + + writeTrashFile("a", "content"); + git.add().addFilepattern("a").call(); + RevCommit initialCommit = git.commit().setMessage("initial commit") + .call(); + + // create branch and add a new file + final String branchName = Constants.R_HEADS + "branch"; + createBranch(initialCommit, branchName); + checkoutBranch(branchName); + writeTrashFile("b", "second file content - branch"); + git.add().addFilepattern("b").call(); + git.commit().setMessage("branch commit") + .call(); + + // checkout master and add the same new file + checkoutBranch(Constants.R_HEADS + Constants.MASTER); + writeTrashFile("b", "second file content - master"); + git.add().addFilepattern("b").call(); + git.commit().setMessage("master commit").call(); + + // Simulate a failed merge of branch into master + DirCacheBuilder builder = db.lockDirCache().builder(); + DirCacheEntry entry = createEntry("a", FileMode.REGULAR_FILE, 0, + "content"); + builder.add(entry); + entry = createEntry("b", FileMode.REGULAR_FILE, 2, + "second file content - master"); + builder.add(entry); + entry = createEntry("b", FileMode.REGULAR_FILE, 3, + "second file content - branch"); + builder.add(entry); + builder.commit(); + + FileTreeIterator iterator = new FileTreeIterator(db); + IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator); + diff.diff(); + + assertTrue(diff.getChanged().isEmpty()); + assertTrue(diff.getAdded().isEmpty()); + assertTrue(diff.getRemoved().isEmpty()); + assertTrue(diff.getMissing().isEmpty()); + assertTrue(diff.getModified().isEmpty()); + assertEquals(1, diff.getConflicting().size()); + assertTrue(diff.getConflicting().contains("b")); + assertEquals(StageState.BOTH_ADDED, diff.getConflictingStageStates() + .get("b")); + assertTrue(diff.getUntrackedFolders().isEmpty()); + } + @Test public void testAutoCRLFInput() throws Exception { Git git = new Git(db);