【问题标题】:Problems with sending commit data on post-receive hook via curl (git)通过 curl (git) 在接收后挂钩上发送提交数据的问题
【发布时间】:2012-02-14 20:12:08
【问题描述】:

我的项目的目的是将开发人员所做的每个提交记录到 mongodb 中。我已经设置了一个
nodejs 监听器,它将在一个帖子中接收到的数据持久化到 mongo。

我正在运行一个 gitolite 服务器,每次开发人员推送时,我都使用 post-receive 挂钩通过 curl 将提交发布到我的节点侦听器。

我成功地做到了这一点,除了在旧版本为 0000000000000000000000000000000000000000 的初始提交中,当我尝试运行 git log 时,我得到一个无效的参数。

模棱两可的论点 '8a2db961045bd4825624b16ad62e75be49dd70b6~1..8a2db961045bd4825624b16ad62e75be49dd70b6': 未知版本或路径不在工作树中。使用“--”分隔 修订路径

下面是我的 bash/post-receive 脚本的摘录。

#!/bin/sh
# Read git data on STDIN
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)

        # Get the name of the repository
        if [ $(git rev-parse --is-bare-repository) = true ]
        then
                 REPOSITORY_BASENAME=$(basename "$PWD")
        else
                REPOSITORY_BASENAME=$(basename $(readlink -nf "$PWD"/..))
        fi
        REPOSITORY_BASENAME=${REPOSITORY_BASENAME%.git}

        for revision in `git rev-parse --not $other_branches | 
            git rev-list --stdin $revspec`; do
                COMMIT_ID=$(git log $revision~1..$revision --pretty=format:'%H')
                DATE=$(git log $revision~1..$revision --date=short --pretty=format:'%ad')
                MSG=$(git log $revision~1..$revision --pretty=format:'%s')
                AUTHOR=$(git log $revision~1..$revision --pretty=format:'%ae')
                curl -s
                    -d "commit_id=$COMMIT_ID&date=$DATE&msg=$MSG&author=$AUTHOR&project=$REPOSITORY_BASENAME"
                    $LISTENER_RECEIVE
        done
    fi
done

我不确定如何在我的 bash 脚本中/使用我正在使用的 git 命令来处理这个问题。

一个(惰性)选项是使用 git log 而不使用任何修订信息,并避免使用项目名称/git commit id 将重复提交添加到我的集合中。但这在大型存储库上会很慢。

【问题讨论】:

    标签: git curl git-post-receive


    【解决方案1】:

    不确定这是否会有所帮助,但在 pre-commit.sample 钩子中,他们使用了这个技巧:

    if git rev-parse --verify HEAD >/dev/null 2>&1
    then
        against=HEAD
    else
        # Initial commit: diff against an empty tree object
        against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
    fi
    

    最后一个哈希是空存储库的哈希,并被硬编码到 git 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 2011-04-15
      相关资源
      最近更新 更多