Python, Git and a post-checkout hook 2009-08-23

When switching between branches with different sets of .py files it can easily happen that code containing broken imports is slipping through unnoticed. By broken import I mean “import foo” with no “” around on that branch.

Such thing can happen, as the .pyc file is still laying around. To prevent it you could stuff this into ./.git/hooks/post-checkout:

#!/usr/bin/env bash
find . -name '*.pyc' -print -delete | \
   sed 's|^|[post-checkout] Deleting |'

or a slightly smarter version of it:

#!/usr/bin/env bash
find . -type f -name '*.pyc' -exec bash -c '
if [[ ! -f "${py_file}" ]]; then
	rm -v "${pyc_file}"
' \; | \
sed 's|^|[post-checkout] |'
Leave a Reply

You must be logged in to post a comment.