Skip to content

Instantly share code, notes, and snippets.

@jasonpilz
Last active January 1, 2017 23:26
Show Gist options
  • Save jasonpilz/b0e0f2c4a211220b5c575168ed08bd89 to your computer and use it in GitHub Desktop.
Save jasonpilz/b0e0f2c4a211220b5c575168ed08bd89 to your computer and use it in GitHub Desktop.
Git hook to run utilities before commit
#!/bin/sh
# https://gist.githubusercontent.com/jasonpilz/b0e0f2c4a211220b5c575168ed08bd89/raw
# This hook runs static analysis tools before each commit. If any of the steps
# are interupted by responding with 'no' or 'n', the commit will be aborted.
# Install to existing project:
# curl -sSL https://git.io/vMt7l > .git/hooks/pre-commit; chmod +x .git/hooks/pre-commit
set -e
set -o pipefail
red=$'\e[1;31m'
cyn=$'\e[1;36m'
tools=(rubocop reek)
exec < /dev/tty
run_tool () {
if ! $1; then
echo "\n$1 found errors in the code. Do you want to continue? ${cyn}no/n or [ENTER]${end}"
read input
if [ "$input" == "no" ] || [ "$input" == "n" ]; then
echo "\n${red}Commit Aborted${end}"
exit 1
fi
fi
}
for tool in ${tools[@]}; do
run_tool $tool
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment