gpt4 book ai didi

Git:如何在pre-push hook中查看当前推送的分支?

转载 作者:太空狗 更新时间:2023-10-29 12:58:39 24 4
gpt4 key购买 nike

在我的项目中,我有一个推送前的 git 钩子(Hook),它在每次推送时运行单元测试。

但是当我尝试从一个分支推送更改同时停留在另一个分支时,单元测试将针对事件分支运行,而不是针对当前推送的分支运行。

例如,如果我尝试从我的 new_feature 分支推送更改,而我的工作目录反射(reflect)了 develop 分支的结构,则预推送 Hook 将为 develop 分支而不是 new_feature 运行单元测试。

消除这种情况的基本思路是在预推送 Hook 中检查当前推送的分支。但我不知道如何获取有关当前推送的分支的信息:该信息不包含在钩子(Hook)参数中。

最佳答案

来自manual 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.

其中 正是您要推送的分支。有了它,您可以在该工作树中进行检查和测试。

这是一个示例钩子(Hook)脚本:

#!/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

关于Git:如何在pre-push hook中查看当前推送的分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20045363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com