How do I react to new tags in git hooks?

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.

Leave a Comment