Search in sources :

Example 1 with DiffCommitFile

use of org.eclipse.che.api.git.shared.DiffCommitFile 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

ArrayList (java.util.ArrayList)1 DiffCommitFile (org.eclipse.che.api.git.shared.DiffCommitFile)1 DiffEntry (org.eclipse.jgit.diff.DiffEntry)1 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)1 EmptyTreeIterator (org.eclipse.jgit.treewalk.EmptyTreeIterator)1 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)1 AndTreeFilter (org.eclipse.jgit.treewalk.filter.AndTreeFilter)1 TreeFilter (org.eclipse.jgit.treewalk.filter.TreeFilter)1