Skip to content

Instantly share code, notes, and snippets.

@nrutman
Last active January 17, 2019 18:58
Show Gist options
  • Save nrutman/d4c49a05e484b896d41a4fadf3037ed3 to your computer and use it in GitHub Desktop.
Save nrutman/d4c49a05e484b896d41a4fadf3037ed3 to your computer and use it in GitHub Desktop.
Checks out the latest hotfix (.x) branch based on semver
#!/usr/bin/env bash
# Checks out the latest hotfix branch based on semver tagging
# Output function
output () {
echo -e "\033[36m\033[1m* $1 \033[0m"
return 0;
}
# Get up to date with origin
git fetch origin &> /dev/null
# Get relevent branches
current=$(git branch | grep \* | cut -d ' ' -f2)
locals=($(git branch --format "%(refname:short)" --sort=v:refname | grep -o "^\\d\{1,2\}\.\\d\{1,2\}\.x\$"))
locals_str="${locals[@]}"
latest=$(git branch -la --format "%(refname:short)" --sort=v:refname | grep -o "\\d\{1,2\}\.\\d\{1,2\}\.x\$" | tail -1)
# If already on the latest branch, pull to make sure it is updated
if [ "$current" = "$latest" ]
then
output "Already on the latest release branch. Pulling from origin..."
git pull
exit 0
fi
# Check out the latest branch
output "Checking out the latest release branch..."
git checkout $latest
# Clean up any legacy hotfix branches
if [ "$latest" != "$locals_str" ]
then
output "Cleaning up stale branches..."
old_branches=${locals_str/$latest/}
git branch -D $old_branches
fi
# Pull latest
output "Pulling from origin..."
git pull
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment