Embed URL


SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Git Pre-commit Hook For Python now with extra Python!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/usr/bin/env python
#-*- mode: python -*-
from subprocess import Popen, PIPE
import sys
syntax_checker = "pyflakes"
def run(command):
p = Popen(command.split(), stdout=PIPE, stderr=PIPE)
return p.returncode,,
_, files_modified, _= run("git diff-index --name-only HEAD")
for fname in files_modified:
if fname.endswith(".py"):
print >>sys.stderr, "Checking syntax on %s: ... "%(fname,),
exit_code, _, errors = run("%s %s"%(syntax_checker, fname))
if exit_code != 0:
print >>sys.stderr, "\rChecking syntax on %s: FAILED! "%(fname,)
print >>sys.stderr, "\rChecking syntax on %s: OK!"%(fname,)

On line 12, why do you strip and split p.stdout exactly? (I notice when reverting this, the precommit hooks fails.)


Because run is first called with a 'git diff-index' and in that case the is a list of filenames. Of course you're right, in the pyflakes case the strip() and split() on the output is unnecessary. This would probably be easier to understand if there was a separate function for getting the list of modified file names.

Ah- Somehow I had completely missed line 14. Much easier to understand the control-flow now.

mtigas commented

Helpful modification I've been using:

L14 should probably be

_, files_modified, _= run("git diff-index --name-only --cached HEAD")

so that non-staged files are ignored. i.e. we only run pyflakes against the files we're trying to commit, not the entire source tree.

steder commented

Thanks @mtigas, I've updated the gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.