@Test public void testReadIndex_DirCacheTree() throws Exception { final Map<String, CGitIndexRecord> cList = readLsFiles(); final Map<String, CGitLsTreeRecord> cTree = readLsTree(); final DirCache dc = new DirCache(index, FS.DETECTED); assertEquals(0, dc.getEntryCount()); dc.read(); assertEquals(cList.size(), dc.getEntryCount()); final DirCacheTree jTree = dc.getCacheTree(false); assertNotNull(jTree); assertEquals("", jTree.getNameString()); assertEquals("", jTree.getPathString()); assertTrue(jTree.isValid()); assertEquals( ObjectId.fromString("698dd0b8d0c299f080559a1cffc7fe029479a408"), jTree.getObjectId()); assertEquals(cList.size(), jTree.getEntrySpan()); final ArrayList<CGitLsTreeRecord> subtrees = new ArrayList<CGitLsTreeRecord>(); for (final CGitLsTreeRecord r : cTree.values()) { if (FileMode.TREE.equals(r.mode)) subtrees.add(r); } assertEquals(subtrees.size(), jTree.getChildCount()); for (int i = 0; i < jTree.getChildCount(); i++) { final DirCacheTree sj = jTree.getChild(i); final CGitLsTreeRecord sc = subtrees.get(i); assertEquals(sc.path, sj.getNameString()); assertEquals(sc.path + "/", sj.getPathString()); assertTrue(sj.isValid()); assertEquals(sc.id, sj.getObjectId()); } }
@Test public void testReadIndex_LsFiles() throws Exception { final Map<String, CGitIndexRecord> ls = readLsFiles(); final DirCache dc = new DirCache(index, FS.DETECTED); assertEquals(0, dc.getEntryCount()); dc.read(); assertEquals(ls.size(), dc.getEntryCount()); { final Iterator<CGitIndexRecord> rItr = ls.values().iterator(); for (int i = 0; rItr.hasNext(); i++) assertEqual(rItr.next(), dc.getEntry(i)); } }
public void testNoPostOrder() throws Exception { final DirCache tree = db.readDirCache(); { final DirCacheBuilder b = tree.builder(); b.add(makeFile("a")); b.add(makeFile("b/c")); b.add(makeFile("b/d")); b.add(makeFile("q")); b.finish(); assertEquals(4, tree.getEntryCount()); } final TreeWalk tw = new TreeWalk(db); tw.reset(); tw.setPostOrderTraversal(false); tw.addTree(new DirCacheIterator(tree)); assertModes("a", REGULAR_FILE, tw); assertModes("b", TREE, tw); assertTrue(tw.isSubtree()); assertFalse(tw.isPostChildren()); tw.enterSubtree(); assertModes("b/c", REGULAR_FILE, tw); assertModes("b/d", REGULAR_FILE, tw); assertModes("q", REGULAR_FILE, tw); }
@Test public void testUnsupportedOptionalExtension() throws Exception { final DirCache dc = new DirCache(pathOf("gitgit.index.ZZZZ"), FS.DETECTED); dc.read(); assertEquals(1, dc.getEntryCount()); assertEquals("A", dc.getEntry(0).getPathString()); }
@Test public void testReadMissing_TempIndex() throws Exception { final File idx = new File(db.getDirectory(), "tmp_index"); assertFalse(idx.exists()); final DirCache dc = DirCache.read(idx, db.getFS()); assertNotNull(dc); assertEquals(0, dc.getEntryCount()); }
@Test public void testBuildThenClear() throws Exception { final DirCache dc = db.readDirCache(); final String[] paths = {"a-", "a.b", "a/b", "a0b"}; final DirCacheEntry[] ents = new DirCacheEntry[paths.length]; for (int i = 0; i < paths.length; i++) { ents[i] = new DirCacheEntry(paths[i]); ents[i].setFileMode(FileMode.REGULAR_FILE); } final DirCacheBuilder b = dc.builder(); for (int i = 0; i < ents.length; i++) b.add(ents[i]); b.finish(); assertFalse(dc.hasUnmergedPaths()); assertEquals(paths.length, dc.getEntryCount()); dc.clear(); assertEquals(0, dc.getEntryCount()); assertFalse(dc.hasUnmergedPaths()); }
@Test public void testTreeWalk_LsFiles() throws Exception { final Repository db = createBareRepository(); final Map<String, CGitIndexRecord> ls = readLsFiles(); final DirCache dc = new DirCache(index, db.getFS()); assertEquals(0, dc.getEntryCount()); dc.read(); assertEquals(ls.size(), dc.getEntryCount()); { final Iterator<CGitIndexRecord> rItr = ls.values().iterator(); final TreeWalk tw = new TreeWalk(db); tw.setRecursive(true); tw.addTree(new DirCacheIterator(dc)); while (rItr.hasNext()) { final DirCacheIterator dcItr; assertTrue(tw.next()); dcItr = tw.getTree(0, DirCacheIterator.class); assertNotNull(dcItr); assertEqual(rItr.next(), dcItr.getDirCacheEntry()); } } }
@Test public void testEntriesWithin() throws Exception { final DirCache dc = db.readDirCache(); final String[] paths = {"a.", "a/b", "a/c", "a/d", "a0b"}; final DirCacheEntry[] ents = new DirCacheEntry[paths.length]; for (int i = 0; i < paths.length; i++) { ents[i] = new DirCacheEntry(paths[i]); ents[i].setFileMode(FileMode.REGULAR_FILE); } final int aFirst = 1; final int aLast = 3; final DirCacheBuilder b = dc.builder(); for (int i = 0; i < ents.length; i++) b.add(ents[i]); b.finish(); assertEquals(paths.length, dc.getEntryCount()); for (int i = 0; i < ents.length; i++) assertSame(ents[i], dc.getEntry(i)); { final DirCacheEntry[] aContents = dc.getEntriesWithin("a"); assertNotNull(aContents); assertEquals(aLast - aFirst + 1, aContents.length); for (int i = aFirst, j = 0; i <= aLast; i++, j++) assertSame(ents[i], aContents[j]); } { final DirCacheEntry[] aContents = dc.getEntriesWithin("a/"); assertNotNull(aContents); assertEquals(aLast - aFirst + 1, aContents.length); for (int i = aFirst, j = 0; i <= aLast; i++, j++) assertSame(ents[i], aContents[j]); } { final DirCacheEntry[] aContents = dc.getEntriesWithin(""); assertNotNull(aContents); assertEquals(ents.length, aContents.length); for (int i = 0; i < ents.length; i++) assertSame(ents[i], aContents[i]); } assertNotNull(dc.getEntriesWithin("a.")); assertEquals(0, dc.getEntriesWithin("a.").length); assertNotNull(dc.getEntriesWithin("a0b")); assertEquals(0, dc.getEntriesWithin("a0b.").length); assertNotNull(dc.getEntriesWithin("zoo")); assertEquals(0, dc.getEntriesWithin("zoo.").length); }
@Test public void testLockMissing_RealIndex() throws Exception { final File idx = new File(db.getDirectory(), "index"); final File lck = new File(db.getDirectory(), "index.lock"); assertFalse(idx.exists()); assertFalse(lck.exists()); final DirCache dc = db.lockDirCache(); assertNotNull(dc); assertFalse(idx.exists()); assertTrue(lck.exists()); assertEquals(0, dc.getEntryCount()); dc.unlock(); assertFalse(idx.exists()); assertFalse(lck.exists()); }
@Test public void testWriteEmptyReadEmpty_RealIndex() throws Exception { final File idx = new File(db.getDirectory(), "index"); final File lck = new File(db.getDirectory(), "index.lock"); assertFalse(idx.exists()); assertFalse(lck.exists()); { final DirCache dc = db.lockDirCache(); dc.write(); assertTrue(dc.commit()); assertTrue(idx.exists()); } { final DirCache dc = db.readDirCache(); assertEquals(0, dc.getEntryCount()); } }
@Test public void testReadWriteV3() throws Exception { final File file = pathOf("gitgit.index.v3"); final DirCache dc = new DirCache(file, FS.DETECTED); dc.read(); assertEquals(10, dc.getEntryCount()); assertV3TreeEntry(0, "dir1/file1.txt", false, false, dc); assertV3TreeEntry(1, "dir2/file2.txt", true, false, dc); assertV3TreeEntry(2, "dir3/file3.txt", false, false, dc); assertV3TreeEntry(3, "dir3/file3a.txt", true, false, dc); assertV3TreeEntry(4, "dir4/file4.txt", true, false, dc); assertV3TreeEntry(5, "dir4/file4a.txt", false, false, dc); assertV3TreeEntry(6, "file.txt", true, false, dc); assertV3TreeEntry(7, "newdir1/newfile1.txt", false, true, dc); assertV3TreeEntry(8, "newdir1/newfile2.txt", false, true, dc); assertV3TreeEntry(9, "newfile.txt", false, true, dc); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); dc.writeTo(bos); final byte[] indexBytes = bos.toByteArray(); final byte[] expectedBytes = IO.readFully(file); assertArrayEquals(expectedBytes, indexBytes); }