/ / bash script per verificare se il corrente git branch = "x" - git, bash, shell, sh

script bash per verificare se l'attuale git branch = "x" - git, bash, shell, sh

Sto molto male con lo scripting di shell (con bash), sto cercando un modo per farlo controlla se il ramo git attuale è "x" e interrompi lo script se non è "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"

ma questo non è giusto

risposte:

22 per risposta № 1

Uso git rev-parse --abbrev-ref HEAD per ottenere il nome dell'attuale filiale.

Quindi si tratta solo di confrontare i valori nel tuo script:

BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$BRANCH" != "x" ]]; then
echo "Aborting script";
exit 1;
fi

echo "Do stuff";

4 per risposta № 2

Un'opzione potrebbe essere quella di analizzare l'output del file git branch comando:

BRANCH=$(git branch | sed -nr "s/*s(.*)/1/p")

if [ -z $BRANCH ] || [ $BRANCH != "master" ]; then
exit 1
fi

Ma una variante che usa i comandi interni di git per ottenere solo il nome del ramo attivo come suggerito da @knittl è meno incline agli errori e preferibile


3 per risposta № 3

Tu vuoi usare exit invece di return.