Skip to content

Instantly share code, notes, and snippets.

@vikytech
Created June 3, 2020 22:46
Show Gist options
  • Save vikytech/c6c92c9301fbc4b93f8eb79dc2e95ced to your computer and use it in GitHub Desktop.
Save vikytech/c6c92c9301fbc4b93f8eb79dc2e95ced to your computer and use it in GitHub Desktop.
Auto version increment of tag using git post-commit hook
#!/bin/bash
message=$(git log -1 --pretty=%B)
PATTERN="^(minor-fix|major-fix|patch): "
incrementVersion () {
version="$1"
major=0
minor=0
patch=0
# break down the version number into it's components
regex="([0-9]+).([0-9]+).([0-9]+)"
if [[ $version =~ $regex ]]; then
major="${BASH_REMATCH[1]}"
minor="${BASH_REMATCH[2]}"
patch="${BASH_REMATCH[3]}"
fi
# check paramater to see which number to increment
case $2 in
patch)
build=$(echo $patch + 1 | bc)
;;
minor-fix)
minor=$(echo $minor + 1 | bc)
;;
major-fix)
major=$(echo $major+1 | bc)
;;
esac
# echo the new version number
newVersionNumber="v${major}.${minor}.${patch}"
if [ "$newVersionNumber" == "$version" ]; then
echo "Error!! Duplicate tag found"
exit -1
fi
echo "Generated tag: " $newVersionNumber
$(git tag ${newVersionNumber})
}
if [[ "$message" =~ $PATTERN ]]; then
echo "Generating tag..."
latestTagVersion=$(git tag | tail -1)
generatedTag=$latestTagVersion
echo "Last tag version: " $latestTagVersion
incrementVersion $latestTagVersion ${BASH_REMATCH[1]}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment