16

我在远程 repo 上有一个 post receive 钩子设置,它试图确定传入推送的分支名称,如下所示:

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

不过,我发现,无论我从 $branch 变量推送的哪个分支都设置为“master”。

有任何想法吗?

4

5 回答 5

25

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

oldRev(空格) newRev(空格) refName(换行)

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

while read oldrev newrev ref
do
    echo "$ref"
done
于 2011-05-12T10:40:17.670 回答
10

你也可以使用 bash 变量替换来做这样的事情:

read oldrev newrev ref

branchname=${ref#refs/heads/}

git checkout ${branchname}
于 2012-11-02T16:07:32.387 回答
2

Magnus 的解决方案对我不起作用,但这确实

#!/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"
于 2012-09-11T10:48:07.420 回答
2

这两个答案都是正确的,但我无法让标准输入到下一个常用功能 post-receive-email。这是我最终得到的结果:

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
于 2012-10-19T18:20:56.507 回答
1

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

于 2011-05-12T06:42:35.150 回答