Esempio n. 1
0
  @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());
    }
  }
Esempio n. 2
0
  @Test
  public void testRemoved() throws IOException {
    writeTrashFile("file2", "file2");
    writeTrashFile("dir/file3", "dir/file3");

    TreeFormatter dir = new TreeFormatter();
    dir.append(
        "file3",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("873fb8d667d05436d728c52b1d7a09528e6eb59b"));

    TreeFormatter tree = new TreeFormatter();
    tree.append(
        "file2",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("30d67d4672d5c05833b7192cc77a79eaafb5c7ad"));
    tree.append("dir", FileMode.TREE, insertTree(dir));
    ObjectId treeId = insertTree(tree);

    FileTreeIterator iterator = new FileTreeIterator(db);
    IndexDiff diff = new IndexDiff(db, treeId, iterator);
    diff.diff();
    assertEquals(2, diff.getRemoved().size());
    assertTrue(diff.getRemoved().contains("file2"));
    assertTrue(diff.getRemoved().contains("dir/file3"));
    assertEquals(0, diff.getChanged().size());
    assertEquals(0, diff.getModified().size());
    assertEquals(0, diff.getAdded().size());
    assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  }
Esempio n. 3
0
  @Test
  public void testAssumeUnchanged() throws Exception {
    try (Git git = new Git(db)) {
      String path = "file";
      writeTrashFile(path, "content");
      git.add().addFilepattern(path).call();
      String path2 = "file2";
      writeTrashFile(path2, "content");
      String path3 = "file3";
      writeTrashFile(path3, "some content");
      git.add().addFilepattern(path2).addFilepattern(path3).call();
      git.commit().setMessage("commit").call();
      assumeUnchanged(path2);
      assumeUnchanged(path3);
      writeTrashFile(path, "more content");
      deleteTrashFile(path3);

      FileTreeIterator iterator = new FileTreeIterator(db);
      IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
      diff.diff();
      assertEquals(2, diff.getAssumeUnchanged().size());
      assertEquals(1, diff.getModified().size());
      assertEquals(0, diff.getChanged().size());
      assertTrue(diff.getAssumeUnchanged().contains("file2"));
      assertTrue(diff.getAssumeUnchanged().contains("file3"));
      assertTrue(diff.getModified().contains("file"));

      git.add().addFilepattern(".").call();

      iterator = new FileTreeIterator(db);
      diff = new IndexDiff(db, Constants.HEAD, iterator);
      diff.diff();
      assertEquals(2, diff.getAssumeUnchanged().size());
      assertEquals(0, diff.getModified().size());
      assertEquals(1, diff.getChanged().size());
      assertTrue(diff.getAssumeUnchanged().contains("file2"));
      assertTrue(diff.getAssumeUnchanged().contains("file3"));
      assertTrue(diff.getChanged().contains("file"));
      assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
    }
  }
Esempio n. 4
0
  @Test
  public void testModified() throws IOException, GitAPIException {

    writeTrashFile("file2", "file2");
    writeTrashFile("dir/file3", "dir/file3");

    try (Git git = new Git(db)) {
      git.add().addFilepattern("file2").addFilepattern("dir/file3").call();
    }

    writeTrashFile("dir/file3", "changed");

    TreeFormatter dir = new TreeFormatter();
    dir.append(
        "file3",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("0123456789012345678901234567890123456789"));

    TreeFormatter tree = new TreeFormatter();
    tree.append("dir", FileMode.TREE, insertTree(dir));
    tree.append(
        "file2",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("0123456789012345678901234567890123456789"));
    ObjectId treeId = insertTree(tree);

    FileTreeIterator iterator = new FileTreeIterator(db);
    IndexDiff diff = new IndexDiff(db, treeId, iterator);
    diff.diff();
    assertEquals(2, diff.getChanged().size());
    assertTrue(diff.getChanged().contains("file2"));
    assertTrue(diff.getChanged().contains("dir/file3"));
    assertEquals(1, diff.getModified().size());
    assertTrue(diff.getModified().contains("dir/file3"));
    assertEquals(0, diff.getAdded().size());
    assertEquals(0, diff.getRemoved().size());
    assertEquals(0, diff.getMissing().size());
    assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  }
Esempio n. 5
0
  @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());
    }
  }
