Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jgit - git diff based on file extension

Tags:

java

git

jgit

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?

like image 618
nantitv Avatar asked Jan 02 '19 14:01

nantitv


People also ask

How do I tell the difference between two files in github?

The git diff command displays the differences between files in two commits or between a commit and your current repository. You can see what text has been added to, removed from, and changed in a file. By default, the git diff command displays any uncommitted changes to your repository.

How does git diff work internally?

Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.

What is git diff R?

I get that R probably stands for "Rename". git rename.

Why is git diff not showing?

In answer to the original question, git diff isn't showing anything because you have a brand new directory, with a newly added file, but there are zero changes in the file for git diff to show. git status is showing that you added a new file, but git diff is for showing changes within files.


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.

like image 177
Rüdiger Herrmann Avatar answered Sep 30 '22 05:09

Rüdiger Herrmann