Exemple #1
0
  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();
  }