Skip to content

Instantly share code, notes, and snippets.

@jswanner
Last active August 29, 2015 14:04
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 jswanner/a4a428084e846cf50059 to your computer and use it in GitHub Desktop.
Save jswanner/a4a428084e846cf50059 to your computer and use it in GitHub Desktop.

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
Copy link
Author

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