JGit:使用之间过滤Revwalk并获取结果的TreeWalk

时间:2015-02-27 20:47:40

标签: java jgit

我基本上想要按月过滤我的TreeWalk。以下是我过去常用的代码:

head = repository.getRef("HEAD");
walk = new RevWalk(repository);

Date since= new Date();
since.setMonth(02);
since.setYear(2014-1900);
since.setDate(01);

Date until= new Date();
until.setMonth(02);
until.setYear(2014-1900);
until.setDate(31);

RevFilter between = CommitTimeRevFilter.between(since,until);
walk.setRevFilter(between);

commit = walk.parseCommit(head.getObjectId());
tree = commit.getTree();

TreeWalk treeWalk = new TreeWalk(repository);
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
TreeFilter treefilter= walk.getTreeFilter();
treeWalk.setFilter();
return treeWalk;

我首先过滤RevWalk然后,我获取一个TreeWalk因为我最终需要一个月的树木行走来从中获取我的详细信息。

问题是,即使在过滤后我得到了头(最后一次提交)。我确定问题是

commit = walk.parseCommit(head.getObjectId());

但我不知道如何修复它以获得过滤的TreeWalk一个月。

1 个答案:

答案 0 :(得分:0)

试试这段代码(我在这篇文章中得到了解决这个问题的灵感:https://stackoverflow.com/a/27993763/4997684):

final RevWalk walk = new RevWalk(repository);

Date since= new Date();
since.setMonth(02);
since.setYear(2014-1900);
since.setDate(01);

Date until= new Date();
until.setMonth(02);
until.setYear(2014-1900);
until.setDate(31);

RevFilter between = CommitTimeRevFilter.between(since, until);

walk.markStart(walk.parseCommit(repository.resolve(Constants.HEAD)));
walk.sort(RevSort.REVERSE);
walk.setRevFilter(between);

for(RevCommit revCommit : walk) {
    System.out.println(new Date((long) revCommit.getCommitTime()*1000L));
}