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!


Solution 1:

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.