Search in sources :

Example 1 with MissingObjectException

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

the class IncludedInResolver method parseCommits.

/** Parse commit of ref and store the relation between ref and commit. */
private void parseCommits(final Collection<Ref> refs) throws IOException {
    if (commitToRef != null) {
        return;
    }
    commitToRef = LinkedListMultimap.create();
    for (Ref ref : refs) {
        final RevCommit commit;
        try {
            commit = rw.parseCommit(ref.getObjectId());
        } catch (IncorrectObjectTypeException notCommit) {
            //
            continue;
        } catch (MissingObjectException notHere) {
            // Log the problem with this branch, but keep processing.
            //
            log.warn("Reference " + ref.getName() + " in " + repo.getDirectory() + " points to dangling object " + ref.getObjectId());
            continue;
        }
        commitToRef.put(commit, ref.getName());
    }
    tipsByCommitTime = Lists.newArrayList(commitToRef.keySet());
    sortOlderFirst(tipsByCommitTime);
}
Also used : Ref(org.eclipse.jgit.lib.Ref) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with MissingObjectException

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

the class SubmitStrategyOp method getAlreadyMergedCommit.

private CodeReviewCommit getAlreadyMergedCommit(RepoContext ctx) throws IOException {
    CodeReviewCommit tip = args.mergeTip.getInitialTip();
    if (tip == null) {
        return null;
    }
    CodeReviewRevWalk rw = (CodeReviewRevWalk) ctx.getRevWalk();
    Change.Id id = getId();
    String refPrefix = id.toRefPrefix();
    Map<String, ObjectId> refs = ctx.getRepoView().getRefs(refPrefix);
    List<CodeReviewCommit> commits = new ArrayList<>(refs.size());
    for (Map.Entry<String, ObjectId> e : refs.entrySet()) {
        PatchSet.Id psId = PatchSet.Id.fromRef(refPrefix + e.getKey());
        if (psId == null) {
            continue;
        }
        try {
            CodeReviewCommit c = rw.parseCommit(e.getValue());
            c.setPatchsetId(psId);
            commits.add(c);
        } catch (MissingObjectException | IncorrectObjectTypeException ex) {
            // Bogus ref, can't be merged into tip so we don't care.
            continue;
        }
    }
    Collections.sort(commits, ReviewDbUtil.intKeyOrdering().reverse().onResultOf(c -> c.getPatchsetId()));
    CodeReviewCommit result = MergeUtil.findAnyMergedInto(rw, commits, tip);
    if (result == null) {
        return null;
    }
    // Some patch set of this change is actually merged into the target
    // branch, most likely because a previous run of MergeOp failed after
    // updateRepo, during updateChange.
    //
    // Do the best we can to clean this up: mark the change as merged and set
    // the current patch set. Don't touch the dest branch at all. This can
    // lead to some odd situations like another change in the set merging in
    // a different patch set of this change, but that's unavoidable at this
    // point.  At least the change will end up in the right state.
    //
    // TODO(dborowitz): Consider deleting later junk patch set refs. They
    // presumably don't have PatchSets pointing to them.
    rw.parseBody(result);
    result.add(args.canMergeFlag);
    PatchSet.Id psId = result.getPatchsetId();
    result.copyFrom(toMerge);
    // Got overwriten by copyFrom.
    result.setPatchsetId(psId);
    result.setStatusCode(CommitMergeStatus.ALREADY_MERGED);
    args.commitStatus.put(result);
    return result;
}
Also used : SubmoduleException(com.google.gerrit.server.git.SubmoduleException) OrmException(com.google.gwtorm.server.OrmException) GroupCollector(com.google.gerrit.server.git.GroupCollector) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) LoggerFactory(org.slf4j.LoggerFactory) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) MergeUtil(com.google.gerrit.server.git.MergeUtil) Map(java.util.Map) Context(com.google.gerrit.server.update.Context) Function(com.google.common.base.Function) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) List(java.util.List) LabelNormalizer(com.google.gerrit.server.git.LabelNormalizer) RefNames(com.google.gerrit.reviewdb.client.RefNames) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) LabelId(com.google.gerrit.reviewdb.client.LabelId) Branch(com.google.gerrit.reviewdb.client.Branch) ChangeMessagesUtil(com.google.gerrit.server.ChangeMessagesUtil) Iterables(com.google.common.collect.Iterables) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) Change(com.google.gerrit.reviewdb.client.Change) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) RepoContext(com.google.gerrit.server.update.RepoContext) HashMap(java.util.HashMap) SubmitRecord(com.google.gerrit.common.data.SubmitRecord) ArrayList(java.util.ArrayList) IntegrationException(com.google.gerrit.server.git.IntegrationException) Account(com.google.gerrit.reviewdb.client.Account) ChangeContext(com.google.gerrit.server.update.ChangeContext) REVIEWER(com.google.gerrit.server.notedb.ReviewerStateInternal.REVIEWER) Project(com.google.gerrit.reviewdb.client.Project) Logger(org.slf4j.Logger) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ProjectState(com.google.gerrit.server.project.ProjectState) ReviewDbUtil(com.google.gerrit.reviewdb.server.ReviewDbUtil) IOException(java.io.IOException) ApprovalsUtil(com.google.gerrit.server.ApprovalsUtil) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Collections(java.util.Collections) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) Change(com.google.gerrit.reviewdb.client.Change) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with MissingObjectException

