git prepush hook:把树枝推到

时间:2016-09-26 19:24:55

标签: git push githooks

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

我想过解析命令,但我担心我可能会忘记某些情况。与git push一样,git push origin master也会推动掌握等等。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

这是一个可能的循环,虽然我不清楚你想用远程分支或标签名称做什么,和/或本地参考:

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

(注意:这未经过充分测试)。