Search in sources :

Example 11 with MissingObjectException

use of org.eclipse.jgit.errors.MissingObjectException 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 12 with MissingObjectException

use of org.eclipse.jgit.errors.MissingObjectException 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 13 with MissingObjectException

use of org.eclipse.jgit.errors.MissingObjectException 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 14 with MissingObjectException

use of org.eclipse.jgit.errors.MissingObjectException 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)

Example 15 with MissingObjectException

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

the class ReceiveCommits method parseMagicBranch.

private void parseMagicBranch(ReceiveCommand cmd) {
    // Permit exactly one new change request per push.
    if (magicBranch != null) {
        reject(cmd, "duplicate request");
        return;
    }
    logDebug("Found magic branch {}", cmd.getRefName());
    magicBranch = new MagicBranchInput(user, cmd, labelTypes, notesMigration);
    magicBranch.reviewer.addAll(reviewersFromCommandLine);
    magicBranch.cc.addAll(ccFromCommandLine);
    String ref;
    CmdLineParser clp = optionParserFactory.create(magicBranch);
    magicBranch.clp = clp;
    try {
        ref = magicBranch.parse(clp, repo, rp.getAdvertisedRefs().keySet(), pushOptions);
    } catch (CmdLineException e) {
        if (!clp.wasHelpRequestedByOption()) {
            logDebug("Invalid branch syntax");
            reject(cmd, e.getMessage());
            return;
        }
        // never happen
        ref = null;
    }
    if (clp.wasHelpRequestedByOption()) {
        StringWriter w = new StringWriter();
        w.write("\nHelp for refs/for/branch:\n\n");
        clp.printUsage(w, null);
        addMessage(w.toString());
        reject(cmd, "see help");
        return;
    }
    if (projectControl.getProjectState().isAllUsers() && RefNames.REFS_USERS_SELF.equals(ref)) {
        logDebug("Handling {}", RefNames.REFS_USERS_SELF);
        ref = RefNames.refsUsers(user.getAccountId());
    }
    if (!rp.getAdvertisedRefs().containsKey(ref) && !ref.equals(readHEAD(repo))) {
        logDebug("Ref {} not found", ref);
        if (ref.startsWith(Constants.R_HEADS)) {
            String n = ref.substring(Constants.R_HEADS.length());
            reject(cmd, "branch " + n + " not found");
        } else {
            reject(cmd, ref + " not found");
        }
        return;
    }
    magicBranch.dest = new Branch.NameKey(project.getNameKey(), ref);
    magicBranch.ctl = projectControl.controlForRef(ref);
    if (projectControl.getProject().getState() != com.google.gerrit.extensions.client.ProjectState.ACTIVE) {
        reject(cmd, "project is read only");
        return;
    }
    if (magicBranch.draft) {
        if (!receiveConfig.allowDrafts) {
            errors.put(Error.CODE_REVIEW, ref);
            reject(cmd, "draft workflow is disabled");
            return;
        } else if (projectControl.controlForRef(MagicBranch.NEW_DRAFT_CHANGE + ref).isBlocked(Permission.PUSH)) {
            errors.put(Error.CODE_REVIEW, ref);
            reject(cmd, "cannot upload drafts");
            return;
        }
    }
    if (!magicBranch.ctl.canUpload()) {
        errors.put(Error.CODE_REVIEW, ref);
        reject(cmd, "cannot upload review");
        return;
    }
    if (magicBranch.isPrivate && magicBranch.removePrivate) {
        reject(cmd, "the options 'private' and 'remove-private' are mutually exclusive");
        return;
    }
    if (magicBranch.workInProgress && magicBranch.ready) {
        reject(cmd, "the options 'wip' and 'ready' are mutually exclusive");
        return;
    }
    if (magicBranch.publishComments && magicBranch.noPublishComments) {
        reject(cmd, "the options 'publish-comments' and 'no-publish-comments' are mutually exclusive");
        return;
    }
    if (magicBranch.draft && magicBranch.submit) {
        reject(cmd, "cannot submit draft");
        return;
    }
    if (magicBranch.submit && !projectControl.controlForRef(MagicBranch.NEW_CHANGE + ref).canSubmit(true)) {
        reject(cmd, "submit not allowed");
        return;
    }
    RevWalk walk = rp.getRevWalk();
    RevCommit tip;
    try {
        tip = walk.parseCommit(magicBranch.cmd.getNewId());
        logDebug("Tip of push: {}", tip.name());
    } catch (IOException ex) {
        magicBranch.cmd.setResult(REJECTED_MISSING_OBJECT);
        logError("Invalid pack upload; one or more objects weren't sent", ex);
        return;
    }
    String destBranch = magicBranch.dest.get();
    try {
        if (magicBranch.merged) {
            if (magicBranch.draft) {
                reject(cmd, "cannot be draft & merged");
                return;
            }
            if (magicBranch.base != null) {
                reject(cmd, "cannot use merged with base");
                return;
            }
            RevCommit branchTip = readBranchTip(cmd, magicBranch.dest);
            if (branchTip == null) {
                // readBranchTip already rejected cmd.
                return;
            }
            if (!walk.isMergedInto(tip, branchTip)) {
                reject(cmd, "not merged into branch");
                return;
            }
        }
        // if %base or %merged was specified, ignore newChangeForAllNotInTarget.
        if (tip.getParentCount() > 1 || magicBranch.base != null || magicBranch.merged || tip.getParentCount() == 0) {
            logDebug("Forcing newChangeForAllNotInTarget = false");
            newChangeForAllNotInTarget = false;
        }
        if (magicBranch.base != null) {
            logDebug("Handling %base: {}", magicBranch.base);
            magicBranch.baseCommit = Lists.newArrayListWithCapacity(magicBranch.base.size());
            for (ObjectId id : magicBranch.base) {
                try {
                    magicBranch.baseCommit.add(walk.parseCommit(id));
                } catch (IncorrectObjectTypeException notCommit) {
                    reject(cmd, "base must be a commit");
                    return;
                } catch (MissingObjectException e) {
                    reject(cmd, "base not found");
                    return;
                } catch (IOException e) {
                    logWarn(String.format("Project %s cannot read %s", project.getName(), id.name()), e);
                    reject(cmd, "internal server error");
                    return;
                }
            }
        } else if (newChangeForAllNotInTarget) {
            RevCommit branchTip = readBranchTip(cmd, magicBranch.dest);
            if (branchTip == null) {
                // readBranchTip already rejected cmd.
                return;
            }
            magicBranch.baseCommit = Collections.singletonList(branchTip);
            logDebug("Set baseCommit = {}", magicBranch.baseCommit.get(0).name());
        }
    } catch (IOException ex) {
        logWarn(String.format("Error walking to %s in project %s", destBranch, project.getName()), ex);
        reject(cmd, "internal server error");
        return;
    }
    //
    try {
        Ref targetRef = rp.getAdvertisedRefs().get(magicBranch.ctl.getRefName());
        if (targetRef == null || targetRef.getObjectId() == null) {
            // The destination branch does not yet exist. Assume the
            // history being sent for review will start it and thus
            // is "connected" to the branch.
            logDebug("Branch is unborn");
            return;
        }
        RevCommit h = walk.parseCommit(targetRef.getObjectId());
        logDebug("Current branch tip: {}", h.name());
        RevFilter oldRevFilter = walk.getRevFilter();
        try {
            walk.reset();
            walk.setRevFilter(RevFilter.MERGE_BASE);
            walk.markStart(tip);
            walk.markStart(h);
            if (walk.next() == null) {
                reject(magicBranch.cmd, "no common ancestry");
            }
        } finally {
            walk.reset();
            walk.setRevFilter(oldRevFilter);
        }
    } catch (IOException e) {
        magicBranch.cmd.setResult(REJECTED_MISSING_OBJECT);
        logError("Invalid pack upload; one or more objects weren't sent", e);
    }
}
Also used : CmdLineParser(com.google.gerrit.util.cli.CmdLineParser) ObjectId(org.eclipse.jgit.lib.ObjectId) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Ref(org.eclipse.jgit.lib.Ref) StringWriter(java.io.StringWriter) RevFilter(org.eclipse.jgit.revwalk.filter.RevFilter) MagicBranch(com.google.gerrit.server.util.MagicBranch) Branch(com.google.gerrit.reviewdb.client.Branch) CmdLineException(org.kohsuke.args4j.CmdLineException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

MissingObjectException (org.eclipse.jgit.errors.MissingObjectException)31 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)22 ObjectId (org.eclipse.jgit.lib.ObjectId)17 RevCommit (org.eclipse.jgit.revwalk.RevCommit)16 RevWalk (org.eclipse.jgit.revwalk.RevWalk)16 IOException (java.io.IOException)13 Repository (org.eclipse.jgit.lib.Repository)12 ArrayList (java.util.ArrayList)7 Ref (org.eclipse.jgit.lib.Ref)6 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)5 Map (java.util.Map)5 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)4 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Iterables (com.google.common.collect.Iterables)3 Nullable (com.google.gerrit.common.Nullable)3 Change (com.google.gerrit.entities.Change)3 PatchSet (com.google.gerrit.entities.PatchSet)3 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)3 InvalidObjectIdException (org.eclipse.jgit.errors.InvalidObjectIdException)3