Package org.eclipse.jgit.dircache

Examples of org.eclipse.jgit.dircache.DirCache


      NoFilepatternException {

    if (filepatterns.isEmpty())
      throw new NoFilepatternException(JGitText.get().atLeastOnePatternIsRequired);
    checkCallable();
    DirCache dc = null;

    try {
      dc = repo.lockDirCache();
      DirCacheBuilder builder = dc.builder();
      final TreeWalk tw = new TreeWalk(repo);
      tw.reset(); // drop the first empty tree, which we do not need here
      tw.setRecursive(true);
      tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));
      tw.addTree(new DirCacheBuildIterator(builder));

      while (tw.next()) {
        if (!cached) {
          final FileMode mode = tw.getFileMode(0);
          if (mode.getObjectType() == Constants.OBJ_BLOB) {
            final File path = new File(repo.getWorkTree(),
                tw.getPathString());
            // Deleting a blob is simply a matter of removing
            // the file or symlink named by the tree entry.
            delete(path);
          }
        }
      }
      builder.commit();
      setCallable(false);
    } catch (IOException e) {
      throw new JGitInternalException(
          JGitText.get().exceptionCaughtDuringExecutionOfRmCommand, e);
    } finally {
      if (dc != null)
        dc.unlock();
    }

    return dc;
  }
View Full Code Here


    assertEquals(Arrays.asList("abc", "c"), getMatchingPaths("c", treeId));
  }

  private ObjectId createTree(String... paths) throws IOException {
    final ObjectInserter odi = db.newObjectInserter();
    final DirCache dc = db.readDirCache();
    final DirCacheBuilder builder = dc.builder();
    for (String path : paths) {
      DirCacheEntry entry = createEntry(path, FileMode.REGULAR_FILE);
      builder.add(entry);
    }
    builder.finish();
    final ObjectId treeId = dc.writeTree(odi);
    odi.flush();
    return treeId;
  }
View Full Code Here

    Ref head = getHead();
    ObjectReader reader = repo.newObjectReader();
    try {
      RevCommit headCommit = parseCommit(reader, head.getObjectId());
      DirCache cache = repo.lockDirCache();
      ObjectInserter inserter = repo.newObjectInserter();
      ObjectId commitId;
      try {
        TreeWalk treeWalk = new TreeWalk(reader);
        treeWalk.setRecursive(true);
        treeWalk.addTree(headCommit.getTree());
        treeWalk.addTree(new DirCacheIterator(cache));
        treeWalk.addTree(new FileTreeIterator(repo));
        treeWalk.setFilter(AndTreeFilter.create(new SkipWorkTreeFilter(
            1), new IndexDiffFilter(1, 2)));

        // Return null if no local changes to stash
        if (!treeWalk.next())
          return null;

        MutableObjectId id = new MutableObjectId();
        List<PathEdit> wtEdits = new ArrayList<PathEdit>();
        List<String> wtDeletes = new ArrayList<String>();
        List<DirCacheEntry> untracked = new ArrayList<DirCacheEntry>();
        boolean hasChanges = false;
        do {
          AbstractTreeIterator headIter = treeWalk.getTree(0,
              AbstractTreeIterator.class);
          DirCacheIterator indexIter = treeWalk.getTree(1,
              DirCacheIterator.class);
          WorkingTreeIterator wtIter = treeWalk.getTree(2,
              WorkingTreeIterator.class);
          if (indexIter != null
              && !indexIter.getDirCacheEntry().isMerged())
            throw new UnmergedPathsException(
                new UnmergedPathException(
                    indexIter.getDirCacheEntry()));
          if (wtIter != null) {
            if (indexIter == null && headIter == null
                && !includeUntracked)
              continue;
            hasChanges = true;
            if (indexIter != null && wtIter.idEqual(indexIter))
              continue;
            if (headIter != null && wtIter.idEqual(headIter))
              continue;
            treeWalk.getObjectId(id, 0);
            final DirCacheEntry entry = new DirCacheEntry(
                treeWalk.getRawPath());
            entry.setLength(wtIter.getEntryLength());
            entry.setLastModified(wtIter.getEntryLastModified());
            entry.setFileMode(wtIter.getEntryFileMode());
            long contentLength = wtIter.getEntryContentLength();
            InputStream in = wtIter.openEntryStream();
            try {
              entry.setObjectId(inserter.insert(
                  Constants.OBJ_BLOB, contentLength, in));
            } finally {
              in.close();
            }

            if (indexIter == null && headIter == null)
              untracked.add(entry);
            else
              wtEdits.add(new PathEdit(entry) {
                public void apply(DirCacheEntry ent) {
                  ent.copyMetaData(entry);
                }
              });
          }
          hasChanges = true;
          if (wtIter == null && headIter != null)
            wtDeletes.add(treeWalk.getPathString());
        } while (treeWalk.next());

        if (!hasChanges)
          return null;

        String branch = Repository.shortenRefName(head.getTarget()
            .getName());

        // Commit index changes
        CommitBuilder builder = createBuilder();
        builder.setParentId(headCommit);
        builder.setTreeId(cache.writeTree(inserter));
        builder.setMessage(MessageFormat.format(indexMessage, branch,
            headCommit.abbreviate(7).name(),
            headCommit.getShortMessage()));
        ObjectId indexCommit = inserter.insert(builder);

        // Commit untracked changes
        ObjectId untrackedCommit = null;
        if (!untracked.isEmpty()) {
          DirCache untrackedDirCache = DirCache.newInCore();
          DirCacheBuilder untrackedBuilder = untrackedDirCache
              .builder();
          for (DirCacheEntry entry : untracked)
            untrackedBuilder.add(entry);
          untrackedBuilder.finish();

          builder.setParentIds(new ObjectId[0]);
          builder.setTreeId(untrackedDirCache.writeTree(inserter));
          builder.setMessage(MessageFormat.format(MSG_UNTRACKED,
              branch, headCommit.abbreviate(7).name(),
              headCommit.getShortMessage()));
          untrackedCommit = inserter.insert(builder);
        }
View Full Code Here

    assertFalse(tw.isPostOrderTraversal());
  }

  @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);
    tw.addTree(new DirCacheIterator(tree));
View Full Code Here

    assertModes("q", REGULAR_FILE, tw);
  }

  @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);
    tw.addTree(new DirCacheIterator(tree));
View Full Code Here

    assertModes("q", REGULAR_FILE, tw);
  }

  @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);
    tw.addTree(new DirCacheIterator(tree));
View Full Code Here

  private static final FileMode EXECUTABLE_FILE = FileMode.EXECUTABLE_FILE;

  @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);
    tw.addTree(new DirCacheIterator(tree0));
    tw.addTree(new DirCacheIterator(tree1));
View Full Code Here

    assertModes("a0b", SYMLINK, MISSING, tw);
  }

  @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);
    tw.addTree(new DirCacheIterator(tree0));
    tw.addTree(new DirCacheIterator(tree1));
View Full Code Here

    assertFalse(tw.isDirectoryFileConflict());
  }

  @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);
    tw.addTree(new DirCacheIterator(tree0));
    tw.addTree(new DirCacheIterator(tree1));
View Full Code Here

    assertFalse(tw.isDirectoryFileConflict());
  }

  @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);
    tw.addTree(new DirCacheIterator(tree0));
    tw.addTree(new DirCacheIterator(tree1));
View Full Code Here

TOP

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

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.