Álvaro Ramírez
Pre-commit hooks to save you from yourself
Wanted to try out some code, but needed to ensure never checked in. Git pre-commit hooks are handy in this space. Add the following script to search for either @COMMITFAIL or @NOCOMMIT in the staged files. If found, attempts to commit will fail.
Based on https://gist.github.com/rex/223b4be50285f6b8b3e06dea50d15887:
#!/bin/bash set -o nounset set -o errexit echo "Arguments:" echo "$@" echo "---" readonly FILES_PATTERN='(\..+)?$' readonly FORBIDDEN='(@?NOCOMMIT|@?COMMITFAIL)' if ( git diff --cached --name-only | grep -E "$FILES_PATTERN" | xargs grep -E --with-filename -n "$FORBIDDEN" ); then echo "ERROR: @COMMITFAIL or @NOCOMMIT found. Exiting to save you from yourself." exit 1 fi
Save to a file and create a symbolic link to your .git/hooks directory:
ln -s ../../git/commit-fail-pre-hook.sh .git/hooks/pre-commit