Example usage for org.eclipse.jgit.api Git open

List of usage examples for org.eclipse.jgit.api Git open

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git open.

Prototype

public static Git open(File dir) throws IOException 

Source Link

Document

Open repository

Usage

From source file:util.DifferenceComputer.java

License:Apache License

public static List<DiffEntry> getChangedFilesBetweenTwoCommits(String repoPath, ObjectId ancestor,
        ObjectId commit) {//from w  w  w .ja v a2  s  .  c  om
    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;
}