5

I have been unable to figure out how to run 'git diff --check' on only Python files in my commit. I have a commit with 3 files in it:

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

The last 2 files have trailing whitespace 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 restrict the whitespace check 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 pipe the filenames 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 the answer.

1
  • Hi, I've edited out your solution and placed it into this community wiki, because solutions only go into answers, so that people can vote on them separately from the question. If you want to write the same answer yourself so that you receive reputation from upvotes, go ahead and let me know, and I'll delete the community wiki. Commented Jul 8, 2014 at 21:55

1 Answer 1

2

From the original poster:

Turns out I was using 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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.