@Override
    public ChangeKind load(Key key) throws IOException {
      if (Objects.equal(key.prior, key.next)) {
        return ChangeKind.NO_CODE_CHANGE;
      }

      RevWalk walk = new RevWalk(key.repo);
      try {
        RevCommit prior = walk.parseCommit(key.prior);
        walk.parseBody(prior);
        RevCommit next = walk.parseCommit(key.next);
        walk.parseBody(next);

        if (!next.getFullMessage().equals(prior.getFullMessage())) {
          if (next.getTree() == prior.getTree() && isSameParents(prior, next)) {
            return ChangeKind.NO_CODE_CHANGE;
          } else {
            return ChangeKind.REWORK;
          }
        }

        if (prior.getParentCount() != 1 || next.getParentCount() != 1) {
          // Trivial rebases done by machine only work well on 1 parent.
          return ChangeKind.REWORK;
        }

        if (next.getTree() == prior.getTree() && isSameParents(prior, next)) {
          return ChangeKind.TRIVIAL_REBASE;
        }

        // A trivial rebase can be detected by looking for the next commit
        // having the same tree as would exist when the prior commit is
        // cherry-picked onto the next commit's new first parent.
        ThreeWayMerger merger =
            MergeUtil.newThreeWayMerger(
                key.repo, MergeUtil.createDryRunInserter(), key.strategyName);
        merger.setBase(prior.getParent(0));
        if (merger.merge(next.getParent(0), prior)
            && merger.getResultTreeId().equals(next.getTree())) {
          return ChangeKind.TRIVIAL_REBASE;
        } else {
          return ChangeKind.REWORK;
        }
      } finally {
        key.repo = null;
        walk.release();
      }
    }
예제 #2
0
 // the RevWalk must be reset by the caller upon return!
 private ArrayList<RevCommit> findCommits(
     RevWalk walk,
     ArrayList<RevCommit> included,
     ArrayList<RevCommit> excluded,
     boolean getBody,
     long after)
     throws MissingObjectException, IncorrectObjectTypeException, IOException {
   ArrayList<RevCommit> commits = new ArrayList<RevCommit>();
   commits.ensureCapacity(allBranches.size()); // heuristic workaround
   walk.sort(RevSort.COMMIT_TIME_DESC, true);
   walk.sort(RevSort.TOPO, true);
   walk.setRetainBody(getBody);
   if (after > 0) walk.setRevFilter(CommitTimeRevFilter.after(after));
   else walk.setRevFilter(null);
   walk.markStart(included);
   RevCommit c;
   Iterator<RevCommit> it = excluded.iterator();
   while (it.hasNext()) {
     walk.markUninteresting(it.next());
   }
   it = walk.iterator();
   while (it.hasNext()) {
     c = it.next();
     if (getBody) walk.parseBody(c);
     // addUnique(commits, c); // commits are naturally ordered by SHA-1
     commits.add(c);
   }
   return commits.size() > 0 ? commits : null;
 }
예제 #3
0
  private String summarize(RevWalk rw, List<CodeReviewCommit> merged) throws IOException {
    if (merged.size() == 1) {
      CodeReviewCommit c = merged.get(0);
      rw.parseBody(c);
      return String.format("Merge \"%s\"", c.getShortMessage());
    }

    LinkedHashSet<String> topics = new LinkedHashSet<>(4);
    for (CodeReviewCommit c : merged) {
      if (!Strings.isNullOrEmpty(c.change().getTopic())) {
        topics.add(c.change().getTopic());
      }
    }

    if (topics.size() == 1) {
      return String.format("Merge changes from topic '%s'", Iterables.getFirst(topics, null));
    } else if (topics.size() > 1) {
      return String.format("Merge changes from topics '%s'", Joiner.on("', '").join(topics));
    } else {
      return String.format(
          "Merge changes %s%s",
          Joiner.on(',')
              .join(
                  Iterables.transform(
                      Iterables.limit(merged, 5),
                      new Function<CodeReviewCommit, String>() {
                        @Override
                        public String apply(CodeReviewCommit in) {
                          return in.change().getKey().abbreviate();
                        }
                      })),
          merged.size() > 5 ? ", ..." : "");
    }
  }
예제 #4
0
  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();
    }
  }
 public PatchSetInfo get(RevWalk rw, RevCommit src, PatchSet.Id psi) throws IOException {
   rw.parseBody(src);
   PatchSetInfo info = new PatchSetInfo(psi);
   info.setSubject(src.getShortMessage());
   info.setMessage(src.getFullMessage());
   info.setAuthor(toUserIdentity(src.getAuthorIdent()));
   info.setCommitter(toUserIdentity(src.getCommitterIdent()));
   info.setRevId(src.getName());
   return info;
 }
 private List<PatchSetInfo.ParentInfo> toParentInfos(final RevCommit[] parents, final RevWalk walk)
     throws IOException, MissingObjectException {
   List<PatchSetInfo.ParentInfo> pInfos = new ArrayList<>(parents.length);
   for (RevCommit parent : parents) {
     walk.parseBody(parent);
     RevId rev = new RevId(parent.getId().name());
     String msg = parent.getShortMessage();
     pInfos.add(new PatchSetInfo.ParentInfo(rev, msg));
   }
   return pInfos;
 }
예제 #7
0
 /**
  * Ensure the body of the given object has been parsed.
  *
  * @param <T> type of object, e.g. {@link RevTag} or {@link RevCommit}.
  * @param object reference to the (possibly unparsed) object to force body parsing of.
  * @return {@code object}
  * @throws Exception
  */
 public <T extends RevObject> T parseBody(final T object) throws Exception {
   pool.parseBody(object);
   return object;
 }