Last active
January 5, 2024 12:26
-
-
Save guypursey/ea24c7a7e454ed7a0312e2dcdcdbfa5e to your computer and use it in GitHub Desktop.
Semver with Git and Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
V=$(git describe --tags --abbrev=0) | |
Y=$(date +"%Y") | |
m=$(date +"%m") | |
d=$(date +"%d") | |
today="$(date +"%Y")$(date +"%m")$(date +"%d")" | |
verregex="([0-9]+)" | |
preregex="([0-9]{8})\.([0-9]+)" | |
bldregex="(\+[a-zA-Z]+)?" | |
regex="^v$verregex\.$verregex\.$verregex-$preregex$bldregex$" | |
if [[ $V =~ $regex ]] | |
then | |
major="${BASH_REMATCH[1]}" | |
minor="${BASH_REMATCH[2]}" | |
patch="${BASH_REMATCH[3]}" | |
stamp="${BASH_REMATCH[4]}" | |
frank="${BASH_REMATCH[5]}" | |
echo "Most recent version: $V" | |
echo "Major: $major | Minor: $minor | Patch: $patch | Stamp: $stamp | Frank: $frank" | |
if [[ "$stamp" == "$today" ]] | |
then | |
frank="$(($frank + 1))" | |
echo "There was a commit earlier today: updating frank to $frank" | |
else | |
stamp="$today" | |
frank=0 | |
echo "First commit today... starting frank at 0" | |
fi | |
if [ $# -eq 0 ]; | |
then | |
echo "Updating tag/version with date only." | |
echo "Today is: $today" | |
else | |
if [[ "$1" == "patch" || "$1" == "minor" || "$1" == "major" ]] | |
then | |
echo "Updating $1 version in tag. Currently at ${!1}" | |
declare $1="$((${!1} + 1))" | |
echo "Will change $1 version to ${!1}" | |
if [[ "$1" == "minor" ]] | |
then | |
patch=0 | |
elif [[ "$1" == "major" ]] | |
then | |
patch=0 | |
minor=0 | |
fi | |
fi | |
fi | |
echo "Now updated version: v$major.$minor.$patch-$stamp.$frank" | |
else | |
echo "The format of the previous Git tag doesn't look right..." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment