Created
May 23, 2023 22:49
-
-
Save Lenart12/058f2efe2ec4890b251557d0d98eae5f to your computer and use it in GitHub Desktop.
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 | |
# https://github.com/unegma/bash-functions/blob/main/update.sh | |
#get parameters | |
VERSION="$1" | |
#get highest tag number, and add 1.0.0 if doesn't exist | |
CURRENT_VERSION=`git describe --abbrev=0 --tags 2>/dev/null` | |
if [[ $CURRENT_VERSION == '' ]] | |
then | |
CURRENT_VERSION='1.0.0' | |
fi | |
echo "Current Version: $CURRENT_VERSION" | |
#replace . with space so can split into an array | |
CURRENT_VERSION_PARTS=(${CURRENT_VERSION//./ }) | |
#get number parts | |
VNUM1=${CURRENT_VERSION_PARTS[0]} | |
VNUM2=${CURRENT_VERSION_PARTS[1]} | |
VNUM3=${CURRENT_VERSION_PARTS[2]} | |
if [[ $VERSION == 'major' ]] | |
then | |
VNUM1=$((VNUM1+1)) | |
VNUM2=0 | |
VNUM3=0 | |
elif [[ $VERSION == 'minor' ]] | |
then | |
VNUM2=$((VNUM2+1)) | |
VNUM3=0 | |
elif [[ $VERSION == 'patch' ]] | |
then | |
VNUM3=$((VNUM3+1)) | |
else | |
echo "No version type (https://semver.org/) or incorrect type specified, try: [major, minor, patch]" | |
exit 1 | |
fi | |
#create new tag | |
NEW_TAG="v$VNUM1.$VNUM2.$VNUM3" | |
echo "($VERSION) updating $CURRENT_VERSION to $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` | |
#only tag if no tag already | |
#to publish, need to be logged in to npm, and with clean working directory: `npm login; git stash` | |
if [ -z "$NEEDS_TAG" ]; then | |
git tag -a "$NEW_TAG" | |
echo "Tagged with $NEW_TAG" | |
git push --follow-tags | |
else | |
echo "Already a tag on this commit" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment