Search in sources :

Example 1 with RevTree

use of org.eclipse.jgit.revwalk.RevTree 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 RevTree

use of org.eclipse.jgit.revwalk.RevTree in project che by eclipse.

the class JGitDiffPage method commitToCommit.

/**
     * Show changes between specified two revisions and index. If
     * <code>commitAId == null</code> then view changes between HEAD and revision commitBId.
     *
     * @param commitAId
     *            id of commit A, pass <code>null</code> is the same as pass HEAD
     * @param commitBId
     *            id of commit B
     * @param formatter
     *            diff formatter
     * @return list of diff entries
     * @throws IOException
     *             if any i/o errors occurs
     */
private List<DiffEntry> commitToCommit(String commitAId, String commitBId, DiffFormatter formatter) throws IOException {
    if (commitAId == null) {
        commitAId = Constants.HEAD;
    }
    ObjectId commitA = repository.resolve(commitAId);
    if (commitA == null) {
        throw new IllegalArgumentException("Invalid commit id " + commitAId);
    }
    ObjectId commitB = repository.resolve(commitBId);
    if (commitB == null) {
        throw new IllegalArgumentException("Invalid commit id " + commitBId);
    }
    RevTree treeA;
    try (RevWalk revWalkA = new RevWalk(repository)) {
        treeA = revWalkA.parseTree(commitA);
    }
    RevTree treeB;
    try (RevWalk revWalkB = new RevWalk(repository)) {
        treeB = revWalkB.parseTree(commitB);
    }
    if (!params.isNoRenames()) {
        // Use embedded RenameDetector it works well with index and revision
        // history.
        formatter.setDetectRenames(true);
        int renameLimit = params.getRenameLimit();
        if (renameLimit > 0) {
            formatter.getRenameDetector().setRenameLimit(renameLimit);
        }
    }
    return formatter.scan(treeA, treeB);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 3 with RevTree

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

use of org.eclipse.jgit.revwalk.RevTree in project gerrit by GerritCodeReview.

the class AbstractSubmoduleSubscription method expectToHaveSubmoduleState.

protected void expectToHaveSubmoduleState(TestRepository<?> repo, String branch, String submodule, ObjectId expectedId) throws Exception {
    submodule = name(submodule);
    ObjectId commitId = repo.git().fetch().setRemote("origin").call().getAdvertisedRef("refs/heads/" + branch).getObjectId();
    RevWalk rw = repo.getRevWalk();
    RevCommit c = rw.parseCommit(commitId);
    rw.parseBody(c.getTree());
    RevTree tree = c.getTree();
    RevObject actualId = repo.get(tree, submodule);
    assertThat(actualId).isEqualTo(expectedId);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevObject(org.eclipse.jgit.revwalk.RevObject) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 5 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project gerrit by GerritCodeReview.

the class AbstractSubmoduleSubscription method expectToHaveSubmoduleState.

protected void expectToHaveSubmoduleState(TestRepository<?> repo, String branch, String submodule, TestRepository<?> subRepo, String subBranch) throws Exception {
    submodule = name(submodule);
    ObjectId commitId = repo.git().fetch().setRemote("origin").call().getAdvertisedRef("refs/heads/" + branch).getObjectId();
    ObjectId subHead = subRepo.git().fetch().setRemote("origin").call().getAdvertisedRef("refs/heads/" + subBranch).getObjectId();
    RevWalk rw = repo.getRevWalk();
    RevCommit c = rw.parseCommit(commitId);
    rw.parseBody(c.getTree());
    RevTree tree = c.getTree();
    RevObject actualId = repo.get(tree, submodule);
    assertThat(actualId).isEqualTo(subHead);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevObject(org.eclipse.jgit.revwalk.RevObject) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

RevTree (org.eclipse.jgit.revwalk.RevTree)54 RevWalk (org.eclipse.jgit.revwalk.RevWalk)41 RevCommit (org.eclipse.jgit.revwalk.RevCommit)28 ObjectId (org.eclipse.jgit.lib.ObjectId)27 ObjectReader (org.eclipse.jgit.lib.ObjectReader)19 IOException (java.io.IOException)16 Repository (org.eclipse.jgit.lib.Repository)13 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)13 DiffEntry (org.eclipse.jgit.diff.DiffEntry)9 RevObject (org.eclipse.jgit.revwalk.RevObject)9 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)9 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)7 File (java.io.File)6 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)6 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)6 Ref (org.eclipse.jgit.lib.Ref)6 ArrayList (java.util.ArrayList)3 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)3 RawTextComparator (org.eclipse.jgit.diff.RawTextComparator)3 DirCache (org.eclipse.jgit.dircache.DirCache)3