Package org.eclipse.jgit.treewalk

Examples of org.eclipse.jgit.treewalk.TreeWalk


    try {
      final ObjectReader reader = repo.newObjectReader();
      try {
        final RevWalk rw = new RevWalk(reader);
        final RevCommit c;
        final TreeWalk tw;

        c = rw.parseCommit(ObjectId.fromString(patchSet.getRevision().get()));
        if (side == 0) {
          fromCommit = c;
          suffix = "new";

        } else if (1 <= side && side - 1 < c.getParentCount()) {
          fromCommit = rw.parseCommit(c.getParent(side - 1));
          if (c.getParentCount() == 1) {
            suffix = "old";
          } else {
            suffix = "old" + side;
          }

        } else {
          rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
          return;
        }

        tw = TreeWalk.forPath(reader, path, fromCommit.getTree());
        if (tw == null) {
          rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
          return;
        }

        if (tw.getFileMode(0).getObjectType() == Constants.OBJ_BLOB) {
          blobLoader = reader.open(tw.getObjectId(0), Constants.OBJ_BLOB);

        } else {
          rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
          return;
        }
View Full Code Here


    boolean hasUnmergedPaths = dc.hasUnmergedPaths();
    if (hasUnmergedPaths)
      throw new UnmergedPathsException();

    // determine whether we need to commit
    TreeWalk treeWalk = new TreeWalk(repo);
    treeWalk.reset();
    treeWalk.setRecursive(true);
    treeWalk.addTree(new DirCacheIterator(dc));
    ObjectId id = repo.resolve(Constants.HEAD + "^{tree}");
    if (id == null)
      throw new NoHeadException(
          JGitText.get().cannotRebaseWithoutCurrentHead);

    treeWalk.addTree(id);

    treeWalk.setFilter(TreeFilter.ANY_DIFF);

    boolean needsCommit = treeWalk.next();
    treeWalk.release();

    if (needsCommit) {
      CommitCommand commit = new Git(repo).commit();
      commit.setMessage(readFile(rebaseDir, MESSAGE));
      commit.setAuthor(parseAuthor());
View Full Code Here

   * @throws IOException
   *             a tree cannot be read to iterate through its entries.
   */
  public void addTree(final byte[] pathPrefix, final int stage,
      final ObjectReader reader, final AnyObjectId tree) throws IOException {
    final TreeWalk tw = new TreeWalk(reader);
    tw.addTree(new CanonicalTreeParser(pathPrefix, reader, tree
        .toObjectId()));
    tw.setRecursive(true);
    if (tw.next()) {
      final DirCacheEntry newEntry = toEntry(stage, tw);
      beforeAdd(newEntry);
      fastAdd(newEntry);
      while (tw.next())
        fastAdd(toEntry(stage, tw));
    }
  }
View Full Code Here

        }

        if (i == rev.length - i)
          return tree.copy();

        TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(),
            new String(rev, i + 1, rev.length - i - 1), tree);
        return tw != null ? tw.getObjectId(0) : null;
      }

      default:
        if (ref != null)
          throw new RevisionSyntaxException(revstr);
View Full Code Here

  private final Repository repository;

  RewriteTreeFilter(final RevWalk walker, final TreeFilter t) {
    repository = walker.repository;
    pathFilter = new TreeWalk(walker.reader);
    pathFilter.setFilter(t);
    pathFilter.setRecursive(t.shouldBeRecursive());
  }
View Full Code Here

      IncorrectObjectTypeException, IOException {
    // Reset the tree filter to scan this commit and parents.
    //
    final RevCommit[] pList = c.parents;
    final int nParents = pList.length;
    final TreeWalk tw = pathFilter;
    final ObjectId[] trees = new ObjectId[nParents + 1];
    for (int i = 0; i < nParents; i++) {
      final RevCommit p = c.parents[i];
      if ((p.flags & PARSED) == 0)
        p.parseHeaders(walker);
      trees[i] = p.getTree();
    }
    trees[nParents] = c.getTree();
    tw.reset(trees);

    if (nParents == 1) {
      // We have exactly one parent. This is a very common case.
      //
      int chgs = 0, adds = 0;
      while (tw.next()) {
        chgs++;
        if (tw.getRawMode(0) == 0 && tw.getRawMode(1) != 0)
          adds++;
        else
          break; // no point in looking at this further.
      }

      if (chgs == 0) {
        // No changes, so our tree is effectively the same as
        // our parent tree. We pass the buck to our parent.
        //
        c.flags |= REWRITE;
        return false;
      } else {
        // We have interesting items, but neither of the special
        // cases denoted above.
        //
        if (adds > 0 && tw.getFilter() instanceof FollowFilter) {
          // One of the paths we care about was added in this
          // commit. We need to update our filter to its older
          // name, if we can discover it. Find out what that is.
          //
          updateFollowFilter(trees);
        }
        return true;
      }
    } else if (nParents == 0) {
      // We have no parents to compare against. Consider us to be
      // REWRITE only if we have no paths matching our filter.
      //
      if (tw.next())
        return true;
      c.flags |= REWRITE;
      return false;
    }

    // We are a merge commit. We can only be REWRITE if we are same
    // to _all_ parents. We may also be able to eliminate a parent if
    // it does not contribute changes to us. Such a parent may be an
    // uninteresting side branch.
    //
    final int[] chgs = new int[nParents];
    final int[] adds = new int[nParents];
    while (tw.next()) {
      final int myMode = tw.getRawMode(nParents);
      for (int i = 0; i < nParents; i++) {
        final int pMode = tw.getRawMode(i);
        if (myMode == pMode && tw.idEqual(i, nParents))
          continue;

        chgs[i]++;
        if (pMode == 0 && myMode != 0)
          adds[i]++;
View Full Code Here

  }

  private void updateFollowFilter(ObjectId[] trees)
      throws MissingObjectException, IncorrectObjectTypeException,
      CorruptObjectException, IOException {
    TreeWalk tw = pathFilter;
    FollowFilter oldFilter = (FollowFilter) tw.getFilter();
    tw.setFilter(TreeFilter.ANY_DIFF);
    tw.reset(trees);

    List<DiffEntry> files = DiffEntry.scan(tw);
    RenameDetector rd = new RenameDetector(repository);
    rd.addAll(files);
    files = rd.compute();

    TreeFilter newFilter = oldFilter;
    for (DiffEntry ent : files) {
      if (isRename(ent) && ent.getNewPath().equals(oldFilter.getPath())) {
        newFilter = FollowFilter.create(ent.getOldPath());
        break;
      }
    }
    tw.setFilter(newFilter);
  }
View Full Code Here

    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()) {
View Full Code Here

  private static byte[] read(Repository db, AnyObjectId treeish, String path)
      throws MissingObjectException, IncorrectObjectTypeException,
      IOException {
    ObjectReader or = db.newObjectReader();
    try {
      TreeWalk tree = TreeWalk.forPath(or, path, asTree(or, treeish));
      if (tree == null)
        throw new FileNotFoundException(MessageFormat.format(JGitText
            .get().entryNotFoundByPath, path));
      return read(or, tree.getObjectId(0));
    } finally {
      or.release();
    }
  }
View Full Code Here

  public boolean diff(final ProgressMonitor monitor, int estWorkTreeSize,
      int estIndexSize, final String title)
      throws IOException {
    dirCache = repository.readDirCache();

    TreeWalk treeWalk = new TreeWalk(repository);
    treeWalk.setRecursive(true);
    // add the trees (tree, dirchache, workdir)
    if (tree != null)
      treeWalk.addTree(tree);
    else
      treeWalk.addTree(new EmptyTreeIterator());
    treeWalk.addTree(new DirCacheIterator(dirCache));
    treeWalk.addTree(initialWorkingTreeIterator);
    Collection<TreeFilter> filters = new ArrayList<TreeFilter>(4);

    if (monitor != null) {
      // Get the maximum size of the work tree and index
      // and add some (quite arbitrary)
      if (estIndexSize == 0)
        estIndexSize = dirCache.getEntryCount();
      int total = Math.max(estIndexSize * 10 / 9,
          estWorkTreeSize * 10 / 9);
      monitor.beginTask(title, total);
      filters.add(new ProgressReportingFilter(monitor, total));
    }

    if (filter != null)
      filters.add(filter);
    filters.add(new SkipWorkTreeFilter(INDEX));
    filters.add(new IndexDiffFilter(INDEX, WORKDIR));
    treeWalk.setFilter(AndTreeFilter.create(filters));
    while (treeWalk.next()) {
      AbstractTreeIterator treeIterator = treeWalk.getTree(TREE,
          AbstractTreeIterator.class);
      DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX,
          DirCacheIterator.class);
      WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR,
          WorkingTreeIterator.class);

      if (treeIterator != null) {
        if (dirCacheIterator != null) {
          if (!treeIterator.idEqual(dirCacheIterator)
              || treeIterator.getEntryRawMode()
              != dirCacheIterator.getEntryRawMode()) {
            // in repo, in index, content diff => changed
            changed.add(treeWalk.getPathString());
          }
        } else {
          // in repo, not in index => removed
          removed.add(treeWalk.getPathString());
          if (workingTreeIterator != null)
            untracked.add(treeWalk.getPathString());
        }
      } else {
        if (dirCacheIterator != null) {
          // not in repo, in index => added
          added.add(treeWalk.getPathString());
        } else {
          // not in repo, not in index => untracked
          if (workingTreeIterator != null
              && !workingTreeIterator.isEntryIgnored()) {
            untracked.add(treeWalk.getPathString());
          }
        }
      }

      if (dirCacheIterator != null) {
        if (workingTreeIterator == null) {
          // in index, not in workdir => missing
          missing.add(treeWalk.getPathString());
        } else {
          if (workingTreeIterator.isModified(
              dirCacheIterator.getDirCacheEntry(), true)) {
            // in index, in workdir, content differs => modified
            modified.add(treeWalk.getPathString());
          }
        }
      }
    }

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.