Exemplo n.º 1
0
 /**
  * Write the entry records from memory to disk.
  *
  * <p>The cache must be locked first by calling {@link #lock()} and receiving true as the return
  * value. Applications are encouraged to lock the index, then invoke {@link #read()} to ensure the
  * in-memory data is current, prior to updating the in-memory entries.
  *
  * <p>Once written the lock is closed and must be either committed with {@link #commit()} or
  * rolled back with {@link #unlock()}.
  *
  * @throws IOException the output file could not be created. The caller no longer holds the lock.
  */
 public void write() throws IOException {
   final LockFile tmp = myLock;
   requireLocked(tmp);
   try {
     writeTo(new SafeBufferedOutputStream(tmp.getOutputStream()));
   } catch (IOException err) {
     tmp.unlock();
     throw err;
   } catch (RuntimeException err) {
     tmp.unlock();
     throw err;
   } catch (Error err) {
     tmp.unlock();
     throw err;
   }
 }
  @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);
  }