Search in sources :

Example 51 with RevTree

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

the class DiffUtils method getDiffStat.

/**
 * Returns the diffstat between the two commits for the specified file or folder.
 *
 * @param repository
 * @param baseCommit
 *            if base commit is unspecified, the diffstat is generated against
 *            the primary parent of the specified commit.
 * @param commit
 * @param path
 *            if path is specified, the diffstat is generated only for the
 *            specified file or folder. if unspecified, the diffstat is
 *            generated for the entire diff between the two commits.
 * @return patch as a string
 */
public static DiffStat getDiffStat(Repository repository, RevCommit baseCommit, RevCommit commit, String path) {
    DiffStat stat = null;
    try {
        RawTextComparator cmp = RawTextComparator.DEFAULT;
        DiffStatFormatter df = new DiffStatFormatter(commit.getName(), repository);
        df.setRepository(repository);
        df.setDiffComparator(cmp);
        df.setDetectRenames(true);
        RevTree commitTree = commit.getTree();
        RevTree baseTree;
        if (baseCommit == null) {
            if (commit.getParentCount() > 0) {
                final RevWalk rw = new RevWalk(repository);
                RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
                baseTree = parent.getTree();
            } else {
                // FIXME initial commit. no parent?!
                baseTree = commitTree;
            }
        } else {
            baseTree = baseCommit.getTree();
        }
        List<DiffEntry> diffEntries = df.scan(baseTree, commitTree);
        if (path != null && path.length() > 0) {
            for (DiffEntry diffEntry : diffEntries) {
                if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
                    df.format(diffEntry);
                    break;
                }
            }
        } else {
            df.format(diffEntries);
        }
        stat = df.getDiffStat();
        df.flush();
    } catch (Throwable t) {
        LOGGER.error("failed to generate commit diff!", t);
    }
    return stat;
}
Also used : RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 52 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project omegat by omegat-org.

the class GITRemoteRepository2 method prepareTreeParser.

private static AbstractTreeIterator prepareTreeParser(Repository repository, ObjectId objId) throws Exception {
    // the TreeParser
    try (RevWalk walk = new RevWalk(repository)) {
        RevCommit commit = walk.parseCommit(objId);
        RevTree tree = walk.parseTree(commit.getTree().getId());
        CanonicalTreeParser treeParser = new CanonicalTreeParser();
        ObjectReader reader = repository.newObjectReader();
        treeParser.reset(reader, tree.getId());
        return treeParser;
    }
}
Also used : ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 53 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project jwt by emweb.

the class Git method treeSize.

public int treeSize(ObjectId treeId) {
    if (treeId.id == null)
        return 0;
    try {
        RevWalk walk = new RevWalk(repository, 0);
        RevTree tree = walk.parseTree(treeId.id);
        TreeWalk treeWalk = new TreeWalk(repository);
        treeWalk.addTree(tree);
        treeWalk.setRecursive(false);
        int count = 0;
        while (treeWalk.next()) ++count;
        return count;
    } catch (MissingObjectException e) {
        throw new RuntimeException(e);
    } catch (IncorrectObjectTypeException e) {
        throw new RuntimeException(e);
    } catch (CorruptObjectException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : CorruptObjectException(org.eclipse.jgit.errors.CorruptObjectException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.DepthWalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException)

Example 54 with RevTree

use of org.eclipse.jgit.revwalk.RevTree in project curiostack by curioswitch.

the class CurioGenericCiPlugin method parserForBranch.

private static CanonicalTreeParser parserForBranch(Git git, Ref branch) throws IOException {
    try (RevWalk walk = new RevWalk(git.getRepository())) {
        RevCommit commit = walk.parseCommit(branch.getObjectId());
        RevTree tree = walk.parseTree(commit.getTree().getId());
        final CanonicalTreeParser parser;
        try (ObjectReader reader = git.getRepository().newObjectReader()) {
            parser = parser(reader, tree.getId());
        }
        walk.dispose();
        return parser;
    }
}
Also used : ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) 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