Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
増えすぎたgitのtagを削除するスクリプト
#!/bin/zsh
case "$1" in
test)
TYPE=$1
;;
clean)
TYPE=$1
;;
*)
echo "Usage: $0 {clean|test}"
exit 2
esac
note() { echo "\033[1;32m$1\033[0m" } # green
notice() { echo "\033[1;34m$1\033[0m" } # blue
alert() { echo "\033[1;33m$1\033[0m" } # yellow
warn() { echo "\033[1;31m$1\033[0m"; } # red
success_face() { echo "\033[1;32m$1\033[1;33m ;)\033[0m" } # green
warn_face() { echo "\033[1;31m$1\033[1;33m O_o\033[0m" } # red
MAX_MINOR=0
MAX_FIXED=0
OLD_MAX_FIXED=0
OLD_MAX_TAG=""
FIRST=1
note "Begins git tags clean."
notice "git fetch --tags"
git fetch --tags
# set array of tag
TAGS=$(git tag)
TAG=(`echo $TAGS`)
#notice "tags: $TAG"
# set last minor version
# TAG_COUNT=${#TAG[@]}
# LAST_TAG=${TAG[$TAG_COUNT]}
# CONVERTED_LAST_TAG=`echo $LAST_TAG | sed 's/\./ /g'`
# LAST_TAG_ARRAY=(`echo $CONVERTED_LAST_TAG`)
# typeset -i LAST_MINOR=$LAST_TAG_ARRAY[2]
MAXIMUM_MAJOR=0
MAXIMUM_MINOR=0
for i in `seq 1 ${#TAG[@]}`; do
VER=`echo ${TAG[$i]} | sed 's/\./ /g'`
VERS=(`echo $VER`)
typeset -i MAJOR=$VERS[1]
typeset -i MINOR=$VERS[2]
if [ $MAXIMUM_MAJOR -lt $MAJOR ]; then
typeset -i MAXIMUM_MAJOR=$MAJOR
typeset -i MAXIMUM_MINOR=$MINOR
elif [ $MAXIMUM_MINOR -lt $MINOR ]; then
typeset -i MAXIMUM_MINOR=$MINOR
fi
done
#alert $MAXIMUM_MAJOR.$MAXIMUM_MINOR
LATEST_MAJOR=0
LATEST_MINOR=0
for i in `seq 1 ${#TAG[@]}`; do
VER=`echo ${TAG[$i]} | sed 's/\./ /g'`
VERS=(`echo $VER`)
typeset -i MAJOR=$VERS[1]
typeset -i MINOR=$VERS[2]
typeset -i FIXED=$VERS[3]
if [ $LATEST_MAJOR -eq $MAXIMUM_MAJOR -a $LATEST_MINOR -eq $MAXIMUM_MINOR ]; then
continue
fi
if [ $FIRST -eq 1 ]; then
typeset -i LATEST_MAJOR=$VERS[1]
typeset -i LATEST_MINOR=$VERS[2]
typeset -i LATEST_FIXED=$VERS[3]
FIRST=0
# $LATEST_MAJOR < $MAJOR
elif [ $LATEST_MAJOR -lt $MAJOR ]; then
# $LATEST_MINOR < $MAX_MINOR
if [ $LATEST_MINOR -lt $MAX_MINOR ]; then
DELETE_TAGS=(${DELETE_TAGS[@]} ${TAG[$i-1]})
else
#echo ${TAG[$i-1]}
fi
#echo "A) Current: $MAJOR.$MINOR.$FIXED, Latest: $LATEST_MAJOR.$LATEST_MINOR.$LATEST_FIXED"
typeset -i LATEST_MAJOR=$VERS[1]
typeset -i LATEST_MINOR=$VERS[2]
typeset -i LATEST_FIXED=$VERS[3]
MAX_MINOR=0
# $LATEST_MINOR < $MINOR
elif [ $LATEST_MINOR -lt $MINOR ]; then
OLD_MAX_FIXED=0
OLD_MAX_TAG=""
# $LATEST_FIXED < $MAX_FIXED
if [ $LATEST_FIXED -lt $MAX_FIXED ]; then
DELETE_TAGS=(${DELETE_TAGS[@]} ${TAG[$i-1]})
else
#echo ${TAG[$i-1]}
fi
# $MAX_MINOR < $MINOR
if [ $MAX_MINOR -lt $MINOR ]; then
MAX_MINOR=$MINOR
fi
#echo "B) Current: $MAJOR.$MINOR.$FIXED, Latest: $LATEST_MAJOR.$LATEST_MINOR.$LATEST_FIXED"
typeset -i LATEST_MINOR=$VERS[2]
typeset -i LATEST_FIXED=$VERS[3]
MAX_FIXED=0
else
# $LATEST_FIXED < $FIXED
if [ $LATEST_FIXED -lt $FIXED ]; then
DELETE_TAGS=(${DELETE_TAGS[@]} ${TAG[$i-1]})
else
if [ $OLD_MAX_FIXED -eq 0 ]; then
elif [ $OLD_MAX_FIXED -lt $LATEST_FIXED ]; then
#echo DEL: $OLD_MAX_TAG
DELETE_TAGS=(${DELETE_TAGS[@]} ${OLD_MAX_TAG})
fi
#echo ${TAG[$i-1]}
typeset -i OLD_MAX_FIXED=$LATEST_FIXED
OLD_MAX_TAG=${TAG[$i-1]}
fi
# $MAX_FIXED < $FIXED
if [ $MAX_FIXED -lt $FIXED ]; then
MAX_FIXED=$FIXED
fi
#echo "C) Current: $MAJOR.$MINOR.$FIXED, Latest: $LATEST_MAJOR.$LATEST_MINOR.$LATEST_FIXED"
typeset -i LATEST_FIXED=$VERS[3]
fi
# for q in `seq 1 ${#VERS[@]}`; do
# echo ${VERS[$q]}
# done
done
if [ ${#DELETE_TAGS[@]} -ne 0 ]; then
for i in `seq 1 ${#TAG[@]}`; do
SEARCH=`echo $TAG[$i] | sed 's/\./-/g'`
SUBJECT=`echo $DELETE_TAGS | sed 's/\./-/g'`
REPLACED=(`echo $SUBJECT | sed -e "s/.*\($SEARCH\).*$/\1/g"`)
REPLACED=$REPLACED""
if [ $REPLACED = $SEARCH ]; then
warn $TAG[$i]
else
echo $TAG[$i]
fi
done
if [ $TYPE = 'clean' ]; then
notice "\n$ git tag -d $DELETE_TAGS"
git tag -d $DELETE_TAGS
for i in `seq 1 ${#DELETE_TAGS[@]}`; do
DELETE_REMOTE_TAGS=(${DELETE_REMOTE_TAGS[@]} ":refs/tags/${DELETE_TAGS[$i]}")
done
notice "\n$ git push origin $DELETE_REMOTE_TAGS"
git push origin $DELETE_REMOTE_TAGS
success_face "Successfully tag clean!"
else
notice "\n$ git tag -d $DELETE_TAGS"
for i in `seq 1 ${#DELETE_TAGS[@]}`; do
DELETE_REMOTE_TAGS=(${DELETE_REMOTE_TAGS[@]} ":refs/tags/${DELETE_TAGS[$i]}")
done
notice "\n$ git push origin $DELETE_REMOTE_TAGS"
fi
else
success_face "Nothing tag clean!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment