Soy muy malo en las secuencias de commands de shell (con bash), estoy buscando una forma de comprobar si la twig actual de git es "x", y abortar la secuencia de commands si no es "x".
#!/usr/bin/env bash CURRENT_BRANCH="$(git branch)" if [[ "$CURRENT_BRANCH" -ne "master" ]]; then echo "Aborting script because you are not on the master branch." return; # I need to abort here! fi echo "foo"
pero esto no es del todo correcto
Utilice git rev-parse --abbrev-ref HEAD
para get el nombre de la twig actual.
Entonces solo es cuestión de simplemente comparar valores en tu script:
BRANCH=$(git rev-parse --abbrev-ref HEAD) if [[ "$BRANCH" != "x" ]]; then echo 'Aborting script'; exit 1; fi echo 'Do stuff';
Una opción sería analizar el resultado del command git branch
:
BRANCH=$(git branch | sed -nr 's/\*\s(.*)/\1/p') if [ -z $BRANCH ] || [ $BRANCH != "master" ]; then exit 1 fi
Pero una variante que utiliza commands internos de git para get solo el nombre de la twig activa como lo sugiere @knittl es less propenso a errores y preferible
Desea usar exit
lugar de return
.