Esempio n. 6
0
  @Test
  public void testUnchangedSimple() throws IOException, GitAPIException {
    writeTrashFile("a.b", "a.b");
    writeTrashFile("a.c", "a.c");
    writeTrashFile("a=c", "a=c");
    writeTrashFile("a=d", "a=d");
    try (Git git = new Git(db)) {
      git.add().addFilepattern("a.b").call();
      git.add().addFilepattern("a.c").call();
      git.add().addFilepattern("a=c").call();
      git.add().addFilepattern("a=d").call();
    }

    TreeFormatter tree = new TreeFormatter();
    // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
    tree.append(
        "a.b",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
    tree.append(
        "a.c",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
    tree.append(
        "a=c",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
    tree.append(
        "a=d",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
    ObjectId treeId = insertTree(tree);

    FileTreeIterator iterator = new FileTreeIterator(db);
    IndexDiff diff = new IndexDiff(db, treeId, iterator);
    diff.diff();
    assertEquals(0, diff.getChanged().size());
    assertEquals(0, diff.getAdded().size());
    assertEquals(0, diff.getRemoved().size());
    assertEquals(0, diff.getMissing().size());
    assertEquals(0, diff.getModified().size());
    assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  }
Esempio n. 7
0
  @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());
  }
Esempio n. 8
0
  @Test
  public void testAdded() throws IOException {
    writeTrashFile("file1", "file1");
    writeTrashFile("dir/subfile", "dir/subfile");
    ObjectId tree = insertTree(new TreeFormatter());

    DirCache index = db.lockDirCache();
    DirCacheEditor editor = index.editor();
    editor.add(add(db, trash, "file1"));
    editor.add(add(db, trash, "dir/subfile"));
    editor.commit();
    FileTreeIterator iterator = new FileTreeIterator(db);
    IndexDiff diff = new IndexDiff(db, tree, iterator);
    diff.diff();
    assertEquals(2, diff.getAdded().size());
    assertTrue(diff.getAdded().contains("file1"));
    assertTrue(diff.getAdded().contains("dir/subfile"));
    assertEquals(0, diff.getChanged().size());
    assertEquals(0, diff.getModified().size());
    assertEquals(0, diff.getRemoved().size());
    assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  }
Esempio n. 9
0
  /**
   * This test has both files and directories that involve the tricky ordering used by Git.
   *
   * @throws IOException
   * @throws GitAPIException
   */
  @Test
  public void testUnchangedComplex() throws IOException, GitAPIException {
    writeTrashFile("a.b", "a.b");
    writeTrashFile("a.c", "a.c");
    writeTrashFile("a/b.b/b", "a/b.b/b");
    writeTrashFile("a/b", "a/b");
    writeTrashFile("a/c", "a/c");
    writeTrashFile("a=c", "a=c");
    writeTrashFile("a=d", "a=d");
    try (Git git = new Git(db)) {
      git.add()
          .addFilepattern("a.b")
          .addFilepattern("a.c")
          .addFilepattern("a/b.b/b")
          .addFilepattern("a/b")
          .addFilepattern("a/c")
          .addFilepattern("a=c")
          .addFilepattern("a=d")
          .call();
    }

    // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
    TreeFormatter bb = new TreeFormatter();
    bb.append(
        "b",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("8d840bd4e2f3a48ff417c8e927d94996849933fd"));

    TreeFormatter a = new TreeFormatter();
    a.append(
        "b",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("db89c972fc57862eae378f45b74aca228037d415"));
    a.append("b.b", FileMode.TREE, insertTree(bb));
    a.append(
        "c",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("52ad142a008aeb39694bafff8e8f1be75ed7f007"));

    TreeFormatter tree = new TreeFormatter();
    tree.append(
        "a.b",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
    tree.append(
        "a.c",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
    tree.append("a", FileMode.TREE, insertTree(a));
    tree.append(
        "a=c",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
    tree.append(
        "a=d",
        FileMode.REGULAR_FILE,
        ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
    ObjectId treeId = insertTree(tree);

    FileTreeIterator iterator = new FileTreeIterator(db);
    IndexDiff diff = new IndexDiff(db, treeId, iterator);
    diff.diff();
    assertEquals(0, diff.getChanged().size());
    assertEquals(0, diff.getAdded().size());
    assertEquals(0, diff.getRemoved().size());
    assertEquals(0, diff.getMissing().size());
    assertEquals(0, diff.getModified().size());
    assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  }