Source: https://whatthepatch.blogspot.com/2017/06/git-auto-increment-tag.html
Last active
July 25, 2019 08:03
-
-
Save xsaamiir/52344303413137425a95ab5d9a93deb1 to your computer and use it in GitHub Desktop.
Git Auto Increment tag
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
#!/bin/bash | |
IS_MAJOR=$(git log -n 1 | grep -i break || true) | |
IS_MINOR=$(git log -n 1 | grep -i minor || true) | |
if [[ -n "$IS_MINOR" ]]; then | |
echo "found minor in commit message. will increment minor version" | |
BUMP_MODE="minor" | |
elif [[ -n "$IS_MAJOR" ]]; then | |
echo "found breaking in the commit message. will increment major version" | |
BUMP_MODE="major" | |
else | |
echo "no version mode specified, defaulting to patch" | |
BUMP_MODE="patch" | |
fi | |
# Get the latest tag | |
VERSION=$(git describe --match "v[0-9]*" --abbrev=0 --tags) | |
# If no tag, give VERSION a default value of 0 | |
VERSION=${VERSION:-'v0.0.0'} | |
# Remove the prefix "v" | |
CLEANED_VERSION=(${VERSION//v/}) | |
MAJOR="${CLEANED_VERSION%%.*}" | |
CLEANED_VERSION="${CLEANED_VERSION#*.}" | |
MINOR="${CLEANED_VERSION%%.*}" | |
CLEANED_VERSION="${CLEANED_VERSION#*.}" | |
PATCH="${CLEANED_VERSION%%.*}" | |
CLEANED_VERSION="${CLEANED_VERSION#*.}" | |
case $BUMP_MODE in | |
"major") | |
MAJOR=$((MAJOR + 1)) | |
MINOR=0 | |
PATCH=0 | |
;; | |
"minor") | |
MINOR=$((MINOR + 1)) | |
PATCH=0 | |
;; | |
"patch") | |
PATCH=$((PATCH + 1)) | |
;; | |
esac | |
# Create new tag | |
NEW_TAG="v$MAJOR.$MINOR.$PATCH" | |
echo "Last tag version $VERSION New tag will be $NEW_TAG" | |
# Get current hash and see if it already has a tag | |
GIT_COMMIT=$(git rev-parse HEAD) | |
NEEDS_TAG=$(git describe --contains "$GIT_COMMIT" 2>/dev/null) | |
echo "###############################################################" | |
# Only tag if no tag already (would be better if the git describe command above could have a silent option) | |
if [[ -z "$NEEDS_TAG" ]]; then | |
echo "Tagged with $NEW_TAG (Ignoring fatal:cannot describe - this means commit is untagged) " | |
git tag -a $NEW_TAG -m "auto bump $BUMP_MODE version 🚀" | |
git push origin --tags | |
else | |
echo "Current commit already has a tag $VERSION" | |
fi | |
echo "###############################################################" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment