Search in sources :

Example 1 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry 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 DiffEntry

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

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

the class JGitDiffPage method writeNamesAndStatus.

private void writeNamesAndStatus(List<DiffEntry> diff, OutputStream out) throws IOException {
    PrintWriter writer = new PrintWriter(out);
    int diffSize = diff.size();
    for (DiffEntry de : diff) {
        if (de.getChangeType() == ChangeType.ADD) {
            writer.print("A\t" + de.getNewPath() + (diffSize != diff.indexOf(de) + 1 ? lineSeparator() : ""));
        } else if (de.getChangeType() == ChangeType.DELETE) {
            writer.print("D\t" + de.getOldPath() + (diffSize != diff.indexOf(de) + 1 ? lineSeparator() : ""));
        } else if (de.getChangeType() == ChangeType.MODIFY) {
            writer.print("M\t" + de.getNewPath() + (diffSize != diff.indexOf(de) + 1 ? lineSeparator() : ""));
        } else if (de.getChangeType() == ChangeType.COPY) {
            writer.print("C\t" + de.getOldPath() + '\t' + de.getNewPath() + (diffSize != diff.indexOf(de) + 1 ? lineSeparator() : ""));
        } else if (de.getChangeType() == ChangeType.RENAME) {
            writer.print("R\t" + de.getOldPath() + '\t' + de.getNewPath() + (diffSize != diff.indexOf(de) + 1 ? lineSeparator() : ""));
        }
    }
    writer.flush();
}
Also used : PrintWriter(java.io.PrintWriter) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 4 with DiffEntry

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

the class JGitDiffPage method emptyToCommit.

/**
     * Show changes between specified revision and empty 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> emptyToCommit(String commitId, DiffFormatter formatter) throws IOException {
    ObjectId commit = repository.resolve(commitId);
    checkArgument(commit != null, "Invalid commit id " + commitId);
    RevTree tree;
    try (RevWalk revWalkA = new RevWalk(repository)) {
        tree = revWalkA.parseTree(commit);
    }
    List<DiffEntry> diff;
    try (ObjectReader reader = repository.newObjectReader()) {
        CanonicalTreeParser iterator = new CanonicalTreeParser();
        iterator.reset(reader, tree);
        diff = formatter.scan(new EmptyTreeIterator(), iterator);
    }
    return diff;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 5 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry 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)

Aggregations

DiffEntry (org.eclipse.jgit.diff.DiffEntry)66 RevCommit (org.eclipse.jgit.revwalk.RevCommit)24 RevWalk (org.eclipse.jgit.revwalk.RevWalk)23 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)22 ObjectReader (org.eclipse.jgit.lib.ObjectReader)21 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)20 IOException (java.io.IOException)19 Git (org.eclipse.jgit.api.Git)18 ObjectId (org.eclipse.jgit.lib.ObjectId)15 ArrayList (java.util.ArrayList)14 File (java.io.File)13 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)12 RevTree (org.eclipse.jgit.revwalk.RevTree)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Path (java.nio.file.Path)8 Test (org.junit.Test)8 FileTreeIterator (org.eclipse.jgit.treewalk.FileTreeIterator)7 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)7 Ref (org.eclipse.jgit.lib.Ref)6 Repository (org.eclipse.jgit.lib.Repository)6