Search in sources :

Example 1 with InvalidObjectIdException

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

the class PureRevert method get.

public boolean get(ChangeNotes notes, Optional<String> claimedOriginal) throws IOException, BadRequestException, ResourceConflictException {
    PatchSet currentPatchSet = notes.getCurrentPatchSet();
    if (currentPatchSet == null) {
        throw new ResourceConflictException("current revision is missing");
    }
    if (!claimedOriginal.isPresent()) {
        return pureRevertCache.isPureRevert(notes);
    }
    ObjectId claimedOriginalObjectId;
    try {
        claimedOriginalObjectId = ObjectId.fromString(claimedOriginal.get());
    } catch (InvalidObjectIdException e) {
        throw new BadRequestException("invalid object ID", e);
    }
    return pureRevertCache.isPureRevert(notes.getProjectName(), notes.getCurrentPatchSet().commitId(), claimedOriginalObjectId);
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PatchSet(com.google.gerrit.entities.PatchSet) InvalidObjectIdException(org.eclipse.jgit.errors.InvalidObjectIdException)

Example 2 with InvalidObjectIdException

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

the class GetMetaDiff method getOldMetaRevId.

private String getOldMetaRevId(ChangeResource resource) throws IOException, BadRequestException, PreconditionFailedException {
    if (!oldMetaRevId.isEmpty()) {
        return oldMetaRevId;
    }
    String newMetaRevId = getNewMetaRevId(resource);
    try (Repository repo = repoManager.openRepository(resource.getProject());
        RevWalk rw = new RevWalk(repo)) {
        ObjectId resourceId = ObjectId.fromString(newMetaRevId);
        RevCommit commit = rw.parseCommit(resourceId);
        return commit.getParentCount() == 0 ? resourceId.getName() : commit.getParent(0).getId().getName();
    } catch (InvalidObjectIdException e) {
        throw new BadRequestException("invalid meta SHA1: " + newMetaRevId, e);
    } catch (MissingObjectException e) {
        throw new PreconditionFailedException(e.getMessage());
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PreconditionFailedException(com.google.gerrit.extensions.restapi.PreconditionFailedException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) InvalidObjectIdException(org.eclipse.jgit.errors.InvalidObjectIdException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 3 with InvalidObjectIdException

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

the class PatchSetEvent method setRevision.

private void setRevision(ChangeUpdate update, PatchSet ps) throws IOException {
    String rev = ps.getRevision().get();
    String cert = ps.getPushCertificate();
    ObjectId id;
    try {
        id = ObjectId.fromString(rev);
    } catch (InvalidObjectIdException e) {
        update.setRevisionForMissingCommit(rev, cert);
        return;
    }
    try {
        update.setCommit(rw, id, cert);
    } catch (MissingObjectException e) {
        update.setRevisionForMissingCommit(rev, cert);
        return;
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) InvalidObjectIdException(org.eclipse.jgit.errors.InvalidObjectIdException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException)

Example 4 with InvalidObjectIdException

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

the class CreateChange method getParentCommit.

@Nullable
private ObjectId getParentCommit(Repository repo, RevWalk revWalk, String inputBranch, @Nullable Boolean newBranch, @Nullable PatchSet basePatchSet, @Nullable String baseCommit, @Nullable MergeInput mergeInput) throws BadRequestException, IOException, UnprocessableEntityException, ResourceConflictException {
    if (basePatchSet != null) {
        return basePatchSet.commitId();
    }
    Ref destRef = repo.getRefDatabase().exactRef(inputBranch);
    ObjectId parentCommit;
    if (baseCommit != null) {
        try {
            parentCommit = ObjectId.fromString(baseCommit);
        } catch (InvalidObjectIdException e) {
            throw new UnprocessableEntityException(String.format("Base %s doesn't represent a valid SHA-1", baseCommit), e);
        }
        RevCommit parentRevCommit;
        try {
            parentRevCommit = revWalk.parseCommit(parentCommit);
        } catch (MissingObjectException e) {
            throw new UnprocessableEntityException(String.format("Base %s doesn't exist", baseCommit), e);
        }
        if (destRef == null) {
            throw new BadRequestException("Destination branch does not exist");
        }
        RevCommit destRefRevCommit = revWalk.parseCommit(destRef.getObjectId());
        if (!revWalk.isMergedInto(parentRevCommit, destRefRevCommit)) {
            throw new BadRequestException(String.format("Commit %s doesn't exist on ref %s", baseCommit, inputBranch));
        }
    } else {
        if (destRef != null) {
            if (Boolean.TRUE.equals(newBranch)) {
                throw new ResourceConflictException(String.format("Branch %s already exists.", inputBranch));
            }
            parentCommit = destRef.getObjectId();
        } else {
            if (Boolean.TRUE.equals(newBranch)) {
                if (mergeInput != null) {
                    throw new BadRequestException("Cannot create merge: destination branch does not exist");
                }
                parentCommit = null;
            } else {
                throw new BadRequestException("Destination branch does not exist");
            }
        }
    }
    return parentCommit;
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Ref(org.eclipse.jgit.lib.Ref) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) InvalidObjectIdException(org.eclipse.jgit.errors.InvalidObjectIdException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Nullable(com.google.gerrit.common.Nullable)

Example 5 with InvalidObjectIdException

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

the class CherryPickChange method getBaseCommit.

private RevCommit getBaseCommit(Ref destRef, String project, RevWalk revWalk, String base) throws RestApiException, IOException {
    RevCommit destRefTip = revWalk.parseCommit(destRef.getObjectId());
    // The tip commit of the destination ref is the default base for the newly created change.
    if (Strings.isNullOrEmpty(base)) {
        return destRefTip;
    }
    ObjectId baseObjectId;
    try {
        baseObjectId = ObjectId.fromString(base);
    } catch (InvalidObjectIdException e) {
        throw new BadRequestException(String.format("Base %s doesn't represent a valid SHA-1", base), e);
    }
    RevCommit baseCommit;
    try {
        baseCommit = revWalk.parseCommit(baseObjectId);
    } catch (MissingObjectException e) {
        throw new UnprocessableEntityException(String.format("Base %s doesn't exist", baseObjectId.name()), e);
    }
    InternalChangeQuery changeQuery = queryProvider.get();
    changeQuery.enforceVisibility(true);
    List<ChangeData> changeDatas = changeQuery.byBranchCommit(project, destRef.getName(), base);
    if (changeDatas.isEmpty()) {
        if (revWalk.isMergedInto(baseCommit, destRefTip)) {
            // The base commit is a merged commit with no change associated.
            return baseCommit;
        }
        throw new UnprocessableEntityException(String.format("Commit %s does not exist on branch %s", base, destRef.getName()));
    } else if (changeDatas.size() != 1) {
        throw new ResourceConflictException("Multiple changes found for commit " + base);
    }
    Change change = changeDatas.get(0).change();
    if (!change.isAbandoned()) {
        // The base commit is a valid change revision.
        return baseCommit;
    }
    throw new ResourceConflictException(String.format("Change %s with commit %s is %s", change.getChangeId(), base, ChangeUtil.status(change)));
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Change(com.google.gerrit.entities.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) InvalidObjectIdException(org.eclipse.jgit.errors.InvalidObjectIdException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

InvalidObjectIdException (org.eclipse.jgit.errors.InvalidObjectIdException)5 ObjectId (org.eclipse.jgit.lib.ObjectId)5 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)4 MissingObjectException (org.eclipse.jgit.errors.MissingObjectException)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)2 Nullable (com.google.gerrit.common.Nullable)1 Change (com.google.gerrit.entities.Change)1 PatchSet (com.google.gerrit.entities.PatchSet)1 PreconditionFailedException (com.google.gerrit.extensions.restapi.PreconditionFailedException)1 ChangeData (com.google.gerrit.server.query.change.ChangeData)1 InternalChangeQuery (com.google.gerrit.server.query.change.InternalChangeQuery)1 Ref (org.eclipse.jgit.lib.Ref)1 Repository (org.eclipse.jgit.lib.Repository)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1