|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
| 3 | +# This script generates the version string used by Envbuilder, including for dev |
| 4 | +# versions. Note: the version returned by this script will NOT include the "v" |
| 5 | +# prefix that is included in the Git tag. |
| 6 | +# |
| 7 | +# If $ENVBUILDER_RELEASE is set to "true", the returned version will equal the |
| 8 | +# current git tag. If the current commit is not tagged, this will fail. |
| 9 | +# |
| 10 | +# If $ENVBUILDER_RELEASE is not set, the returned version will always be a dev |
| 11 | +# version. |
| 12 | + |
3 | 13 | set -euo pipefail |
4 | | -cd "$(dirname "${BASH_SOURCE[0]}")" |
| 14 | +# shellcheck source=scripts/lib.sh |
| 15 | +source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" |
| 16 | +cdroot |
| 17 | + |
| 18 | +if [[ -n "${ENVBUILDER_FORCE_VERSION:-}" ]]; then |
| 19 | + echo "${ENVBUILDER_FORCE_VERSION}" |
| 20 | + exit 0 |
| 21 | +fi |
| 22 | + |
| 23 | +# To make contributing easier, if there are no tags, we'll use a default |
| 24 | +# version. |
| 25 | +tag_list=$(git tag) |
| 26 | +if [[ -z ${tag_list} ]]; then |
| 27 | + log |
| 28 | + log "INFO(version.sh): It appears you've checked out a fork or shallow clone of Envbuilder." |
| 29 | + log "INFO(version.sh): By default GitHub does not include tags when forking." |
| 30 | + log "INFO(version.sh): We will use the default version 0.0.1 for this build." |
| 31 | + log "INFO(version.sh): To pull tags from upstream, use the following commands:" |
| 32 | + log "INFO(version.sh): - git remote add upstream https://github.com/coder/envbuilder.git" |
| 33 | + log "INFO(version.sh): - git fetch upstream" |
| 34 | + log |
| 35 | + last_tag="v0.0.1" |
| 36 | +else |
| 37 | + current_commit=$(git rev-parse HEAD) |
| 38 | + # Try to find the last tag that contains the current commit |
| 39 | + last_tag=$(git tag --contains "$current_commit" --sort=version:refname | head -n 1) |
| 40 | + # If there is no tag that contains the current commit, |
| 41 | + # get the latest tag sorted by semver. |
| 42 | + if [[ -z "${last_tag}" ]]; then |
| 43 | + last_tag=$(git tag --sort=version:refname | tail -n 1) |
| 44 | + fi |
| 45 | +fi |
| 46 | + |
| 47 | +version="${last_tag}" |
| 48 | + |
| 49 | +# If the HEAD has extra commits since the last tag then we are in a dev version. |
| 50 | +# |
| 51 | +# Dev versions are denoted by the "-dev+" suffix with a trailing commit short |
| 52 | +# SHA. |
| 53 | +if [[ "${ENVBUILDER_RELEASE:-}" == *t* ]]; then |
| 54 | + # $last_tag will equal `git describe --always` if we currently have the tag |
| 55 | + # checked out. |
| 56 | + if [[ "${last_tag}" != "$(git describe --always)" ]]; then |
| 57 | + # make won't exit on $(shell cmd) failures, so we have to kill it :( |
| 58 | + if [[ "$(ps -o comm= "${PPID}" || true)" == *make* ]]; then |
| 59 | + log "ERROR: version.sh: the current commit is not tagged with an annotated tag" |
| 60 | + kill "${PPID}" || true |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + |
| 64 | + error "version.sh: the current commit is not tagged with an annotated tag" |
| 65 | + fi |
| 66 | +else |
| 67 | + rev=$(git rev-parse --short HEAD) |
| 68 | + version="0.0.0+dev-${rev}" |
| 69 | + # If the git repo has uncommitted changes, mark the version string as 'dirty'. |
| 70 | + dirty_files=$(git ls-files --other --modified --exclude-standard) |
| 71 | + if [[ -n "${dirty_files}" ]]; then |
| 72 | + version+="-dirty" |
| 73 | + fi |
| 74 | +fi |
5 | 75 |
|
6 | | -last_tag="$(git describe --tags --abbrev=0)" |
7 | | -version="$last_tag" |
8 | 76 |
|
9 | 77 | # Remove the "v" prefix. |
10 | 78 | echo "${version#v}" |
0 commit comments