jgit - git diff based on file extension

元气小坏坏 提交于 2019-12-23 08:56:12

问题


I am using JGit API (https://www.eclipse.org/jgit/) to access a git repository.

In the git repository, I am storing .txt files and other file formats also. I ran into a requirement where I should get the diff of only .txt files.

Basically I am trying to achieve the equivalent of

git diff master HEAD -- '*.txt'  

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

From this answer, (Equivalent of git diff in JGit) 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).

Could some one please give some pointer?


回答1:


If you use a DiffFormatter as suggested in Equivalent of git diff in JGit, you can specify a tree filter like this:

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

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

The example uses a PathSuffixFilter to exclude files ending with .txt.



来源:https://stackoverflow.com/questions/54008247/jgit-git-diff-based-on-file-extension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!