问题
I want to do some action in GIT's post-receive hook when only new tag is pushed to the repository. How to accomplish this?
Thanks
SOLUTION (answer by Jan Krüger)
New tag has $oldrev equalled zeroes. Removed tag has $newrev equalled zeroes. For both $ref value starts with refs/tags/.
#!/bin/sh
#
read oldrev newrev ref
if [[ "0000000000000000000000000000000000000000" == $oldrev ]] && [[ $ref == refs\/tags\/* ]];
then
echo "New tag added"
fi
回答1:
The post-receive hook receives information on stdin about all the refs (branches, tags, ...) that were updated in that operation. Each line has the following format, taken from the githooks manpage:
<old-value> SP <new-value> SP <ref-name> LF
So this would be an example of a new tag being created:
0000000000000000000000000000000000000000 0123456789abcdef0123456789abcdef01234567 refs/tags/mytag
You simply need to read from stdin and check whether a line matches this format. Basically the first "word" is all zeroes and the third word starts with refs/tags/.
来源:https://stackoverflow.com/questions/55278195/trigger-something-in-post-receive-only-when-new-tag-is-added