Search in sources :

Example 96 with Repository

use of org.eclipse.jgit.lib.Repository in project gitiles by GerritCodeReview.

the class RepositoryIndexServlet 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);
    RepositoryDescription desc = access.getRepositoryDescription();
    try (RevWalk walk = new RevWalk(repo)) {
        Paginator paginator = null;
        Map<String, Object> data = Maps.newHashMapWithExpectedSize(7);
        List<Map<String, Object>> tags = RefServlet.getTagsSoyData(req, timeCache, walk, REF_LIMIT);
        ObjectId headId = repo.resolve(Constants.HEAD);
        if (headId != null) {
            RevObject head = walk.parseAny(headId);
            int limit = LOG_LIMIT;
            Map<String, Object> readme = renderReadme(req, walk, view, access.getConfig(), head);
            if (readme != null) {
                data.putAll(readme);
                limit = LOG_WITH_README_LIMIT;
            }
            // TODO(dborowitz): Handle non-commit or missing HEAD?
            if (head.getType() == Constants.OBJ_COMMIT) {
                walk.reset();
                walk.markStart((RevCommit) head);
                paginator = new Paginator(walk, limit, null);
            }
        }
        if (!data.containsKey("entries")) {
            data.put("entries", ImmutableList.of());
        }
        List<Map<String, Object>> branches = RefServlet.getBranchesSoyData(req, REF_LIMIT);
        data.put("cloneUrl", desc.cloneUrl);
        data.put("mirroredFromUrl", Strings.nullToEmpty(desc.mirroredFromUrl));
        data.put("description", Strings.nullToEmpty(desc.description));
        data.put("branches", trim(branches));
        if (branches.size() > REF_LIMIT) {
            data.put("moreBranchesUrl", GitilesView.refs().copyFrom(view).toUrl());
        }
        data.put("tags", trim(tags));
        data.put("hasLog", paginator != null);
        if (tags.size() > REF_LIMIT) {
            data.put("moreTagsUrl", GitilesView.refs().copyFrom(view).toUrl());
        }
        GitilesConfig.putVariant(getAccess(req).getConfig(), "logEntry", "logEntryVariant", data);
        if (paginator != null) {
            DateFormatter df = new DateFormatter(access, Format.DEFAULT);
            try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.repositoryIndex", data)) {
                Writer w = newWriter(out, res);
                new LogSoyData(req, access, "oneline").renderStreaming(paginator, "HEAD", renderer, w, df);
                w.flush();
            }
        } else {
            renderHtml(req, res, "gitiles.repositoryIndex", data);
        }
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevObject(org.eclipse.jgit.revwalk.RevObject) OutputStream(java.io.OutputStream) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Repository(org.eclipse.jgit.lib.Repository) RevObject(org.eclipse.jgit.revwalk.RevObject) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Writer(java.io.Writer)

Example 97 with Repository

use of org.eclipse.jgit.lib.Repository 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 98 with Repository

use of org.eclipse.jgit.lib.Repository in project gitiles by GerritCodeReview.

the class DocServlet method doGetHtml.

@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
    MarkdownConfig cfg = MarkdownConfig.get(getAccess(req).getConfig());
    if (!cfg.render) {
        res.setStatus(SC_NOT_FOUND);
        return;
    }
    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);
    try (RevWalk rw = new RevWalk(repo)) {
        ObjectReader reader = rw.getObjectReader();
        String path = view.getPathPart();
        RevTree root;
        try {
            root = rw.parseTree(view.getRevision().getId());
        } catch (IncorrectObjectTypeException e) {
            res.setStatus(SC_NOT_FOUND);
            return;
        }
        MarkdownFile srcmd = findFile(rw, root, path);
        if (srcmd == null) {
            res.setStatus(SC_NOT_FOUND);
            return;
        }
        MarkdownFile navmd = findFile(rw, root, NAVBAR_MD);
        String curEtag = etag(srcmd, navmd);
        if (etagMatch(req, curEtag)) {
            res.setStatus(SC_NOT_MODIFIED);
            return;
        }
        view = view.toBuilder().setPathPart(srcmd.path).build();
        try {
            srcmd.read(reader, cfg);
            if (navmd != null) {
                navmd.read(reader, cfg);
            }
        } catch (LargeObjectException.ExceedsLimit errBig) {
            fileTooBig(res, view, errBig);
            return;
        } catch (IOException err) {
            readError(res, view, err);
            return;
        }
        MarkdownToHtml.Builder fmt = MarkdownToHtml.builder().setConfig(cfg).setGitilesView(view).setRequestUri(req.getRequestURI()).setReader(reader).setRootTree(root);
        res.setHeader(HttpHeaders.ETAG, curEtag);
        showDoc(req, res, view, cfg, fmt, navmd, srcmd);
    }
}
Also used : GitilesView(com.google.gitiles.GitilesView) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 99 with Repository

use of org.eclipse.jgit.lib.Repository 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 100 with Repository

use of org.eclipse.jgit.lib.Repository in project gitiles by GerritCodeReview.

the class PathServlet method doGetText.

@Override
protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);
    try (RevWalk rw = new RevWalk(repo);
        WalkResult wr = WalkResult.forPath(rw, view, false)) {
        if (wr == null) {
            res.setStatus(SC_NOT_FOUND);
            return;
        }
        // which would be bad.
        switch(wr.type) {
            case SYMLINK:
            case REGULAR_FILE:
            case EXECUTABLE_FILE:
                writeBlobText(req, res, wr);
                break;
            case TREE:
                writeTreeText(req, res, wr);
                break;
            case GITLINK:
            default:
                renderTextError(req, res, SC_NOT_FOUND, "Not a file");
                break;
        }
    } catch (LargeObjectException e) {
        res.setStatus(SC_INTERNAL_SERVER_ERROR);
    }
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Aggregations

Repository (org.eclipse.jgit.lib.Repository)322 RevWalk (org.eclipse.jgit.revwalk.RevWalk)102 IOException (java.io.IOException)101 Test (org.junit.Test)80 RevCommit (org.eclipse.jgit.revwalk.RevCommit)76 ObjectId (org.eclipse.jgit.lib.ObjectId)72 File (java.io.File)42 TestRepository (org.eclipse.jgit.junit.TestRepository)40 Change (com.google.gerrit.reviewdb.client.Change)39 OrmException (com.google.gwtorm.server.OrmException)39 Ref (org.eclipse.jgit.lib.Ref)35 Project (com.google.gerrit.reviewdb.client.Project)32 ArrayList (java.util.ArrayList)31 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)27 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)26 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)24 Map (java.util.Map)23 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)23 RepositoryModel (com.gitblit.models.RepositoryModel)20 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)20