Git:如何检查预推钩中当前推动的分支?

Dmi*_*kov 7 git githooks

在我的项目中,我有一个预推git钩子,可以在每次推送时运行单元测试.

但是当我尝试在另一个分支上推送更改时,单元测试将针对活动分支运行,而不是针对当前推送的分支运行.

例如,如果我尝试从new_feature分支推送更改,而我的工作目录反映了develop分支的结构,则pre-push hook将运行develop分支的单元测试,而不是new_feature.

摆脱这个的基本想法是在预推钩中检查当前被推动的分支.但我不知道如何在钩子内获取有关当前推送分支的信息:这些信息不包含在钩子参数中.

dyn*_*yng 8

手工githooks:

Information about what is to be pushed is provided on the hook's standard input
with lines of the form:

   <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF

For instance, if the command git push origin master:foreign were run the hook would
receive a line like the following:

   refs/heads/master 67890 refs/heads/foreign 12345

although the full, 40-character SHA-1s would be supplied.
Run Code Online (Sandbox Code Playgroud)

在其中 正是你推动的分支.有了它,您可以在该工作树中签出并进行测试.

这是一个示例钩子脚本:

#!/bin/sh

z40=0000000000000000000000000000000000000000

IFS=' '
while read local_ref local_sha remote_ref remote_sha
do
    current_sha1=$(git rev-parse HEAD)
    current_branch=$(git rev-parse --abbrev-ref HEAD)
    if [ "$local_sha" != $z40 ] && [ "$local_sha" != "$current_sha1" ]; then
        git checkout $local_sha

        # do unit testing...

        git checkout $current_branch
    fi
done

exit 0
Run Code Online (Sandbox Code Playgroud)