Search in sources :

Example 11 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project gerrit by GerritCodeReview.

the class ConfigChangeIT method readProjectConfig.

private Config readProjectConfig() throws Exception {
    RevWalk rw = testRepo.getRevWalk();
    RevTree tree = rw.parseTree(testRepo.getRepository().resolve("HEAD"));
    RevObject obj = rw.parseAny(testRepo.get(tree, "project.config"));
    ObjectLoader loader = rw.getObjectReader().open(obj);
    String text = new String(loader.getCachedBytes(), UTF_8);
    Config cfg = new Config();
    cfg.fromText(text);
    return cfg;
}
Also used : RevObject(org.eclipse.jgit.revwalk.RevObject) Config(org.eclipse.jgit.lib.Config) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 12 with ObjectLoader

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

the class BlobSoyData method toSoyData.

public Map<String, Object> toSoyData(String path, ObjectId blobId) throws MissingObjectException, IOException {
    Map<String, Object> data = Maps.newHashMapWithExpectedSize(4);
    data.put("sha", ObjectId.toString(blobId));
    ObjectLoader loader = reader.open(blobId, Constants.OBJ_BLOB);
    String content;
    try {
        byte[] raw = loader.getCachedBytes(MAX_FILE_SIZE);
        content = !RawText.isBinary(raw) ? RawParseUtils.decode(raw) : null;
    } catch (LargeObjectException.OutOfMemory e) {
        throw e;
    } catch (LargeObjectException e) {
        content = null;
    }
    if (content != null) {
        data.put("lines", prettify(path, content));
        if (path != null && path.endsWith(".md")) {
            data.put("docUrl", GitilesView.doc().copyFrom(view).toUrl());
        }
    } else {
        data.put("lines", null);
        data.put("size", Long.toString(loader.getSize()));
    }
    if (path != null && view.getRevision().getPeeledType() == OBJ_COMMIT) {
        data.put("fileUrl", GitilesView.path().copyFrom(view).toUrl());
        data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
        data.put("blameUrl", GitilesView.blame().copyFrom(view).toUrl());
    }
    return data;
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader)

Example 13 with ObjectLoader

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

the class PathServlet method showSymlink.

private void showSymlink(HttpServletRequest req, HttpServletResponse res, WalkResult wr) throws IOException {
    GitilesView view = ViewFilter.getView(req);
    Map<String, Object> data = Maps.newHashMap();
    ObjectLoader loader = wr.getObjectReader().open(wr.id, OBJ_BLOB);
    String target;
    try {
        target = RawParseUtils.decode(loader.getCachedBytes(TreeSoyData.MAX_SYMLINK_SIZE));
    } catch (LargeObjectException.OutOfMemory e) {
        throw e;
    } catch (LargeObjectException e) {
        data.put("sha", ObjectId.toString(wr.id));
        data.put("data", null);
        data.put("size", Long.toString(loader.getSize()));
        renderHtml(req, res, "gitiles.pathDetail", ImmutableMap.of("title", ViewFilter.getView(req).getPathPart(), "breadcrumbs", view.getBreadcrumbs(wr.hasSingleTree), "type", FileType.REGULAR_FILE.toString(), "data", data));
        return;
    }
    String url = resolveTargetUrl(GitilesView.path().copyFrom(view).setPathPart(dirname(view.getPathPart())).build(), target);
    data.put("title", view.getPathPart());
    data.put("target", target);
    if (url != null) {
        data.put("targetUrl", url);
    }
    // TODO(sop): Allow caching files by SHA-1 when no S cookie is sent.
    renderHtml(req, res, "gitiles.pathDetail", ImmutableMap.of("title", ViewFilter.getView(req).getPathPart(), "breadcrumbs", view.getBreadcrumbs(wr.hasSingleTree), "type", FileType.SYMLINK.toString(), "data", data));
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) RevObject(org.eclipse.jgit.revwalk.RevObject) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) QuotedString(org.eclipse.jgit.util.QuotedString)

Example 14 with ObjectLoader

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

the class RevisionServlet method doGetText.

@Override
protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);
    try (ObjectReader reader = repo.newObjectReader()) {
        ObjectLoader loader = reader.open(view.getRevision().getId());
        if (loader.getType() != OBJ_COMMIT) {
            res.setStatus(SC_NOT_FOUND);
        } else {
            PathServlet.setTypeHeader(res, loader.getType());
            try (Writer writer = startRenderText(req, res);
                OutputStream out = BaseEncoding.base64().encodingStream(writer)) {
                loader.copyTo(out);
            }
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) OutputStream(java.io.OutputStream) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) Writer(java.io.Writer)

Example 15 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project gerrit by GerritCodeReview.

the class FileContentUtil method downloadContent.

public BinaryResult downloadContent(ProjectState project, ObjectId revstr, String path, @Nullable Integer parent) throws ResourceNotFoundException, IOException {
    try (Repository repo = openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        String suffix = "new";
        RevCommit commit = rw.parseCommit(revstr);
        if (parent != null && parent > 0) {
            if (commit.getParentCount() == 1) {
                suffix = "old";
            } else {
                suffix = "old" + parent;
            }
            commit = rw.parseCommit(commit.getParent(parent - 1));
        }
        ObjectReader reader = rw.getObjectReader();
        TreeWalk tw = TreeWalk.forPath(reader, path, commit.getTree());
        if (tw == null) {
            throw new ResourceNotFoundException();
        }
        int mode = tw.getFileMode(0).getObjectType();
        if (mode != Constants.OBJ_BLOB) {
            throw new ResourceNotFoundException();
        }
        ObjectId id = tw.getObjectId(0);
        ObjectLoader obj = repo.open(id, OBJ_BLOB);
        byte[] raw;
        try {
            raw = obj.getCachedBytes(MAX_SIZE);
        } catch (LargeObjectException e) {
            raw = null;
        }
        MimeType contentType = registry.getMimeType(path, raw);
        return registry.isSafeInline(contentType) ? wrapBlob(path, obj, raw, contentType, suffix) : zipBlob(path, obj, commit, suffix);
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MimeType(eu.medsea.mimeutil.MimeType) LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)16 RevWalk (org.eclipse.jgit.revwalk.RevWalk)11 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 ObjectId (org.eclipse.jgit.lib.ObjectId)7 ObjectReader (org.eclipse.jgit.lib.ObjectReader)6 IOException (java.io.IOException)5 LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)5 FileMode (org.eclipse.jgit.lib.FileMode)5 MutableObjectId (org.eclipse.jgit.lib.MutableObjectId)3 PathFilter (org.eclipse.jgit.treewalk.filter.PathFilter)3 FilestoreModel (com.gitblit.models.FilestoreModel)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)2 Repository (org.eclipse.jgit.lib.Repository)2 RevObject (org.eclipse.jgit.revwalk.RevObject)2 RevTree (org.eclipse.jgit.revwalk.RevTree)2