Search in sources :

Example 1 with AbstractTreeIterator

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

the class DiffServlet method doGetText.

@Override
protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);
    try (RevWalk walk = new RevWalk(repo)) {
        AbstractTreeIterator oldTree;
        AbstractTreeIterator newTree;
        try {
            oldTree = getTreeIterator(walk, view.getOldRevision().getId());
            newTree = getTreeIterator(walk, view.getRevision().getId());
        } catch (MissingObjectException | IncorrectObjectTypeException e) {
            res.setStatus(SC_NOT_FOUND);
            return;
        }
        try (Writer writer = startRenderText(req, res);
            OutputStream out = BaseEncoding.base64().encodingStream(writer);
            DiffFormatter diff = new DiffFormatter(out)) {
            formatDiff(repo, oldTree, newTree, view.getPathPart(), diff);
        }
    }
}
Also used : AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) Repository(org.eclipse.jgit.lib.Repository) OutputStream(java.io.OutputStream) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Writer(java.io.Writer)

Example 2 with AbstractTreeIterator

use of org.eclipse.jgit.treewalk.AbstractTreeIterator in project compiler by boalang.

the class GitCommit method getChangeFiles.

private void getChangeFiles(final RevCommit parent, final RevCommit rc, final HashMap<String, String> rChangedPaths, final HashMap<String, String> rRemovedPaths, final HashMap<String, String> rAddedPaths) {
    final DiffFormatter df = new DiffFormatter(NullOutputStream.INSTANCE);
    df.setRepository(repository);
    df.setDiffComparator(RawTextComparator.DEFAULT);
    df.setDetectRenames(true);
    try {
        final AbstractTreeIterator parentIter;
        if (parent == null)
            parentIter = new EmptyTreeIterator();
        else
            parentIter = new CanonicalTreeParser(null, repository.newObjectReader(), parent.getTree());
        for (final DiffEntry diff : df.scan(parentIter, new CanonicalTreeParser(null, repository.newObjectReader(), rc.getTree()))) {
            if (diff.getChangeType() == ChangeType.MODIFY || diff.getChangeType() == ChangeType.COPY || diff.getChangeType() == ChangeType.RENAME) {
                if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB && diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
                    String path = diff.getNewPath();
                    rChangedPaths.put(path, diff.getOldPath());
                    filePathGitObjectIds.put(path, diff.getNewId().toObjectId());
                }
            } else if (diff.getChangeType() == ChangeType.ADD) {
                if (diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
                    String path = diff.getNewPath();
                    rAddedPaths.put(path, null);
                    filePathGitObjectIds.put(path, diff.getNewId().toObjectId());
                }
            } else if (diff.getChangeType() == ChangeType.DELETE) {
                if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB) {
                    rRemovedPaths.put(diff.getOldPath(), diff.getOldPath());
                }
            }
        }
    } catch (final IOException e) {
        if (debug)
            System.err.println("Git Error getting commit diffs: " + e.getMessage());
    }
    df.close();
}
Also used : AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) IOException(java.io.IOException) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 3 with AbstractTreeIterator

use of org.eclipse.jgit.treewalk.AbstractTreeIterator 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)

Aggregations

DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)3 AbstractTreeIterator (org.eclipse.jgit.treewalk.AbstractTreeIterator)3 OutputStream (java.io.OutputStream)2 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)2 MissingObjectException (org.eclipse.jgit.errors.MissingObjectException)2 Repository (org.eclipse.jgit.lib.Repository)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 Field (com.google.gitiles.CommitData.Field)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 DiffEntry (org.eclipse.jgit.diff.DiffEntry)1 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)1 EmptyTreeIterator (org.eclipse.jgit.treewalk.EmptyTreeIterator)1 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)1