How to setup server side pre-receive hook?
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.
- pre-receive
- update
- post-receive
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
Related videos on Youtube
Comments
-
Techie almost 2 years
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 dogit push
it will trigger .git/hooks/pre-push which is a symbolic link for my scriptgit_hooks/pre-push
EDIT:
I thought
pre-push
andpre-receive
hooks are same as both are triggered ongit push
command butpre-push
is working only client side,pre-recieve
is working on server side. I createdpre-receive
hook and pushed it to master branch. Now, when I dogit 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. -
Techie almost 6 yearsThanks for your help. I thought pre-push and pre-receive are same but both are different . pre-push is also triggered for
git push
but only client side. I have createdpre-receive
hook but I am getting error: cannot spawn hooks/pre-receive: No such file or directory. -
Eugen Konkov almost 6 years@Rekha: I have updated answer. You just put
pre-receive
script on the server into.git/hooks
directory. That is all. How to make this script to works properly is different question. -
Techie almost 6 yearsyes. I added that also. hooks/pre-receive: No such file or directory this represents .git/hooks only. File is there but still showing such error. I am not able to figure it out. Some how it is triggering the event but not working properly.
-
Eugen Konkov almost 6 years@Rekha: I suppose you use
pre-push
aspre-receive
. You can not do that. Look at my script. -
Techie almost 6 yearsI do not have problem with script. I just have simple print statement. First hook should work properly then I can have any type of script based on my requirement right?
-
Techie almost 6 years@ Eugen Konkov Thanks for your help.
-
Eugen Konkov almost 6 years@Rekha: The script have 3 parameters:
old_sha
new_sha
refname
. You can do anything you want. One requirement is only exit status. When script exits with zero statuspush
is applied. When non zero status -push
is not applied and error reported to client. -
Techie almost 6 years@ Eugen Konkov Can you just do one more favor for me ? Server repository is on linux platform. How can we put hook in .git/hooks folder as I can not source control the hidden files ?
-
Eugen Konkov almost 6 years@Rekha: You
ssh
to server with your repository and put required file into.git/hooks
folder in same way as you do that locally. Do not try to put that file under version control. Nobody MUST NOT has right and ability to change that file. Other wise they may bypass restriction implemented by that hook. -
Eugen Konkov almost 6 years@Rekha: here is it
-
Techie almost 6 years@ Eugen Konkov Thanks for your help.