Search in sources :

Example 6 with IncorrectObjectTypeException

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

the class DashboardsCollection method parse.

@Override
public DashboardResource parse(ProjectResource parent, IdString id) throws ResourceNotFoundException, IOException, ConfigInvalidException {
    ProjectControl myCtl = parent.getControl();
    if (id.toString().equals("default")) {
        return DashboardResource.projectDefault(myCtl);
    }
    List<String> parts = Lists.newArrayList(Splitter.on(':').limit(2).split(id.get()));
    if (parts.size() != 2) {
        throw new ResourceNotFoundException(id);
    }
    CurrentUser user = myCtl.getUser();
    String ref = parts.get(0);
    String path = parts.get(1);
    for (ProjectState ps : myCtl.getProjectState().tree()) {
        try {
            return parse(ps.controlFor(user), ref, path, myCtl);
        } catch (AmbiguousObjectException | ConfigInvalidException | IncorrectObjectTypeException e) {
            throw new ResourceNotFoundException(id);
        } catch (ResourceNotFoundException e) {
            continue;
        }
    }
    throw new ResourceNotFoundException(id);
}
Also used : AmbiguousObjectException(org.eclipse.jgit.errors.AmbiguousObjectException) CurrentUser(com.google.gerrit.server.CurrentUser) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IdString(com.google.gerrit.extensions.restapi.IdString) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 7 with IncorrectObjectTypeException

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

the class CreateBranch method apply.

@Override
public BranchInfo apply(ProjectResource rsrc, BranchInput input) throws BadRequestException, AuthException, ResourceConflictException, IOException {
    if (input == null) {
        input = new BranchInput();
    }
    if (input.ref != null && !ref.equals(input.ref)) {
        throw new BadRequestException("ref must match URL");
    }
    if (input.revision == null) {
        input.revision = Constants.HEAD;
    }
    while (ref.startsWith("/")) {
        ref = ref.substring(1);
    }
    ref = RefNames.fullName(ref);
    if (!Repository.isValidRefName(ref)) {
        throw new BadRequestException("invalid branch name \"" + ref + "\"");
    }
    if (MagicBranch.isMagicBranch(ref)) {
        throw new BadRequestException("not allowed to create branches under \"" + MagicBranch.getMagicRefNamePrefix(ref) + "\"");
    }
    final Branch.NameKey name = new Branch.NameKey(rsrc.getNameKey(), ref);
    final RefControl refControl = rsrc.getControl().controlForRef(name);
    try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
        ObjectId revid = RefUtil.parseBaseRevision(repo, rsrc.getNameKey(), input.revision);
        RevWalk rw = RefUtil.verifyConnected(repo, revid);
        RevObject object = rw.parseAny(revid);
        if (ref.startsWith(Constants.R_HEADS)) {
            //
            try {
                object = rw.parseCommit(object);
            } catch (IncorrectObjectTypeException notCommit) {
                throw new BadRequestException("\"" + input.revision + "\" not a commit");
            }
        }
        if (!refControl.canCreate(db.get(), repo, object)) {
            throw new AuthException("Cannot create \"" + ref + "\"");
        }
        try {
            final RefUpdate u = repo.updateRef(ref);
            u.setExpectedOldObjectId(ObjectId.zeroId());
            u.setNewObjectId(object.copy());
            u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
            u.setRefLogMessage("created via REST from " + input.revision, false);
            refCreationValidator.validateRefOperation(rsrc.getName(), identifiedUser.get(), u);
            final RefUpdate.Result result = u.update(rw);
            switch(result) {
                case FAST_FORWARD:
                case NEW:
                case NO_CHANGE:
                    referenceUpdated.fire(name.getParentKey(), u, ReceiveCommand.Type.CREATE, identifiedUser.get().getAccount());
                    break;
                case LOCK_FAILURE:
                    if (repo.getRefDatabase().exactRef(ref) != null) {
                        throw new ResourceConflictException("branch \"" + ref + "\" already exists");
                    }
                    String refPrefix = RefUtil.getRefPrefix(ref);
                    while (!Constants.R_HEADS.equals(refPrefix)) {
                        if (repo.getRefDatabase().exactRef(refPrefix) != null) {
                            throw new ResourceConflictException("Cannot create branch \"" + ref + "\" since it conflicts with branch \"" + refPrefix + "\".");
                        }
                        refPrefix = RefUtil.getRefPrefix(refPrefix);
                    }
                //$FALL-THROUGH$
                case FORCED:
                case IO_FAILURE:
                case NOT_ATTEMPTED:
                case REJECTED:
                case REJECTED_CURRENT_BRANCH:
                case RENAMED:
                default:
                    {
                        throw new IOException(result.name());
                    }
            }
            BranchInfo info = new BranchInfo();
            info.ref = ref;
            info.revision = revid.getName();
            info.canDelete = permissionBackend.user(identifiedUser).ref(name).testOrFalse(RefPermission.DELETE) ? true : null;
            return info;
        } catch (IOException err) {
            log.error("Cannot create branch \"" + name + "\"", err);
            throw err;
        }
    } catch (RefUtil.InvalidRevisionException e) {
        throw new BadRequestException("invalid revision \"" + input.revision + "\"");
    }
}
Also used : BranchInfo(com.google.gerrit.extensions.api.projects.BranchInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) RevObject(org.eclipse.jgit.revwalk.RevObject) AuthException(com.google.gerrit.extensions.restapi.AuthException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) MagicBranch(com.google.gerrit.server.util.MagicBranch) Branch(com.google.gerrit.reviewdb.client.Branch) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) BranchInput(com.google.gerrit.extensions.api.projects.BranchInput) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 8 with IncorrectObjectTypeException

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

