[英]git hook pre-push bash script validate tag
I'm trying to validate a git tag using the pre-push git hook but whenever I execute git push origin <tag-name>
it takes the previous tag as the latest from /refs/tags.我正在尝试使用 pre-push git hook 验证 git 标签,但是每当我执行
git push origin <tag-name>
它都会将前一个标签作为 /refs/tags 中的最新标签。
To replicate the issue:要复制问题:
Step 1. git commit
step 2. git tag V1.0.0-US123-major
step 3. git push origin V1.0.0-US123-major
So when step 3 executes the pre-push script should take "V1.0.0-US123-major" tag and validates against the below regex.因此,当第 3 步执行 pre-push 脚本时,应采用“V1.0.0-US123-major”标签并根据以下正则表达式进行验证。 If the tag matches with regex then it is a valid tag else abort git push.
如果标签与正则表达式匹配,那么它是一个有效的标签,否则会中止 git push。
#!/bin/sh
read_tag="$(git describe --abbrev=0 --tags)"
if [[ $read_tag =~ (^v[0-9]{1}.[0-9]{1}.[0-9]{1}-[a-zA-Z]+-[a-zA-Z]+$) ]]; then
echo "matched"
else
echo "not matched"
exit 1
fi
My expectation is when I use git push origin 2.2.2.2
, the pre-push script does not return exit1 rather it accepts the tag and pushing to origin which is not correct.我的期望是当我使用
git push origin 2.2.2.2
,预推送脚本不会返回 exit1 而是它接受标签并推送到不正确的原点。
git push origin 2.2.2
latest tag: v5.5.5-abcd-tues
matched
Can someone help me with this, please?有人可以帮我解决这个问题吗?
Your pre-push
hook is checking the current revision, not the tag you're pushing, because git describe
describes HEAD
if you don't specify otherwise.您的
pre-push
钩子正在检查当前修订版,而不是您正在推送的标签,因为如果您没有另外指定, git describe
描述HEAD
。
When you use a pre-push
hook, the references being pushed are passed in on standard input.当您使用
pre-push
钩子时,被推入的引用会在标准输入中传入。 Assuming the thing you want to check is the name of the remote reference (that is, the one that's going to end up on the server), then it could look something like this (using POSIX syntax):假设您要检查的是远程引用的名称(即最终会出现在服务器上的名称),那么它可能看起来像这样(使用 POSIX 语法):
#!/bin/sh
set -e
while read lref new rref old
do
case $rref in
refs/tags/*)
if echo "$rref" | \
grep -qsE '(^refs/tags/v[0-9]{1}.[0-9]{1}.[0-9]{1}-[a-zA-Z]+-[a-zA-Z]+$)'
then
echo "matched"
else
echo "not matched"
exit 1
fi;;
*)
;;
esac
done
Do note that while a pre-push
hook can help the developer make good choices and avoid mistakes, it's not an effective control, because it can be trivially bypassed.请注意,虽然
pre-push
hook 可以帮助开发人员做出正确的选择并避免错误,但它不是一种有效的控制,因为它可以被轻易绕过。 If you need to restrict what gets pushed to the server, you need to do that either with a pre-receive
hook, your server implementation, or a CI system.如果您需要限制推送到服务器的内容,则需要使用
pre-receive
挂钩、服务器实现或 CI 系统来实现。 See the relevant Git FAQ entry for more.有关更多信息,请参阅相关的 Git 常见问题条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.