Reputation: 5470
I've got a repo that I'm working with that has no other hooks of any kind. Today, after an embarrassment of having a console.log getting up to dev multiple times, I put in the following pre-commit hook:
#!/bin/sh
count=`git diff | grep -i 'console\.' | wc -l | awk '{print $1}'`
if [[ "$count" -ge 1 ]]; then
echo " remove any console statements in javascript sources"
exit 1
fi
The script in itself works fine, and git fires it as long as I use 'git commit filename'. It doesn't fire if I add a file via 'git add', or if I do 'git commit -a' (which I assume simply adds all of the modified files and then runs the commit). Why is this happening? I'm no git master, and I've done some hunting on Google, but haven't come up with anything concrete.
Upvotes: 2
Views: 240
Reputation: 301327
The pre-commit hook is fired / executed every time you do a commit on your repo. It has nothing to do with how your file is added to the index. I think you are confusing between firing and the script doing what you want. Add an echo
outside conditionals to see if the script is being triggered at all times.
I haven't tried the script that you have posted, but I believe the problem could be in the git diff
- it should be git diff --cached
Update:
I have confirmed that you have to use --cached
.
Upvotes: 2