I host git on my server and have webhooks that run commands like
branch=$(expr "$refname" : "refs/heads/\(.*\)")
This works perfectly with SSH git pushes but when trying to use the HTTPS protocol I get the error
hooks/pre-receive: expr: not found
I guess this is because the script runs as www-data when using https.
Any ideas how to solve this problem?
If this is the same server, you should check that the PATH
environment variable is set appropriately when this hook is invoked from your HTTP server; it's likely to be missing /bin
or /usr/bin
(whichever stores your expr
command).
That said, there's no need for expr
here at all. Assuming that the value in $refname
starts with refs/heads
:
# Delete the leading refs/heads/ from refname to get branch
branch=${refname#refs/heads/}
Otherwise:
# Delete everything up to and including the first instance of refs/heads/
branch=${refname#*refs/heads/}
If you want to check whether refname
contains refs/heads/
at all, this is most easily done in bash (your script starting with #!/bin/bash
):
if [[ $refname = *refs/heads/ ]]; then
branch=${refname#*/refs/heads/}
else
# refname does not match the pattern
branch=
fi
If you are in fact using bash, you can actually use even more similar code:
refname_re='refs/heads/(.*)'
if [[ $refname =~ $refname_re ]]; then
branch=${BASH_REMATCH[1]}
fi
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.