C'è un modo per innescare un hook dopo che un nuovo ramo è stato estratto in Git?

StackOverflow https://stackoverflow.com/questions/1011557

  •  06-07-2019
  •  | 
  •  

Domanda

C'è un modo per innescare un hook dopo che un nuovo ramo è stato estratto in Git?

È stato utile?

Soluzione

Se uno di questi ganci non riuscisse a farlo, sarei stupito:

https://schacon.github.io/git/githooks.html

Forse questo :

  

post-checkout

     

Questo hook viene invocato quando a   git-checkout viene eseguito dopo aver   aggiornato il worktree. Il gancio è   dati tre parametri: il riferimento di   precedente HEAD, il riferimento del nuovo HEAD   (che può essere cambiato o meno),   e una bandiera che indica se il   il checkout è stato un checkout di filiale   (cambio di rami, flag = 1) o un file   checkout (recupero di un file dal   indice, flag = 0). Questo gancio non può   influisce sul risultato di git-checkout.

Altri suggerimenti

Un hook git è uno script collocato in una posizione speciale del repository, tale posizione è:

  

.git / ganci

Lo script può essere di qualsiasi tipo eseguibile nel proprio ambiente, ad esempio bash, python, ruby ??ecc.

L'hook che viene eseguito dopo un checkout è post-checkout . Dai documenti:

  

... All'hook vengono dati tre parametri ...

Esempio:

  1. Crea l'hook (script):

    touch .git/hooks/post-checkout
    chmod u+x .git/hooks/post-checkout
    
  2. Aggancia il contenuto di esempio:

#!/bin/bash                                                                      

set -e                                                                           

printf '\npost-checkout hook\n\n'                                                

prevHEAD=$1                                                                      
newHEAD=$2                                                                       
checkoutType=$3                                                                  

[[ $checkoutType == 1 ]] && checkoutType='branch' ||                             
                            checkoutType='file' ;                                

echo 'Checkout type: '$checkoutType                                              
echo '    prev HEAD: '`git name-rev --name-only $prevHEAD`                       
echo '     new HEAD: '`git name-rev --name-only $newHEAD`

Nota: lo shebang nella prima riga indica il tipo di script.

Questo script (git hook) acquisirà solo i tre parametri passati e li stamperà in un formato a misura d'uomo.

Simile ad altri ma verifica che il ramo sia stato estratto una volta.

#!/bin/bash

# this is a file checkout – do nothing
if [ "$3" == "0" ]; then exit; fi

BRANCH_NAME=$(git symbolic-ref --short -q HEAD)
NUM_CHECKOUTS=`git reflog --date=local | grep -o ${BRANCH_NAME} | wc -l`

#if the refs of the previous and new heads are the same 
#AND the number of checkouts equals one, a new branch has been created
if [ "$1" == "$2"  ] && [ ${NUM_CHECKOUTS} -eq 1 ]; then
    git push origin ${BRANCH_NAME}
fi

Il post-checkout hook riceve tre parametri:

  1. Rif. della precedente HEAD
  2. Ref of new HEAD
  3. Se si tratta di un checkout di file ( 0 ) o di un branch checkout ( 1 )

Puoi utilizzare il fatto che un ramo creato dall'HEAD corrente avrà lo stesso valore per i parametri 1 e 2.

cat > .git/hooks/post-checkout <<"EOF"
if [ "$3" == "0" ]; then exit; fi
if [ "$1" == "$2" ]; then 
  echo "New branch created. (Probably)."
fi
EOF

chmod u+x .git/hooks/post-checkout

Limitazioni:

  • Il check-out di una filiale esistente che si trova nello stesso HEAD dell'attuale HEAD lo ingannerà.
  • La creazione di un nuovo ramo non dall'attuale HEAD non verrà rilevata.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top