Search in sources :

Example 6 with RevObject

use of org.eclipse.jgit.revwalk.RevObject in project gitiles by GerritCodeReview.

the class RevisionServlet method listObjects.

// TODO(dborowitz): Extract this.
static List<RevObject> listObjects(RevWalk walk, Revision rev) throws MissingObjectException, IOException {
    List<RevObject> objects = Lists.newArrayListWithExpectedSize(1);
    ObjectId id = rev.getId();
    RevObject cur;
    while (true) {
        cur = walk.parseAny(id);
        objects.add(cur);
        if (cur.getType() != Constants.OBJ_TAG) {
            break;
        }
        id = ((RevTag) cur).getObject();
    }
    if (cur.getType() == Constants.OBJ_COMMIT) {
        objects.add(walk.parseTree(((RevCommit) cur).getTree()));
    }
    return objects;
}
Also used : RevObject(org.eclipse.jgit.revwalk.RevObject) ObjectId(org.eclipse.jgit.lib.ObjectId) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 7 with RevObject

use of org.eclipse.jgit.revwalk.RevObject 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)

Example 8 with RevObject

use of org.eclipse.jgit.revwalk.RevObject in project gitiles by GerritCodeReview.

the class RootedDocServlet method service.

@Override
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    try (Repository repo = resolver.open(req, null);
        RevWalk rw = new RevWalk(repo)) {
        ObjectId id = repo.resolve(BRANCH);
        if (id == null) {
            res.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        RevObject obj = rw.peel(rw.parseAny(id));
        if (!(obj instanceof RevCommit)) {
            res.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        req.setAttribute(ATTRIBUTE_REPOSITORY, repo);
        ViewFilter.setView(req, GitilesView.rootedDoc().setHostName(req.getServerName()).setServletPath(req.getContextPath() + req.getServletPath()).setRevision(BRANCH, obj).setPathPart(req.getPathInfo()).build());
        docServlet.service(req, res);
    } catch (RepositoryNotFoundException | ServiceNotAuthorizedException | ServiceNotEnabledException e) {
        log.error(String.format("cannot open repository for %s", req.getServerName()), e);
        res.sendError(HttpServletResponse.SC_NOT_FOUND);
    } finally {
        ViewFilter.removeView(req);
        req.removeAttribute(ATTRIBUTE_REPOSITORY);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ServiceNotEnabledException(org.eclipse.jgit.transport.resolver.ServiceNotEnabledException) ObjectId(org.eclipse.jgit.lib.ObjectId) RevObject(org.eclipse.jgit.revwalk.RevObject) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ServiceNotAuthorizedException(org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 9 with RevObject

use of org.eclipse.jgit.revwalk.RevObject in project gitiles by GerritCodeReview.

the class TimeCache method getTime.

Long getTime(final RevWalk walk, final ObjectId id) throws IOException {
    try {
        return cache.get(id, () -> {
            RevObject o = walk.parseAny(id);
            while (o instanceof RevTag) {
                RevTag tag = (RevTag) o;
                PersonIdent ident = tag.getTaggerIdent();
                if (ident != null) {
                    return ident.getWhen().getTime() / 1000;
                }
                o = tag.getObject();
                walk.parseHeaders(o);
            }
            if (o.getType() == Constants.OBJ_COMMIT) {
                return Long.valueOf(((RevCommit) o).getCommitTime());
            }
            return Long.MIN_VALUE;
        });
    } catch (ExecutionException e) {
        Throwables.throwIfInstanceOf(e.getCause(), IOException.class);
        throw new IOException(e);
    }
}
Also used : RevTag(org.eclipse.jgit.revwalk.RevTag) RevObject(org.eclipse.jgit.revwalk.RevObject) PersonIdent(org.eclipse.jgit.lib.PersonIdent) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with RevObject

use of org.eclipse.jgit.revwalk.RevObject in project gerrit by GerritCodeReview.

the class ReceiveCommits method validateNewCommits.

private void validateNewCommits(RefControl ctl, ReceiveCommand cmd) {
    if (ctl.canForgeAuthor() && ctl.canForgeCommitter() && ctl.canForgeGerritServerIdentity() && ctl.canUploadMerges() && !projectControl.getProjectState().isUseSignedOffBy() && Iterables.isEmpty(rejectCommits) && !RefNames.REFS_CONFIG.equals(ctl.getRefName()) && !(MagicBranch.isMagicBranch(cmd.getRefName()) || NEW_PATCHSET.matcher(cmd.getRefName()).matches())) {
        logDebug("Short-circuiting new commit validation");
        return;
    }
    boolean defaultName = Strings.isNullOrEmpty(user.getAccount().getFullName());
    RevWalk walk = rp.getRevWalk();
    walk.reset();
    walk.sort(RevSort.NONE);
    try {
        RevObject parsedObject = walk.parseAny(cmd.getNewId());
        if (!(parsedObject instanceof RevCommit)) {
            return;
        }
        ListMultimap<ObjectId, Ref> existing = changeRefsById();
        walk.markStart((RevCommit) parsedObject);
        markHeadsAsUninteresting(walk, cmd.getRefName());
        int i = 0;
        for (RevCommit c; (c = walk.next()) != null; ) {
            i++;
            if (existing.keySet().contains(c)) {
                continue;
            } else if (!validCommit(walk, ctl, cmd, c)) {
                break;
            }
            if (defaultName && user.hasEmailAddress(c.getCommitterIdent().getEmailAddress())) {
                try {
                    Account a = db.accounts().get(user.getAccountId());
                    if (a != null && Strings.isNullOrEmpty(a.getFullName())) {
                        a.setFullName(c.getCommitterIdent().getName());
                        db.accounts().update(Collections.singleton(a));
                        user.getAccount().setFullName(a.getFullName());
                        accountCache.evict(a.getId());
                    }
                } catch (OrmException e) {
                    logWarn("Cannot default full_name", e);
                } finally {
                    defaultName = false;
                }
            }
        }
        logDebug("Validated {} new commits", i);
    } catch (IOException err) {
        cmd.setResult(REJECTED_MISSING_OBJECT);
        logError("Invalid pack upload; one or more objects weren't sent", err);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Ref(org.eclipse.jgit.lib.Ref) RevObject(org.eclipse.jgit.revwalk.RevObject) ObjectId(org.eclipse.jgit.lib.ObjectId) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

RevObject (org.eclipse.jgit.revwalk.RevObject)25 RevWalk (org.eclipse.jgit.revwalk.RevWalk)14 IOException (java.io.IOException)10 ObjectId (org.eclipse.jgit.lib.ObjectId)10 RevCommit (org.eclipse.jgit.revwalk.RevCommit)10 Repository (org.eclipse.jgit.lib.Repository)8 Ref (org.eclipse.jgit.lib.Ref)7 RevTag (org.eclipse.jgit.revwalk.RevTag)6 RevTree (org.eclipse.jgit.revwalk.RevTree)6 Map (java.util.Map)4 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)4 TagCommand (org.eclipse.jgit.api.TagCommand)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 PersonIdent (org.eclipse.jgit.lib.PersonIdent)3 ImmutableMap (com.google.common.collect.ImmutableMap)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 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2