JGit - Using parameters with LogCommand

1.1k views Asked by At

I'm using JGit to provide a service that will give information about various remote repositories. I'm trying to use JGit's LogCommand to do this, however I have been unable to find a way to do this.

I'm trying to achieve something analogous to doing the following:

git log --author="<username>" --pretty=tformat: --shortstat

However, I can't find any functionality that does that. Is there a way I could do this, with or without JGit?

1

There are 1 answers

2
RĂ¼diger Herrmann On BEST ANSWER

Compared with native Git's log command, the LogCommand if JGit offers only basic options. But there is a RevWalk in JGit that allows to specify custom filters while iterating over commits.

For example:

RevWalk walk = new RevWalk( repo );
walk.markStart( walk.parseCommit( repo.resolve( Constants.HEAD ) ) );
walk.sort( RevSort.REVERSE ); // chronological order
walk.setRevFilter( myFilter );
for( RevCommit commit : walk ) {
  // print commit
}
walk.close();

An example RevFilter that includes only commits of 'author' could look like this:

RevFilter filter = new RevFilter() {
  @Override
  public boolean include( RevWalk walker, RevCommit commit )
    throws StopWalkException, IOException
  {
    return commit.getAuthorIdent().getName().equals( "author" );
  }

  @Override
  public RevFilter clone() {
    return this; // may return this, or a copy if filter is not immutable
  }
};

To abort the walk, a filter may throws a StopWalkException.