You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

version.sh 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env bash
  2. # Copyright © 2023 OpenIM. All rights reserved.
  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. # Version management helpers. These functions help to set, save and load the
  17. # following variables:
  18. #
  19. # OPENIM_GIT_COMMIT - The git commit id corresponding to this
  20. # source code.
  21. # OPENIM_GIT_TREE_STATE - "clean" indicates no changes since the git commit id
  22. # "dirty" indicates source code changes after the git commit id
  23. # "archive" indicates the tree was produced by 'git archive'
  24. # OPENIM_GIT_VERSION - "vX.Y" used to indicate the last release version.
  25. # OPENIM_GIT_MAJOR - The major part of the version
  26. # OPENIM_GIT_MINOR - The minor component of the version
  27. # Grovels through git to set a set of env variables.
  28. #
  29. # If OPENIM_GIT_VERSION_FILE, this function will load from that file instead of
  30. # querying git.
  31. openim::version::get_version_vars() {
  32. if [[ -n ${OPENIM_GIT_VERSION_FILE-} ]]; then
  33. openim::version::load_version_vars "${OPENIM_GIT_VERSION_FILE}"
  34. return
  35. fi
  36. # If the iamrnetes source was exported through git archive, then
  37. # we likely don't have a git tree, but these magic values may be filled in.
  38. # shellcheck disable=SC2016,SC2050
  39. # Disabled as we're not expanding these at runtime, but rather expecting
  40. # that another tool may have expanded these and rewritten the source (!)
  41. if [[ '$Format:%%$' == "%" ]]; then
  42. OPENIM_GIT_COMMIT='$Format:%H$'
  43. OPENIM_GIT_TREE_STATE="archive"
  44. # When a 'git archive' is exported, the '$Format:%D$' below will look
  45. # something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: '
  46. # can be extracted from it.
  47. if [[ '$Format:%D$' =~ tag:\ (v[^ ,]+) ]]; then
  48. OPENIM_GIT_VERSION="${BASH_REMATCH[1]}"
  49. fi
  50. fi
  51. local git=(git --work-tree "${OPENIM_ROOT}")
  52. if [[ -n ${OPENIM_GIT_COMMIT-} ]] || OPENIM_GIT_COMMIT=$("${git[@]}" rev-parse "HEAD^{commit}" 2>/dev/null); then
  53. if [[ -z ${OPENIM_GIT_TREE_STATE-} ]]; then
  54. # Check if the tree is dirty. default to dirty
  55. if git_status=$("${git[@]}" status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
  56. OPENIM_GIT_TREE_STATE="clean"
  57. else
  58. OPENIM_GIT_TREE_STATE="dirty"
  59. fi
  60. fi
  61. # Use git describe to find the version based on tags.
  62. if [[ -n ${OPENIM_GIT_VERSION-} ]] || OPENIM_GIT_VERSION=$("${git[@]}" describe --tags --always --match='v*' 2>/dev/null); then
  63. # This translates the "git describe" to an actual semver.org
  64. # compatible semantic version that looks something like this:
  65. # v1.1.0-alpha.0.6+84c76d1142ea4d
  66. #
  67. # TODO: We continue calling this "git version" because so many
  68. # downstream consumers are expecting it there.
  69. #
  70. # These regexes are painful enough in sed...
  71. # We don't want to do them in pure shell, so disable SC2001
  72. # shellcheck disable=SC2001
  73. DASHES_IN_VERSION=$(echo "${OPENIM_GIT_VERSION}" | sed "s/[^-]//g")
  74. if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then
  75. # shellcheck disable=SC2001
  76. # We have distance to subversion (v1.1.0-subversion-1-gCommitHash)
  77. OPENIM_GIT_VERSION=$(echo "${OPENIM_GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\+\2/")
  78. elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then
  79. # shellcheck disable=SC2001
  80. # We have distance to base tag (v1.1.0-1-gCommitHash)
  81. OPENIM_GIT_VERSION=$(echo "${OPENIM_GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/")
  82. fi
  83. if [[ "${OPENIM_GIT_TREE_STATE}" == "dirty" ]]; then
  84. # git describe --dirty only considers changes to existing files, but
  85. # that is problematic since new untracked .go files affect the build,
  86. # so use our idea of "dirty" from git status instead.
  87. # TODO?
  88. #OPENIM_GIT_VERSION+="-dirty"
  89. :
  90. fi
  91. # Try to match the "git describe" output to a regex to try to extract
  92. # the "major" and "minor" versions and whether this is the exact tagged
  93. # version or whether the tree is between two tagged versions.
  94. if [[ "${OPENIM_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then
  95. OPENIM_GIT_MAJOR=${BASH_REMATCH[1]}
  96. OPENIM_GIT_MINOR=${BASH_REMATCH[2]}
  97. if [[ -n "${BASH_REMATCH[4]}" ]]; then
  98. OPENIM_GIT_MINOR+="+"
  99. fi
  100. fi
  101. # If OPENIM_GIT_VERSION is not a valid Semantic Version, then refuse to build.
  102. if ! [[ "${OPENIM_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
  103. echo "OPENIM_GIT_VERSION should be a valid Semantic Version. Current value: ${OPENIM_GIT_VERSION}"
  104. echo "Please see more details here: https://semver.org"
  105. exit 1
  106. fi
  107. fi
  108. fi
  109. }
  110. # Saves the environment flags to $1
  111. openim::version::save_version_vars() {
  112. local version_file=${1-}
  113. [[ -n ${version_file} ]] || {
  114. echo "!!! Internal error. No file specified in openim::version::save_version_vars"
  115. return 1
  116. }
  117. cat <<EOF >"${version_file}"
  118. OPENIM_GIT_COMMIT='${OPENIM_GIT_COMMIT-}'
  119. OPENIM_GIT_TREE_STATE='${OPENIM_GIT_TREE_STATE-}'
  120. OPENIM_GIT_VERSION='${OPENIM_GIT_VERSION-}'
  121. OPENIM_GIT_MAJOR='${OPENIM_GIT_MAJOR-}'
  122. OPENIM_GIT_MINOR='${OPENIM_GIT_MINOR-}'
  123. EOF
  124. }
  125. # Loads up the version variables from file $1
  126. openim::version::load_version_vars() {
  127. local version_file=${1-}
  128. [[ -n ${version_file} ]] || {
  129. echo "!!! Internal error. No file specified in openim::version::load_version_vars"
  130. return 1
  131. }
  132. source "${version_file}"
  133. }