Skip to content

Instantly share code, notes, and snippets.

@rcassani
Last active February 13, 2023 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcassani/5ff18bb1520a27f72d332385aa6220f2 to your computer and use it in GitHub Desktop.
Save rcassani/5ff18bb1520a27f72d332385aa6220f2 to your computer and use it in GitHub Desktop.
post-checkout Git hook for switching branches
#!/bin/bash
set -e # Stop execution of script if a command or pipeline has an error
printf '\nExample post-checkout hook\n'
# These are the parameters given to the hook
# https://git-scm.com/docs/githooks.html#_post_checkout
oldHEAD=$1 # ref of the previous (old) HEAD
newHEAD=$2 # ref of the new HEAD
checkoutType=$3 # checkout type flag: 1 = changing branches,
# 0 = retrieving a file from the index
# If checkoutType is 0, exit
if [[ $checkoutType -eq 0 ]]; then
exit 0
fi
# Since $1 and $2 are the 40-character SHA-1,
# it is not possible to unambiguously determine old- and new branch names
# [[ $checkoutType == 1 ]] && checkoutType='branch' || checkoutType='file' ;
# echo 'Checkout type: '$checkoutType
# echo ' old HEAD: '`git name-rev --name-only $oldHEAD`
# echo ' new HEAD: '`git name-rev --name-only $newHEAD`
# https://stackoverflow.com/q/25590267/4859684
# Get branch names from git-reflog
oldBRANCH=`git reflog | awk 'NR==1{ print $6; exit }'`
newBRANCH=`git reflog | awk 'NR==1{ print $8; exit }'`
echo "Old branch: $oldBRANCH"
echo "New branch: $newBRANCH"
echo ""
# If any of the branches is empty, it's rebase
if [ -z "$oldBRANCH" ] || [ -z "$newBRANCH" ]; then
exit 0
fi
exists_in_list() {
# Function for determine if a word is in a space-separated list
# https://www.baeldung.com/linux/check-variable-exists-in-list
LIST=$1
WORD=$2
if [[ "$LIST" =~ (" "|^)$WORD(" "|$) ]]; then
echo 1
else
echo 0
fi
}
starts_with(){
# Function to check if a word start with a given prefix
WORD=$1
PREF=$2
if [[ $WORD =~ ^$PREF* ]]; then
echo 1
else
echo 0
fi
}
# ACTION IF NEW BRANCH IS IN A GIVEN LIST
# Determine if NEW branch name belongs to a list of names
listBranches="dev tmp test"
isNewList="$(exists_in_list "$listBranches" "$newBRANCH")"
# NEW branch was in list
if [[ $isNewList -eq 1 ]]; then
printf "New branch: $newBRANCH was in list\n"
printf ' Do something 0\n'
exit 0
fi
# ACTION ACCORDING THE NAMES OF OLD TO NEW BRANCH
# Determine if OLD and NEW branch names have a given prefix
isOldFtr="$(starts_with "$oldBRANCH" "ftr-")"
isNewFtr="$(starts_with "$newBRANCH" "ftr-")"
# If any of the branches is empty, it's rebase
if [ -z "$oldBRANCH" ] || [ -z "$newBRANCH" ]; then
exit 0
fi
# NoFeature branch to Feature branch
if [[ $isOldFtr -eq 0 ]] && [[ $isNewFtr -eq 1 ]]; then
printf "Branch switch: $oldBRANCH -> $newBRANCH\n"
printf ' Do something 1\n'
exit 0
fi
# Feature branch to NoFeature branch
if [[ $isOldFtr -eq 1 ]] && [[ $isNewFtr -eq 0 ]]; then
printf "Branch switch: $oldBRANCH -> $newBRANCH\n"
printf ' Do something 2\n'
exit 0
fi
# No change in type of branch
if ([[ $isOldFtr -eq 0 ]] && [[ $isNewFtr -eq 0 ]]) || ([[ $isOldFtr -eq 1 ]] && [[ $isNewFtr -eq 1 ]]); then
printf "Branch switch: $oldBRANCH -> $newBRANCH\n"
printf ' Do something 3\n'
exit 0
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment