Skip to content

Instantly share code, notes, and snippets.

@jewilmeer
Created April 13, 2015 12:40
Show Gist options
  • Save jewilmeer/4de4181c084caade1bc3 to your computer and use it in GitHub Desktop.
Save jewilmeer/4de4181c084caade1bc3 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Git pre-commit hook to keep you from shooting yourself in the foot by testing
# for common mistakes and errors.
#
# Installation:
#
# 1. Copy this file into your Git hooks directory (i.e. ./.git/hooks).
# 2. Make it executable: chmod +x .git/hooks/pre-commit
set -e
grep_diff() {
git diff --cached | grep -E "$1" > /dev/null
}
# Reject any commit that would add a line with tabs.
if grep_diff '^\+\t'
then
echo "\033[0;31mError:\033[0m This commit would contain a tab, which is against our style guide."
echo 'If you really, REALLY want to commit this code, skip your pre-commit'
echo 'hooks with `git commit --no-verify` (but remember: for every tab you commit,'
echo 'a kitten dies).'
echo
echo 'This message was generated by the Git pre-commit hook in ./.git/hooks/pre-commit'
echo
echo 'Found tabs:'
git diff --cached --name-status --diff-filter=ACM | awk '{ print $2 }' | xargs grep -H -n -E '\t'
exit 1
fi
# Reject any commit that would introduce a debug statement in a Ruby file
if grep_diff 'binding\.pry'
then
echo "\033[0;31mError:\033[0m your code contains debugging statements."
echo 'Your commit is bad and you should feel bad.'
echo
echo 'This message was generated by the Git pre-commit hook in ./.git/hooks/pre-commit'
echo
git diff --cached --name-only --diff-filter=ACM | xargs grep -H -n -E 'binding\.pry'
exit 1
fi
# Reject any commit that would include Git conflict markers.
if grep_diff '^(<|>|=|\|){7}'
then
echo "\033[0;31mError:\033[0m You have left some Git conflict markers in."
echo 'Your commit is bad and you should feel bad.'
echo
echo 'This message was generated by the Git pre-commit hook in ./.git/hooks/pre-commit'
echo
git diff --cached --name-only --diff-filter=ACM | xargs grep -H -n -E '^(<|>|=|\|){7}'
exit 1
fi
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment