3

I write a Java program, and I use jgit. In this Java program, I want to list all commits of some git repository g that changed at least one file in a directory x.

I do not want to solve the problem "on command line", but within Java.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • interesting question, 1+; but what did you try? It might be easier to think in reverse, like how would you solve that with plain command line and then reverse engineer to jgit – Eugene Jan 07 '23 at 09:46
  • jgit is still a bit of a mystery to me. I tried to find information about changed files in RevCommit, and I also tried to google this, but had the impression that trying to list all changed files etc. is overkill. – J Fabian Meier Jan 07 '23 at 10:01

1 Answers1

1

In the high-level API of JGit you can use Git.log() with "addPath" to get a simple list of related commits.

   logs = git.log()
            .addPath("pom.xml")
            .call();

See this snippet in the jgit-cookbook for a ready-to-run example.

In general the methods on the entry-class Git provide similar functionality to the regular git commands.


For the low-level API which provides much more fine-grained control/results, you can use Git.diff() with "newTree" and "oldTree" and a "pathFilter". The trees are constructed via RevWalk and CanonicalTreeParser

There is a related snippet for this approach in the jgit-cookbook at ShowFileDiff.java

centic
  • 15,565
  • 9
  • 68
  • 125