简体   繁体   中英

Using JGit to count the number of commits on a file

I am trying to use Java list all the files that are present in a git repository by the number of commits. For this, I am using the JGit library.

String filename = "/a/b/c.java";
String localPath = localPath;
Repository localRepo = new FileRepository(localPath + "/.git");
Git git = new Git(localRepo);
ObjectId head = localRepo.resolve(Constants.HEAD);
Iterable<RevCommit> logs = git.log().add(head).addPath(filename).call();
for (RevCommit revCommit : logs) {
    count++;
}

However, I am getting count as 0 always.

When I use a file that is existing in the repo home directory I am getting the correct answer:

String filename = "d.java";
String localPath = localPath;
Repository localRepo = new FileRepository(localPath + "/.git");
Git git = new Git(localRepo);
ObjectId head = localRepo.resolve(Constants.HEAD);
Iterable<RevCommit> logs = git.log().add(head).addPath(filename).call();
for (RevCommit revCommit : logs) {
    count++;
}

However, I am not getting the right answer even if I change to the directory which contains c.java , I am still not getting the right answer:

String filename = "c.java";
String localPath = localPath;
Repository localRepo = new FileRepository(localPath + "/.git");
Git git = new Git(localRepo);
ObjectId head = localRepo.resolve(Constants.HEAD);
System.setProperty("user.dir", localPath + "a/b";
Iterable<RevCommit> logs = git.log().add(head).addPath(filename).call();
for (RevCommit revCommit : logs) {
    count++;
}

Can someone please help me understand what I have done wrong?

Your snippets don't show how the commits are created / in which state the repository is. Though, one is for sure: setting the system property user.dir like in your last snippet is useless, it is not evaluated by JGit.

The following is not quite the answer but a working example of how git log can be limited to a specific file in a folder. Running the code with a recent version of JGit succeeds.

public class LogLearningTest {
  @Rule
  public TemporaryFolder tempFolder = new TemporaryFolder();

  private Git git;
  private File file;

  @Test
  public void testCountNumberOfCommitsOnFile() throws Exception {
    writeFile( "initial content" );
    commitAll( "Create file in folder" );
    commitAll( "Unrelated commit" );
    writeFile( "changed content" );
    commitAll( "Change file in folder" );
    git.commit().setMessage( "Other commit" ).call();

    Iterable<RevCommit> logs = git.log().addPath( "folder/file" ).call();
    int count = 0;
    for( RevCommit revCommit : logs ) {
      count++;
    }

    assertEquals( 2, count );
  }

  @Before
  public void setUp() throws GitAPIException {
    git = Git.init().setDirectory( tempFolder.getRoot() ).call();
    File folder = new File( git.getRepository().getWorkTree(), "folder" );
    folder.mkdir();
    file = new File( folder, "file" );
  }

  @After
  public void tearDown() {
    git.getRepository().close();
  }

  private void writeFile( String content ) throws IOException {
    FileOutputStream outputStream = new FileOutputStream( file );
    outputStream.write( content.getBytes( "UTF-8" ) );
    outputStream.close();
  }

  private void commitAll( String message ) throws GitAPIException {
    git.add().addFilepattern( "." ).call();
    git.commit().setMessage( message ).call();
  }

}

The error is here:

String filename = "/a/b/c.java";

Try a repository-relative path instead (assuming a is a directory in the root of your repository):

String filename = "a/b/c.java";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM