jgit - git 基于文件扩展名的差异

jgit - git diff based on file extension

我正在使用 JGit API (https://www.eclipse.org/jgit/) 访问 git 存储库。

在 git 存储库中,我还存储 .txt 文件和其他文件格式。我 运行 进入一个要求,我应该只获得 .txt 文件的差异。

基本上我正在努力实现相当于

git diff master HEAD -- '*.txt'  

How to filter git diff based on file extensions? 使用 JGit API.

根据这个回答,() I understood how to get the normal diff. But I would like to add the file extension restriction to that but I could not see anything in the DiffCommand doc (https://download.eclipse.org/jgit/site/5.2.0.201812061821-r/apidocs/index.html)。

哪位大侠能指点一下吗?

如果您按照 中的建议使用 DiffFormatter,您可以像这样指定一个树过滤器:

TreeFilter treeFilter = PathSuffixFilter.create(".txt")

DiffFormatter diffFormatter = ...
diffFormatter.setPathFilter(treeFilter);

示例使用 PathSuffixFilter 排除以 .txt 结尾的文件。