Beispiel #1
0
  @Test
  public void testPathsReset() throws Exception {
    setupRepository();

    DirCacheEntry preReset =
        DirCache.read(db.getIndexFile(), db.getFS()).getEntry(indexFile.getName());
    assertNotNull(preReset);

    git.add().addFilepattern(untrackedFile.getName()).call();

    // 'a.txt' has already been modified in setupRepository
    // 'notAddedToIndex.txt' has been added to repository
    git.reset().addPath(indexFile.getName()).addPath(untrackedFile.getName()).call();

    DirCacheEntry postReset =
        DirCache.read(db.getIndexFile(), db.getFS()).getEntry(indexFile.getName());
    assertNotNull(postReset);
    Assert.assertNotSame(preReset.getObjectId(), postReset.getObjectId());
    Assert.assertEquals(prestage.getObjectId(), postReset.getObjectId());

    // check that HEAD hasn't moved
    ObjectId head = db.resolve(Constants.HEAD);
    assertEquals(secondCommit, head);
    // check if files still exist
    assertTrue(untrackedFile.exists());
    assertTrue(indexFile.exists());
    assertTrue(inHead(indexFile.getName()));
    assertTrue(inIndex(indexFile.getName()));
    assertFalse(inIndex(untrackedFile.getName()));
  }
  @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 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 testCorruptChecksumAtFooter() throws Exception {
   final DirCache dc = new DirCache(pathOf("gitgit.index.badchecksum"), FS.DETECTED);
   try {
     dc.read();
     fail("Cache loaded despite corrupt checksum");
   } catch (CorruptObjectException err) {
     assertEquals("DIRC checksum mismatch", err.getMessage());
   }
 }
 @Test
 public void testUnsupportedRequiredExtension() throws Exception {
   final DirCache dc = new DirCache(pathOf("gitgit.index.aaaa"), FS.DETECTED);
   try {
     dc.read();
     fail("Cache loaded an unsupported extension");
   } catch (CorruptObjectException err) {
     assertEquals("DIRC extension 'aaaa'" + " not supported by this version.", err.getMessage());
   }
 }
 @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));
   }
 }
Beispiel #8
0
  @Test
  public void testHardResetOnTag() throws Exception {
    setupRepository();
    String tagName = "initialtag";
    git.tag().setName(tagName).setObjectId(secondCommit).setMessage("message").call();

    DirCacheEntry preReset =
        DirCache.read(db.getIndexFile(), db.getFS()).getEntry(indexFile.getName());
    assertNotNull(preReset);

    git.add().addFilepattern(untrackedFile.getName()).call();

    git.reset().setRef(tagName).setMode(HARD).call();

    ObjectId head = db.resolve(Constants.HEAD);
    assertEquals(secondCommit, head);
  }
Beispiel #9
0
  public void setupRepository() throws IOException, JGitInternalException, GitAPIException {

    // create initial commit
    git = new Git(db);
    initialCommit = git.commit().setMessage("initial commit").call();

    // create nested file
    File dir = resolve(db.getWorkTree(), "dir");
    FileUtils.mkdir(dir);
    File nestedFile = resolve(dir, "b.txt");
    FileUtils.createNewFile(nestedFile);

    PrintWriter nesterFileWriter = getPrintWriter(nestedFile);
    nesterFileWriter.print("content");
    nesterFileWriter.flush();

    // create file
    indexFile = resolve(db.getWorkTree(), "a.txt");
    FileUtils.createNewFile(indexFile);
    PrintWriter writer = getPrintWriter(indexFile);
    writer.print("content");
    writer.flush();

    // add file and commit it
    git.add().addFilepattern("dir").addFilepattern("a.txt").call();
    secondCommit = git.commit().setMessage("adding a.txt and dir/b.txt").call();

    prestage = DirCache.read(db.getIndexFile(), db.getFS()).getEntry(indexFile.getName());

    // modify file and add to index
    writer.print("new content");
    writer.close();
    nesterFileWriter.print("new content");
    nesterFileWriter.close();
    git.add().addFilepattern("a.txt").addFilepattern("dir").call();

    // create a file not added to the index
    untrackedFile = resolve(db.getWorkTree(), "notAddedToIndex.txt");
    FileUtils.createNewFile(untrackedFile);
    PrintWriter writer2 = getPrintWriter(untrackedFile);
    writer2.print("content");
    writer2.close();
  }
Beispiel #10
0
  /**
   * Create a new in-core index representation, lock it, and read from disk.
   *
   * <p>The new index will be locked and then read before it is returned to the caller. Read
   * failures are reported as exceptions and therefore prevent the method from returning a partially
   * populated index. On read failure, the lock is released.
   *
   * @param indexLocation location of the index file on disk.
   * @param fs the file system abstraction which will be necessary to perform certain file system
   *     operations.
   * @return a cache representing the contents of the specified index file (if it exists) or an
   *     empty cache if the file does not exist.
   * @throws IOException the index file is present but could not be read, or the lock could not be
   *     obtained.
   * @throws CorruptObjectException the index file is using a format or extension that this library
   *     does not support.
   */
  public static DirCache lock(final File indexLocation, final FS fs)
      throws CorruptObjectException, IOException {
    final DirCache c = new DirCache(indexLocation, fs);
    if (!c.lock()) throw new LockFailedException(indexLocation);

    try {
      c.read();
    } catch (IOException e) {
      c.unlock();
      throw e;
    } catch (RuntimeException e) {
      c.unlock();
      throw e;
    } catch (Error e) {
      c.unlock();
      throw e;
    }

    return c;
  }
  @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);
  }
  @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());
      }
    }
  }
Beispiel #13
0
  public int getDirCacheEntryLength(String path) throws IOException {
    String repoPath = getRepoRelativePath(path);
    DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS());

    return dc.getEntry(repoPath).getLength();
  }
Beispiel #14
0
  public long lastModifiedInIndex(String path) throws IOException {
    String repoPath = getRepoRelativePath(path);
    DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS());

    return dc.getEntry(repoPath).getLastModified();
  }
Beispiel #15
0
  public boolean inIndex(String path) throws IOException {
    String repoPath = getRepoRelativePath(path);
    DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS());

    return dc.getEntry(repoPath) != null;
  }
 private boolean isInIndex(Repository db, String path) throws IOException {
   DirCache dc = DirCache.read(db.getIndexFile(), db.getFS());
   return dc.getEntry(path) != null;
 }
Beispiel #17
0
 /**
  * Create a new in-core index representation and read an index from disk.
  *
  * <p>The new index will be read before it is returned to the caller. Read failures are reported
  * as exceptions and therefore prevent the method from returning a partially populated index.
  *
  * @param indexLocation location of the index file on disk.
  * @param fs the file system abstraction which will be necessary to perform certain file system
  *     operations.
  * @return a cache representing the contents of the specified index file (if it exists) or an
  *     empty cache if the file does not exist.
  * @throws IOException the index file is present but could not be read.
  * @throws CorruptObjectException the index file is using a format or extension that this library
  *     does not support.
  */
 public static DirCache read(final File indexLocation, final FS fs)
     throws CorruptObjectException, IOException {
   final DirCache c = new DirCache(indexLocation, fs);
   c.read();
   return c;
 }