Skip to content

Instantly share code, notes, and snippets.

@ed-george
Last active April 9, 2018 19:07
Show Gist options
  • Save ed-george/e51de8cd705fc2d7eee4c5f689855720 to your computer and use it in GitHub Desktop.
Save ed-george/e51de8cd705fc2d7eee4c5f689855720 to your computer and use it in GitHub Desktop.
A simple script for finding non-v.X.Y.Z tags in a repository, renaming and replacing them
#!/bin/bash
# A simple dumb tag clean-up utility that appends a 'v' to non X.Y.Z tags stored locally and remotely
# Written with love by Ed George - [email protected]
# =========== =========== ===========
# Constants
# =========== =========== ===========
# Reset
end="\033[0m" # Text Reset
# Regular Colors
red="\033[0;31m" # Red
green="\033[0;32m" # Green
yellow="\033[0;33m" # Yellow
# =========== =========== ===========
# Methods
# =========== =========== ===========
function verbose {
echo -e "${green}▶ ${1}${end}"
}
function warn {
echo -e "${yellow}${1}${end}"
}
function error {
echo -e "${red}${1}${end}"
}
function fatal {
error "• FATAL: ${1}"
exit 1
}
# Helper method for prompting continue
# EXAMPLE USAGE:
# prompt_confirm "Delete all tags? || exit 0
prompt_confirm() {
echo -e "${yellow}"
while true; do
read -r -n 1 -p "${1:-Continue?} [y/n]: " REPLY
case $REPLY in
[yY]) echo -e "${end}" ; return 0 ;;
[nN]) echo -e "${end}" ; return 1 ;;
*) error "invalid input"
esac
done
echo -e "${end}"
}
# Helper method for showing all local tags
show_local_tags(){
verbose "Local tags"
git tag || fatal "Could not display local tags"
}
# =========== =========== ===========
# Main
# =========== =========== ===========
verbose "Version Tag Cleanup"
# Check current directory is a git repo
git rev-parse --is-inside-work-tree 1>/dev/null || fatal "Current directory $(pwd) is not a git repo"
# Prune local tags that are not present on remote
prompt_confirm "Prune local tags that are not present on remote?" || exit 0
git fetch origin --prune --tags || fatal "Could not prune local tags"
# Get all tags from remote
verbose "Fetching tags from remote"
git fetch --tags || fatal "Could not get remote tags"
show_local_tags
# Remove all remote tags that do not start with 'v' prefix
prompt_confirm "Remove all non vX.Y.Z tags on remote?" || exit 0
verbose "Removing all non-prefixed remote tags"
git tag | grep -v v | xargs -n 1 -I% git push origin :refs/tags/% || fatal "Could not remove remote tags - you may need to 'git push --tags' to restore them"
# Create duplicate local tags by appending 'v'
prompt_confirm "Create replacement vX.Y.Z tags locally?" || exit 0
verbose "Creating prefixed local tags"
git tag | grep -v v | xargs -n 1 -I% git tag v% % || fatal "Could not create local tags, run 'git tag' to see current local tags"
show_local_tags
# Remove all local tags that do not start with 'v' prefix
prompt_confirm "Remove all non vX.Y.Z tags locally and push changes?" || exit 0
verbose "Removing all non-prefixed local tags"
git tag | grep -v v | xargs -n 1 -I% git tag -d % || fatal "Could not remove local non-prefixed tags, run 'git tag' to see current local tags"
# Push tags to remote
verbose "Pushing tags to remote"
git push --tags || fatal "Could not push tags to remote"
show_local_tags
verbose "Completed cleanup"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment