Skip to content

Instantly share code, notes, and snippets.

@jswanner jswanner/README.md
Last active Aug 29, 2015

Embed
What would you like to do?

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

## ----- Tag ----- ##
## Automatically set a git tag based on Semantic Versioning keyword
## Note: This assumes you prefix your git tags with 'v' (e.g 'v1.2.0')
##
## $1 - 'major', 'minor', 'patch'
##
## Usage: `git_tag major`
##
function git_tag() {
local tag=$(git describe --abbrev=0 --tags)
local prefix=$(echo $tag | grep -o --color=never "^[A-z]\+")
local major=$(echo $tag | cut -d '.' -f1 | sed "s/^$prefix//")
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"
esac
if [[ "$version" ]]
then
git tag -a "$prefix$version" -m "Version $version"
echo "$prefix$version created!"
fi
}
@jswanner

This comment has been minimized.

Copy link
Owner Author

jswanner commented Jul 29, 2014

Differs from original in that: don't leak as globals, v tag prefix is optional, and it prints usage text if you don't give it one of the SemVer keywords.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.