version.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env bash
  2. ##
  3. # Inspired by github.com/kubernetes/kubernetes/hack/lib/version.sh
  4. ##
  5. # -----------------------------------------------------------------------------
  6. # Version management helpers. These functions help to set the
  7. # following variables:
  8. #
  9. # GIT_TREE_STATE - "clean" indicates no changes since the git commit id.
  10. # "dirty" indicates source code changes after the git commit id.
  11. # "archive" indicates the tree was produced by 'git archive'.
  12. # "unknown" indicates cannot find out the git tree.
  13. # GIT_COMMIT - The git commit id corresponding to this
  14. # source code.
  15. # GIT_VERSION - "vX.Y" used to indicate the last release version,
  16. # it can be specified via "VERSION".
  17. # BUILD_DATE - The build date of the version.
  18. function gpustack::version::get_version_vars() {
  19. #shellcheck disable=SC2034
  20. BUILD_DATE=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
  21. GIT_TREE_STATE="unknown"
  22. GIT_COMMIT="unknown"
  23. GIT_VERSION="unknown"
  24. # get the git tree state if the source was exported through git archive.
  25. # shellcheck disable=SC2016,SC2050
  26. if [[ '$Format:%%$' == "%" ]]; then
  27. GIT_TREE_STATE="archive"
  28. GIT_COMMIT='$Format:%H$'
  29. # when a 'git archive' is exported, the '$Format:%D$' below will look
  30. # something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: '
  31. # can be extracted from it.
  32. if [[ '$Format:%D$' =~ tag:\ (v[^ ,]+) ]]; then
  33. GIT_VERSION="${BASH_REMATCH[1]}"
  34. else
  35. GIT_VERSION="${GIT_COMMIT:0:7}"
  36. fi
  37. # respect specified version.
  38. GIT_VERSION="${VERSION:-${GIT_VERSION}}"
  39. return
  40. fi
  41. # return directly if not found git client.
  42. if [[ -z "$(command -v git)" ]]; then
  43. # respect specified version.
  44. GIT_VERSION=${VERSION:-${GIT_VERSION}}
  45. return
  46. fi
  47. # find out git info via git client.
  48. if GIT_COMMIT=$(git rev-parse "HEAD^{commit}" 2>/dev/null); then
  49. # specify as dirty if the tree is not clean.
  50. if git_status=$(git status --porcelain 2>/dev/null) && [[ -n ${git_status} ]]; then
  51. GIT_TREE_STATE="dirty"
  52. else
  53. GIT_TREE_STATE="clean"
  54. fi
  55. # specify with the tag if the head is tagged.
  56. if GIT_VERSION="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"; then
  57. if git_tag=$(git tag -l --contains HEAD 2>/dev/null | head -n 1 2>/dev/null) && [[ -n ${git_tag} ]]; then
  58. GIT_VERSION="${git_tag}"
  59. fi
  60. fi
  61. # specify to v0.0.0 if the tree is dirty.
  62. if [[ "${GIT_TREE_STATE:-dirty}" == "dirty" ]]; then
  63. echo "GIT Tree is dirty"
  64. echo "======= GIT STATUS ======="
  65. git status --short
  66. echo "======= GIT DIFF ======="
  67. git --no-pager diff
  68. GIT_VERSION="v0.0.0"
  69. elif ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-?[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
  70. GIT_VERSION="v0.0.0"
  71. fi
  72. # respect specified version
  73. GIT_VERSION=${VERSION:-${GIT_VERSION}}
  74. fi
  75. }