private AbstractTreeIterator getTreeIterator(Repository db, String name) throws IOException {
   final ObjectId id = db.resolve(name);
   if (id == null) throw new IllegalArgumentException(name);
   final CanonicalTreeParser p = new CanonicalTreeParser();
   final ObjectReader or = db.newObjectReader();
   try {
     p.reset(or, new RevWalk(db).parseTree(id));
     return p;
   } finally {
     or.release();
   }
 }
示例#2
0
  /**
   * Reset this walker to run over a set of existing trees.
   *
   * @param ids the trees we need to parse. The walker will execute over this many parallel trees if
   *     the reset is successful.
   * @throws MissingObjectException the given tree object does not exist in this repository.
   * @throws IncorrectObjectTypeException the given object id does not denote a tree, but instead
   *     names some other non-tree type of object. Note that commits are not trees, even if they are
   *     sometimes called a "tree-ish".
   * @throws CorruptObjectException the object claimed to be a tree, but its contents did not appear
   *     to be a tree. The repository may have data corruption.
   * @throws IOException a loose object or pack file could not be read.
   */
  public void reset(final AnyObjectId[] ids)
      throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException,
          IOException {
    final int oldLen = trees.length;
    final int newLen = ids.length;
    final AbstractTreeIterator[] r = newLen == oldLen ? trees : new AbstractTreeIterator[newLen];
    for (int i = 0; i < newLen; i++) {
      AbstractTreeIterator o;

      if (i < oldLen) {
        o = trees[i];
        while (o.parent != null) o = o.parent;
        if (o instanceof CanonicalTreeParser && o.pathOffset == 0) {
          o.matches = null;
          o.matchShift = 0;
          ((CanonicalTreeParser) o).reset(db, ids[i], curs);
          r[i] = o;
          continue;
        }
      }

      o = parserFor(ids[i]);
      r[i] = o;
    }

    trees = r;
    advance = false;
    depth = 0;
  }
示例#3
0
  /**
   * Reset this walker to run over a single existing tree.
   *
   * @param id the tree we need to parse. The walker will execute over this single tree if the reset
   *     is successful.
   * @throws MissingObjectException the given tree object does not exist in this repository.
   * @throws IncorrectObjectTypeException the given object id does not denote a tree, but instead
   *     names some other non-tree type of object. Note that commits are not trees, even if they are
   *     sometimes called a "tree-ish".
   * @throws CorruptObjectException the object claimed to be a tree, but its contents did not appear
   *     to be a tree. The repository may have data corruption.
   * @throws IOException a loose object or pack file could not be read.
   */
  public void reset(final AnyObjectId id)
      throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException,
          IOException {
    if (trees.length == 1) {
      AbstractTreeIterator o = trees[0];
      while (o.parent != null) o = o.parent;
      if (o instanceof CanonicalTreeParser) {
        o.matches = null;
        o.matchShift = 0;
        ((CanonicalTreeParser) o).reset(db, id, curs);
        trees[0] = o;
      } else {
        trees[0] = parserFor(id);
      }
    } else {
      trees = new AbstractTreeIterator[] {parserFor(id)};
    }

    advance = false;
    depth = 0;
  }
示例#4
0
 private CanonicalTreeParser parserFor(final AnyObjectId id)
     throws IncorrectObjectTypeException, IOException {
   final CanonicalTreeParser p = new CanonicalTreeParser();
   p.reset(db, id, curs);
   return p;
 }
  private boolean handleGetDiff(
      HttpServletRequest request,
      HttpServletResponse response,
      Repository db,
      String scope,
      String pattern,
      OutputStream out)
      throws Exception {
    Git git = new Git(db);
    DiffCommand diff = git.diff();
    diff.setOutputStream(new BufferedOutputStream(out));
    AbstractTreeIterator oldTree;
    AbstractTreeIterator newTree = new FileTreeIterator(db);
    if (scope.contains("..")) { // $NON-NLS-1$
      String[] commits = scope.split("\\.\\."); // $NON-NLS-1$
      if (commits.length != 2) {
        String msg = NLS.bind("Failed to generate diff for {0}", scope);
        return statusHandler.handleRequest(
            request,
            response,
            new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
      }
      oldTree = getTreeIterator(db, commits[0]);
      newTree = getTreeIterator(db, commits[1]);
    } else if (scope.equals(GitConstants.KEY_DIFF_CACHED)) {
      ObjectId head = db.resolve(Constants.HEAD + "^{tree}"); // $NON-NLS-1$
      if (head == null) {
        String msg = NLS.bind("Failed to generate diff for {0}, no HEAD", scope);
        return statusHandler.handleRequest(
            request,
            response,
            new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
      }
      CanonicalTreeParser p = new CanonicalTreeParser();
      ObjectReader reader = db.newObjectReader();
      try {
        p.reset(reader, head);
      } finally {
        reader.release();
      }
      oldTree = p;
      newTree = new DirCacheIterator(db.readDirCache());
    } else if (scope.equals(GitConstants.KEY_DIFF_DEFAULT)) {
      oldTree = new DirCacheIterator(db.readDirCache());
    } else {
      oldTree = getTreeIterator(db, scope);
    }

    String[] paths = request.getParameterValues(ProtocolConstants.KEY_PATH);
    TreeFilter filter = null;
    TreeFilter pathFilter = null;
    if (paths != null) {
      if (paths.length > 1) {
        Set<TreeFilter> pathFilters = new HashSet<TreeFilter>(paths.length);
        for (String path : paths) {
          pathFilters.add(PathFilter.create(path));
        }
        pathFilter = OrTreeFilter.create(pathFilters);
      } else if (paths.length == 1) {
        pathFilter = PathFilter.create(paths[0]);
      }
    }
    if (pattern != null) {
      PathFilter patternFilter = PathFilter.create(pattern);
      if (pathFilter != null) filter = AndTreeFilter.create(patternFilter, pathFilter);
      else filter = patternFilter;
    } else {
      filter = pathFilter;
    }
    if (filter != null) diff.setPathFilter(filter);

    diff.setOldTree(oldTree);
    diff.setNewTree(newTree);
    diff.call();
    return true;
  }