Skip to content

Instantly share code, notes, and snippets.

@dcvezzani
Created September 24, 2012 18:07
Show Gist options
  • Save dcvezzani/3777359 to your computer and use it in GitHub Desktop.
Save dcvezzani/3777359 to your computer and use it in GitHub Desktop.
pre-commit hook for Git on a system running OSX (Mac)
#!/bin/sh
if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
# put the first changeset hash value in your project here (I think)
against=facf87a3fa1c853a7dd8dde8cba32e1fc8fde3ea
fi
for FILE in `git diff-index --name-status $against -- | cut -c3-` ; do
# Check if the file contains 'debugger'
if [ "grep 'debugger' $FILE" ]
then
echo $FILE ' contains debugger!'
exit 1
fi
done
exit
@archfizz
Copy link

On line 7, you'll need against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 instead. This is a magic hash present in all git repositories. Just google the hash and you'll see

@twobitlabs
Copy link

If you add the --cached flag to your git diff-index call, it'll only run against files that are staged for commit, which is generally what I think you want.

@twobitlabs
Copy link

On my system, if [ "grep 'debugger' $FILE" ] always evaluates to true. Changing it to if grep -q 'debugger' "$FILE" fixes that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment