|
#!/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 - ed@himumsaiddad.com |
|
|
|
# =========== =========== =========== |
|
# 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" |