Example usage for org.eclipse.jgit.lib Repository newObjectReader

List of usage examples for org.eclipse.jgit.lib Repository newObjectReader

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository newObjectReader.

Prototype

@NonNull
public ObjectReader newObjectReader() 

Source Link

Document

Create a new reader to read objects from #getObjectDatabase() .

Usage

From source file:svnserver.repository.git.GitRepository.java

License:GNU General Public License

@NotNull
public Iterable<GitTreeEntry> loadTree(@Nullable GitTreeEntry tree) throws IOException {
    final GitObject<ObjectId> treeId = getTreeObject(tree);
    // Loading tree.
    if (treeId == null) {
        return Collections.emptyList();
    }/*from   www. j  a  v a 2  s. c o m*/
    final List<GitTreeEntry> result = new ArrayList<>();
    final Repository repo = treeId.getRepo();
    final CanonicalTreeParser treeParser = new CanonicalTreeParser(GitRepository.emptyBytes,
            repo.newObjectReader(), treeId.getObject());
    while (!treeParser.eof()) {
        result.add(new GitTreeEntry(treeParser.getEntryFileMode(),
                new GitObject<>(repo, treeParser.getEntryObjectId()), treeParser.getEntryPathString()));
        treeParser.next();
    }
    return result;
}

From source file:util.DifferenceComputer.java

License:Apache License

public static List<DiffEntry> getChangedFilesBetweenTwoCommits(String repoPath, ObjectId ancestor,
        ObjectId commit) {//from   www.  jav  a  2 s. co m
    List<DiffEntry> diffs = null;

    try {
        // The {tree} will return the underlying tree-id instead of the commit-id itself!
        // For a description of what the carets do see e.g. http://www.paulboxley.com/blog/2011/06/git-caret-and-tilde
        // This means we are selecting the parent of the parent of the parent of the parent of current HEAD and
        // take the tree-ish of it

        Git git = Git.open(new File(repoPath));
        Repository repository = git.getRepository();

        //ObjectId oldHead = repository.resolve(repository.findRef(branchToLookInto).getObjectId().getName()+"^^{tree}");//http://download.eclipse.org/jgit/site/4.2.0.201601211800-r/apidocs/org/eclipse/jgit/lib/Repository.html#resolve(java.lang.String)
        //ObjectId head = repository.resolve(repository.findRef(branchToLookInto).getObjectId().getName()+"^{tree}");

        ObjectId oldHead = repository.resolve(ancestor.getName() + "^{tree}");//http://download.eclipse.org/jgit/site/4.2.0.201601211800-r/apidocs/org/eclipse/jgit/lib/Repository.html#resolve(java.lang.String)
        ObjectId head = repository.resolve(commit.getName() + "^{tree}");

        System.out.println("Printing diff between tree: " + oldHead + "and" + head);

        // prepare the two iterators to compute the diff between

        ObjectReader reader = repository.newObjectReader();

        CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
        oldTreeIter.reset(reader, oldHead);
        CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
        newTreeIter.reset(reader, head);

        // finally get the list of changed files
        git = new Git(repository);

        diffs = git.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call();
        int changedFiles = 0;
        for (DiffEntry diff : diffs) {
            System.out.println("Entry: " + diff);
            System.out.println("getNewPath: " + diff.getNewPath());
            System.out.println("getScore: " + diff.getScore());
            System.out.println("getChangeType: " + diff.getChangeType());
            System.out.println("getNewMode: " + diff.getNewMode());
            System.out.println("getPath: " + diff.getPath(null));
            changedFiles++;
        }
        System.out.println("Changed Files: " + changedFiles);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("Done");

    return diffs;
}