Search in sources :

Example 21 with IncorrectObjectTypeException

use of org.eclipse.jgit.errors.IncorrectObjectTypeException in project gerrit by GerritCodeReview.

the class MergeOp method getAlreadyAccepted.

private Set<RevCommit> getAlreadyAccepted(OpenRepo or, CodeReviewCommit branchTip) throws IntegrationException {
    Set<RevCommit> alreadyAccepted = new HashSet<>();
    if (branchTip != null) {
        alreadyAccepted.add(branchTip);
    }
    try {
        for (Ref r : or.repo.getRefDatabase().getRefs(Constants.R_HEADS).values()) {
            try {
                CodeReviewCommit aac = or.rw.parseCommit(r.getObjectId());
                if (!commitStatus.commits.values().contains(aac)) {
                    alreadyAccepted.add(aac);
                }
            } catch (IncorrectObjectTypeException iote) {
            // Not a commit? Skip over it.
            }
        }
    } catch (IOException e) {
        throw new IntegrationException("Failed to determine already accepted commits.", e);
    }
    logDebug("Found {} existing heads", alreadyAccepted.size());
    return alreadyAccepted;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IOException(java.io.IOException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 22 with IncorrectObjectTypeException

use of org.eclipse.jgit.errors.IncorrectObjectTypeException in project gerrit by GerritCodeReview.

the class BanCommit method ban.

public BanCommitResult ban(final ProjectControl projectControl, final List<ObjectId> commitsToBan, final String reason) throws PermissionDeniedException, IOException, ConcurrentRefUpdateException {
    if (!projectControl.isOwner()) {
        throw new PermissionDeniedException("Not project owner: not permitted to ban commits");
    }
    final BanCommitResult result = new BanCommitResult();
    NoteMap banCommitNotes = NoteMap.newEmptyMap();
    // Add a note for each banned commit to notes.
    final Project.NameKey project = projectControl.getProject().getNameKey();
    try (Repository repo = repoManager.openRepository(project);
        RevWalk revWalk = new RevWalk(repo);
        ObjectInserter inserter = repo.newObjectInserter()) {
        ObjectId noteId = null;
        for (final ObjectId commitToBan : commitsToBan) {
            try {
                revWalk.parseCommit(commitToBan);
            } catch (MissingObjectException e) {
            // Ignore exception, non-existing commits can be banned.
            } catch (IncorrectObjectTypeException e) {
                result.notACommit(commitToBan);
                continue;
            }
            if (noteId == null) {
                noteId = createNoteContent(reason, inserter);
            }
            banCommitNotes.set(commitToBan, noteId);
        }
        NotesBranchUtil notesBranchUtil = notesBranchUtilFactory.create(project, repo, inserter);
        NoteMap newlyCreated = notesBranchUtil.commitNewNotes(banCommitNotes, REFS_REJECT_COMMITS, createPersonIdent(), buildCommitMessage(commitsToBan, reason));
        for (Note n : banCommitNotes) {
            if (newlyCreated.contains(n)) {
                result.commitBanned(n);
            } else {
                result.commitAlreadyBanned(n);
            }
        }
        return result;
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) Note(org.eclipse.jgit.notes.Note) PermissionDeniedException(com.google.gerrit.common.errors.PermissionDeniedException)

Example 23 with IncorrectObjectTypeException

use of org.eclipse.jgit.errors.IncorrectObjectTypeException in project gerrit by GerritCodeReview.

the class CommitsCollection method parse.

@Override
public CommitResource parse(ProjectResource parent, IdString id) throws ResourceNotFoundException, IOException {
    ObjectId objectId;
    try {
        objectId = ObjectId.fromString(id.get());
    } catch (IllegalArgumentException e) {
        throw new ResourceNotFoundException(id);
    }
    try (Repository repo = repoManager.openRepository(parent.getNameKey());
        RevWalk rw = new RevWalk(repo)) {
        RevCommit commit = rw.parseCommit(objectId);
        rw.parseBody(commit);
        if (!parent.getControl().canReadCommit(db.get(), repo, commit)) {
            throw new ResourceNotFoundException(id);
        }
        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);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) 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

IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)23 MissingObjectException (org.eclipse.jgit.errors.MissingObjectException)13 RevWalk (org.eclipse.jgit.revwalk.RevWalk)13 IOException (java.io.IOException)10 Repository (org.eclipse.jgit.lib.Repository)10 ObjectId (org.eclipse.jgit.lib.ObjectId)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)9 Ref (org.eclipse.jgit.lib.Ref)4 RevTree (org.eclipse.jgit.revwalk.RevTree)4 Branch (com.google.gerrit.reviewdb.client.Branch)3 MoreObjects.firstNonNull (com.google.common.base.MoreObjects.firstNonNull)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)2 Project (com.google.gerrit.reviewdb.client.Project)2 Map (java.util.Map)2 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)2