Package org.eclipse.jgit.treewalk

Examples of org.eclipse.jgit.treewalk.TreeWalk


        error(e1, repository, "{0} failed to open {1} stream", algorithm);
      }
    }
    boolean success = false;
    RevWalk rw = new RevWalk(repository);
    TreeWalk tw = new TreeWalk(repository);
    try {
      tw.reset();
      tw.addTree(commit.getTree());
      TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
      tos.setAddPaxHeadersForNonAsciiNames(true);
      tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
      if (!StringUtils.isEmpty(basePath)) {
        PathFilter f = PathFilter.create(basePath);
        tw.setFilter(f);
      }
      tw.setRecursive(true);
      MutableObjectId id = new MutableObjectId();
      long modified = commit.getAuthorIdent().getWhen().getTime();
      while (tw.next()) {
        FileMode mode = tw.getFileMode(0);
        if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
          continue;
        }
        tw.getObjectId(id, 0);

        ObjectLoader loader = repository.open(id);
        if (FileMode.SYMLINK == mode) {
          TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString(),TarArchiveEntry.LF_SYMLINK);
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          loader.copyTo(bos);
          entry.setLinkName(bos.toString());
          entry.setModTime(modified);
          tos.putArchiveEntry(entry);
          tos.closeArchiveEntry();
        } else {
          TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString());
          entry.setMode(mode.getBits());
          entry.setModTime(modified);
          entry.setSize(loader.getSize());
          tos.putArchiveEntry(entry);
          loader.copyTo(tos);
          tos.closeArchiveEntry();
        }
      }
      tos.finish();
      tos.close();
      cos.close();
      success = true;
    } catch (IOException e) {
      error(e, repository, "{0} failed to {1} stream files from commit {2}", algorithm, commit.getName());
    } finally {
      tw.release();
      rw.dispose();
    }
    return success;
  }
View Full Code Here


      if (ticketsBranch == null) {
        throw new RuntimeException(BRANCH + " does not exist!");
      }
      String ticketPath = toTicketPath(ticket.number);

      TreeWalk treeWalk = null;
      try {
        ObjectId treeId = db.resolve(BRANCH + "^{tree}");

        // Create the in-memory index of the new/updated ticket
        DirCache index = DirCache.newInCore();
        DirCacheBuilder builder = index.builder();

        // Traverse HEAD to add all other paths
        treeWalk = new TreeWalk(db);
        int hIdx = -1;
        if (treeId != null) {
          hIdx = treeWalk.addTree(treeId);
        }
        treeWalk.setRecursive(true);
        while (treeWalk.next()) {
          String path = treeWalk.getPathString();
          CanonicalTreeParser hTree = null;
          if (hIdx != -1) {
            hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
          }
          if (!path.startsWith(ticketPath)) {
            // add entries from HEAD for all other paths
            if (hTree != null) {
              final DirCacheEntry entry = new DirCacheEntry(path);
              entry.setObjectId(hTree.getEntryObjectId());
              entry.setFileMode(hTree.getEntryFileMode());

              // add to temporary in-core index
              builder.add(entry);
            }
          }
        }

        // finish temporary in-core index used for this commit
        builder.finish();

        success = commitIndex(db, index, deletedBy, "- " + ticket.number);

      } catch (Throwable t) {
        log.error(MessageFormat.format("Failed to delete ticket {0,number,0} from {1}",
            ticket.number, db.getDirectory()), t);
      } finally {
        // release the treewalk
        if (treeWalk != null) {
          treeWalk.release();
        }
      }
    } finally {
      db.close();
    }
View Full Code Here

   * @param dcBuilder
   * @throws IOException
   */
  private List<DirCacheEntry> getTreeEntries(Repository db, Collection<String> ignorePaths) throws IOException {
    List<DirCacheEntry> list = new ArrayList<DirCacheEntry>();
    TreeWalk tw = null;
    try {
      ObjectId treeId = db.resolve(BRANCH + "^{tree}");
      if (treeId == null) {
        // branch does not exist yet, could be migrating tickets
        return list;
      }
      tw = new TreeWalk(db);
      int hIdx = tw.addTree(treeId);
      tw.setRecursive(true);

      while (tw.next()) {
        String path = tw.getPathString();
        CanonicalTreeParser hTree = null;
        if (hIdx != -1) {
          hTree = tw.getTree(hIdx, CanonicalTreeParser.class);
        }
        if (!ignorePaths.contains(path)) {
          // add all other tree entries
          if (hTree != null) {
            final DirCacheEntry entry = new DirCacheEntry(path);
            entry.setObjectId(hTree.getEntryObjectId());
            entry.setFileMode(hTree.getEntryFileMode());
            list.add(entry);
          }
        }
      }
    } finally {
      if (tw != null) {
        tw.release();
      }
    }
    return list;
  }
