pulumi/scripts/get-version
Matt Ellis 1e2f4107c4 Reset patch to 0 when generating a version for a minor update
This fixes an issue we didn't forsee. If we do a patch release by
tagging a commit as `v1.1.1`, the next version we generate will be
`v1.2.1-alpha.....` which is not what we want. When bumping the minor
version, we should reset the patch version to zero, regardless of what
it is.
2019-09-13 15:50:38 -07:00

60 lines
1.8 KiB
Bash
Executable file

#!/bin/bash
set -o nounset -o errexit -o pipefail
SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
COMMITISH=${1:-HEAD}
DIRTY_TAG=""
# Figure out if the worktree is dirty, we run update-index first
# as we've seen cases in Travis where not doing so causes git to
# treat the worktree as dirty when it is not.
git update-index -q --refresh
if ! git diff-files --quiet; then
DIRTY_TAG="dirty"
fi
# If we have an exact tag, just use it.
if git describe --tags --exact-match "${COMMITISH}" >/dev/null 2>&1; then
echo -n "$(git describe --tags --exact-match "${COMMITISH}")"
if [ ! -z "${DIRTY_TAG}" ]; then
echo -n "+${DIRTY_TAG}"
fi
echo ""
exit 0
fi
# Otherwise, increment the minor version version (if the package is 1.X or later) or the
# patch version (if the package is pre 1.0), add the -alpha tag and some
# commit metadata. If there's no existing tag, pretend a v0.0.0 was
# there so we'll produce v0.0.1-dev builds.
if git describe --tags --abbrev=0 "${COMMITISH}" > /dev/null 2>&1; then
TAG=$(git describe --tags --abbrev=0 "${COMMITISH}")
else
TAG="v0.0.0"
fi
# Strip off any pre-release tag we might have (e.g. from doing a -rc build)
TAG=${TAG%%-*}
MAJOR=$(cut -d. -f1 <<< "${TAG}")
MINOR=$(cut -d. -f2 <<< "${TAG}")
PATCH=$(cut -d. -f3 <<< "${TAG}")
if [ "${MAJOR}" = "v0" ]; then
PATCH=$((${PATCH}+1))
else
MINOR=$((${MINOR}+1))
PATCH=0
fi
# We want to include some additional information. To the base tag we
# add a timestamp and commit hash. We use the timestamp of the commit
# itself, not the date it was authored (so it will change when someone
# rebases a PR into master, for example).
echo -n "${MAJOR}.${MINOR}.${PATCH}-alpha.$(git show -s --format='%ct+g%h' ${COMMITISH})"
if [ ! -z "${DIRTY_TAG}" ]; then
echo -n ".${DIRTY_TAG}"
fi
echo ""