Search code examples
githookgit-post-receive

Creating Git post-receive hook to post Tag name


I have a question about ad-hoc Git configuration.

I need to pass tag name on tag post or any commit to e.g.

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    curl -X POST "https://en4c6i2k7daz5.x.pipedream.net/" \
        -H 'Content-Type: application/json' \
        -d '{"version": "$refname"}'            
done

In the result I'm getting (see https://requestbin.com/r/en4c6i2k7daz5/1cxY1mJeMlejAnF0OJ8B8iCd6sl)

{"version": "$refname"}

So for some reason it is not sending tag name (value of the $refname) - what is wrong in the script?


Solution

  • You're single-quoting your data, single-quotes exist to shut off the shell's string-construction syntax, expansions and wordsplitting and the rest. Since refnames can't have white space or glob syntax in them I think you can get by without quoting their expansionss, but for safety I usually do, to control the expansions I'll usually switch from single quotes to double quotes as needed to get that:

    -d '{"version": "'"$refname"'"}'
    

    or you could just do it all double-quotes and escape json's redundant ones to shoehorn them in that way:

    -d "{\"version\": \"$refname\"}"
    

    edit: hmmm, from git help check-ref-format I'm getting

    These rules make it easy for shell script based tools to parse reference names, pathname expansion by the shell when a reference name is used unquoted (by mistake), and also avoid ambiguities in certain reference name expressions (see gitrevisions(7)):

    i.e. you' guaranteed to not have to quote refname expansions at all.