Skip to content

Instantly share code, notes, and snippets.

@jan-swiecki
Created August 27, 2019 13:48
Show Gist options
  • Save jan-swiecki/d0701383c6c5cc8b7822d92acca4584b to your computer and use it in GitHub Desktop.
Save jan-swiecki/d0701383c6c5cc8b7822d92acca4584b to your computer and use it in GitHub Desktop.
local.sh
#!/bin/bash
set -eo pipefail
help () {
cat <<EOF
Usage: ./local.bash <action>
Actions
get_version print current version
bump_version bumps version
EOF
}
current_branch="$(git rev-parse --abbrev-ref HEAD)"
get_version () {
# <git describe>-<date in UTC>
echo "$(git describe --tags --first-parent)-$(date -u "+%Y%m%dT%H%M%S")"
}
# Check if on current_branch there is only one commit to be rebased
# that is authored by me
check_safe_rebase () {
git_email="$(git config user.email)"
git fetch origin "$current_branch" >/dev/null 2>&1
commits_behind="$(git log "origin/$current_branch".."$current_branch" --pretty=oneline | wc -l)"
commits_ahead="$(git log "$current_branch".."origin/$current_branch" --pretty=oneline | wc -l)"
if [ "$commits_ahead" -gt 0 ]; then
echo "check_safe_rebase: fatal: there are $commits_ahead between $current_branch and origin/$current_branch" >&2
exit 1
fi
}
bump_version () (
# works with a file called VERSION in the current directory,
# the contents of which should be a semantic version number
# such as "1.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.
# if [ "$current_branch" != "master" ]; then
# echo "Must be on master branch" >&2
# exit 1
# fi
check_safe_rebase
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"
if [ "$1" == "patch" -o -z "$1" ]; then
V_PATCH=$((V_PATCH + 1))
elif [ "$1" == "minor" ]; then
V_MINOR=$((V_MINOR + 1))
V_PATCH=0
elif [ "$1" == "major" ]; then
V_MAJOR=$((V_MAJOR + 1))
V_MINOR=0
V_PATCH=0
elif [ ! -z "$1" ]; then
NEW_VERSION="$1"
fi
if [ -z "$NEW_VERSION" ]; then
NEW_VERSION="$V_MAJOR.$V_MINOR.$V_PATCH"
fi
INPUT_STRING=$NEW_VERSION
echo "Will set new version to be $INPUT_STRING"
read -p "This will update current commit and push force new version with tags. Confirm? [y]" RESP
if [ "$RESP" != "y" ]; then
exit
fi
echo $INPUT_STRING > VERSION
echo "Version $INPUT_STRING:" > tmpfile
git log --pretty=format:" - %s" "v$BASE_STRING"...HEAD >> tmpfile
echo "" >> tmpfile
echo "" >> tmpfile
cat CHANGES >> tmpfile
mv tmpfile CHANGES
git add CHANGES VERSION
git commit --amend -m "$(git log -1 --pretty=%B)"
git tag "v$INPUT_STRING"
git push origin +master --tags
else
echo "Could not find a VERSION file"
read -p "Do you want to create a version file and start from scratch? [y]" RESP
if [ "$RESP" != "y" ]; then
exit
fi
read -p "Set current version: " RESP
INPUT_STRING="$RESP"
echo $INPUT_STRING > VERSION
echo "Version $INPUT_STRING:" > CHANGES
git log --pretty=format:" - %s" >> CHANGES
echo "" >> CHANGES
echo "" >> CHANGES
TAG="v$INPUT_STRING"
set -x
cat VERSION
cat CHANGES
read -p "Ammend and add tag $TAG? [y]" RESP
if [ "$RESP" != "y" ]; then
echo "Abort"
exit 1
fi
git add CHANGES VERSION
git commit --amend -m "$(git log -1 --pretty=%B)"
git tag "$TAG"
git push origin +master --tags
fi
)
if [ -z "$1" ]; then
help
else
"$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment