2016-09-26 2 views
0

にプッシュするプッシュプッシュフックのコンテキストでプッシュされているブランチのリストを取得するにはどうすればよいですか?現在のブランチを取得する方法は分かっていますが、プッシュされているブランチとは異なる場合があります。gitプリプッシュフック:ブランチを

私はコマンドの構文解析を考えましたが、私はいくつかのケースを忘れるかもしれないと心配しています。同様git pushgit push origin masterは両方習得するプッシュしますなど

答えて

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

閉じるが正しくはない:gitフックのドキュメントのpre-push hookセクションの 'refs/heads/master 67890 refs/heads/foreign 12345'の例を見てください:https://www.kernel.org/ pub/software/scm/git/docs/githooks.html – torek

+0

'$(echo $ {refname#refs/heads /} | awk '{print $ 1;}') '' 'のように見えるので、動作します – Guig

+0

@Guig:simpler : '読み込み中localhash foreignref foreignhash; 'refs/heads /'で始まる 'refname'(' localref'と改名する)も実際には 'refs/heads /'で始まっていることを確認する必要があります。そうでなければタグ 'v1.2'を押すと*ブランチ* 'v1.2'はおそらく存在しません。 – torek

0

それはあなたがリモートブランチやタグ名、および/またはローカル参照して何をしたいのか私にははっきりしていないが、ここでは一つの可能​​なループは、です:

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

(注:これは十分にテストされていません)。