0

我的项目的目的是将开发人员所做的每个提交记录到 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 将重复的提交添加到我的集合中。但这在大型存储库上会很慢。

4

1 回答 1

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 中。

于 2012-02-14T12:12:08.170 回答