Skip to content

Instantly share code, notes, and snippets.

@Muriano
Created August 22, 2023 08:29
Show Gist options
  • Save Muriano/cd39d1cdbc311e77890401f62b56828a to your computer and use it in GitHub Desktop.
Save Muriano/cd39d1cdbc311e77890401f62b56828a to your computer and use it in GitHub Desktop.
Remove tags that are not semver tags. Has preview and confirmation.
#!/bin/bash
TAGS=$(git tag)
SEMVER_REGEX='^[0-9]+\.[0-9]+\.[0-9]+$'
NON_SEMVER_TAGS=()
for tag in $TAGS; do
# Check if the tag matches the SemVer pattern
if [[ ! $tag =~ $SEMVER_REGEX ]]; then
NON_SEMVER_TAGS+=("$tag")
fi
done
if [ ${#NON_SEMVER_TAGS[@]} -eq 0 ]; then
echo "No non-SemVer tags to remove."
exit 0
fi
FILTERED_NON_SEMVER_TAGS=()
for tag in "${NON_SEMVER_TAGS[@]}"; do
if [[ ! $tag =~ ^[0-9]+\.[0-9]+$ ]]; then
FILTERED_NON_SEMVER_TAGS+=("$tag")
fi
done
if [ ${#FILTERED_NON_SEMVER_TAGS[@]} -eq 0 ]; then
echo "No non-SemVer tags to remove after filtering."
exit 0
fi
echo "The following non-SemVer tags will be removed:"
for tag in "${FILTERED_NON_SEMVER_TAGS[@]}"; do
echo "- $tag"
done
read -p "Confirm removal of these non-SemVer tags? (y/n): " confirm
if [ "$confirm" == "y" ]; then
for tag in "${FILTERED_NON_SEMVER_TAGS[@]}"; do
echo "Removing tag: $tag"
git tag -d $tag # Delete the tag locally
git push origin :refs/tags/$tag # Delete the tag on the remote repository
done
echo "Tags removed."
else
echo "Tags removal cancelled."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment