|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +if [[ -n ${GIT_COMMIT-} ]] || GIT_COMMIT=$(git rev-parse "HEAD^{commit}" 2>/dev/null); then |
| 5 | + if [[ -z ${GIT_TREE_STATE-} ]]; then |
| 6 | + # Check if the tree is dirty. default to dirty |
| 7 | + if git_status=$(git status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then |
| 8 | + GIT_TREE_STATE="clean" |
| 9 | + else |
| 10 | + GIT_TREE_STATE="dirty" |
| 11 | + fi |
| 12 | + fi |
| 13 | + |
| 14 | + # Use git describe to find the version based on tags. |
| 15 | + if [[ -n ${GIT_VERSION-} ]] || GIT_VERSION=$(git describe --tags --abbrev=14 "${GIT_COMMIT}^{commit}" 2>/dev/null); then |
| 16 | + # This translates the "git describe" to an actual semver.org |
| 17 | + # compatible semantic version that looks something like this: |
| 18 | + # v1.0.0-beta.0.10+4c183422345d8f |
| 19 | + # |
| 20 | + # downstream consumers are expecting it there. |
| 21 | + DASHES_IN_VERSION=$(echo "${GIT_VERSION}" | sed "s/[^-]//g") |
| 22 | + if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then |
| 23 | + # We have distance to subversion (v1.1.0-subversion-1-gCommitHash) |
| 24 | + GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\+\2/") |
| 25 | + elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then |
| 26 | + # We have distance to base tag (v1.1.0-1-gCommitHash) |
| 27 | + GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/") |
| 28 | + fi |
| 29 | + if [[ "${GIT_TREE_STATE}" == "dirty" ]]; then |
| 30 | + # git describe --dirty only considers changes to existing files, but |
| 31 | + # that is problematic since new untracked .go files affect the build, |
| 32 | + # so use our idea of "dirty" from git status instead. |
| 33 | + GIT_VERSION+="-dirty" |
| 34 | + fi |
| 35 | + |
| 36 | + |
| 37 | + # If GIT_VERSION is not a valid Semantic Version, then refuse to build. |
| 38 | + if ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then |
| 39 | + echo "GIT_VERSION should be a valid Semantic Version. Current value: ${GIT_VERSION}" |
| 40 | + echo "Please see more details here: https://semver.org" |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + fi |
| 44 | +fi |
| 45 | + |
| 46 | +echo "-X 'github.com/aylei/kubectl-debug/version.gitVersion=${GIT_VERSION}'" |
0 commit comments