简体   繁体   中英

Find all commits that changed a given directory from Java

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.

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

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