Hi.
I have some bash script and I want to run this script in server hooks.
I created the directory - custom_hooks
and the file pre-receive
#!/usr/bin/env bash
pattern="^dev-[0-9]-[a-z]"
while read oldrev newrev refname; do
if [[ $refname =~ ^dev ]]; then
if [[ ! $refname =~ $pattern ]]; then
echo "Blocking creation of new branch $refname"
echo "because it does not match pattern $pattern"
exit 1
fi
fi
done
exit 0
But when i push a branch with name dev-ice9
to my repository i don’t get error.
Could you help me find my mistake?
Hi,
Git on the server works in the way that it passes the old revision commit, new revision commit and the ref name as single line into the call hook script.
With that knowledge, you can test-drive your script locally instead of pushing things all the time.
vim gl
#!/usr/bin/env bash
pattern="^dev-[0-9]-[a-z]"
while read oldrev newrev refname; do
if [[ $refname =~ ^dev ]]; then
if [[ ! $refname =~ $pattern ]]; then
echo "Blocking creation of new branch $refname"
echo "because it does not match pattern $pattern"
exit 1
fi
fi
done
exit 0
michi@imagine ~ $ echo 123 456 dev-bla | bash gl
Blocking creation of new branch dev-bla
because it does not match pattern ^dev-[0-9]-[a-z]
michi@imagine ~ $ echo $?
1
michi@imagine ~ $ echo 123 456 dev-1-bla | bash gl
michi@imagine ~ $ echo $?
0
Looks sane to me, so the next question is why the script is not loaded as a hook on the server. Following the docs, I’d check the following:
ls -lah pre-receive
If the file is not executable and not owned by the git user, do it like so.
chmod +x pre-receive
chown git:git pre-receive
Then push something again.
Cheers,
Michael
For gitlab the script should be little different.
pattern='refs/heads/dev-[0-9]-[a-z]'
while read oldrev newrev refname; do
if [[ $refname =~ ^refs/heads/dev ]]; then
if [[ ! $refname =~ $pattern ]]; then
echo "Blocking creation of new branch $refname"
echo "Because it does not match pattern $pattern"
exit 1
fi
fi
done
exit 0
Hi,
ah, the refs/heads/
prefix. Yep, that’s correct. A blank Git server would also require that, I remember that from very old installations. Hacking Git hooks is somewhat complicated, but glad you’ve solved it
Cheers,
Michael