Package org.eclipse.jgit.dircache

Examples of org.eclipse.jgit.dircache.DirCacheBuilder


  @Test
  public void testNoPostOrder() throws Exception {
    final DirCache tree = db.readDirCache();
    {
      final DirCacheBuilder b = tree.builder();

      b.add(makeFile("a"));
      b.add(makeFile("b/c"));
      b.add(makeFile("b/d"));
      b.add(makeFile("q"));

      b.finish();
      assertEquals(4, tree.getEntryCount());
    }

    final TreeWalk tw = new TreeWalk(db);
    tw.setPostOrderTraversal(false);
View Full Code Here


  @Test
  public void testWithPostOrder_EnterSubtree() throws Exception {
    final DirCache tree = db.readDirCache();
    {
      final DirCacheBuilder b = tree.builder();

      b.add(makeFile("a"));
      b.add(makeFile("b/c"));
      b.add(makeFile("b/d"));
      b.add(makeFile("q"));

      b.finish();
      assertEquals(4, tree.getEntryCount());
    }

    final TreeWalk tw = new TreeWalk(db);
    tw.setPostOrderTraversal(true);
View Full Code Here

  @Test
  public void testWithPostOrder_NoEnterSubtree() throws Exception {
    final DirCache tree = db.readDirCache();
    {
      final DirCacheBuilder b = tree.builder();

      b.add(makeFile("a"));
      b.add(makeFile("b/c"));
      b.add(makeFile("b/d"));
      b.add(makeFile("q"));

      b.finish();
      assertEquals(4, tree.getEntryCount());
    }

    final TreeWalk tw = new TreeWalk(db);
    tw.setPostOrderTraversal(true);
View Full Code Here

  @Test
  public void testNoDF_NoGap() throws Exception {
    final DirCache tree0 = db.readDirCache();
    final DirCache tree1 = db.readDirCache();
    {
      final DirCacheBuilder b0 = tree0.builder();
      final DirCacheBuilder b1 = tree1.builder();

      b0.add(createEntry("a", REGULAR_FILE));
      b0.add(createEntry("a.b", EXECUTABLE_FILE));
      b1.add(createEntry("a/b", REGULAR_FILE));
      b0.add(createEntry("a0b", SYMLINK));

      b0.finish();
      b1.finish();
      assertEquals(3, tree0.getEntryCount());
      assertEquals(1, tree1.getEntryCount());
    }

    final TreeWalk tw = new TreeWalk(db);
View Full Code Here

  @Test
  public void testDF_NoGap() throws Exception {
    final DirCache tree0 = db.readDirCache();
    final DirCache tree1 = db.readDirCache();
    {
      final DirCacheBuilder b0 = tree0.builder();
      final DirCacheBuilder b1 = tree1.builder();

      b0.add(createEntry("a", REGULAR_FILE));
      b0.add(createEntry("a.b", EXECUTABLE_FILE));
      b1.add(createEntry("a/b", REGULAR_FILE));
      b0.add(createEntry("a0b", SYMLINK));

      b0.finish();
      b1.finish();
      assertEquals(3, tree0.getEntryCount());
      assertEquals(1, tree1.getEntryCount());
    }

    final NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
View Full Code Here

  @Test
  public void testDF_GapByOne() throws Exception {
    final DirCache tree0 = db.readDirCache();
    final DirCache tree1 = db.readDirCache();
    {
      final DirCacheBuilder b0 = tree0.builder();
      final DirCacheBuilder b1 = tree1.builder();

      b0.add(createEntry("a", REGULAR_FILE));
      b0.add(createEntry("a.b", EXECUTABLE_FILE));
      b1.add(createEntry("a.b", EXECUTABLE_FILE));
      b1.add(createEntry("a/b", REGULAR_FILE));
      b0.add(createEntry("a0b", SYMLINK));

      b0.finish();
      b1.finish();
      assertEquals(3, tree0.getEntryCount());
      assertEquals(2, tree1.getEntryCount());
    }

    final NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
View Full Code Here

  @Test
  public void testDF_SkipsSeenSubtree() throws Exception {
    final DirCache tree0 = db.readDirCache();
    final DirCache tree1 = db.readDirCache();
    {
      final DirCacheBuilder b0 = tree0.builder();
      final DirCacheBuilder b1 = tree1.builder();

      b0.add(createEntry("a", REGULAR_FILE));
      b1.add(createEntry("a.b", EXECUTABLE_FILE));
      b1.add(createEntry("a/b", REGULAR_FILE));
      b0.add(createEntry("a0b", SYMLINK));
      b1.add(createEntry("a0b", SYMLINK));

      b0.finish();
      b1.finish();
      assertEquals(2, tree0.getEntryCount());
      assertEquals(3, tree1.getEntryCount());
    }

    final NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
View Full Code Here

  @Test
  public void testDF_DetectConflict() throws Exception {
    final DirCache tree0 = db.readDirCache();
    final DirCache tree1 = db.readDirCache();
    {
      final DirCacheBuilder b0 = tree0.builder();
      final DirCacheBuilder b1 = tree1.builder();

      b0.add(createEntry("0", REGULAR_FILE));
      b0.add(createEntry("a", REGULAR_FILE));
      b1.add(createEntry("0", REGULAR_FILE));
      b1.add(createEntry("a.b", REGULAR_FILE));
      b1.add(createEntry("a/b", REGULAR_FILE));
      b1.add(createEntry("a/c/e", REGULAR_FILE));

      b0.finish();
      b1.finish();
      assertEquals(2, tree0.getEntryCount());
      assertEquals(4, tree1.getEntryCount());
    }

    final NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
View Full Code Here

    ObjectInserter inserter = repo.newObjectInserter();
    try {
      dc = repo.lockDirCache();
      DirCacheIterator c;

      DirCacheBuilder builder = dc.builder();
      final TreeWalk tw = new TreeWalk(repo);
      tw.addTree(new DirCacheBuildIterator(builder));
      if (workingTreeIterator == null)
        workingTreeIterator = new FileTreeIterator(repo);
      tw.addTree(workingTreeIterator);
      tw.setRecursive(true);
      if (!addAll)
        tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));

      String lastAddedFile = null;

      while (tw.next()) {
        String path = tw.getPathString();

        WorkingTreeIterator f = tw.getTree(1, WorkingTreeIterator.class);
        if (tw.getTree(0, DirCacheIterator.class) == null &&
            f != null && f.isEntryIgnored()) {
          // file is not in index but is ignored, do nothing
        }
        // In case of an existing merge conflict the
        // DirCacheBuildIterator iterates over all stages of
        // this path, we however want to add only one
        // new DirCacheEntry per path.
        else if (!(path.equals(lastAddedFile))) {
          if (!(update && tw.getTree(0, DirCacheIterator.class) == null)) {
            c = tw.getTree(0, DirCacheIterator.class);
            if (f != null) { // the file exists
              long sz = f.getEntryLength();
              DirCacheEntry entry = new DirCacheEntry(path);
              if (c == null || c.getDirCacheEntry() == null
                  || !c.getDirCacheEntry().isAssumeValid()) {
                FileMode mode = f.getIndexFileMode(c);
                entry.setFileMode(mode);

                if (FileMode.GITLINK != mode) {
                  entry.setLength(sz);
                  entry.setLastModified(f
                      .getEntryLastModified());
                  long contentSize = f
                      .getEntryContentLength();
                  InputStream in = f.openEntryStream();
                  try {
                    entry.setObjectId(inserter.insert(
                        Constants.OBJ_BLOB, contentSize, in));
                  } finally {
                    in.close();
                  }
                } else
                  entry.setObjectId(f.getEntryObjectId());
                builder.add(entry);
                lastAddedFile = path;
              } else {
                builder.add(c.getDirCacheEntry());
              }

            } else if (c != null
                && (!update || FileMode.GITLINK == c
                    .getEntryFileMode()))
              builder.add(c.getDirCacheEntry());
          }
        }
      }
      inserter.flush();
      builder.commit();
      setCallable(false);
    } catch (IOException e) {
      throw new JGitInternalException(
          JGitText.get().exceptionCaughtDuringExecutionOfAddCommand, e);
    } finally {
View Full Code Here

      RevWalk rw)
      throws IOException {
    ObjectInserter inserter = null;

    // get DirCacheBuilder for existing index
    DirCacheBuilder existingBuilder = index.builder();

    // get DirCacheBuilder for newly created in-core index to build a
    // temporary index for this commit
    DirCache inCoreIndex = DirCache.newInCore();
    DirCacheBuilder tempBuilder = inCoreIndex.builder();

    onlyProcessed = new boolean[only.size()];
    boolean emptyCommit = true;

    TreeWalk treeWalk = new TreeWalk(repo);
    int dcIdx = treeWalk.addTree(new DirCacheBuildIterator(existingBuilder));
    int fIdx = treeWalk.addTree(new FileTreeIterator(repo));
    int hIdx = -1;
    if (headId != null)
      hIdx = treeWalk.addTree(rw.parseTree(headId));
    treeWalk.setRecursive(true);

    String lastAddedFile = null;
    while (treeWalk.next()) {
      String path = treeWalk.getPathString();
      // check if current entry's path matches a specified path
      int pos = lookupOnly(path);

      CanonicalTreeParser hTree = null;
      if (hIdx != -1)
        hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);

      DirCacheIterator dcTree = treeWalk.getTree(dcIdx,
          DirCacheIterator.class);

      if (pos >= 0) {
        // include entry in commit

        FileTreeIterator fTree = treeWalk.getTree(fIdx,
            FileTreeIterator.class);

        // check if entry refers to a tracked file
        boolean tracked = dcTree != null || hTree != null;
        if (!tracked)
          break;

        // for an unmerged path, DirCacheBuildIterator will yield 3
        // entries, we only want to add one
        if (path.equals(lastAddedFile))
          continue;

        lastAddedFile = path;

        if (fTree != null) {
          // create a new DirCacheEntry with data retrieved from disk
          final DirCacheEntry dcEntry = new DirCacheEntry(path);
          long entryLength = fTree.getEntryLength();
          dcEntry.setLength(entryLength);
          dcEntry.setLastModified(fTree.getEntryLastModified());
          dcEntry.setFileMode(fTree.getIndexFileMode(dcTree));

          boolean objectExists = (dcTree != null && fTree
              .idEqual(dcTree))
              || (hTree != null && fTree.idEqual(hTree));
          if (objectExists) {
            dcEntry.setObjectId(fTree.getEntryObjectId());
          } else {
            if (FileMode.GITLINK.equals(dcEntry.getFileMode()))
              dcEntry.setObjectId(fTree.getEntryObjectId());
            else {
              // insert object
              if (inserter == null)
                inserter = repo.newObjectInserter();
              long contentLength = fTree.getEntryContentLength();
              InputStream inputStream = fTree.openEntryStream();
              try {
                dcEntry.setObjectId(inserter.insert(
                    Constants.OBJ_BLOB, contentLength,
                    inputStream));
              } finally {
                inputStream.close();
              }
            }
          }

          // add to existing index
          existingBuilder.add(dcEntry);
          // add to temporary in-core index
          tempBuilder.add(dcEntry);

          if (emptyCommit
              && (hTree == null || !hTree.idEqual(fTree) || hTree
                  .getEntryRawMode() != fTree
                  .getEntryRawMode()))
            // this is a change
            emptyCommit = false;
        } else {
          // if no file exists on disk, neither add it to
          // index nor to temporary in-core index

          if (emptyCommit && hTree != null)
            // this is a change
            emptyCommit = false;
        }

        // keep track of processed path
        onlyProcessed[pos] = true;
      } else {
        // add entries from HEAD for all other paths
        if (hTree != null) {
          // create a new DirCacheEntry with data retrieved from HEAD
          final DirCacheEntry dcEntry = new DirCacheEntry(path);
          dcEntry.setObjectId(hTree.getEntryObjectId());
          dcEntry.setFileMode(hTree.getEntryFileMode());

          // add to temporary in-core index
          tempBuilder.add(dcEntry);
        }

        // preserve existing entry in index
        if (dcTree != null)
          existingBuilder.add(dcTree.getDirCacheEntry());
      }
    }

    // there must be no unprocessed paths left at this point; otherwise an
    // untracked or unknown path has been specified
    for (int i = 0; i < onlyProcessed.length; i++)
      if (!onlyProcessed[i])
        throw new JGitInternalException(MessageFormat.format(
            JGitText.get().entryNotFoundByPath, only.get(i)));

    // there must be at least one change
    if (emptyCommit)
      throw new JGitInternalException(JGitText.get().emptyCommit);

    // update index
    existingBuilder.commit();
    // finish temporary in-core index used for this commit
    tempBuilder.finish();
    return inCoreIndex;
  }
View Full Code Here

TOP

Related Classes of org.eclipse.jgit.dircache.DirCacheBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.