Skip to content

Instantly share code, notes, and snippets.

@ndarville
Last active December 16, 2015 17:19
Show Gist options
  • Save ndarville/5469173 to your computer and use it in GitHub Desktop.
Save ndarville/5469173 to your computer and use it in GitHub Desktop.
git hooks—remember `chmod +x <file>`
# ~/.bash_aliases
alias gc='git commit'
alias gcv='git commit --no-verify'
#!/usr/bin/env bash
### On post-checkout, clean up files
### http://codeinthehole.com/writing/a-useful-git-post-checkout-hook-for-python-repos/
# Delete .pyc files and empty directories from root of project
cd ./$(git rev-parse --show-cdup)
# Clean-up
find . -name ".DS_Store" -delete
NUM_PYC_FILES=$( find . -name "*.pyc" | wc -l | tr -d ' ' )
if [ $NUM_PYC_FILES -gt 0 ]; then
find . -name "*.pyc" -delete
printf "\e[00;31mDeleted $NUM_PYC_FILES .pyc files\e[00m\n"
fi
NUM_EMPTY_DIRS=$( find . -type d -empty | wc -l | tr -d ' ' )
if [ $NUM_EMPTY_DIRS -gt 0 ]; then
find . -type d -empty -delete
printf "\e[00;31mDeleted $NUM_EMPTY_DIRS empty directories\e[00m\n"
fi
# Via https://news.ycombinator.com/item?id=3245011
MIGRATIONS_DIRECTORIES="$(find -type d -iname migrations)"
UNTRACKED_MIGRATIONS="$(git ls-files --exclude-standard --others -- $MIGRATIONS_DIRECTORIES | egrep '(.*)[.]py$')"
if test -z "$UNTRACKED_MIGRATIONS"; then
# If there are no untracked .py files in the migrations directory, do nothing, allow commit.
true;
else
# If there are untracked files in the migrations directory print a warning message.
echo "Warning -- Untracked files in the migrations directory"
echo 'The commit may be forced with "git commit --no-verify"'
echo
echo "$UNTRACKED_MIGRATIONS"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment