Example #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();
  }
Example #2
0
 private static byte[] readFully(final InputStream is) throws IOException {
   final TemporaryBuffer b = new TemporaryBuffer.LocalFile();
   try {
     b.copy(is);
     b.close();
     return b.toByteArray();
   } finally {
     b.destroy();
   }
 }
Example #3
0
  private String[] extractFileLines(final Charset[] csGuess) {
    final TemporaryBuffer[] tmp = new TemporaryBuffer[getParentCount() + 1];
    try {
      for (int i = 0; i < tmp.length; i++) tmp[i] = new TemporaryBuffer.LocalFile();
      for (final HunkHeader h : getHunks()) h.extractFileLines(tmp);

      final String[] r = new String[tmp.length];
      for (int i = 0; i < tmp.length; i++) {
        Charset cs = csGuess != null ? csGuess[i] : null;
        if (cs == null) cs = Constants.CHARSET;
        r[i] = RawParseUtils.decode(cs, tmp[i].toByteArray());
      }
      return r;
    } catch (IOException ioe) {
      throw new RuntimeException(JGitText.get().cannotConvertScriptToText, ioe);
    } finally {
      for (final TemporaryBuffer b : tmp) {
        if (b != null) b.destroy();
      }
    }
  }