Skip to content

Instantly share code, notes, and snippets.

@natenho
Created September 18, 2022 19:21
Show Gist options
  • Save natenho/f0458ed595038b5002780d9bcaf224d2 to your computer and use it in GitHub Desktop.
Save natenho/f0458ed595038b5002780d9bcaf224d2 to your computer and use it in GitHub Desktop.
Bash git version bump
#!/bin/sh
# Prints the next version based on git tags in the pattern v0.0.0
# Uses regexp to infer which part of the version will be incremented
# Inspired by https://gitversion.net/docs/reference/version-increments
MAJOR_REGEXP="\+semver:\s?(breaking|major)"
MINOR_REGEXP="\+semver:\s?(feature|minor)"
COMMIT_TAG=$(git describe --exact-match --tags HEAD 2>/dev/null || true)
if [ -z $COMMIT_TAG ]; then
COMMIT_MSG="$(git log -1 --pretty='%B')"
if echo $COMMIT_MSG | grep -Eqi "$MAJOR_REGEXP"; then INCREMENT_PART=1; fi
if echo $COMMIT_MSG | grep -Eqi "$MINOR_REGEXP"; then INCREMENT_PART=2; else INCREMENT_PART=3; fi
LATEST_VERSION=$(git describe --tags --abbrev=0 --match "v*" $(git rev-list --tags --max-count=1))
NEW_VERSION=$(echo $LATEST_VERSION | awk -F. -v OFS=. "{\$${INCREMENT_PART} += 1; print}")
echo ${NEW_VERSION}
else
echo ${COMMIT_TAG}
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment