/** * Obtain (or build) the current cache tree structure. * * <p>This method can optionally recreate the cache tree, without flushing the tree objects * themselves to disk. * * @param build if true and the cache tree is not present in the index it will be generated and * returned to the caller. * @return the cache tree; null if there is no current cache tree available and <code>build</code> * was false. */ public DirCacheTree getCacheTree(final boolean build) { if (build) { if (tree == null) tree = new DirCacheTree(); tree.validate(sortedEntries, entryCnt, 0, 0); } return tree; }
void writeTo(final OutputStream os) throws IOException { final MessageDigest foot = Constants.newMessageDigest(); final DigestOutputStream dos = new DigestOutputStream(os, foot); boolean extended = false; for (int i = 0; i < entryCnt; i++) extended |= sortedEntries[i].isExtended(); // Write the header. // final byte[] tmp = new byte[128]; System.arraycopy(SIG_DIRC, 0, tmp, 0, SIG_DIRC.length); NB.encodeInt32(tmp, 4, extended ? 3 : 2); NB.encodeInt32(tmp, 8, entryCnt); dos.write(tmp, 0, 12); // Write the individual file entries. final int smudge_s; final int smudge_ns; if (myLock != null) { // For new files we need to smudge the index entry // if they have been modified "now". Ideally we'd // want the timestamp when we're done writing the index, // so we use the current timestamp as a approximation. myLock.createCommitSnapshot(); snapshot = myLock.getCommitSnapshot(); smudge_s = (int) (snapshot.lastModified() / 1000); smudge_ns = ((int) (snapshot.lastModified() % 1000)) * 1000000; } else { // Used in unit tests only smudge_ns = 0; smudge_s = 0; } // Check if tree is non-null here since calling updateSmudgedEntries // will automatically build it via creating a DirCacheIterator final boolean writeTree = tree != null; if (repository != null && entryCnt > 0) updateSmudgedEntries(); for (int i = 0; i < entryCnt; i++) { final DirCacheEntry e = sortedEntries[i]; if (e.mightBeRacilyClean(smudge_s, smudge_ns)) e.smudgeRacilyClean(); e.write(dos); } if (writeTree) { final TemporaryBuffer bb = new TemporaryBuffer.LocalFile(); tree.write(tmp, bb); bb.close(); NB.encodeInt32(tmp, 0, EXT_TREE); NB.encodeInt32(tmp, 4, (int) bb.length()); dos.write(tmp, 0, 8); bb.writeTo(dos, null); } writeIndexChecksum = foot.digest(); os.write(writeIndexChecksum); os.close(); }
int nextEntry(final byte[] p, final int pLen, int nextIdx) { while (nextIdx < entryCnt) { final DirCacheEntry next = sortedEntries[nextIdx]; if (!DirCacheTree.peq(p, next.path, pLen)) break; nextIdx++; } return nextIdx; }
@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()); } }