@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()); }
@NotNull private static ObjectId createFirstRevision(@NotNull Repository repository) throws IOException { final ObjectInserter inserter = repository.newObjectInserter(); // Create commit tree. final TreeFormatter rootBuilder = new TreeFormatter(); rootBuilder.append( ".gitattributes", FileMode.REGULAR_FILE, insertFile(inserter, "example/_gitattributes")); new ObjectChecker().checkTree(rootBuilder.toByteArray()); final ObjectId rootId = inserter.insert(rootBuilder); // Create first commit with message. final CommitBuilder commitBuilder = new CommitBuilder(); commitBuilder.setAuthor(new PersonIdent("", "", 0, 0)); commitBuilder.setCommitter(new PersonIdent("", "", 0, 0)); commitBuilder.setMessage("Initial commit"); commitBuilder.setTreeId(rootId); final ObjectId commitId = inserter.insert(commitBuilder); inserter.flush(); return commitId; }
@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()); }
@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()); }
/** * Insert a single tree into the store, returning its unique name. * * @param formatter the formatter containing the proposed tree's data. * @return the name of the tree object. * @throws IOException the object could not be stored. */ public final ObjectId insert(TreeFormatter formatter) throws IOException { // Delegate to the formatter, as then it can pass the raw internal // buffer back to this inserter, avoiding unnecessary data copying. // return formatter.insertTo(this); }
/** * Compute the ObjectId for the given tree without inserting it. * * @param formatter * @return the computed ObjectId */ public ObjectId idFor(TreeFormatter formatter) { return formatter.computeId(this); }
/** * 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()); }