Search in sources :

Example 1 with LargeObjectException

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

the class RulesCache method read.

private String read(Project.NameKey project, ObjectId rulesId) throws CompileException {
    try (Repository git = gitMgr.openRepository(project)) {
        try {
            ObjectLoader ldr = git.open(rulesId, Constants.OBJ_BLOB);
            byte[] raw = ldr.getCachedBytes(maxSrcBytes);
            return RawParseUtils.decode(raw);
        } catch (LargeObjectException e) {
            throw new CompileException("rules of " + project + " are too large", e);
        } catch (RuntimeException | IOException e) {
            throw new CompileException("Cannot load rules of " + project, e);
        }
    } catch (IOException e) {
        throw new CompileException("Cannot open repository " + project, e);
    }
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException)

Example 2 with LargeObjectException

use of org.eclipse.jgit.errors.LargeObjectException 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)

Example 3 with LargeObjectException

use of org.eclipse.jgit.errors.LargeObjectException 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)

Example 4 with LargeObjectException

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

the class FileContentUtil method getContent.

public BinaryResult getContent(Repository repo, ProjectState project, ObjectId revstr, String path) throws IOException, ResourceNotFoundException {
    try (RevWalk rw = new RevWalk(repo)) {
        RevCommit commit = rw.parseCommit(revstr);
        ObjectReader reader = rw.getObjectReader();
        TreeWalk tw = TreeWalk.forPath(reader, path, commit.getTree());
        if (tw == null) {
            throw new ResourceNotFoundException();
        }
        org.eclipse.jgit.lib.FileMode mode = tw.getFileMode(0);
        ObjectId id = tw.getObjectId(0);
        if (mode == org.eclipse.jgit.lib.FileMode.GITLINK) {
            return BinaryResult.create(id.name()).setContentType(X_GIT_GITLINK).base64();
        }
        ObjectLoader obj = repo.open(id, OBJ_BLOB);
        byte[] raw;
        try {
            raw = obj.getCachedBytes(MAX_SIZE);
        } catch (LargeObjectException e) {
            raw = null;
        }
        String type;
        if (mode == org.eclipse.jgit.lib.FileMode.SYMLINK) {
            type = X_GIT_SYMLINK;
        } else {
            type = registry.getMimeType(path, raw).toString();
            type = resolveContentType(project, path, FileMode.FILE, type);
        }
        return asBinaryResult(raw, obj).setContentType(type).base64();
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) 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)

Example 5 with LargeObjectException

use of org.eclipse.jgit.errors.LargeObjectException 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)

Aggregations

LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)8 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)5 Repository (org.eclipse.jgit.lib.Repository)5 RevWalk (org.eclipse.jgit.revwalk.RevWalk)5 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 ObjectReader (org.eclipse.jgit.lib.ObjectReader)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)2 QuotedString (org.eclipse.jgit.util.QuotedString)2 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)1 MimeType (eu.medsea.mimeutil.MimeType)1 IOException (java.io.IOException)1 RevObject (org.eclipse.jgit.revwalk.RevObject)1