Skip to content

Instantly share code, notes, and snippets.

@paulormart
Last active October 15, 2019 23:40
Show Gist options
  • Save paulormart/dcc5b390b334ffd23a4473a36743109c to your computer and use it in GitHub Desktop.
Save paulormart/dcc5b390b334ffd23a4473a36743109c to your computer and use it in GitHub Desktop.
Version script
#!/bin/bash
# credits for https://gist.github.com/pete-otaqui/4188238
#
# > make a file executable
# chmod +x ./bump-version.sh
#
# works with a file called VERSION in the current directory,
# the contents of which should be a semantic version number
# such as "v1.2.3"
# this script will display the current version, automatically
# suggest a "minor" version update, and ask for input to use
# the suggestion, or a newly entered value.
# once the new version number is determined, the script will
# pull a list of changes from git history, prepend this to
# a file called CHANGES (under the title of the new version
# number) and create a GIT tag.
git fetch --tags
if [ -f VERSION ]; then
BASE_STRING=`cat VERSION`
BASE_LIST=(`echo $BASE_STRING | tr '.' ' '`)
V_MAJOR=${BASE_LIST[0]}
V_MINOR=${BASE_LIST[1]}
V_PATCH=${BASE_LIST[2]}
echo "Current version : $BASE_STRING"
V_MINOR=$((V_MINOR + 1))
V_PATCH=0
SUGGESTED_VERSION="$V_MAJOR.$V_MINOR.$V_PATCH"
# read first argument as auto
if [ "$1" = "auto" ]; then
INPUT_STRING=$SUGGESTED_VERSION
else
read -p "Enter a version number [$SUGGESTED_VERSION]: " INPUT_STRING
if [ "$INPUT_STRING" = "" ]; then
INPUT_STRING=$SUGGESTED_VERSION
fi
fi
echo "Will set new version to be v$INPUT_STRING"
git checkout develop
git pull origin develop
git checkout -b "release-v$INPUT_STRING" develop
echo $INPUT_STRING > VERSION
echo "Version v$INPUT_STRING:" > tmpfile
git log --pretty=format:" - %s" "$BASE_STRING"..HEAD >> tmpfile
echo "" >> tmpfile
echo "" >> tmpfile
cat CHANGES >> tmpfile
mv tmpfile CHANGES
git add CHANGES VERSION
git commit -m "Version bump to v$INPUT_STRING"
git checkout master
git pull origin master
git merge --no-ff "release-v$INPUT_STRING" -m "Merge branch release-v$INPUT_STRING"
git tag -a -m "Tagging version v$INPUT_STRING" "v$INPUT_STRING"
git checkout develop
git merge --no-ff "release-v$INPUT_STRING" -m "Merge branch release-v$INPUT_STRING"
git branch -d "release-v$INPUT_STRING"
git push origin --all
git push origin --tags
else
echo "Could not find a VERSION file"
read -p "Do you want to create a version file and start from scratch? [y]" RESPONSE
if [ "$RESPONSE" = "" ]; then RESPONSE="y"; fi
if [ "$RESPONSE" = "Y" ]; then RESPONSE="y"; fi
if [ "$RESPONSE" = "Yes" ]; then RESPONSE="y"; fi
if [ "$RESPONSE" = "yes" ]; then RESPONSE="y"; fi
if [ "$RESPONSE" = "YES" ]; then RESPONSE="y"; fi
if [ "$RESPONSE" = "y" ]; then
git checkout develop
git pull origin develop
git checkout -b release-v0.1.0 develop
echo "0.1.0" > VERSION
echo "Version v0.1.0" > CHANGES
git log --pretty=format:" - %s" >> CHANGES
echo "" >> CHANGES
echo "" >> CHANGES
git add VERSION CHANGES
git commit -m "Added VERSION and CHANGES files, Version bump to v0.1.0"
git checkout master
git pull origin master
git merge --no-ff release-v0.1.0 -m "Merge branch release-v0.1.0"
git tag -a -m "Tagging version v0.1.0" "v0.1.0"
git checkout develop
git merge --no-ff release-v0.1.0 -m "Merge branch release-v0.1.0"
git branch -d release-v0.1.0
git push origin --all
git push origin --tags
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment