Skip to content

Instantly share code, notes, and snippets.

@vicenterusso
Created December 4, 2015 13:37
Show Gist options
  • Save vicenterusso/fa65994bd6bd1152102b to your computer and use it in GitHub Desktop.
Save vicenterusso/fa65994bd6bd1152102b to your computer and use it in GitHub Desktop.
Automatic Semantic Versioning via Git Tags

This function automatically grabs the latest git tag and, based on keyword (major, minor, patch), adds a new tag. (e.g. git_tag patch for v1.2.0 would create v1.2.1)

Drop this into your ~/.bash_profile and run source ~/.bash_profile to use it.

You can find all of my dotfiles here: https://github.com/drewbarontini/dotfiles

Credit to Jacob Swanner for cleaning up my function :)

## ----- Tag ----- ##
## Automatically set a git tag based on Semantic Versioning keyword
##
## $1 - 'major', 'minor', 'patch'
##
## Usage: `git_tag major`
##
function git_tag() {
local tag=$(git describe --abbrev=0 --tags 2>/dev/null)
local prefix=$(echo $tag | grep -o --color=never "[A-z]\+")
local major=$(echo $tag | cut -d '.' -f1 | grep -o --color=never "[0-9]")
local minor=$(echo $tag | cut -d '.' -f2)
local patch=$(echo $tag | cut -d '.' -f3)
case "$1" in
'major')
local nmajor=$(($major+1))
local version="$nmajor.0.0"
;;
'minor')
local nminor=$(($minor+1))
local version="$major.$nminor.0"
;;
'patch')
local npatch=$(($patch+1))
local version="$major.$minor.$npatch"
;;
*)
echo "Automatically set a git tag based on Semantic Versioning keyword"
echo
echo "Usage: git_tag <major|minor|patch>"
echo
echo "Example:"
echo " git_tag major"
echo
echo "If this is your first tag, use 'git tag' to set up your first one."
esac
if [[ "$version" ]]
then
git tag -a "$prefix$version" -m "Version $version"
echo "$prefix$version created!"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment