Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jGit using RevWalk to get RevCommit returns nothing

Tags:

java

git

jgit

I'm newbie on jGit and Git. I'm trying to query the historic of commit of a git repository, but I would like to get only the commit of a specific user. Reading the docs I saw that RevWalk will allow me to add RevFilters in orther to limit the search.

First, I use a Git object and its log method to list the commits, and it works very well. But using RevWalk, nothing happens.

public static void main(String[] args) throws IOException, GitAPIException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.setGitDir(new File("/home/joan/testGit/testMockito/.git"))
    .readEnvironment().findGitDir().build();

    //This works but I get all the commits
    Git git = new Git(repository);
    Iterable<RevCommit> log = git.log().call();
    for (Iterator<RevCommit> iterator = log.iterator(); iterator.hasNext();) {
        RevCommit rev = iterator.next();
        System.out.println(rev.getAuthorIdent().getName());
        System.out.println(rev.getFullMessage());
    }

    RevWalk walk = new RevWalk(repository);
    for (Iterator<RevCommit> iterator = walk.iterator(); iterator.hasNext();) {
        //It never cames in this block

        RevCommit rev = iterator.next();
        System.out.println(rev.getAuthorIdent().getName());
        System.out.println(rev.getFullMessage());
    }

}

Any advice? Am I doing something wrong?

Thanks.

like image 274
jomaora Avatar asked Aug 08 '12 16:08

jomaora


2 Answers

You need to add commits to the RevWalk by callingmarkStart before iterating over it.

For example to start the RevWalk with the current repository's HEAD commit:

walk.markStart(walk.parseCommit(repository.resolve("HEAD")));

like image 87
Kevin Sawicki Avatar answered Sep 27 '22 20:09

Kevin Sawicki


Why not just use LogCommand from org.eclipse.jgit.api?

Git git = new Git(db);

Iterable<RevCommit> log = git.log().call();

Then you could filter author based on the commits you get back. I also recommend looking at Gitective which is a layer above JGit. You can learn how to implement JGit related things with it: https://github.com/kevinsawicki/gitective/

You could do something like this:

PersonIdent person = new PersonIdent("Chris", "[email protected]");

filters.add(new AuthorFilter(person));

And then you can call CommitFinder... with the desired filters...

like image 45
Chris Aniszczyk Avatar answered Sep 27 '22 21:09

Chris Aniszczyk