Skip to content

Instantly share code, notes, and snippets.

@phpguru
Created December 6, 2024 16:01
Show Gist options
  • Save phpguru/b72628d76aff90889c2b21b70a163cf6 to your computer and use it in GitHub Desktop.
Save phpguru/b72628d76aff90889c2b21b70a163cf6 to your computer and use it in GitHub Desktop.
Git Tag Manager ... Paginate through Git Tags matching a wildcard pattern and provide ability to delete tags
#!/bin/bash
#
# I have this saved to /usr/local/bin/gtmgr (git tag manager)
# and chmod +x it, or paste it into your bash profile and alias it.
#
# Run `gtmgr` in the context of your repo root on your local machine.
# The script will present you with a portion of your tags matching pattern.
#
# Sample Usage
# --------------------------------------------------------------------------
#
# 08:55:35 me@mybox myrepo ±|JIRA-123|→ gtmgr
# Enter the wildcard pattern to match tags (e.g., 'v*'):
# *staging*
#
# Page 1 of 80
# ---------------------------------------
# 1) 1.4.310-staging - Created on: 2022-10-03 13:16:10 +0300
# 2) 1.4.314-staging - Created on: 2022-10-04 00:04:53 -0400
# 3) 1.4.315-staging - Created on: 2022-10-04 12:27:17 +0300
# 4) 1.4.343-staging - Created on: 2022-10-14 13:29:20 +0300
# 5) 1.4.344-staging - Created on: 2022-10-17 13:41:44 -0700
# 6) 1.4.345-staging - Created on: 2022-10-17 14:18:08 -0700
# 7) 1.4.474-staging - Created on: 2023-05-16 20:36:33 +0000
# 8) v1.0.0-staging - Created on: 2021-12-21 11:14:49 -0700
# 9) v1.0.001-staging - Created on: 2023-06-07 13:22:03 +0200
# 10) v1.0.002-staging - Created on: 2023-06-07 15:05:40 +0200
#
# [N] Next Page | [P] Previous Page | [D] Delete Tag | [Q] Quit
# Choose an option:
#
# Function to manage Git tags with pagination
manage_tags_with_pagination() {
# Read the wildcard pattern from the user
echo "Enter the wildcard pattern to match tags (e.g., 'v*'): "
read -r pattern
# Find matching tags
matching_tags=($(git tag -l "$pattern"))
total_tags=${#matching_tags[@]}
if [[ $total_tags -eq 0 ]]; then
echo "No tags found matching pattern '$pattern'."
exit 1
fi
# Pagination setup
page_size=10
current_page=0
total_pages=$(( (total_tags + page_size - 1) / page_size )) # Calculate total pages
show_page() {
echo
echo "Page $((current_page + 1)) of $total_pages"
echo "---------------------------------------"
start=$((current_page * page_size))
end=$((start + page_size - 1))
if ((end >= total_tags)); then end=$((total_tags - 1)); fi
for i in $(seq $start $end); do
tag=${matching_tags[$i]}
tag_date=$(git log -1 --format="%ai" "$tag")
echo "$((i + 1))) $tag - Created on: $tag_date"
done
echo
echo "[N] Next Page | [P] Previous Page | [D] Delete Tag | [Q] Quit"
echo "Choose an option:"
}
delete_tag() {
echo "Enter the number corresponding to the tag you want to delete:"
read -r choice
if [[ "$choice" =~ ^[0-9]+$ && $choice -ge 1 && $choice -le $total_tags ]]; then
tag_to_delete=${matching_tags[$((choice - 1))]}
echo "Deleting tag '$tag_to_delete'..."
git tag -d "$tag_to_delete" # Delete the local tag
git push origin --delete "$tag_to_delete" # Delete the remote tag (if applicable)
echo "Tag '$tag_to_delete' deleted locally and remotely (if it existed)."
# Remove the tag from the array and adjust indices
unset 'matching_tags[$((choice - 1))]'
matching_tags=("${matching_tags[@]}") # Rebuild the array
total_tags=${#matching_tags[@]}
total_pages=$(( (total_tags + page_size - 1) / page_size ))
else
echo "Invalid choice. Please try again."
fi
}
# Main loop
while true; do
show_page
read -r option
case $option in
N|n)
if ((current_page < total_pages - 1)); then
((current_page++))
else
echo "You are already on the last page."
fi
;;
P|p)
if ((current_page > 0)); then
((current_page--))
else
echo "You are already on the first page."
fi
;;
D|d)
delete_tag
if ((current_page >= total_pages)); then
current_page=$((total_pages - 1)) # Adjust if we delete the last tag on the last page
fi
;;
Q|q)
echo "Exiting."
break
;;
*)
echo "Invalid option. Please choose N, P, D, or Q."
;;
esac
done
}
# Run the function
manage_tags_with_pagination
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment