sigs.k8s.io/cluster-api-provider-azure@v1.14.3/hack/print-workspace-status.sh (about) 1 #!/bin/bash 2 # Copyright 2018 The Kubernetes Authors. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 set -o errexit 17 set -o nounset 18 set -o pipefail 19 20 GIT_COMMIT="$(git describe --always --dirty --abbrev=14)" 21 22 if git_status=$(git status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then 23 GIT_TREE_STATE="clean" 24 else 25 GIT_TREE_STATE="dirty" 26 fi 27 28 # stolen from k8s.io/hack/lib/version.sh 29 # Use git describe to find the version based on tags. 30 if GIT_VERSION=$(git describe --tags --abbrev=14 2>/dev/null); then 31 # This translates the "git describe" to an actual semver.org 32 # compatible semantic version that looks something like this: 33 # v1.1.0-alpha.0.6+84c76d1142ea4d 34 # 35 # TODO: We continue calling this "git version" because so many 36 # downstream consumers are expecting it there. 37 # shellcheck disable=SC2001 38 DASHES_IN_VERSION=$(echo "${GIT_VERSION}" | sed "s/[^-]//g") 39 if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then 40 # We have distance to subversion (v1.1.0-subversion-1-gCommitHash) 41 # shellcheck disable=SC2001 42 GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\-\2/") 43 elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then 44 # We have distance to base tag (v1.1.0-1-gCommitHash) 45 # shellcheck disable=SC2001 46 GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/-\1/") 47 fi 48 if [[ "${GIT_TREE_STATE}" == "dirty" ]]; then 49 # git describe --dirty only considers changes to existing files, but 50 # that is problematic since new untracked .go files affect the build, 51 # so use our idea of "dirty" from git status instead. 52 GIT_VERSION+="-dirty" 53 fi 54 55 56 # Try to match the "git describe" output to a regex to try to extract 57 # the "major" and "minor" versions and whether this is the exact tagged 58 # version or whether the tree is between two tagged versions. 59 if [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then 60 GIT_MAJOR=${BASH_REMATCH[1]} 61 GIT_MINOR=${BASH_REMATCH[2]} 62 fi 63 64 # If GIT_VERSION is not a valid Semantic Version, then refuse to build. 65 if ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then 66 echo "GIT_VERSION should be a valid Semantic Version. Current value: ${GIT_VERSION}" 67 echo "Please see more details here: https://semver.org" 68 exit 1 69 fi 70 fi 71 72 GIT_BRANCH=$(git branch | grep '\*' | cut -d ' ' -f2) 73 GIT_RELEASE_TAG=$(git describe --abbrev=0 --tags) 74 GIT_RELEASE_COMMIT=$(git rev-list -n 1 "${GIT_RELEASE_TAG}" | head -c 14) 75 76 cat <<EOF 77 GIT_COMMIT ${GIT_COMMIT-} 78 GIT_TREE_STATE ${GIT_TREE_STATE-} 79 GIT_MAJOR ${GIT_MAJOR-} 80 GIT_MINOR ${GIT_MINOR-} 81 GIT_VERSION ${GIT_VERSION-} 82 GIT_BRANCH ${GIT_BRANCH-} 83 GIT_RELEASE_TAG ${GIT_RELEASE_TAG-} 84 GIT_RELEASE_COMMIT ${GIT_RELEASE_COMMIT-} 85 EOF