Search in sources :

Example 1 with TreeFilterMarker

use of org.eclipse.jgit.treewalk.filter.TreeFilterMarker in project egit by eclipse.

the class FileDiff method compute.

/**
 * Computer file diffs for specified tree walk and commit
 *
 * @param repository
 * @param walk
 * @param commit
 * @param parents
 * @param markTreeFilters
 *            optional filters for marking entries, see
 *            {@link #isMarked(int)}
 * @return non-null but possibly empty array of file diffs
 * @throws MissingObjectException
 * @throws IncorrectObjectTypeException
 * @throws CorruptObjectException
 * @throws IOException
 */
public static FileDiff[] compute(final Repository repository, final TreeWalk walk, final RevCommit commit, final RevCommit[] parents, final TreeFilter... markTreeFilters) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {
    final ArrayList<FileDiff> r = new ArrayList<>();
    if (parents.length > 0) {
        walk.reset(trees(commit, parents));
    } else {
        walk.reset();
        walk.addTree(new EmptyTreeIterator());
        walk.addTree(commit.getTree());
    }
    if (walk.getTreeCount() <= 2) {
        List<DiffEntry> entries = DiffEntry.scan(walk, false, markTreeFilters);
        List<DiffEntry> xentries = new LinkedList<>(entries);
        RenameDetector detector = new RenameDetector(repository);
        detector.addAll(entries);
        List<DiffEntry> renames = detector.compute(walk.getObjectReader(), org.eclipse.jgit.lib.NullProgressMonitor.INSTANCE);
        for (DiffEntry m : renames) {
            final FileDiff d = new FileDiff(commit, m);
            r.add(d);
            for (Iterator<DiffEntry> i = xentries.iterator(); i.hasNext(); ) {
                DiffEntry n = i.next();
                if (m.getOldPath().equals(n.getOldPath()))
                    i.remove();
                else if (m.getNewPath().equals(n.getNewPath()))
                    i.remove();
            }
        }
        for (DiffEntry m : xentries) {
            final FileDiff d = new FileDiff(commit, m);
            r.add(d);
        }
    } else {
        // DiffEntry does not support walks with more than two trees
        final int nTree = walk.getTreeCount();
        final int myTree = nTree - 1;
        TreeFilterMarker treeFilterMarker = new TreeFilterMarker(markTreeFilters);
        while (walk.next()) {
            if (matchAnyParent(walk, myTree))
                continue;
            int treeFilterMarks = treeFilterMarker.getMarks(walk);
            final FileDiffForMerges d = new FileDiffForMerges(commit, treeFilterMarks);
            d.path = walk.getPathString();
            int m0 = 0;
            for (int i = 0; i < myTree; i++) m0 |= walk.getRawMode(i);
            final int m1 = walk.getRawMode(myTree);
            d.change = ChangeType.MODIFY;
            if (m0 == 0 && m1 != 0)
                d.change = ChangeType.ADD;
            else if (m0 != 0 && m1 == 0)
                d.change = ChangeType.DELETE;
            else if (m0 != m1 && walk.idEqual(0, myTree))
                // there is no ChangeType.TypeChanged
                d.change = ChangeType.MODIFY;
            d.blobs = new ObjectId[nTree];
            d.modes = new FileMode[nTree];
            for (int i = 0; i < nTree; i++) {
                d.blobs[i] = walk.getObjectId(i);
                d.modes[i] = walk.getFileMode(i);
            }
            r.add(d);
        }
    }
    final FileDiff[] tmp = new FileDiff[r.size()];
    r.toArray(tmp);
    return tmp;
}
Also used : TreeFilterMarker(org.eclipse.jgit.treewalk.filter.TreeFilterMarker) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) RenameDetector(org.eclipse.jgit.diff.RenameDetector) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 DiffEntry (org.eclipse.jgit.diff.DiffEntry)1 RenameDetector (org.eclipse.jgit.diff.RenameDetector)1 EmptyTreeIterator (org.eclipse.jgit.treewalk.EmptyTreeIterator)1 TreeFilterMarker (org.eclipse.jgit.treewalk.filter.TreeFilterMarker)1