Beispiel #1
0
 @Override
 protected IStatus performJob() {
   try {
     // list all tags
     File gitDir = GitUtils.getGitDir(path);
     Repository db = new FileRepository(gitDir);
     Git git = new Git(db);
     List<Ref> refs = git.tagList().call();
     JSONObject result = new JSONObject();
     List<Tag> tags = new ArrayList<Tag>();
     for (Ref ref : refs) {
       Tag tag = new Tag(cloneLocation, db, ref);
       tags.add(tag);
     }
     Collections.sort(tags, Tag.COMPARATOR);
     JSONArray children = new JSONArray();
     int firstTag = pageSize > 0 ? pageSize * (pageNo - 1) : 0;
     int lastTag = pageSize > 0 ? firstTag + pageSize - 1 : tags.size() - 1;
     lastTag = lastTag > tags.size() - 1 ? tags.size() - 1 : lastTag;
     if (pageNo > 1 && baseLocation != null) {
       String prev = baseLocation + "?page=" + (pageNo - 1) + "&pageSize=" + pageSize;
       if (commitsSize > 0) {
         prev += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
       }
       result.put(ProtocolConstants.KEY_PREVIOUS_LOCATION, prev);
     }
     if (lastTag < tags.size() - 1) {
       String next = baseLocation + "?page=" + (pageNo + 1) + "&pageSize=" + pageSize;
       if (commitsSize > 0) {
         next += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
       }
       result.put(ProtocolConstants.KEY_NEXT_LOCATION, next);
     }
     for (int i = firstTag; i <= lastTag; i++) {
       Tag tag = tags.get(i);
       if (this.commitsSize == 0) {
         children.put(tag.toJSON());
       } else {
         // add info about commits if requested
         LogCommand lc = git.log();
         String toCommitName = tag.getRevCommitName();
         ObjectId toCommitId = db.resolve(toCommitName);
         Ref toCommitRef = db.getRef(toCommitName);
         toCommitId = getCommitObjectId(db, toCommitId);
         lc.add(toCommitId);
         lc.setMaxCount(this.commitsSize);
         Iterable<RevCommit> commits = lc.call();
         Log log = new Log(cloneLocation, db, commits, null, null, toCommitRef);
         children.put(tag.toJSON(log.toJSON(1, commitsSize)));
       }
     }
     result.put(ProtocolConstants.KEY_CHILDREN, children);
     result.put(ProtocolConstants.KEY_TYPE, Tag.TYPE);
     return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
   } catch (Exception e) {
     String msg = NLS.bind("An error occured when listing tags for {0}", path);
     return new Status(IStatus.ERROR, GitActivator.PI_GIT, msg, e);
   }
 }
  private boolean tag(
      HttpServletRequest request,
      HttpServletResponse response,
      Repository db,
      String commitId,
      String tagName,
      boolean isRoot)
      throws JSONException, URISyntaxException, ServletException {
    Git git = new Git(db);
    RevWalk walk = new RevWalk(db);
    try {
      ObjectId objectId = db.resolve(commitId);
      RevCommit revCommit = walk.lookupCommit(objectId);
      walk.parseBody(revCommit);

      GitTagHandlerV1.tag(git, revCommit, tagName);

      URI cloneLocation =
          BaseToCloneConverter.getCloneLocation(
              getURI(request), BaseToCloneConverter.COMMIT_REFRANGE);
      Commit commit = new Commit(cloneLocation, db, revCommit, null);
      JSONObject result = commit.toJSON();
      OrionServlet.writeJSONResponse(
          request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT);
      return true;
    } catch (IOException e) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              "An error occured when tagging.",
              e));
    } catch (GitAPIException e) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              "An error occured when tagging.",
              e));
    } catch (CoreException e) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              "An error occured when tagging.",
              e));
    } finally {
      walk.dispose();
    }
  }
  private boolean cherryPick(
      HttpServletRequest request,
      HttpServletResponse response,
      Repository db,
      String commitToCherryPick)
      throws ServletException, JSONException {
    RevWalk revWalk = new RevWalk(db);
    try {

      Ref headRef = db.getRef(Constants.HEAD);
      if (headRef == null)
        return statusHandler.handleRequest(
            request,
            response,
            new ServerStatus(
                IStatus.ERROR,
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "An error occured when cherry-picking.",
                null));
      RevCommit head = revWalk.parseCommit(headRef.getObjectId());

      ObjectId objectId = db.resolve(commitToCherryPick);
      Git git = new Git(db);
      CherryPickResult cherryPickResult = git.cherryPick().include(objectId).call();
      RevCommit newHead = cherryPickResult.getNewHead();

      JSONObject result = new JSONObject();
      result.put(GitConstants.KEY_RESULT, cherryPickResult.getStatus().name());
      result.put(GitConstants.KEY_HEAD_UPDATED, !head.equals(newHead));
      OrionServlet.writeJSONResponse(
          request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT);
      return true;
    } catch (IOException e) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              "An error occured when cherry-picking.",
              e));
    } catch (GitAPIException e) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              "An error occured when cherry-picking.",
              e));
    } finally {
      revWalk.release();
    }
  }
 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();
   }
 }
  protected static RevCommit resolveCommitIdByTagName(Repository repository, String tagName)
      throws IOException, GitAPIException {
    if (tagName == null || tagName.isEmpty()) return null;
    RevCommit revCommit = null;
    Map<String, Ref> tagMap = repository.getTags();
    Ref ref = tagMap.get(tagName);
    if (ref != null) {
      RevWalk walk = new RevWalk(repository);
      // some reduce memory effors as described in jgit user guide
      walk.setRetainBody(false);
      ObjectId from;

      from = repository.resolve("refs/heads/master");
      if (from == null) {
        Git git = new Git(repository);
        String lastTagName = git.describe().call();
        from = repository.resolve("refs/tags/" + lastTagName);
      }
      ObjectId to = repository.resolve("refs/remotes/origin/master");

      if (from == null) {
        throw new IllegalStateException("cannot determinate start commit");
      }
      walk.markStart(walk.parseCommit(from));
      walk.markUninteresting(walk.parseCommit(to));
      try {

        RevObject revObject = walk.parseAny(ref.getObjectId());
        if (revObject != null) {
          revCommit = walk.parseCommit(revObject.getId());
        }

      } finally {
        walk.close();
      }
    }

    return revCommit;
  }
  public static Set<String> extractJiraIssues(
      Repository repository, String sinceTagName, String untilTagName, String pattern)
      throws IOException, GitAPIException {
    Git git = new Git(repository);
    RevCommit startCommitId = resolveCommitIdByTagName(repository, sinceTagName);
    if (startCommitId == null) {
      throw new IOException("cannot resolveCommitIdByTagName by  " + sinceTagName);
    }
    ObjectId endCommitId = resolveCommitIdByTagName(repository, untilTagName);
    if (endCommitId == null) {
      endCommitId = repository.resolve(Constants.HEAD);
    }
    Iterable<RevCommit> commits = git.log().addRange(startCommitId, endCommitId).call();

    return extractJiraIssues(pattern, commits);
  }
 private boolean merge(
     HttpServletRequest request,
     HttpServletResponse response,
     Repository db,
     String commitToMerge,
     boolean squash)
     throws ServletException, JSONException {
   try {
     ObjectId objectId = db.resolve(commitToMerge);
     Git git = new Git(db);
     MergeResult mergeResult = git.merge().setSquash(squash).include(objectId).call();
     JSONObject result = new JSONObject();
     result.put(GitConstants.KEY_RESULT, mergeResult.getMergeStatus().name());
     if (mergeResult.getFailingPaths() != null && !mergeResult.getFailingPaths().isEmpty())
       result.put(GitConstants.KEY_FAILING_PATHS, mergeResult.getFailingPaths());
     OrionServlet.writeJSONResponse(
         request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT);
     return true;
   } catch (CheckoutConflictException e) {
     return workaroundBug356918(request, response, e);
   } catch (IOException e) {
     return statusHandler.handleRequest(
         request,
         response,
         new ServerStatus(
             IStatus.ERROR,
             HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
             "An error occured when merging.",
             e));
   } catch (GitAPIException e) {
     return statusHandler.handleRequest(
         request,
         response,
         new ServerStatus(
             IStatus.ERROR,
             HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
             "An error occured when merging.",
             e));
   }
 }
  private boolean handleGetCommitBody(
      HttpServletRequest request,
      HttpServletResponse response,
      Repository db,
      String ref,
      String pattern)
      throws IOException, ServletException, CoreException {
    ObjectId refId = db.resolve(ref);
    if (refId == null) {
      String msg = NLS.bind("Failed to get commit body for ref {0}", ref);
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
    }
    RevWalk walk = new RevWalk(db);
    walk.setTreeFilter(
        AndTreeFilter.create(
            PathFilterGroup.createFromStrings(Collections.singleton(pattern)),
            TreeFilter.ANY_DIFF));
    RevCommit revCommit = walk.parseCommit(refId);
    walk.dispose();

    Commit commit = new Commit(null /* not needed */, db, revCommit, pattern);
    ObjectStream stream = commit.toObjectStream();
    if (stream == null) {
      String msg = NLS.bind("Commit body for ref {0} not found", ref);
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
    }
    IOUtilities.pipe(stream, response.getOutputStream(), true, false);

    return true;
  }
  private boolean revert(
      HttpServletRequest request,
      HttpServletResponse response,
      Repository db,
      String commitToRevert)
      throws ServletException, JSONException {
    RevWalk revWalk = new RevWalk(db);
    try {

      Ref headRef = db.getRef(Constants.HEAD);
      if (headRef == null)
        return statusHandler.handleRequest(
            request,
            response,
            new ServerStatus(
                IStatus.ERROR,
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "An error occured when reverting.",
                null));

      ObjectId objectId = db.resolve(commitToRevert);
      Git git = new Git(db);

      RevertCommand revertCommand = git.revert().include(objectId);
      RevCommit revertedCommit = revertCommand.call();

      if (revertedCommit == null) {
        JSONObject result = new JSONObject();
        result.put(GitConstants.KEY_RESULT, "FAILURE"); // $NON-NLS-1$
        OrionServlet.writeJSONResponse(
            request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT);
        return true;
      }

      JSONObject result = new JSONObject();
      result.put(GitConstants.KEY_RESULT, "OK"); // $NON-NLS-1$
      OrionServlet.writeJSONResponse(
          request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT);
      return true;
    } catch (IOException e) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              "An error occured when reverting.",
              e));
    } catch (GitAPIException e) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              "An error occured when reverting.",
              e));
    } finally {
      revWalk.release();
    }
  }
 private boolean rebase(
     HttpServletRequest request,
     HttpServletResponse response,
     Repository db,
     String commitToRebase,
     String rebaseOperation)
     throws ServletException, JSONException, AmbiguousObjectException, IOException {
   JSONObject result = new JSONObject();
   try {
     Git git = new Git(db);
     RebaseCommand rebase = git.rebase();
     Operation operation;
     if (rebaseOperation != null) {
       operation = Operation.valueOf(rebaseOperation);
     } else {
       operation = Operation.BEGIN;
     }
     if (commitToRebase != null && !commitToRebase.isEmpty()) {
       ObjectId objectId = db.resolve(commitToRebase);
       rebase.setUpstream(objectId);
     } else if (operation.equals(Operation.BEGIN)) {
       return statusHandler.handleRequest(
           request,
           response,
           new ServerStatus(
               IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Missing commit refId.", null));
     }
     rebase.setOperation(operation);
     RebaseResult rebaseResult = rebase.call();
     result.put(GitConstants.KEY_RESULT, rebaseResult.getStatus().name());
   } catch (UnmergedPathsException e) {
     // this error should be handled by client, so return a proper status
     result.put(GitConstants.KEY_RESULT, AdditionalRebaseStatus.FAILED_UNMERGED_PATHS.name());
   } catch (WrongRepositoryStateException e) {
     // this error should be handled by client, so return a proper status
     result.put(
         GitConstants.KEY_RESULT, AdditionalRebaseStatus.FAILED_WRONG_REPOSITORY_STATE.name());
   } catch (IllegalArgumentException e) {
     return statusHandler.handleRequest(
         request,
         response,
         new ServerStatus(
             IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Invalid rebase operation.", e));
   } catch (GitAPIException e) {
     // get cause and try to handle
     if (e.getCause() instanceof org.eclipse.jgit.errors.CheckoutConflictException) {
       // this error should be handled by client, so return a proper status
       result.put(GitConstants.KEY_RESULT, AdditionalRebaseStatus.FAILED_PENDING_CHANGES.name());
     } else {
       return statusHandler.handleRequest(
           request,
           response,
           new ServerStatus(
               IStatus.ERROR,
               HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
               "An error occured when rebasing.",
               e));
     }
   }
   OrionServlet.writeJSONResponse(
       request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT);
   return true;
 }
  @Override
  protected boolean handlePost(RequestInfo requestInfo) throws ServletException {
    String gitSegment = requestInfo.gitSegment;
    HttpServletRequest request = requestInfo.request;
    HttpServletResponse response = requestInfo.response;
    Repository db = requestInfo.db;
    String pattern = requestInfo.relativePath;
    JSONObject requestObject = requestInfo.getJSONRequest();
    try {
      String commitToMerge = requestObject.optString(GitConstants.KEY_MERGE, null);
      if (commitToMerge != null) {
        boolean squash = requestObject.optBoolean(GitConstants.KEY_SQUASH, false);
        return merge(request, response, db, commitToMerge, squash);
      }

      String commitToRebase = requestObject.optString(GitConstants.KEY_REBASE, null);
      String rebaseOperation = requestObject.optString(GitConstants.KEY_OPERATION, null);
      if (commitToRebase != null) {
        return rebase(request, response, db, commitToRebase, rebaseOperation);
      }

      String commitToCherryPick = requestObject.optString(GitConstants.KEY_CHERRY_PICK, null);
      if (commitToCherryPick != null) {
        return cherryPick(request, response, db, commitToCherryPick);
      }

      String commitToRevert = requestObject.optString(GitConstants.KEY_REVERT, null);
      if (commitToRevert != null) {
        return revert(request, response, db, commitToRevert);
      }

      String newCommit = requestObject.optString(GitConstants.KEY_COMMIT_NEW, null);
      if (newCommit != null) return identifyNewCommitResource(request, response, db, newCommit);

      String reviewReqLogin = requestObject.optString(GitConstants.KEY_REVIEW_REQ_NOTIFY_LOGIN);
      if (reviewReqLogin != null && reviewReqLogin.length() != 0) {
        String reviewReqUrl = requestObject.optString(GitConstants.KEY_REVIEW_REQ_URL);
        String ReviewReqCommit = requestObject.optString(GitConstants.KEY_REVIEW_REQ_COMMIT);
        String ReviewReqAuthorName =
            requestObject.optString(GitConstants.KEY_REVIEW_REQ_AUTHOR_NAME);
        String ReviewMessage = requestObject.optString(GitConstants.KEY_REVIEW_REQ_MESSAGE);
        return sendNotification(
            request,
            response,
            db,
            reviewReqLogin,
            ReviewReqCommit,
            reviewReqUrl,
            ReviewReqAuthorName,
            ReviewMessage);
      }

      ObjectId refId = db.resolve(gitSegment);
      if (refId == null || !Constants.HEAD.equals(gitSegment)) {
        String msg = NLS.bind("Commit failed. Ref must be HEAD and is {0}", gitSegment);
        return statusHandler.handleRequest(
            request,
            response,
            new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
      }

      String message = requestObject.optString(GitConstants.KEY_COMMIT_MESSAGE, null);
      if (message == null || message.isEmpty()) {
        return statusHandler.handleRequest(
            request,
            response,
            new ServerStatus(
                IStatus.ERROR,
                HttpServletResponse.SC_BAD_REQUEST,
                "Missing commit message.",
                null));
      }

      Git git = new Git(db);
      CommitCommand cc = git.commit();
      Config config = git.getRepository().getConfig();

      boolean amend =
          Boolean.parseBoolean(requestObject.optString(GitConstants.KEY_COMMIT_AMEND, null));
      boolean insertChangeId =
          GitUtils.isGerrit(config)
              || Boolean.parseBoolean(requestObject.optString(GitConstants.KEY_CHANGE_ID, null));

      String committerName = requestObject.optString(GitConstants.KEY_COMMITTER_NAME, null);
      String committerEmail = requestObject.optString(GitConstants.KEY_COMMITTER_EMAIL, null);
      String authorName = requestObject.optString(GitConstants.KEY_AUTHOR_NAME, null);
      String authorEmail = requestObject.optString(GitConstants.KEY_AUTHOR_EMAIL, null);

      // workaround of a bug in JGit which causes invalid
      // support of null values of author/committer name/email, see bug 352984
      PersonIdent defPersonIdent = new PersonIdent(db);
      if (committerName == null) committerName = defPersonIdent.getName();
      if (committerEmail == null) committerEmail = defPersonIdent.getEmailAddress();
      if (authorName == null) authorName = committerName;
      if (authorEmail == null) authorEmail = committerEmail;
      cc.setCommitter(committerName, committerEmail);
      cc.setAuthor(authorName, authorEmail);
      if (insertChangeId) cc.setInsertChangeId(true);

      // support for committing by path: "git commit -o path"
      if (!pattern.isEmpty()) {
        cc.setOnly(pattern);
      }

      try {
        // "git commit [--amend] -m '{message}' [-a|{path}]"
        RevCommit lastCommit = cc.setAmend(amend).setMessage(message).call();

        URI cloneLocation =
            BaseToCloneConverter.getCloneLocation(
                getURI(request), BaseToCloneConverter.COMMIT_REFRANGE);
        Commit commit = new Commit(cloneLocation, db, lastCommit, pattern);
        JSONObject result = commit.toJSON();
        OrionServlet.writeJSONResponse(
            request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT);
        return true;
      } catch (GitAPIException e) {
        return statusHandler.handleRequest(
            request,
            response,
            new ServerStatus(
                IStatus.ERROR,
                HttpServletResponse.SC_BAD_REQUEST,
                "An error occured when commiting.",
                e));
      } catch (UnmergedPathException e) {
        return statusHandler.handleRequest(
            request,
            response,
            new ServerStatus(
                IStatus.ERROR,
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "An internal error occured when commiting.",
                e));
      }
    } catch (Exception e) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              "An error occured when requesting a commit info.",
              e));
    }
  }
  private boolean handleGetCommitLog(
      HttpServletRequest request,
      HttpServletResponse response,
      IPath filePath,
      Repository db,
      String refIdsRange,
      String pattern)
      throws AmbiguousObjectException, IOException, ServletException, JSONException,
          URISyntaxException, CoreException {
    int page =
        request.getParameter("page") != null
            ? new Integer(request.getParameter("page")).intValue()
            : 0; //$NON-NLS-1$ //$NON-NLS-2$
    int pageSize =
        request.getParameter("pageSize") != null
            ? new Integer(request.getParameter("pageSize")).intValue()
            : PAGE_SIZE; //$NON-NLS-1$ //$NON-NLS-2$

    ObjectId toObjectId = null;
    ObjectId fromObjectId = null;

    Ref toRefId = null;
    Ref fromRefId = null;

    if (refIdsRange != null) {
      // git log <since>..<until>
      if (refIdsRange.contains("..")) { // $NON-NLS-1$
        String[] commits = refIdsRange.split("\\.\\."); // $NON-NLS-1$
        if (commits.length != 2) {
          String msg = NLS.bind("Failed to generate commit log for ref {0}", refIdsRange);
          return statusHandler.handleRequest(
              request,
              response,
              new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
        }

        fromObjectId = db.resolve(commits[0]);
        fromRefId = db.getRef(commits[0]);
        if (fromObjectId == null) {
          String msg = NLS.bind("Failed to generate commit log for ref {0}", commits[0]);
          return statusHandler.handleRequest(
              request,
              response,
              new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
        }

        toObjectId = db.resolve(commits[1]);
        toRefId = db.getRef(commits[1]);
        if (toObjectId == null) {
          String msg = NLS.bind("No ref or commit found: {0}", commits[1]);
          return statusHandler.handleRequest(
              request,
              response,
              new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
        }
      } else {
        toObjectId = db.resolve(refIdsRange);
        toRefId = db.getRef(refIdsRange);
        if (toObjectId == null) {
          String msg = NLS.bind("No ref or commit found: {0}", refIdsRange);
          return statusHandler.handleRequest(
              request,
              response,
              new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
        }
      }
      toObjectId = getCommitObjectId(db, toObjectId);
    }

    URI baseLocation = getURI(request);
    URI cloneLocation =
        BaseToCloneConverter.getCloneLocation(
            baseLocation,
            refIdsRange == null
                ? BaseToCloneConverter.COMMIT
                : BaseToCloneConverter.COMMIT_REFRANGE);

    LogJob job =
        new LogJob(
            TaskJobHandler.getUserId(request),
            filePath,
            cloneLocation,
            page,
            pageSize,
            toObjectId,
            fromObjectId,
            toRefId,
            fromRefId,
            refIdsRange,
            pattern);
    return TaskJobHandler.handleTaskJob(
        request, response, job, statusHandler, JsonURIUnqualificationStrategy.ALL_NO_GIT);
  }
  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;
  }