pulumi/scripts/get-version
Matt Ellis 5c4a31f692 Adopt new version strategy
Our previous strategy of just using `git describe --tags --dirty` to
compute a version caused issues. The major one was that since version
sort lexigrapically, git's strategy of having a commit count without
leading zeros lead to cases where 0.11.0-dev-9 was "newer than"
0.11.0-dev-10 which is not what you want at all.

With this change, we compute a version by first seeing if the commit
is tagged, and if so, we use that tag. Otherwise, we take the closest
tag and to it append the unix timestamp of the commit and then append
a git hash.

Because we use the commit timestamp, things will sort correctly again.

Part of pulumi/home#174
2018-03-15 18:06:04 -07:00

26 lines
1,008 B
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 the commit in question has a tag applied to it directly, use it.
if git describe --tags --exact-match "${COMMITISH}" >/dev/null 2>&1; then
echo "$(git describe --tags --exact-match "${COMMITISH}")${DIRTY_TAG}"
exit 0
fi
# Otherwise we don't have an exact tag, so produce one that is the
# base tag, plus 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 "$(git describe --tags --abbrev=0 ${COMMITISH})-$(git show -s --format='%ct-g%h' ${COMMITISH})${DIRTY_TAG}"