Skip to content

Instantly share code, notes, and snippets.

@johndevs
Created February 5, 2023 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johndevs/06e0335df297474a501e57b0a664fe71 to your computer and use it in GitHub Desktop.
Save johndevs/06e0335df297474a501e57b0a664fe71 to your computer and use it in GitHub Desktop.
Increment semantic git version tag by one
#!/bin/bash
### Increments the part of the string
## $2: number of part: 0 – major, 1 – minor, 2 – patch
increment_version() {
local delimiter=.
local array=($(echo "$1" | tr $delimiter '\n'))
array[$2]=$((array[$2]+1))
if [ $2 -lt 2 ]; then array[2]=0; fi
if [ $2 -lt 1 ]; then array[1]=0; fi
NEW_TAG=$(local IFS=$delimiter ; echo "${array[*]}")
}
LATEST_TAG=$(git describe --tags --abbrev=0)
increment_version "$LATEST_TAG" $1
read -r -p "Apply tag $NEW_TAG to branch? [Y/n]" response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
git tag -a $NEW_TAG -m "$NEW_TAG"
else
exit 1
fi
read -r -p "Push tag $NEW_TAG to origin? [Y/n]" response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
git push origin $NEW_TAG --follow-tags
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment