jgit - git diff based on file extension

Viewed 550

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 Answers

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.

Related