Created
September 1, 2020 13:38
-
-
Save wijourdil/7eb53dd200c30d8958ad7ee2bc1357bf to your computer and use it in GitHub Desktop.
Command alias to search by name and checkout on another existing branch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function b() { | |
# Couleurs | |
red=`tput setaf 1` | |
green=`tput setaf 2` | |
yellow=`tput setaf 3` | |
magenta=`tput setaf 5` | |
blue=`tput setaf 6` | |
reset=`tput sgr0` | |
branch_to_use='' | |
if [ -z $1 ]; then | |
echo "${red}Erreur : Merci de préciser une branche !${reset}" | |
return | |
fi | |
echo "${green}\$ git fetch${reset}"; | |
git fetch | |
# Nombre de branches trouvées | |
nb_branches_trouvees_total=$(git branch -a | grep $1 | wc -l | awk '{$1=$1;print}') | |
nb_branches_trouvees_remote=$(git branch -r | grep $1 | wc -l | awk '{$1=$1;print}') | |
nb_branches_trouvees_local=$(git branch -l | grep $1 | wc -l | awk '{$1=$1;print}') | |
echo "$nb_branches_trouvees_total branches trouvée(s) ($nb_branches_trouvees_local local / $nb_branches_trouvees_remote remote)" | |
# Branches locales | |
branches="$(git branch -l | grep "$1")" | |
compteur=1 | |
while read -r -u 3 current_branch | |
do | |
# Supprime l'étoile s'il y en a une (c'est le cas quand c'est la branche courante) | |
current_branch="${current_branch/\*/}" | |
# Trim les espaces | |
current_branch=$(echo "$current_branch" | awk '{$1=$1;print}') | |
# Ignore une ligne vide | |
[ -z $current_branch ] && continue | |
echo -n "${magenta}[${compteur}/${nb_branches_trouvees_total}]${reset} Utiliser la branche locale ${blue}${current_branch}${reset} ? ${green}[Y/n]${reset} " | |
read -n 1 -r choix | |
echo "" | |
case $choix in | |
y|Y|'' ) | |
branch_to_use=$current_branch | |
break | |
;; | |
esac | |
compteur=$(($compteur+1)) | |
done 3<<< "$branches" | |
# Branches distantes : seulement si aucune branche locale n'est choisie | |
if [[ -z $branch_to_use ]]; then | |
branches="$(git branch -r | grep "$1")" | |
while read -r -u 3 current_branch | |
do | |
# Supprime l'étoile s'il y en a une (c'est le cas quand c'est la branche courante) | |
current_branch="${current_branch/\*/}" | |
# Trim les espaces | |
current_branch=$(echo "$current_branch" | awk '{$1=$1;print}') | |
# On supprime "origin/" | |
current_branch="${current_branch/origin\//}" | |
# Ignore une ligne vide | |
[[ -z ${current_branch} ]] && continue | |
# Ignore la ligne "HEAD -> ..." | |
if [[ $current_branch == HEAD* ]]; then | |
echo "${magenta}[${compteur}/${nb_branches_trouvees_total}]${reset} On ignore la branche distante ${blue}${current_branch}${reset}." | |
compteur=$(($compteur+1)) | |
continue | |
fi | |
echo -n "${magenta}[${compteur}/${nb_branches_trouvees_total}]${reset} Utiliser la branche distante ${blue}origin/${current_branch}${reset} ? ${green}[Y/n]${reset} " | |
read -n 1 -r choix | |
echo "" | |
case $choix in | |
y|Y|'' ) | |
branch_to_use=$current_branch | |
break | |
;; | |
esac | |
compteur=$(($compteur+1)) | |
done 3<<< "$branches" | |
fi | |
if [ -z $branch_to_use ]; then | |
echo "${red}Erreur : Aucune branche ne correspond à la recherche ${yellow}$1${red} !${reset}" | |
return | |
fi | |
echo "${green}\$ git checkout $branch_to_use${reset}" | |
git checkout $branch_to_use | |
if [[ 0 -ne $? ]]; then | |
echo "${red}Erreur : Il y a eu une erreur lors du changement de branche, vérifier l'état des fichiers.${reset}" | |
return | |
fi | |
echo "" | |
echo -n "Récupérer les dernières modifications ? ${green}[Y/n]${reset} " | |
read -n 1 -r | |
echo "" | |
case $REPLY in | |
y|Y|'' ) | |
echo "${green}\$ git pull${reset}" | |
git pull | |
;; | |
* ) ;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment