问题
I'm using jGit to walk over the commits in a repository. When I use the RevWalk
class by itself, I get back all of the commits in the repository. When I use revWalk.setFilter()
to add a filter, though, I no longer get any commits whatsoever, even though I know for sure that commits exist that match my filter's criteria.
Here is my code:
RevWalk walk = new RevWalk(repo);
// These two lines give me trouble:
// RevFilter filter = CommitTimeRevFilter.between(sinceDate, untilDate);
// walk.setRevFilter(filter);
Ref head = repo.getRef("refs/heads/master");
RevCommit headCommit = walk.parseCommit(head.getObjectId());
// omitting some exception handling for conciseness
walk.markStart(headCommit);
List<MyCommitPOCO> commits = new LinkedList<>();
for(RevCommit commit : walk) {
MyCommitPOCO processedCommit = processRevCommit(commit);
commits.add(processedCommit);
}
walk.dispose();
return commits;
When I run this code without the two lines that are commented out at the top, the for loop iterating over walk
's commits processes every commit in my repository, which I expected. When I uncomment the lines with the RevFilter
, however, the for loop does not iterate over even a single commit, which doesn't seem right to me. I've tried running the program with multiple values for sinceDate
and untilDate
that I know are appropriate for the repository I'm trying to work with, but none of the attempts I've made have worked.
Why isn't my RevWalk
letting me iterate over any commits when I use a RevFilter
with it? What's wrong with the two lines of code that cause the problem when I uncomment them?
回答1:
I found the CommitTimeRevFilter
to work as expected. Have you debugged your code to see that the desired commits arrive at the filter code?
Below is a test that might serve as a starting point. It does four commits with timestamps of 1, 2, 3, 4 second and then uses a RevWalk with a time-filter to collect those between second 2 and 3:
public class RevWalkFilterLearningTest {
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
private Git git;
@Test
public void testTimeFilter() throws Exception {
commit( 1000 );
commit( 2000 );
commit( 3000 );
commit( 4000 );
RevWalk revWalk = new RevWalk( git.getRepository() );
RevFilter filter = CommitTimeRevFilter.between( new Date( 2000 ), new Date( 3000 ) );
revWalk.setRevFilter( filter );
Ref headRef = git.getRepository().getRef( Constants.HEAD );
RevCommit headCommit = revWalk.parseCommit( headRef.getObjectId() );
revWalk.markStart( headCommit );
int count = 0;
for( RevCommit revCommit : revWalk ) {
count++;
}
assertEquals( 2, count );
}
private void commit( int time ) throws Exception {
PersonIdent committer = new PersonIdent( "committer", "committer@example.com" );
git.commit().setCommitter( new PersonIdent( committer, new Date( time ) ) ).setMessage( "msg" ).call();
}
@Before
public void setUp() throws GitAPIException {
git = Git.init().setDirectory( tempFolder.getRoot() ).call();
}
@After
public void tearDown() {
git.close();
}
}
来源:https://stackoverflow.com/questions/28056917/when-i-use-a-revfilter-my-revwalk-isnt-selecting-any-commits