Search in sources :

Example 1 with RenameDetector

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

the class JGitDiffPage method commitToWorkingTree.

/**
     * Show changes between specified revision and working tree.
     *
     * @param commitId
     *            id of commit
     * @param formatter
     *            diff formatter
     * @return list of diff entries
     * @throws IOException
     *             if any i/o errors occurs
     */
private List<DiffEntry> commitToWorkingTree(String commitId, DiffFormatter formatter) throws IOException {
    ObjectId commitA = repository.resolve(commitId);
    if (commitA == null) {
        File heads = new File(repository.getWorkTree().getPath() + "/.git/refs/heads");
        if (heads.exists() && heads.list().length == 0) {
            return Collections.emptyList();
        }
        throw new IllegalArgumentException("Invalid commit id " + commitId);
    }
    RevTree treeA;
    try (RevWalk revWalkA = new RevWalk(repository)) {
        treeA = revWalkA.parseTree(commitA);
    }
    List<DiffEntry> diff;
    try (ObjectReader reader = repository.newObjectReader()) {
        CanonicalTreeParser iterA = new CanonicalTreeParser();
        iterA.reset(reader, treeA);
        FileTreeIterator iterB = new FileTreeIterator(repository);
        // Seems bug in DiffFormatter when work with working. Disable detect
        // renames by formatter and do it later.
        formatter.setDetectRenames(false);
        diff = formatter.scan(iterA, iterB);
        if (!params.isNoRenames()) {
            // Detect renames.
            RenameDetector renameDetector = createRenameDetector();
            ContentSource.Pair sourcePairReader = new ContentSource.Pair(ContentSource.create(reader), ContentSource.create(iterB));
            renameDetector.addAll(diff);
            diff = renameDetector.compute(sourcePairReader, NullProgressMonitor.INSTANCE);
        }
    }
    return diff;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) ContentSource(org.eclipse.jgit.diff.ContentSource) RenameDetector(org.eclipse.jgit.diff.RenameDetector) ObjectReader(org.eclipse.jgit.lib.ObjectReader) File(java.io.File) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) RevTree(org.eclipse.jgit.revwalk.RevTree) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 2 with RenameDetector

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

the class JGitDiffPage method createRenameDetector.

private RenameDetector createRenameDetector() {
    RenameDetector renameDetector = new RenameDetector(repository);
    int renameLimit = params.getRenameLimit();
    if (renameLimit > 0) {
        renameDetector.setRenameLimit(renameLimit);
    }
    return renameDetector;
}
Also used : RenameDetector(org.eclipse.jgit.diff.RenameDetector)

Example 3 with RenameDetector

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

the class JGitDiffPage method indexToWorkingTree.

/**
     * Show changes between index and working tree.
     *
     * @param formatter
     *            diff formatter
     * @return list of diff entries
     * @throws IOException
     *             if any i/o errors occurs
     */
private List<DiffEntry> indexToWorkingTree(DiffFormatter formatter) throws IOException {
    DirCache dirCache = null;
    ObjectReader reader = repository.newObjectReader();
    List<DiffEntry> diff;
    try {
        dirCache = repository.lockDirCache();
        DirCacheIterator iterA = new DirCacheIterator(dirCache);
        FileTreeIterator iterB = new FileTreeIterator(repository);
        // Seems bug in DiffFormatter when work with working. Disable detect
        // renames by formatter and do it later.
        formatter.setDetectRenames(false);
        diff = formatter.scan(iterA, iterB);
        if (!params.isNoRenames()) {
            // Detect renames.
            RenameDetector renameDetector = createRenameDetector();
            ContentSource.Pair sourcePairReader = new ContentSource.Pair(ContentSource.create(reader), ContentSource.create(iterB));
            renameDetector.addAll(diff);
            diff = renameDetector.compute(sourcePairReader, NullProgressMonitor.INSTANCE);
        }
    } finally {
        reader.close();
        if (dirCache != null) {
            dirCache.unlock();
        }
    }
    return diff;
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) ContentSource(org.eclipse.jgit.diff.ContentSource) RenameDetector(org.eclipse.jgit.diff.RenameDetector) ObjectReader(org.eclipse.jgit.lib.ObjectReader) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Aggregations

RenameDetector (org.eclipse.jgit.diff.RenameDetector)3 ContentSource (org.eclipse.jgit.diff.ContentSource)2 DiffEntry (org.eclipse.jgit.diff.DiffEntry)2 ObjectReader (org.eclipse.jgit.lib.ObjectReader)2 FileTreeIterator (org.eclipse.jgit.treewalk.FileTreeIterator)2 File (java.io.File)1 DirCache (org.eclipse.jgit.dircache.DirCache)1 DirCacheIterator (org.eclipse.jgit.dircache.DirCacheIterator)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 RevTree (org.eclipse.jgit.revwalk.RevTree)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)1