2016-09-26 61 views
0

我怎样才能像已经被推到在预推挂钩的背景下,新的分支?我知道如何获得当前分支,但它可能与推送到的一个/多个分支不同。混帐prepush挂钩:获取分支推到

我以为解析命令,但我担心我可能会忘记某些情况下。像git pushgit push origin master都将推动掌握等

回答

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

接近,但并不完全正确:看到git的钩子文件的预推钩部分的'裁判/头/主裁判67890 /头/国外12345'例如:https://www.kernel.org/酒吧/软件/ SCM /混帐/文档/ githooks.html – torek

+0

看起来像'$(回声$ {refname#裁判/头/} | awk的 '{$打印1;}')''会使其工作 – Guig

+0

@Guig:简单:'while read ref localhash foreignref foreignhash; do ...'但是你也应该检查'refname'(我将重命名为'localref')实际上也是以'refs/heads /'开始的,否则推动标签'v1.2'将尝试去做*分支*'v1.2',可能不存在。 – torek

0

这里有一个可能的循环,但要与远程分支或标记名称做什么目前尚不清楚对我来说,和/或本地REF:

#! /bin/sh 

NULLSHA=0000000000000000000000000000000000000000 # 40 0's 

say() { 
    echo "[email protected]" 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 

(注:这是不是很好的测试)。