Browse files

added pep8 git pre commit hook

  • Loading branch information...
1 parent 6975ca6 commit 7fae3cb5d00472f9794d187fb0a767fd85dcfd80 @metachris committed Dec 23, 2011
Showing with 46 additions and 4 deletions.
  1. +10 −4
  2. +36 −0 python/pep8-git-pre-commit-hook
@@ -1,16 +1,22 @@
-#Boilerplate Collection
+# Boilerplate Collection
Boilerplates are basic setups with best practices. Just take the parts you like.
+### Python
* [Python Script](
* [Tornado Website](
* [App Engine Website](
+### PEP8 Pre Commit Hook for Git
+This [git pre-commit hook]( checks for pep8 compatibility on committing, and aborts the commit if errors are found. Can be skipped by applying the `--no-verify` flag to `git commit`. This requires the tool `pep8` which can be installed via package managers and homebrew.
+To use the commit hook in all projects on this system, copy it into the global git hook-templates directory (eg. `/usr/share/git-core/templates/hooks/`) as a file called `pre-commit` and set the permissions to executable. Afterwards you can enable this hook in existing repositories by calling `git init`. New repositories will use it by default.
-[Boilerplate dotfiles]( is a collection of command line goodness for OSX and Linux. Includes `.bash_profile`, `.aliases`, `.exports`, and others.
+### Dotfiles
+[Boilerplate dotfiles]( is a collection of command line goodness for OSX and Linux. Includes `.bash_profile`, `.aliases`, `.exports`, and others.
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+from __future__ import with_statement
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+def system(*args, **kwargs):
+ kwargs.setdefault('stdout', subprocess.PIPE)
+ proc = subprocess.Popen(args, **kwargs)
+ out, err = proc.communicate()
+ return out
+def main():
+ files = (file for file in system('git', 'diff', '--name-only', '--staged', '--diff-filter=AM').splitlines() if file.endswith('.py'))
+ tempdir = tempfile.mkdtemp()
+ for name in files:
+ filename = os.path.join(tempdir, name)
+ filepath = os.path.dirname(filename)
+ if not os.path.exists(filepath):
+ os.makedirs(filepath)
+ with file(filename, 'w') as f:
+ system('git', 'show', ':' + name, stdout=f)
+ output = system('pep8', '.', cwd=tempdir)
+ shutil.rmtree(tempdir)
+ if output:
+ print output,
+ sys.exit(1)
+if __name__ == '__main__':
+ main()

0 comments on commit 7fae3cb

Please sign in to comment.