use of org.eclipse.jgit.errors.MissingObjectException in project jwt by emweb.

the class Git method treeGetObject.

public Object treeGetObject(ObjectId treeObject, int childIndex) {
    try {
        RevWalk walk = new RevWalk(repository, 0);
        RevTree tree = walk.parseTree(treeObject.id);
        TreeWalk treeWalk = new TreeWalk(repository);
        treeWalk.addTree(tree);
        treeWalk.setRecursive(false);
        for (int i = 0; i <= childIndex; ++i) if (!treeWalk.next())
            throw new RuntimeException("No object " + childIndex + " in tree " + treeObject.id.toString());
        Object object = new Object();
        object.id = new ObjectId(treeWalk.getObjectId(0));
        object.type = treeWalk.getFileMode(0) == FileMode.TREE ? ObjectType.Tree : ObjectType.Blob;
        object.name = treeWalk.getNameString();
        return object;
    } catch (MissingObjectException e) {
        throw new RuntimeException(e);
    } catch (IncorrectObjectTypeException e) {
        throw new RuntimeException(e);
    } catch (CorruptObjectException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : CorruptObjectException(org.eclipse.jgit.errors.CorruptObjectException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.DepthWalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException)

Example 4 with MissingObjectException

use of org.eclipse.jgit.errors.MissingObjectException in project maven-scm by apache.

the class JGitUtils method getRevCommits.

/**
 * Get a list of commits between two revisions.
 *
 * @param repo     the repository to work on
 * @param sortings sorting
 * @param fromRev  start revision
 * @param toRev    if null, falls back to head
 * @param fromDate from which date on
 * @param toDate   until which date
 * @param maxLines max number of lines
 * @return a list of commits, might be empty, but never <code>null</code>
 * @throws IOException
 * @throws MissingObjectException
 * @throws IncorrectObjectTypeException
 */
public static List<RevCommit> getRevCommits(Repository repo, RevSort[] sortings, String fromRev, String toRev, final Date fromDate, final Date toDate, int maxLines) throws IOException, MissingObjectException, IncorrectObjectTypeException {
    List<RevCommit> revs = new ArrayList<RevCommit>();
    RevWalk walk = new RevWalk(repo);
    ObjectId fromRevId = fromRev != null ? repo.resolve(fromRev) : null;
    ObjectId toRevId = toRev != null ? repo.resolve(toRev) : null;
    if (sortings == null || sortings.length == 0) {
        sortings = new RevSort[] { RevSort.TOPO, RevSort.COMMIT_TIME_DESC };
    }
    for (final RevSort s : sortings) {
        walk.sort(s, true);
    }
    if (fromDate != null && toDate != null) {
        // walk.setRevFilter( CommitTimeRevFilter.between( fromDate, toDate ) );
        walk.setRevFilter(new RevFilter() {

            @Override
            public boolean include(RevWalk walker, RevCommit cmit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException {
                int cmtTime = cmit.getCommitTime();
                return (cmtTime >= (fromDate.getTime() / 1000)) && (cmtTime <= (toDate.getTime() / 1000));
            }

            @Override
            public RevFilter clone() {
                return this;
            }
        });
    } else {
        if (fromDate != null) {
            walk.setRevFilter(CommitTimeRevFilter.after(fromDate));
        }
        if (toDate != null) {
            walk.setRevFilter(CommitTimeRevFilter.before(toDate));
        }
    }
    if (fromRevId != null) {
        RevCommit c = walk.parseCommit(fromRevId);
        c.add(RevFlag.UNINTERESTING);
        RevCommit real = walk.parseCommit(c);
        walk.markUninteresting(real);
    }
    if (toRevId != null) {
        RevCommit c = walk.parseCommit(toRevId);
        c.remove(RevFlag.UNINTERESTING);
        RevCommit real = walk.parseCommit(c);
        walk.markStart(real);
    } else {
        final ObjectId head = repo.resolve(Constants.HEAD);
        if (head == null) {
            throw new RuntimeException("Cannot resolve " + Constants.HEAD);
        }
        RevCommit real = walk.parseCommit(head);
        walk.markStart(real);
    }
    int n = 0;
    for (final RevCommit c : walk) {
        n++;
        if (maxLines != -1 && n > maxLines) {
            break;
        }
        revs.add(c);
    }
    return revs;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) CommitTimeRevFilter(org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter) RevFilter(org.eclipse.jgit.revwalk.filter.RevFilter) StopWalkException(org.eclipse.jgit.errors.StopWalkException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RevSort(org.eclipse.jgit.revwalk.RevSort)

Example 5 with MissingObjectException

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

the class RevisionServlet method doGetHtml.

@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);
    GitilesAccess access = getAccess(req);
    Config cfg = getAccess(req).getConfig();
    try (RevWalk walk = new RevWalk(repo)) {
        DateFormatter df = new DateFormatter(access, Format.DEFAULT);
        List<RevObject> objects = listObjects(walk, view.getRevision());
        List<Map<String, ?>> soyObjects = Lists.newArrayListWithCapacity(objects.size());
        boolean hasBlob = false;
        boolean hasReadme = false;
        // TODO(sop): Allow caching commits by SHA-1 when no S cookie is sent.
        for (RevObject obj : objects) {
            try {
                switch(obj.getType()) {
                    case OBJ_COMMIT:
                        soyObjects.add(ImmutableMap.of("type", Constants.TYPE_COMMIT, "data", new CommitSoyData().setLinkifier(linkifier).setRevWalk(walk).setArchiveFormat(getArchiveFormat(access)).toSoyData(req, (RevCommit) obj, COMMIT_SOY_FIELDS, df)));
                        break;
                    case OBJ_TREE:
                        Map<String, Object> tree = new TreeSoyData(walk.getObjectReader(), view, cfg, (RevTree) obj, req.getRequestURI()).toSoyData(obj);
                        soyObjects.add(ImmutableMap.of("type", Constants.TYPE_TREE, "data", tree));
                        hasReadme = tree.containsKey("readmeHtml");
                        break;
                    case OBJ_BLOB:
                        soyObjects.add(ImmutableMap.of("type", Constants.TYPE_BLOB, "data", new BlobSoyData(walk.getObjectReader(), view).toSoyData(obj)));
                        hasBlob = true;
                        break;
                    case OBJ_TAG:
                        soyObjects.add(ImmutableMap.of("type", Constants.TYPE_TAG, "data", new TagSoyData(linkifier, req).toSoyData((RevTag) obj, df)));
                        break;
                    default:
                        log.warn("Bad object type for {}: {}", ObjectId.toString(obj.getId()), obj.getType());
                        res.setStatus(SC_NOT_FOUND);
                        return;
                }
            } catch (MissingObjectException e) {
                log.warn("Missing object " + ObjectId.toString(obj.getId()), e);
                res.setStatus(SC_NOT_FOUND);
                return;
            } catch (IncorrectObjectTypeException e) {
                log.warn("Incorrect object type for " + ObjectId.toString(obj.getId()), e);
                res.setStatus(SC_NOT_FOUND);
                return;
            }
        }
        renderHtml(req, res, "gitiles.revisionDetail", ImmutableMap.of("title", view.getRevision().getName(), "objects", soyObjects, "hasBlob", hasBlob, "hasReadme", hasReadme));
    }
}
Also used : RevObject(org.eclipse.jgit.revwalk.RevObject) Config(org.eclipse.jgit.lib.Config) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Repository(org.eclipse.jgit.lib.Repository) RevObject(org.eclipse.jgit.revwalk.RevObject) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) RevTree(org.eclipse.jgit.revwalk.RevTree)

Aggregations

MissingObjectException (org.eclipse.jgit.errors.MissingObjectException)30 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)21 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)12 Repository (org.eclipse.jgit.lib.Repository)12 ArrayList (java.util.ArrayList)6 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 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 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)3