Search in sources :

Example 1 with DiffFormatter

use of org.eclipse.jgit.diff.DiffFormatter in project che by eclipse.

the class JGitDiffPage method writeTo.

@Override
public final void writeTo(OutputStream out) throws IOException {
    DiffFormatter formatter = new DiffFormatter(new BufferedOutputStream(out));
    formatter.setRepository(repository);
    List<String> rawFileFilter = params.getFileFilter();
    TreeFilter pathFilter = (rawFileFilter != null && rawFileFilter.size() > 0) ? PathFilterGroup.createFromStrings(rawFileFilter) : TreeFilter.ALL;
    formatter.setPathFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter));
    try {
        String commitA = params.getCommitA();
        String commitB = params.getCommitB();
        boolean cached = params.isCached();
        List<DiffEntry> diff;
        if (commitA == null && commitB == null && !cached) {
            diff = indexToWorkingTree(formatter);
        } else if (commitA != null && commitB == null && !cached) {
            diff = commitToWorkingTree(commitA, formatter);
        } else if (commitA == null && commitB != null) {
            diff = emptyToCommit(commitB, formatter);
        } else if (commitB == null) {
            diff = commitToIndex(commitA, formatter);
        } else {
            diff = commitToCommit(commitA, commitB, formatter);
        }
        DiffType type = params.getType();
        if (type == DiffType.NAME_ONLY) {
            writeNames(diff, out);
        } else if (type == DiffType.NAME_STATUS) {
            writeNamesAndStatus(diff, out);
        } else {
            writeRawDiff(diff, formatter);
        }
    } finally {
        formatter.close();
        repository.close();
    }
}
Also used : AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) BufferedOutputStream(java.io.BufferedOutputStream) DiffType(org.eclipse.che.api.git.shared.DiffType) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 2 with DiffFormatter

use of org.eclipse.jgit.diff.DiffFormatter in project gerrit by GerritCodeReview.

the class ChangeEmail method getUnifiedDiff.

/** Show patch set as unified difference. */
public String getUnifiedDiff() {
    PatchList patchList;
    try {
        patchList = getPatchList();
        if (patchList.getOldId() == null) {
            // Currently these always have a null oldId in the PatchList.
            return "[Octopus merge; cannot be formatted as a diff.]\n";
        }
    } catch (PatchListNotAvailableException e) {
        log.error("Cannot format patch", e);
        return "";
    }
    int maxSize = args.settings.maximumDiffSize;
    TemporaryBuffer.Heap buf = new TemporaryBuffer.Heap(Math.min(HEAP_EST_SIZE, maxSize), maxSize);
    try (DiffFormatter fmt = new DiffFormatter(buf)) {
        try (Repository git = args.server.openRepository(change.getProject())) {
            try {
                fmt.setRepository(git);
                fmt.setDetectRenames(true);
                fmt.format(patchList.getOldId(), patchList.getNewId());
                return RawParseUtils.decode(buf.toByteArray());
            } catch (IOException e) {
                if (JGitText.get().inMemoryBufferLimitExceeded.equals(e.getMessage())) {
                    return "";
                }
                log.error("Cannot format patch", e);
                return "";
            }
        } catch (IOException e) {
            log.error("Cannot open repository to format patch", e);
            return "";
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) PatchList(com.google.gerrit.server.patch.PatchList) TemporaryBuffer(org.eclipse.jgit.util.TemporaryBuffer) PatchListNotAvailableException(com.google.gerrit.server.patch.PatchListNotAvailableException) IOException(java.io.IOException) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter)

Example 3 with DiffFormatter

use of org.eclipse.jgit.diff.DiffFormatter 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 4 with DiffFormatter

use of org.eclipse.jgit.diff.DiffFormatter 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 5 with DiffFormatter

use of org.eclipse.jgit.diff.DiffFormatter in project che by eclipse.

the class JGitConnection method getCommitDiffFiles.

private List<DiffCommitFile> getCommitDiffFiles(RevCommit revCommit, String pattern) throws IOException {
    List<DiffEntry> diffs;
    TreeFilter filter = null;
    if (!isNullOrEmpty(pattern)) {
        filter = AndTreeFilter.create(PathFilterGroup.createFromStrings(Collections.singleton(pattern)), TreeFilter.ANY_DIFF);
    }
    List<DiffCommitFile> commitFilesList = new ArrayList<>();
    try (TreeWalk tw = new TreeWalk(repository)) {
        tw.setRecursive(true);
        // and to get the list of DiffEntry.
        if (revCommit.getParentCount() > 0) {
            RevCommit parent = parseCommit(revCommit.getParent(0));
            tw.reset(parent.getTree(), revCommit.getTree());
            if (filter != null) {
                tw.setFilter(filter);
            } else {
                tw.setFilter(TreeFilter.ANY_DIFF);
            }
            diffs = DiffEntry.scan(tw);
        } else {
            // list of DiffEntry.
            try (RevWalk rw = new RevWalk(repository);
                DiffFormatter diffFormat = new DiffFormatter(NullOutputStream.INSTANCE)) {
                diffFormat.setRepository(repository);
                if (filter != null) {
                    diffFormat.setPathFilter(filter);
                }
                diffs = diffFormat.scan(new EmptyTreeIterator(), new CanonicalTreeParser(null, rw.getObjectReader(), revCommit.getTree()));
            }
        }
    }
    if (diffs != null) {
        commitFilesList.addAll(diffs.stream().map(diff -> newDto(DiffCommitFile.class).withOldPath(diff.getOldPath()).withNewPath(diff.getNewPath()).withChangeType(diff.getChangeType().name())).collect(Collectors.toList()));
    }
    return commitFilesList;
}
Also used : DiffCommitFile(org.eclipse.che.api.git.shared.DiffCommitFile) ArrayList(java.util.ArrayList) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DiffEntry(org.eclipse.jgit.diff.DiffEntry) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)13 DiffEntry (org.eclipse.jgit.diff.DiffEntry)7 RevWalk (org.eclipse.jgit.revwalk.RevWalk)6 RevCommit (org.eclipse.jgit.revwalk.RevCommit)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Repository (org.eclipse.jgit.lib.Repository)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)3 MissingObjectException (org.eclipse.jgit.errors.MissingObjectException)3 RevTree (org.eclipse.jgit.revwalk.RevTree)3 AbstractTreeIterator (org.eclipse.jgit.treewalk.AbstractTreeIterator)3 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)3 AndTreeFilter (org.eclipse.jgit.treewalk.filter.AndTreeFilter)3 TreeFilter (org.eclipse.jgit.treewalk.filter.TreeFilter)3 ObjectReader (org.eclipse.jgit.lib.ObjectReader)2 EmptyTreeIterator (org.eclipse.jgit.treewalk.EmptyTreeIterator)2 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)2 PathChangeModel (com.gitblit.models.PathModel.PathChangeModel)1