【问题标题】:git post-receive hook that grabs commit messages and posts back to URLgit post-receive 钩子,用于抓取提交消息并回发到 URL
【发布时间】:2012-01-05 23:37:36
【问题描述】:

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

我想实现这个:

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

就是这样。虽然我已经检查过与这种想法相关的somesamples,但没有人能做到这一点。怎么可能做到这一点?

【问题讨论】:

    标签: git githooks


    【解决方案1】:

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

    【讨论】:

    • 小心。如果你在同一次推送中创建了一个修订并将其合并到另一个分支中,上面的代码将跳过它。
    猜你喜欢
    • 2019-01-16
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 2014-10-28
    • 1970-01-01
    • 2011-07-20
    相关资源
    最近更新 更多