How to run 'git diff --check' only for certain file types

I was not able to figure out how to run 'git diff --check' only in Python files in my commit. I have a commit with three files in it:

$ git show --name-only ... tools/gitHooks/preReceive.py tools/gitHooks/unittests/testPreReceive.py Makefile 

The last 2 files have spaces in them.

 $ git diff --check HEAD~1 HEAD tools/gitHooks/unittests/testPreReceive.py:75: trailing whitespace. +newmock = Mock() Makefile:41: new blank line at EOF. 

I would like to limit space checking to only Python files. Doing this gives me the correct files:

 $ git diff --name-only HEAD~1 HEAD | grep ".py" tools/gitHooks/preReceive.py tools/gitHooks/unittests/testPreReceive.py 

But when I pass the file names to 'git diff --check (with or without xargs), I get the wrong output.

 $ git diff --name-only HEAD~1 HEAD | grep ".py" | xargs git diff --check HEAD~1 HEAD -- $ $ git diff --name-only HEAD~1 HEAD | grep ".py" | git diff --check HEAD~1 HEAD -- tools/gitHooks/unittests/testPreReceive.py:75: trailing whitespace. +newmock = Mock() Makefile:41: new blank line at EOF. 

I also tried this, as well as a few variations on it with no luck:

 $ git diff --check HEAD~1 HEAD -- "*.py" $ 

Can anyone help? I feel like I'm close to an answer.

+4
source share
1 answer

From the original poster :

Turns out I used the wrong grep.

 $ git diff --name-only HEAD~1 HEAD | grep \.py$ | \ $ xargs git diff --check HEAD~1 HEAD -- tools/gitHooks/unittests/testPreReceive.py:75: trailing whitespace. +newmock = Mock() 
+3
source

Source: https://habr.com/ru/post/1434237/


All Articles