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

标签 git githooks

我们正在使用一个票务系统,我想在开发人员将他们的更改推送到服务器时自动更新该系统。为了更新它,我只需要提供一个带有提交消息的特定 URL 作为 GET 变量。被调用的页面随后将记录此更改。我知道我要走的路是 hooks ,但我对 Bash 和 Perl 都不熟悉,所以这很有挑战性。

我想实现这个:

  • 开发者推送到服务器
  • post-receive Hook 运行并检查哪些不同的提交是新的(因为一次推送可能有多个)
  • 它循环遍历它们,对于每次提交,它将打开一个包含提交消息的 URL(curl http://server.com/logthis.asp?msg=Here_goes_the_commit_message,类似这样的内容)

就是这样。虽然我查过了some samples与这种想法相关,没有人这样做。这怎么可能呢?

最佳答案

主要的 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

关于git post-receive hook 抓取提交消息并回发到 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8263186/

相关文章:

git - 如何获取 Git merge 日期修订日志历史记录?

git - 如何丢弃 Git 中的本地提交?

git - 使用 gitosis 管理对 git 存储库的 http 访问

git - 在预提交 Hook 中,查找正在创建的提交的父级的哈希值

python - 当 git commit-msg Hook 失败时,如何恢复提交消息?

git - git push 后本地执行 Hook ?

git submodule init 忽略失败

git - 意外删除 heroku 上的应用程序后无法推送到 heroku

Git 在不克隆的情况下获取标记文件

git - 如何绕过 : remote: GitLab: Author <non-member-email-here> is not a member of team?