Skip to content

Instantly share code, notes, and snippets.

@paulormart
Last active April 28, 2021 14:13
Show Gist options
  • Save paulormart/e8c8e659f78d0ef6a497f22b41d814f9 to your computer and use it in GitHub Desktop.
Save paulormart/e8c8e659f78d0ef6a497f22b41d814f9 to your computer and use it in GitHub Desktop.
Version script for Cargo Rust projects
#!/bin/bash
#
# > 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
CURRENT_VERSION=$(cargo pkgid | cut -d# -f2 | cut -d: -f2)
BASE_LIST=(`echo $CURRENT_VERSION | tr '.' ' '`)
V_MAJOR=${BASE_LIST[0]}
V_MINOR=${BASE_LIST[1]}
V_PATCH=${BASE_LIST[2]}
echo "Current version : $CURRENT_VERSION"
V_MINOR=$((V_MINOR + 1))
V_PATCH=0
NEXT_VERSION="$V_MAJOR.$V_MINOR.$V_PATCH"
# read first argument as auto
if [ "$1" = "auto" ]; then
INPUT_VERSION=$NEXT_VERSION
else
read -p "Enter a version number [$NEXT_VERSION]: " INPUT_VERSION
if [ "$INPUT_VERSION" = "" ]; then
INPUT_VERSION=$NEXT_VERSION
fi
fi
echo "Will set new version to be v$INPUT_VERSION"
git checkout main
git pull origin main
git checkout -b "release-v$INPUT_VERSION" main
change Cargo.toml version
CURRENT="version = \"$CURRENT_VERSION\""
NEXT="version = \"$INPUT_VERSION\""
awk "{sub($CURRENT, $NEXT)}1" Cargo.toml > tmpfile && mv tmpfile Cargo.toml
git add Cargo.toml
git commit -m "Version bump to v$INPUT_VERSION"
git checkout main
git merge --no-ff "release-v$INPUT_VERSION" -m "Merge branch release-v$INPUT_VERSION"
git tag -a -m "Tagging version v$INPUT_VERSION" "v$INPUT_VERSION"
git branch -d "release-v$INPUT_VERSION"
git push origin --all
git push origin --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment