Thursday, October 20, 2011

Madly follow PEP8 with git help

If you intended or are obliged to strictly comply to PEP8 it may be useful to have a git hook that takes care of checking your code before you have committed and prevents you from amending previous commit again and again.

So, in .git/hooks/ directory of your repository create a file with name pre-commit and put

#!/bin/sh

# comma-separated list of files and directories which are not for checking
SKIP=migrations,bootstrap.py,dodo.py,manage.py

pep8 -r --exclude=$SKIP .

UPDATE: The code above checks all the project Python files. To limit checking only for files going to be committed

#!/bin/sh

FILES=$(git diff --cached --name-status | awk '$1 $2 { print $2}' | grep -e \.py$)
if [ -n "$FILES" ]; then
    pep8 -r $FILES
fi

The command shows all staged files, next only the filenames are left and filters Python files. If any they are to be checked with PEP8 utility.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.