7

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?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
nantitv
  • 3,539
  • 4
  • 38
  • 61

1 Answers1

5

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.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • Could you please have a look at the follow up question https://stackoverflow.com/questions/54019259/jgit-how-to-reuse-the-diffformatter – nantitv Jan 03 '19 at 09:15