@Test
  public void testStageState_mergeAndReset_bug() throws Exception {
    try (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());
    }
  }
  private void verifyStageState(StageState expected, int... stages) throws IOException {
    DirCacheBuilder builder = db.lockDirCache().builder();
    for (int stage : stages) {
      DirCacheEntry entry = createEntry("a", FileMode.REGULAR_FILE, stage, "content");
      builder.add(entry);
    }
    builder.commit();

    IndexDiff diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
    diff.diff();

    assertEquals(
        "Conflict for entries in stages " + Arrays.toString(stages),
        expected,
        diff.getConflictingStageStates().get("a"));
  }
  @Test
  public void testStageState_simulated_bug() throws Exception {
    try (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 testConflicting() throws Exception {
    try (Git git = new Git(db)) {
      writeTrashFile("a", "1\na\n3\n");
      writeTrashFile("b", "1\nb\n3\n");
      git.add().addFilepattern("a").addFilepattern("b").call();
      RevCommit initialCommit = git.commit().setMessage("initial").call();

      // create side branch with two modifications
      createBranch(initialCommit, "refs/heads/side");
      checkoutBranch("refs/heads/side");
      writeTrashFile("a", "1\na(side)\n3\n");
      writeTrashFile("b", "1\nb\n3\n(side)");
      git.add().addFilepattern("a").addFilepattern("b").call();
      RevCommit secondCommit = git.commit().setMessage("side").call();

      // update a on master to generate conflict
      checkoutBranch("refs/heads/master");
      writeTrashFile("a", "1\na(main)\n3\n");
      git.add().addFilepattern("a").call();
      git.commit().setMessage("main").call();

      // merge side with master
      MergeResult result =
          git.merge().include(secondCommit.getId()).setStrategy(MergeStrategy.RESOLVE).call();
      assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
    }

    FileTreeIterator iterator = new FileTreeIterator(db);
    IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
    diff.diff();

    assertEquals("[b]", new TreeSet<String>(diff.getChanged()).toString());
    assertEquals("[]", diff.getAdded().toString());
    assertEquals("[]", diff.getRemoved().toString());
    assertEquals("[]", diff.getMissing().toString());
    assertEquals("[]", diff.getModified().toString());
    assertEquals("[a]", diff.getConflicting().toString());
    assertEquals(StageState.BOTH_MODIFIED, diff.getConflictingStageStates().get("a"));
    assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  }