git预推钩子:将分支推到

git预推钩子:将分支推到,git,push,githooks,Git,Push,Githooks,在预推钩子的上下文中,如何获取正在推送到的分支的列表?我知道如何获取当前分支,但它可能不同于被推送到的一个/多个分支 我曾想过解析该命令,但我担心我可能会忘记某些情况。像git push,git push-origin-master都将推送到master等。这里有一个可能的循环,尽管我不清楚您想对远程分支或标记名和/或本地引用做什么: while read oldrev newrev refname do if [[ "$newrev" != "0000000000000000000

在预推钩子的上下文中,如何获取正在推送到的分支的列表?我知道如何获取当前分支,但它可能不同于被推送到的一个/多个分支


我曾想过解析该命令,但我担心我可能会忘记某些情况。像
git push
git push-origin-master
都将推送到master等。

这里有一个可能的循环,尽管我不清楚您想对远程分支或标记名和/或本地引用做什么:

while read oldrev newrev refname 
do
     if [[ "$newrev" != "0000000000000000000000000000000000000000" ]] ; then
          branch=${refname#refs/heads/} 
     fi
done
#! /bin/sh

NULLSHA=0000000000000000000000000000000000000000 # 40 0's

say() {
    echo "$@" 1>&2
}

while read localref localhash foreignref foreignhash; do
    case $foreignref in
    refs/heads/*)
        reftype=branch
        shortref=${foreignref#refs/heads/};;
    refs/tags/*)
        reftype=tag
        shortref=${foreignref#refs/tags/};;
    *)  reftype=unknown-ref-type
        shortref=$foreignref;;
    esac
    say "pushing to $reftype $shortref"
    case $localhash,$foreignhash in
    $NULLSHA,*) say "(push with intent to delete)";;
    *,$NULLSHA) say "(push with intent to create)";;
    *) say "(push with intent to update from $foreignhash to $localhash)"
       # for branch updates only, let's see if it's a fast-forward
       if [ $reftype = branch ]; then
           if git merge-base --is-ancestor $foreignhash $localhash; then
               say "(this is a fast-forward)"
           else
               say "(this can only work if forced)"
           fi
       fi;;
    esac
done

(注意:这没有经过很好的测试)。

关闭,但不完全正确:请参阅git hooks文档的pre-push hook部分中的
refs/heads/master 67890 refs/heads/foreign 12345
示例:看起来像
$(echo${refname}refs/heads/}awk{print$1;})“
会成功的work@Guig:更简单:
读取refname localhash foreignref foreignhash时;执行…
但您还应该检查
refname
(我将其重命名为
localref
)是否也以
refs/heads/
开头,否则按下tag
v1.2
将尝试对分支
v1.2
执行操作,这可能不存在。我修改了答案以过滤掉仍然不正确的标记,这里的主要问题是,您将其结构类似于接收前或接收后挂钩,它读取具有三个值的行。预推钩的输入由四个值的线组成。这四个值依次为本地ref name、本地hash ID、远程(外部)ref name和远程hash。根据sh
read
的工作方式,您将在
$refname
中获得最后两个,本地ref名称在
$oldrev
中,本地哈希将被推入
$newrev