6

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.

2 Answers 2

11

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")));

1
  • Thanks Kevin. You're right, I've added the HEAD, and I've got the list of commits. :)
    – jomaora
    Aug 9, 2012 at 7:52
2

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.