无法从 Git post-receive hook 检测分支

发布于 2024-11-06 10:56:11 字数 183 浏览 4 评论 0原文

我在远程存储库上设置了一个后接收挂钩,它尝试确定传入推送的分支名称,如下所示:

$branch = `git rev-parse --abbrev-ref HEAD`

不过,我发现的是,无论我从 $branch 变量推送哪个分支,都会被设置与‘主人’。

有什么想法吗?

I've got a post receive hook setup on the remote repo that tries to determine the branch name of the incoming push as follows:

$branch = `git rev-parse --abbrev-ref HEAD`

What i'm finding, though, is that no matter what branch I push from my $branch variable gets set with 'master'.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

病毒体 2024-11-13 10:56:11

post-receive 钩子获取与 pre-receive 相同的数据,但不是作为参数,而是从 stdin 获取。为所有引用发送以下内容:

oldRev (space) newRev (space) refName (Line feed)

您可以使用以下 bash 脚本解析出引用名称:

while read oldrev newrev ref
do
    echo "$ref"
done

The post-receive hook gets the same data as the pre-receive and not as arguments, but from stdin. The following is sent for all refs:

oldRev (space) newRev (space) refName (Line feed)

You could parse out the ref name with this bash script:

while read oldrev newrev ref
do
    echo "$ref"
done
我喜欢麦丽素 2024-11-13 10:56:11

您还可以使用 bash 变量替换执行类似的操作:

read oldrev newrev ref

branchname=${ref#refs/heads/}

git checkout ${branchname}

You could also do something like this using bash variable substitution:

read oldrev newrev ref

branchname=${ref#refs/heads/}

git checkout ${branchname}
倾城°AllureLove 2024-11-13 10:56:11

马格努斯的解决方案对我不起作用,但 这个 做到了:

#!/bin/bash

echo "determining branch"

if ! [ -t 0 ]; then
  read -a ref
fi

IFS='/' read -ra REF <<< "${ref[2]}"
branch="${REF[2]}"

if [ "master" == "$branch" ]; then
  echo 'master was pushed'
fi

if [ "staging" == "$branch" ]; then
  echo 'staging was pushed'
fi

echo "done"

Magnus' solution didnt work for me, but this did:

#!/bin/bash

echo "determining branch"

if ! [ -t 0 ]; then
  read -a ref
fi

IFS='/' read -ra REF <<< "${ref[2]}"
branch="${REF[2]}"

if [ "master" == "$branch" ]; then
  echo 'master was pushed'
fi

if [ "staging" == "$branch" ]; then
  echo 'staging was pushed'
fi

echo "done"
套路撩心 2024-11-13 10:56:11

这两个答案都是正确的,但我在将标准输入接收到电子邮件后的下一个常用功能时遇到问题。这是我最终得到的结果:

read oldrev newrev ref
echo "$oldrev" "$newrev" "$ref" | . /usr/share/git-core/contrib/hooks/post-receive-email


if [ "refs/heads/qa" == "$ref" ]; then
  # Big Tuna YO!
  wget -q -O - --connect-timeout=2 http://127.0.0.1:3000/hooks/build/qa_now
fi

Both these answers are correct, but I was having trouble getting stdin to the next common function post-receive-email. Here is what I ended up with:

read oldrev newrev ref
echo "$oldrev" "$newrev" "$ref" | . /usr/share/git-core/contrib/hooks/post-receive-email


if [ "refs/heads/qa" == "$ref" ]; then
  # Big Tuna YO!
  wget -q -O - --connect-timeout=2 http://127.0.0.1:3000/hooks/build/qa_now
fi
梦巷 2024-11-13 10:56:11

您需要读取传递给脚本的参数。那应该有分支名称和新旧修订,并为每个推送的分支运行

You need to read the arguments that are being passed to the script. That should have the branch name and new and old revisions and run for each branch pushed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文