How do I use filters in jGit?

时光怂恿深爱的人放手 提交于 2019-12-24 02:43:19

问题


How do I use RevFilters 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 RevFilters!

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!