Skip to content

Instantly share code, notes, and snippets.

@zhangjiayin
Created March 14, 2024 11:00
Show Gist options
  • Save zhangjiayin/aec3a8974e5f6cf49de18118ac9167dc to your computer and use it in GitHub Desktop.
Save zhangjiayin/aec3a8974e5f6cf49de18118ac9167dc to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#usage: bash tag.sh [comment]
#This script will tag the current commit with the latest tag and increment the version
#increment the patch version if the patch version is greater than 9 increment the minor version and reset the patch version to 0
#increment the minor version if the minor version is greater than 9 increment the major version and reset the minor version to 0
#The tag will be in the format v0.0.0-000000000000 [major.minor.patch-date]
#The script will also push the tag to the remote repository
#The script will also tag the commit with the comment provided
if [ -z $1 ];then
echo "special comment please!"
echo "usage: bash $0 [comment]"
exit
fi
# get the latest tag with format v0.0.0-000000000000 [major.minor.patch-date]
TAGS=`git tag -l|grep '^v\d\.\d\.\d\-\d*'|sort -t '-' -k 2 -r`
DF=`date +%Y%m%d%H%M`
TAGS=($TAGS)
if [ -z "$TAGS" ];then
PREV_TAG="v0.9.9-${DF}"
else
PREV_TAG=${TAGS[0]}
fi
#extract the major minor patch version
IFS="v"
read -ra PREV_TAG_ARRAY <<< "$PREV_TAG"
IFS="-"
read -ra PREV_TAG_ARRAY <<< "${PREV_TAG_ARRAY[1]}"
echo ${PREV_TAG_ARRAY[0]}
IFS="."
read -ra PREV_TAG_ARRAY <<< "${PREV_TAG_ARRAY[0]}"
MAJOR=${PREV_TAG_ARRAY[0]}
MINOR=${PREV_TAG_ARRAY[1]}
PATCH=${PREV_TAG_ARRAY[2]}
#increment the patch version
PATCH=$(($PATCH + 1))
if [ $PATCH -eq 10 ];then
PATCH=0
MINOR=$(($MINOR + 1))
fi
if [ $MINOR -eq 10 ];then
MINOR=0
MAJOR=$(($MAJOR+ 1))
fi
IFS=" "
#compacted the version and tag
TAG="v${MAJOR}.${MINOR}.${PATCH}-${DF}"
git tag -a $TAG -m "$1"
git push origin $TAG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment