Reputation: 3743
I read all the pre-push stackoverflow questions and I followed every single instruction but still my hook is not triggering when I call git push
here is my hook
#!/bin/bash
protected_branch='master'
echo "Pre push hook is running..." # Even this line I can't see it in the output
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
echo "You can't push to master directly"
exit 1 # push will not execute
else
exit 0 # push will execute
fi
I also made sure the hook file is named pre-push, and I made sure it has execute permissions.
I really can't see what am I missing, all I want to do is to just trigger the hook. and I'll figure out the rest.
Note: I have this repo and hooks on Debian 8 Jessie
Upvotes: 0
Views: 660
Reputation: 30958
I see two glitches in your hook. First, in one push, there could be more than one ref to be updated and you might have more than one protect branch. Better to test all of them. Second, you can push a branch that's not the current one. So it's not safe to test the current branch.
Here is a hook based on the template pre-push.sample. You can find a local copy of pre-push.sample
under .git/hooks
.
#!/bin/sh
protected_branch='refs/heads/master'
echo "Pre push hook is running..." # Even this line I can't see it in the output
while read local_ref local_sha remote_ref remote_sha
do
if [ "$remote_ref" = $protected_branch ];then
echo "You can't push to master directly"
exit 1 # push will not execute
fi
done
exit 0
Name it pre-push
, grant it executable permissions, and put it under .git/hooks
of the local repository.
Here is a sample of pre-receive
. It should be deployed under .git/hooks
of the remote repository.
#!/bin/sh
protected_branch='refs/heads/master'
while read old_value new_value ref_name;do
if [ "$ref_name" = $protected_branch ];then
echo "You can't push to master directly"
exit 1
fi
done
exit 0
Reference: pre-push, pre-receive
Upvotes: 2