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.
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")));
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With