Skip to content

Instantly share code, notes, and snippets.

@PaulHatch
Last active September 23, 2023 07:47
Show Gist options
  • Save PaulHatch/6ad6d03222a8247f8a0fb9f19207d4c3 to your computer and use it in GitHub Desktop.
Save PaulHatch/6ad6d03222a8247f8a0fb9f19207d4c3 to your computer and use it in GitHub Desktop.
Semantic Versioning Helper for CI
#!/bin/bash
# Semantic Versioning Helper for CI
# Created by Paul Hatcherian
# Usage:
# Use the phrase "(FEATURE)" and "(BREAKING)" in commit messages to
# indicate changes as needed.
# Use 'full', 'ver', or 'ci' to generate version types from git. Tag using
# the version number to begin next version.
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
declare TAG=$(git describe --abbrev=0)
declare LOG=$(git log --pretty="format:%s" --tags ${TAG}..HEAD)
declare FEATURE_COUNT=$(echo "${LOG}" | grep -c "(FEATURE)")
declare BREAKING_COUNT=$(echo "${LOG}" | grep -c "(BREAKING)")
declare TYPE
declare REV
declare NEXT
# If no previous tags then this is the first release
if [ -z "$TAG" ] ; then
NEXT=0.0.0
TYPE=NEW
REV=0
else
declare P=( ${TAG//./ } )
if [ $BREAKING_COUNT -ne 0 ] ; then
TYPE=MAJOR
REV=$(echo "${LOG%(BREAKING)*}" | wc -l | xargs echo)
((P[0]++))
P[1]=0
P[2]=0
elif [ $FEATURE_COUNT -ne 0 ] ; then
TYPE=MINOR
REV=$(echo "${LOG%(FEATURE)*}" | wc -l | xargs echo)
((P[1]++))
P[2]=0
else
TYPE=PATCH
REV=$(git rev-list ${TAG}..HEAD --count)
((P[2]++))
fi
NEXT=${P[0]}.${P[1]}.${P[2]}
fi
case $1 in
"next")
echo ${NEXT}
;;
"full")
echo ${NEXT}.${REV}
;;
"ci")
echo ${NEXT}-${2:-CI}${REV}
;;
*)
echo Version Information
echo "Last: ${TAG}"
echo "Next: ${NEXT}"
echo "Revision: ${REV}"
echo "Version: ${NEXT}.${REV}"
echo "Type: ${TYPE}"
echo "Breaking: ${BREAKING_COUNT} change(s)"
echo "Features: ${FEATURE_COUNT} change(s)"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment