Search in sources :

Example 1 with CommitResource

use of com.google.gerrit.server.project.CommitResource in project gerrit by GerritCodeReview.

the class CommitsCollection method parse.

@Override
public CommitResource parse(ProjectResource parent, IdString id) throws RestApiException, IOException {
    parent.getProjectState().checkStatePermitsRead();
    ObjectId objectId;
    try {
        objectId = ObjectId.fromString(id.get());
    } catch (IllegalArgumentException e) {
        throw new ResourceNotFoundException(id, e);
    }
    try (Repository repo = repoManager.openRepository(parent.getNameKey());
        RevWalk rw = new RevWalk(repo)) {
        RevCommit commit = rw.parseCommit(objectId);
        if (!canRead(parent.getProjectState(), repo, commit)) {
            throw new ResourceNotFoundException(id);
        }
        // GetCommit depends on the body of both the commit and parent being parsed, to get the
        // subject.
        rw.parseBody(commit);
        for (int i = 0; i < commit.getParentCount(); i++) {
            rw.parseBody(rw.parseCommit(commit.getParent(i)));
        }
        return new CommitResource(parent, commit);
    } catch (MissingObjectException | IncorrectObjectTypeException e) {
        throw new ResourceNotFoundException(id, e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) CommitResource(com.google.gerrit.server.project.CommitResource) ObjectId(org.eclipse.jgit.lib.ObjectId) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 CommitResource (com.google.gerrit.server.project.CommitResource)1 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)1 MissingObjectException (org.eclipse.jgit.errors.MissingObjectException)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 Repository (org.eclipse.jgit.lib.Repository)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1