问题
How do I use RevFilter
s in jGit?
I found an answer to a question I had about completing a particular task (getting the commits made between two dates), and the answer said to use a particular subclass of RevFilter
. However, I don't know how to use RevFilter
s!
In particular, I would like to know what I need to do to take the answer in the question I linked to, which says
Date since = getClock(); Date until = getClock(); RevFilter between = CommitTimeRevFilter.between(since, until);
And use it to actually iterate over the commits between the two dates. Something like:
RevFilter between = CommitTimeRevFilter.between(since, until);
RevWalk walk = new RevWalk(repository);
walk.magicallyApplyFilters(between);
for(RevCommit commit : RevWalk) {
// Do my thing
}
I have read the jGit documentation. Sadly, in the section that would show how to use filters, there is the line:
TODO talk about filters
So the documentation does not help me. And although I get the impression that using filters is a basic part of using jGit, nobody else has asked how to use them on StackOverflow yet!
回答1:
Judging by the source, all you need to do is create an instance of the RevWalk
iterator, set the filter, then iterate over the walker.
RevWalk walk = new RevWalk(repo);
walk.markStart(walk.parseCommit(repo.resolve(Constants.HEAD)));
walk.sort(RevSort.REVERSE); // chronological order
final LocalDate thisYear = new LocalDate(2015, 1, 1); // joda
walk.setRevFilter(CommitTimeRevFilter.after(thisYear.toDate()));
for(RevCommit commit : walk) {
// do your thing
}
The filter is essentially just a predicate on whether or not a commit is yielded from the walker. There are a number of filters that you could use found in the org.eclipse.jgit.revwalk.filter
package. Or you can create your own by extending the RevFilter
class.
来源:https://stackoverflow.com/questions/27993576/how-do-i-use-filters-in-jgit