Example usage for org.eclipse.jgit.lib ObjectLoader isLarge

List of usage examples for org.eclipse.jgit.lib ObjectLoader isLarge

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ObjectLoader isLarge.

Prototype

public boolean isLarge() 

Source Link

Document

Whether this object is too large to obtain as a byte array.

Usage

From source file:com.google.gerrit.httpd.raw.CatServlet.java

License:Apache License

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse rsp) throws IOException {
    String keyStr = req.getPathInfo();

    // We shouldn't have to do this extra decode pass, but somehow we
    // are now receiving our "^1" suffix as "%5E1", which confuses us
    // downstream. Other times we get our embedded "," as "%2C", which
    // is equally bad. And yet when these happen a "%2F" is left as-is,
    // rather than escaped as "%252F", which makes me feel really really
    // uncomfortable with a blind decode right here.
    ///*  ww  w . jav a2 s.  c  o  m*/
    keyStr = Url.decode(keyStr);

    if (!keyStr.startsWith("/")) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    keyStr = keyStr.substring(1);

    final Patch.Key patchKey;
    final int side;
    {
        final int c = keyStr.lastIndexOf('^');
        if (c == 0) {
            rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        if (c < 0) {
            side = 0;

        } else {
            try {
                side = Integer.parseInt(keyStr.substring(c + 1));
                keyStr = keyStr.substring(0, c);
            } catch (NumberFormatException e) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
        }

        try {
            patchKey = Patch.Key.parse(keyStr);
        } catch (NumberFormatException e) {
            rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
    }

    final Change.Id changeId = patchKey.getParentKey().getParentKey();
    final Project project;
    final String revision;
    try {
        final ReviewDb db = requestDb.get();
        final ChangeControl control = changeControl.validateFor(changeId, userProvider.get());

        project = control.getProject();

        if (patchKey.getParentKey().get() == 0) {
            // change edit
            try {
                Optional<ChangeEdit> edit = changeEditUtil.byChange(control.getChange());
                if (edit.isPresent()) {
                    revision = edit.get().getRevision().get();
                } else {
                    rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }
            } catch (AuthException e) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
        } else {
            PatchSet patchSet = db.patchSets().get(patchKey.getParentKey());
            if (patchSet == null) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
            revision = patchSet.getRevision().get();
        }
    } catch (NoSuchChangeException e) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    } catch (OrmException e) {
        getServletContext().log("Cannot query database", e);
        rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }

    ObjectLoader blobLoader;
    RevCommit fromCommit;
    String suffix;
    String path = patchKey.getFileName();
    try (Repository repo = repoManager.openRepository(project.getNameKey())) {
        try (ObjectReader reader = repo.newObjectReader(); RevWalk rw = new RevWalk(reader)) {
            RevCommit c;

            c = rw.parseCommit(ObjectId.fromString(revision));
            if (side == 0) {
                fromCommit = c;
                suffix = "new";

            } else if (1 <= side && side - 1 < c.getParentCount()) {
                fromCommit = rw.parseCommit(c.getParent(side - 1));
                if (c.getParentCount() == 1) {
                    suffix = "old";
                } else {
                    suffix = "old" + side;
                }

            } else {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }

            try (TreeWalk tw = TreeWalk.forPath(reader, path, fromCommit.getTree())) {
                if (tw == null) {
                    rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }

                if (tw.getFileMode(0).getObjectType() == Constants.OBJ_BLOB) {
                    blobLoader = reader.open(tw.getObjectId(0), Constants.OBJ_BLOB);

                } else {
                    rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    return;
                }
            }
        }
    } catch (RepositoryNotFoundException e) {
        getServletContext().log("Cannot open repository", e);
        rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    } catch (IOException | RuntimeException e) {
        getServletContext().log("Cannot read repository", e);
        rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }

    final byte[] raw = blobLoader.isLarge() ? null : blobLoader.getCachedBytes();
    final long when = fromCommit.getCommitTime() * 1000L;

    rsp.setDateHeader("Last-Modified", when);
    CacheHeaders.setNotCacheable(rsp);

    try (OutputStream out = openOutputStream(req, rsp, blobLoader, fromCommit, when, path, suffix, raw)) {
        if (raw != null) {
            out.write(raw);
        } else {
            blobLoader.copyTo(out);
        }
    }
}

From source file:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java

License:Open Source License

public static Blob getBlob(Repository r, String revision, String path)
        throws IOException, EntityNotFoundException {

    if (path.startsWith("/")) {
        path = path.substring(1);/*from w ww .  j a v a 2  s  . c  o  m*/
    }

    String id = resolve(r, r.resolve(revision), path);
    if (id == null) {
        throw new EntityNotFoundException();
    }
    ObjectId objectId = ObjectId.fromString(id);

    ObjectLoader loader = r.getObjectDatabase().open(objectId, Constants.OBJ_BLOB);

    Blob b = new Blob(id);

    if (loader.isLarge()) {
        b.setLarge(true);
        InputStream is = null;
        IOException ioex = null;
        try {
            is = loader.openStream();
            b.setBinary(RawText.isBinary(is));
        } catch (IOException ex) {
            ioex = ex;
        } finally {
            if (is != null) {
                is.close();
            }
            if (ioex != null) {
                throw ioex;
            }
        }

    } else {
        byte[] raw = loader.getBytes();

        boolean binary = RawText.isBinary(raw);

        if (binary) {
            b.setBinary(true);
            b.setLines(Collections.<String>emptyList());
        } else {

            RawText rt = new RawText(raw);
            List<String> lines = new ArrayList<String>(rt.size());

            for (int i = 0; i < rt.size(); i++) {
                lines.add(rt.getString(i));
            }

            b.setLines(lines);
        }
    }

    return b;
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.patch.LoadContentAction.java

License:Apache License

@NotNull
private InputStream openContentStream(@NotNull final ObjectLoader loader) throws IOException {
    return loader.isLarge() ? loader.openStream() : new ByteArrayInputStream(loader.getCachedBytes());
}

From source file:net.erdfelt.android.sdkfido.git.internal.GitInfo.java

License:Apache License

public static String getObjectName(Repository repo, ObjectId objectId) {
    try {/*from w  w  w .j a va  2  s.c om*/
        ObjectLoader loader = repo.open(objectId);
        StringBuilder ret = new StringBuilder();

        if (loader.isLarge()) {
            ret.append("LARGE! ");
        }

        switch (loader.getType()) {
        case Constants.OBJ_BAD:
            ret.append("BAD ");
            break;
        case Constants.OBJ_BLOB:
            ret.append("BLOB ");
            break;
        case Constants.OBJ_COMMIT:
            ret.append("COMMIT ");
            break;
        case Constants.OBJ_EXT:
            ret.append("EXT ");
            break;
        case Constants.OBJ_OFS_DELTA:
            ret.append("OFS_DELTA ");
            break;
        case Constants.OBJ_REF_DELTA:
            ret.append("REF_DELTA ");
            break;
        case Constants.OBJ_TAG:
            ret.append("TAG ");
            break;
        case Constants.OBJ_TREE:
            ret.append("TREE ");
            break;
        case Constants.OBJ_TYPE_5:
            ret.append("TYPE_5 ");
            break;
        default:
            ret.append("UNKNOWN[").append(loader.getType()).append("] ");
            break;
        }

        ret.append(String.format("Size=%,d", loader.getSize()));
        return ret.toString();
    } catch (MissingObjectException e) {
        LOG.log(Level.WARNING, "Unable to open objectId: " + objectId, e);
        return "<missing object>";
    } catch (IOException e) {
        LOG.log(Level.WARNING, "Unable to open objectId: " + objectId, e);
        return "<unable to open object>";
    }
}

From source file:org.eclipse.egit.ui.internal.commit.RepositoryCommitNote.java

License:Open Source License

/**
 * Get note text. This method open and read the note blob each time it is
 * called.//from w  w w  . jav  a  2 s .  c  o  m
 *
 * @return note text or empty string if lookup failed.
 */
public String getNoteText() {
    try {
        ObjectLoader loader = commit.getRepository().open(note.getData(), Constants.OBJ_BLOB);
        byte[] contents;
        if (loader.isLarge())
            contents = IO.readWholeStream(loader.openStream(), (int) loader.getSize()).array();
        else
            contents = loader.getCachedBytes();
        return new String(contents);
    } catch (IOException e) {
        Activator.logError("Error loading note text", e); //$NON-NLS-1$
    }
    return ""; //$NON-NLS-1$
}

From source file:org.modeshape.connector.git.GitTree.java

License:Apache License

protected void addInformationForPath(Repository repository, Git git, DocumentWriter writer, RevCommit commit,
        String path, CallSpecification spec, Values values) throws GitAPIException, IOException {
    // Make sure the path is in the canonical form we need ...
    if (path.startsWith("/")) {
        if (path.length() == 1)
            path = "";
        else//from   w w  w . j  a  v  a 2  s .c o m
            path = path.substring(1);
    }

    // Now see if we're actually referring to the "jcr:content" node ...
    boolean isContentNode = false;
    if (path.endsWith(JCR_CONTENT_SUFFIX)) {
        isContentNode = true;
        path = path.substring(0, path.length() - JCR_CONTENT_SUFFIX.length());
    }

    // Create the TreeWalk that we'll use to navigate the files/directories ...
    final TreeWalk tw = new TreeWalk(repository);
    tw.addTree(commit.getTree());
    if ("".equals(path)) {
        // This is the top-level directory, so we don't need to pre-walk to find anything ...
        tw.setRecursive(false);
        while (tw.next()) {
            String childName = tw.getNameString();
            String childId = spec.childId(childName);
            writer.addChild(childId, childName);
        }
    } else {
        // We need to first find our path *before* we can walk the children ...
        PathFilter filter = PathFilter.create(path);
        tw.setFilter(filter);
        while (tw.next()) {
            if (filter.isDone(tw)) {
                break;
            } else if (tw.isSubtree()) {
                tw.enterSubtree();
            }
        }
        // Now that the TreeWalk is the in right location given by the 'path', we can get the
        if (tw.isSubtree()) {
            // The object at the 'path' is a directory, so go into it ...
            tw.enterSubtree();

            // Find the commit in which this folder was last modified ...
            // This may not be terribly efficient, but it seems to work faster on subsequent runs ...
            RevCommit folderCommit = git.log().addPath(path).call().iterator().next();
            writer.setPrimaryType(GitLexicon.FOLDER);

            // could happen if not enough permissions, for example
            if (folderCommit != null) {
                // Add folder-related properties ...
                String committer = commiterName(folderCommit);
                String author = authorName(folderCommit);
                DateTime committed = values.dateFrom(folderCommit.getCommitTime());
                writer.addProperty(JcrLexicon.CREATED, committed);
                writer.addProperty(JcrLexicon.CREATED_BY, committer);
                writer.addProperty(GitLexicon.OBJECT_ID, folderCommit.getId().name());
                writer.addProperty(GitLexicon.AUTHOR, author);
                writer.addProperty(GitLexicon.COMMITTER, committer);
                writer.addProperty(GitLexicon.COMMITTED, committed);
                writer.addProperty(GitLexicon.TITLE, folderCommit.getShortMessage());
            } else {
                connector.getLogger().warn(GitI18n.cannotReadCommit, path);
            }

            // And now walk the contents of the directory ...
            while (tw.next()) {
                String childName = tw.getNameString();
                String childId = spec.childId(childName);
                writer.addChild(childId, childName);
            }
        } else {
            // The path specifies a file (or a content node) ...

            // Find the commit in which this folder was last modified ...
            // This may not be terribly efficient, but it seems to work faster on subsequent runs ...
            RevCommit fileCommit = git.log().addPath(path).call().iterator().next();

            if (isContentNode) {
                writer.setPrimaryType(GitLexicon.RESOURCE);
                if (fileCommit == null) {
                    // could happen if not enough permissions, for example
                    connector.getLogger().warn(GitI18n.cannotReadCommit, path);
                    return;
                }
                // Add file-related properties ...
                String committer = commiterName(fileCommit);
                String author = authorName(fileCommit);
                DateTime committed = values.dateFrom(fileCommit.getCommitTime());

                writer.addProperty(JcrLexicon.LAST_MODIFIED, committed);
                writer.addProperty(JcrLexicon.LAST_MODIFIED_BY, committer);
                writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name());
                writer.addProperty(GitLexicon.AUTHOR, author);
                writer.addProperty(GitLexicon.COMMITTER, committer);
                writer.addProperty(GitLexicon.COMMITTED, committed);
                writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage());
                // Create the BinaryValue ...
                ObjectId fileObjectId = tw.getObjectId(0);
                ObjectLoader fileLoader = repository.open(fileObjectId);
                BinaryKey key = new BinaryKey(fileObjectId.getName());
                BinaryValue value = values.binaryFor(key, fileLoader.getSize());
                if (value == null) {
                    // It wasn't found in the binary store ...
                    if (fileLoader.isLarge()) {
                        // Too large to hold in memory, so use the binary store (which reads the file immediately) ...
                        value = values.binaryFrom(fileLoader.openStream());
                    } else {
                        // This is small enough to fit into a byte[], but it still may be pretty big ...
                        value = new GitBinaryValue(fileObjectId, fileLoader, connector.getSourceName(), name,
                                connector.getMimeTypeDetector());
                    }
                }
                writer.addProperty(JcrLexicon.DATA, value);
                if (connector.includeMimeType()) {
                    try {
                        String filename = spec.parameter(spec.parameterCount() - 1); // the last is 'jcr:content'
                        String mimeType = value.getMimeType(filename);
                        if (mimeType != null)
                            writer.addProperty(JcrLexicon.MIMETYPE, mimeType);
                    } catch (RepositoryException e) {
                        // do nothing
                    } catch (IOException e) {
                        // do nothing
                    }
                }
            } else {
                writer.setPrimaryType(GitLexicon.FILE);
                if (fileCommit == null) {
                    // could happen if not enough permissions, for example
                    connector.getLogger().warn(GitI18n.cannotReadCommit, path);
                    return;
                }
                // Add file-related properties ...
                String committer = commiterName(fileCommit);
                String author = authorName(fileCommit);
                DateTime committed = values.dateFrom(fileCommit.getCommitTime());

                writer.addProperty(JcrLexicon.CREATED, committed);
                writer.addProperty(JcrLexicon.CREATED_BY, committer);
                writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name());
                writer.addProperty(GitLexicon.AUTHOR, author);
                writer.addProperty(GitLexicon.COMMITTER, committer);
                writer.addProperty(GitLexicon.COMMITTED, committed);
                writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage());

                // Add the "jcr:content" child node ...
                String childId = spec.childId(JCR_CONTENT);
                writer.addChild(childId, JCR_CONTENT);
            }
        }
    }
}