the class WalkSorter method byCommit.

private ListMultimap<RevCommit, PatchSetData> byCommit(RevWalk rw, Collection<ChangeData> in) throws OrmException, IOException {
    ListMultimap<RevCommit, PatchSetData> byCommit = MultimapBuilder.hashKeys(in.size()).arrayListValues(1).build();
    for (ChangeData cd : in) {
        PatchSet maxPs = null;
        for (PatchSet ps : cd.patchSets()) {
            if (shouldInclude(ps) && (maxPs == null || ps.getId().get() > maxPs.getId().get())) {
                maxPs = ps;
            }
        }
        if (maxPs == null) {
            // No patch sets matched.
            continue;
        }
        ObjectId id = ObjectId.fromString(maxPs.getRevision().get());
        try {
            RevCommit c = rw.parseCommit(id);
            byCommit.put(c, PatchSetData.create(cd, maxPs, c));
        } catch (MissingObjectException | IncorrectObjectTypeException e) {
            log.warn("missing commit " + id.name() + " for patch set " + maxPs.getId(), e);
        }
    }
    return byCommit;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) ChangeData(com.google.gerrit.server.query.change.ChangeData) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 9 with IncorrectObjectTypeException

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

the class DiffServlet method doGetText.

@Override
protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);
    try (RevWalk walk = new RevWalk(repo)) {
        AbstractTreeIterator oldTree;
        AbstractTreeIterator newTree;
        try {
            oldTree = getTreeIterator(walk, view.getOldRevision().getId());
            newTree = getTreeIterator(walk, view.getRevision().getId());
        } catch (MissingObjectException | IncorrectObjectTypeException e) {
            res.setStatus(SC_NOT_FOUND);
            return;
        }
        try (Writer writer = startRenderText(req, res);
            OutputStream out = BaseEncoding.base64().encodingStream(writer);
            DiffFormatter diff = new DiffFormatter(out)) {
            formatDiff(repo, oldTree, newTree, view.getPathPart(), diff);
        }
    }
}
Also used : AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) Repository(org.eclipse.jgit.lib.Repository) OutputStream(java.io.OutputStream) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Writer(java.io.Writer)

Example 10 with IncorrectObjectTypeException

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

the class LogServlet method newWalk.

private static RevWalk newWalk(Repository repo, GitilesView view, GitilesAccess access) throws MissingObjectException, IOException {
    RevWalk walk = new RevWalk(repo);
    try {
        walk.markStart(walk.parseCommit(view.getRevision().getId()));
        if (view.getOldRevision() != Revision.NULL) {
            walk.markUninteresting(walk.parseCommit(view.getOldRevision().getId()));
        }
    } catch (IncorrectObjectTypeException iote) {
        return null;
    }
    setTreeFilter(walk, view, access);
    setRevFilter(walk, view);
    if (isTrue(view, "topo-order")) {
        walk.sort(RevSort.TOPO, true);
    }
    if (isTrue(view, "reverse")) {
        walk.sort(RevSort.REVERSE, true);
    }
    return walk;
}
Also used : IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

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