Search in sources :

Example 26 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk 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 27 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gitiles by GerritCodeReview.

the class TreeSoyData method toSoyData.

public Map<String, Object> toSoyData(ObjectId treeId) throws MissingObjectException, IOException {
    TreeWalk tw = new TreeWalk(reader);
    tw.addTree(treeId);
    tw.setRecursive(false);
    return toSoyData(treeId, tw);
}
Also used : TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 28 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gitiles by GerritCodeReview.

the class ImageLoader method inlineMaybe.

private String inlineMaybe(@Nullable String markdownPath, String imagePath) {
    if (config.imageLimit <= 0) {
        return null;
    }
    String path = PathResolver.resolve(markdownPath, imagePath);
    if (path == null) {
        return null;
    }
    String type = MimeTypes.getMimeType(path);
    if (!ALLOWED_TYPES.contains(type)) {
        return null;
    }
    try {
        TreeWalk tw = TreeWalk.forPath(reader, path, root);
        if (tw == null || tw.getFileMode(0) != FileMode.REGULAR_FILE) {
            return null;
        }
        ObjectId id = tw.getObjectId(0);
        byte[] raw = reader.open(id, Constants.OBJ_BLOB).getCachedBytes(config.imageLimit);
        if (raw.length > config.imageLimit) {
            return null;
        }
        return "data:" + type + ";base64," + BaseEncoding.base64().encode(raw);
    } catch (LargeObjectException.ExceedsLimit e) {
        return null;
    } catch (IOException err) {
        String repo = view != null ? view.getRepositoryName() : "<unknown>";
        log.error(String.format("cannot read repo %s image %s from %s", repo, path, root.name()), err);
        return null;
    }
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 29 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gitiles by GerritCodeReview.

the class DiffServlet method doGetHtml.

@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);
    try (RevWalk walk = new RevWalk(repo);
        TreeWalk tw = newTreeWalk(walk, view)) {
        boolean showCommit;
        boolean isFile;
        AbstractTreeIterator oldTree;
        AbstractTreeIterator newTree;
        try {
            if (tw == null && !view.getPathPart().isEmpty()) {
                res.setStatus(SC_NOT_FOUND);
                return;
            }
            isFile = tw != null && isFile(tw);
            // If we are viewing the diff between a commit and one of its parents,
            // include the commit detail in the rendered page.
            showCommit = isParentOf(walk, view.getOldRevision(), view.getRevision());
            oldTree = getTreeIterator(walk, view.getOldRevision().getId());
            newTree = getTreeIterator(walk, view.getRevision().getId());
        } catch (MissingObjectException | IncorrectObjectTypeException e) {
            res.setStatus(SC_NOT_FOUND);
            return;
        }
        Map<String, Object> data = getData(req);
        data.put("title", "Diff - " + view.getRevisionRange());
        if (showCommit) {
            Set<Field> fs = CommitSoyData.DEFAULT_FIELDS;
            if (isFile) {
                fs = Field.setOf(fs, Field.PARENT_BLAME_URL);
            }
            GitilesAccess access = getAccess(req);
            DateFormatter df = new DateFormatter(access, Format.DEFAULT);
            data.put("commit", new CommitSoyData().setLinkifier(linkifier).setArchiveFormat(getArchiveFormat(access)).toSoyData(req, walk.parseCommit(view.getRevision().getId()), fs, df));
        }
        if (!data.containsKey("repositoryName") && (view.getRepositoryName() != null)) {
            data.put("repositoryName", view.getRepositoryName());
        }
        if (!data.containsKey("breadcrumbs")) {
            data.put("breadcrumbs", view.getBreadcrumbs());
        }
        setCacheHeaders(req, res);
        try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.diffDetail", data);
            DiffFormatter diff = new HtmlDiffFormatter(renderer, view, out)) {
            formatDiff(repo, oldTree, newTree, view.getPathPart(), diff);
        }
    }
}
Also used : OutputStream(java.io.OutputStream) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) Field(com.google.gitiles.CommitData.Field) Repository(org.eclipse.jgit.lib.Repository) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 30 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gerrit by GerritCodeReview.

the class RenameFileModification method getPathEdits.

@Override
public List<DirCacheEditor.PathEdit> getPathEdits(Repository repository, RevCommit baseCommit) throws IOException {
    try (RevWalk revWalk = new RevWalk(repository)) {
        revWalk.parseHeaders(baseCommit);
        try (TreeWalk treeWalk = TreeWalk.forPath(revWalk.getObjectReader(), currentFilePath, baseCommit.getTree())) {
            if (treeWalk == null) {
                return Collections.emptyList();
            }
            DirCacheEditor.DeletePath deletePathEdit = new DirCacheEditor.DeletePath(currentFilePath);
            AddPath addPathEdit = new AddPath(newFilePath, treeWalk.getFileMode(0), treeWalk.getObjectId(0));
            return Arrays.asList(deletePathEdit, addPathEdit);
        }
    }
}
Also used : DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Aggregations

TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)31 RevWalk (org.eclipse.jgit.revwalk.RevWalk)19 RevCommit (org.eclipse.jgit.revwalk.RevCommit)14 ObjectId (org.eclipse.jgit.lib.ObjectId)11 ArrayList (java.util.ArrayList)10 IOException (java.io.IOException)9 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)9 ObjectReader (org.eclipse.jgit.lib.ObjectReader)6 Repository (org.eclipse.jgit.lib.Repository)6 FileMode (org.eclipse.jgit.lib.FileMode)5 RevTree (org.eclipse.jgit.revwalk.RevTree)5 PathFilter (org.eclipse.jgit.treewalk.filter.PathFilter)5 FilestoreModel (com.gitblit.models.FilestoreModel)4 PathModel (com.gitblit.models.PathModel)4 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)3 LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)3 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)3 RefModel (com.gitblit.models.RefModel)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2