git post-receive hook,它抓取提交消息并回发到URL

8 git githooks

我们正在使用我希望在开发人员将其更改推送到服务器时自动更新的票务系统.为了更新它,我只需要提供一个特定的URL,并将提交消息作为GET变量.被调用的页面将记录此更改.我知道我的方法是使用钩子,但我不熟悉Bash和Perl所以它非常具有挑战性.

我想实现这个目标:

  • 开发人员推送到服务器
  • post-receive 钩子运行并检查哪些不同的提交是新的(因为可能有多个一次推送)
  • 它遍历它们,并且对于每次提交,它将打开一个带有提交消息的URL(curl http://server.com/logthis.asp?msg=Here_goes_the_commit_message类似的东西)

而已.虽然我已经检查了一些 与这种想法相关的样本,但没有一个做到这一点.怎么可以这样做?

Rud*_*udi 9

主要的PITA是隔离正确的新修订列表,我从/ usr/share/doc/git/contrib/hooks/post-receive-email(show_new_revisions)借用了这些修订.

while read oval nval ref ; do
    if expr "$ref" : "^refs/heads/"; then
        if expr "$oval" : '0*$' >/dev/null
        then
            revspec=$nval
        else
            revspec=$oval..$nval
        fi
        other_branches=$(git for-each-ref --format='%(refname)' refs/heads/ |
            grep -F -v $ref)

        # You may want to collect the revisions to sort out
        # duplicates before the transmission to the bugtracker,
        # but not sorting is easier ;-)
        for revision in `git rev-parse --not $other_branches | git rev-list --stdin $revspec`; do
                    # I don't know if you need to url-escape the content
                    # Also you may want to transmit the data in a POST request,
            wget "http://server.com/logthis.asp?msg=$(git log $revision~1..$revision)"
        done
    fi
done
Run Code Online (Sandbox Code Playgroud)