View Full Code Here

        String keyName = getBranchKey(branchName);
        config.setString(CONF_ALIAS, null, keyName, branchName);
        config.setString(CONF_BRANCH, null, keyName, tipId);

        // index the blob contents of the tree
        TreeWalk treeWalk = new TreeWalk(repository);
        treeWalk.addTree(tip.getTree());
        treeWalk.setRecursive(true);

        Map<String, ObjectId> paths = new TreeMap<String, ObjectId>();
        while (treeWalk.next()) {
          // ensure path is not in a submodule
          if (treeWalk.getFileMode(0) != FileMode.GITLINK) {
            paths.put(treeWalk.getPathString(), treeWalk.getObjectId(0));
          }
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] tmp = new byte[32767];

        RevWalk commitWalk = new RevWalk(reader);
        commitWalk.markStart(tip);

        RevCommit commit;
        while ((paths.size() > 0) && (commit = commitWalk.next()) != null) {
          TreeWalk diffWalk = new TreeWalk(reader);
          int parentCount = commit.getParentCount();
          switch (parentCount) {
          case 0:
            diffWalk.addTree(new EmptyTreeIterator());
            break;
          case 1:
            diffWalk.addTree(getTree(commitWalk, commit.getParent(0)));
            break;
          default:
            // skip merge commits
            continue;
          }
          diffWalk.addTree(getTree(commitWalk, commit));
          diffWalk.setFilter(ANY_DIFF);
          diffWalk.setRecursive(true);
          while ((paths.size() > 0) && diffWalk.next()) {
            String path = diffWalk.getPathString();
            if (!paths.containsKey(path)) {
              continue;
            }

            // remove path from set
View Full Code Here

        }
      }
      if (matchingPath == null) {
        // path not in commit
        // manually locate path in tree
        TreeWalk tw = new TreeWalk(r);
        tw.reset();
        tw.setRecursive(true);
        try {
          tw.addTree(commit.getTree());
          tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
          while (tw.next()) {
            if (tw.getPathString().equals(path)) {
              matchingPath = new PathChangeModel(tw.getPathString(), tw.getPathString(), 0, tw
                .getRawMode(0), tw.getObjectId(0).getName(), commit.getId().getName(),
                ChangeType.MODIFY);
            }
          }
        } catch (Exception e) {
        } finally {
          tw.release();
        }
      }
    }

    final boolean isTree = matchingPath == null ? true : matchingPath.isTree();
View Full Code Here

  }

  @Override
  public boolean include(final RevWalk walker, final RevCommit commit)
      throws IOException {
    final TreeWalk walk = new TreeWalk(walker.getObjectReader());
    walk.addTree(commit.getTree());
    while (walk.next()) {
      if (!filter.include(walker, commit, walk))
        return include(false);
      if (walk.isSubtree())
        walk.enterSubtree();
    }
    return true;
  }
View Full Code Here

  }

  @Override
  public boolean include(final RevWalk walker, final RevCommit commit)
      throws IOException {
    final TreeWalk walk = new TreeWalk(walker.getObjectReader());
    walk.addTree(commit.getTree());
    RevTree tree = null;
    for (RevCommit parent : commit.getParents()) {
      tree = parent.getTree();
      if (tree == null) {
        walker.parseHeaders(parent);
        tree = parent.getTree();
      }
      walk.addTree(tree);
    }

    while (walk.next()) {
      if (filter.include(walker, commit, walk))
        return true;
      if (walk.isSubtree())
        walk.enterSubtree();
    }
    return include(false);
  }
View Full Code Here

   */
  @Test
  public void diffWithNoParents() throws Exception {
    RevCommit commit = add("test.txt", "content");
    Repository repo = new FileRepository(testRepo);
    TreeWalk walk = TreeUtils.diffWithParents(repo, Constants.HEAD);
    assertNotNull(walk);
    assertEquals(2, walk.getTreeCount());
    assertTrue(walk.next());
    assertEquals("test.txt", walk.getPathString());
    assertEquals(ObjectId.zeroId(), walk.getObjectId(0));
    assertEquals(BlobUtils.getId(repo, commit, "test.txt"),
        walk.getObjectId(1));
    assertFalse(walk.next());
  }
View Full Code Here

  @Test
  public void diffWithOneParent() throws Exception {
    Repository repo = new FileRepository(testRepo);
    RevCommit commit1 = add("test.txt", "content");
    RevCommit commit2 = add("test.txt", "content2");
    TreeWalk walk = TreeUtils.diffWithParents(repo, Constants.HEAD);
    assertNotNull(walk);
    assertEquals(2, walk.getTreeCount());
    assertTrue(walk.next());
    assertEquals("test.txt", walk.getPathString());
    assertEquals(BlobUtils.getId(repo, commit1, "test.txt"),
        walk.getObjectId(0));
    assertEquals(BlobUtils.getId(repo, commit2, "test.txt"),
        walk.getObjectId(1));
    assertFalse(walk.next());
  }
View Full Code Here

  @Test
  public void diffRevisions() throws Exception {
    Repository repo = new FileRepository(testRepo);
    RevCommit commit1 = add("test.txt", "content");
    RevCommit commit2 = add("test.txt", "content2");
    TreeWalk walk = TreeUtils.diffWithCommits(repo,
        Constants.MASTER + "~1", Constants.MASTER);
    assertNotNull(walk);
    assertEquals(2, walk.getTreeCount());
    assertTrue(walk.next());
    assertEquals("test.txt", walk.getPathString());
    assertEquals(BlobUtils.getId(repo, commit1, "test.txt"),
        walk.getObjectId(0));
    assertEquals(BlobUtils.getId(repo, commit2, "test.txt"),
        walk.getObjectId(1));
    assertFalse(walk.next());
  }
View Full Code Here

TOP

Related Classes of org.eclipse.jgit.treewalk.TreeWalk

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.