I am new to git hooks and server side git functionality. I worked on client side git to commit and push my code and we do git merge using Application Lifecycle management(ALM) tools.
I am trying to write git hooks to do some testing on new/modified files in the repository. I am able to write and test client side hooks like pre-commit
Now, I need to add some server side git hook to verify files before merging to master because there is a change to skip client side verification using -no-verify
option. When I go through some git hook tutorials, pre-push
hook is the server side hook. I tried to create pre-push hook and it is working on client side. Now, How can make it as a server side hook and force to verify files when user try to push changes even with --no-verify
option(should not be controled on client).
My big question is how server hook is triggered when we do git push
from local branch/repo.
Creation of hook:
Created a hook called pre-commit and placed it under some folder git_hooks/pre-push
and also in .git/hooks/pre-push
. Now, created a symbolic link for my pre-push script. So, whenever I do git push
it will trigger .git/hooks/pre-push which is a symbolic link for my script git_hooks/pre-push
EDIT:
I thought pre-push
and pre-receive
hooks are same as both are triggered on git push
command but pre-push
is working only client side, pre-recieve
is working on server side.
I created pre-receive
hook and pushed it to master branch. Now, when I do git push
getting this error: cannot spawn hooks/pre-receive: No such file or directory.
I am trying this on both Windows and Linux platforms. On Windows I am getting this error, On Linux it is not even getting triggered. I placed pre-receive
hook on master branch on both the platforms.
You should look into Server side hooks documentation section.
There are three hooks that let you react to different stages of the git push process.
When you push to server pre-receive
hook is triggered. Then for each branch you have pushed the update
hook is triggered. When these hooks are finished without errors your patches are applied and post-receive
hook is triggered
More detailed DOC about pre-receive
hook:
This hook is invoked by git-receive-pack when it reacts to git push and updates reference(s) in its repository. Just before starting to update refs on the remote repository, the pre-receive hook is invoked. Its exit status determines the success or failure of the update.
UPD
To setup pre-receive
server side hook you should place script into .git/hooks
directory on the server. and name it pre-receive
. That is all.
You should not create hooks
directory at your repo and commit it. The pre-receive
script is outside of repo
UPD
Here is example script:
#!/bin/bash
# check each branch being pushed
echo "pre-receive HOOK"
while read old_sha new_sha refname
do
if git diff "$old_sha" "$new_sha" | grep -qE '^\+(<<<<<<<|>>>>>>>)'; then
echo "Saw a conflict marker in $(basename "$refname")."
git diff "$old_sha" "$new_sha" | grep -nE '^\+(<<<<<<<|>>>>>>>)'
exit 1
fi
if git diff "$old_sha" "$new_sha" | grep -qE '^\+.*\s+$'; then
echo "Saw whitespaces at EOL."
git diff "$old_sha" "$new_sha" | grep -nE '^\+.*\s+$'
exit 1
fi
done
exit 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With