jgit-基于文件扩展名的git diff

时间:2019-01-02 14:38:18

标签: java git jgit

我正在使用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。

从这个答案,(Equivalent of git diff in JGit)我了解了如何获得正常的差异。但是我想为此添加文件扩展名限制,但在DiffCommand文档(https://download.eclipse.org/jgit/site/5.2.0.201812061821-r/apidocs/index.html)中看不到任何内容。

有人可以给我一些指点吗?

1 个答案:

答案 0 :(得分:4)

如果您按照Equivalent of git diff in JGit中的建议使用DiffFormatter,则可以指定如下所示的树过滤器:

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

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

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