Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

githooks - How do I react to new tags in git hooks?

I'd like to set up a git hook that creates a CDN-style directory structure based on incoming tags. So, for example, if the last tag in the local repository is "v1.2.1" and I pull a commit with "v1.2.2", it should see the new tag and clone the repository into a new directly (../1.2.2) accordingly.

I'm pretty sure I want to attach this to post-receive, however I can't find anything in the documentation about git hooks about how to read the incoming tags. Are they delivered on a different hook? Do I actually need to have the shell script run a git command to see if any of the new commits have new tags?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Tags are refs like any other (like commit).
If tags are pushed to a repo with a post-receive hook, that hook will be called and will list all updated refs, that is both old and new values of all the refs in addition to their names (on its standard input).

See this server post-receive email hook for example.

#!/bin/bash

. $(dirname $0)/functions

process_ref() {
    oldrev=$(git rev-parse $1)
    newrev=$(git rev-parse $2)
    refname="$3"

    set_change_type
    set_rev_types
    set_describe_tags

    case "$refname","$rev_type" in
      refs/tags/*,tag)
        # annotated tag
        refname_type="annotated tag"
        function="atag"
        short_refname=${refname##refs/tags/}
        # change recipients
        if [ -n "$announcerecipients" ]; then
          recipients="$announcerecipients"
        fi
      ;;
    esac 
}

while read REF; do process_ref $REF; done

For this to work you also must install the functions file from the aforementioned example hook repository.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...