Exemple #1
0
  /**
   * Determine the differences between two trees.
   *
   * <p>No output is created, instead only the file paths that are different are returned. Callers
   * may choose to format these paths themselves, or convert them into {@link FileHeader} instances
   * with a complete edit list by calling {@link #toFileHeader(DiffEntry)}.
   *
   * @param a the old (or previous) side.
   * @param b the new (or updated) side.
   * @return the paths that are different.
   * @throws IOException trees cannot be read or file contents cannot be read.
   */
  public List<DiffEntry> scan(RevTree a, RevTree b) throws IOException {
    assertHaveRepository();

    CanonicalTreeParser aParser = new CanonicalTreeParser();
    CanonicalTreeParser bParser = new CanonicalTreeParser();

    aParser.reset(reader, a);
    bParser.reset(reader, b);

    return scan(aParser, bParser);
  }
 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();
   }
 }
  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;
  }