version 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. function version::get_version_vars() {
  3. BUILD_DATE=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
  4. GIT_TREE_STATE="unknown"
  5. GIT_COMMIT="unknown"
  6. GIT_VERSION="unknown"
  7. # get the git tree state if the source was exported through git archive.
  8. # shellcheck disable=SC2016,SC2050
  9. if [[ '$Format:%%$' == "%" ]]; then
  10. GIT_TREE_STATE="archive"
  11. GIT_COMMIT='$Format:%H$'
  12. # when a 'git archive' is exported, the '$Format:%D$' below will look
  13. # something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: '
  14. # can be extracted from it.
  15. if [[ '$Format:%D$' =~ tag:\ (v[^ ,]+) ]]; then
  16. GIT_VERSION="${BASH_REMATCH[1]}"
  17. else
  18. GIT_VERSION="${GIT_COMMIT:0:7}"
  19. fi
  20. # respect specified version.
  21. GIT_VERSION="${VERSION:-${GIT_VERSION}}"
  22. return
  23. fi
  24. # return directly if not found git client.
  25. if [[ -z "$(command -v git)" ]]; then
  26. # respect specified version.
  27. GIT_VERSION=${VERSION:-${GIT_VERSION}}
  28. return
  29. fi
  30. # find out git info via git client.
  31. if GIT_COMMIT=$(git rev-parse "HEAD^{commit}" 2>/dev/null); then
  32. # specify as dirty if the tree is not clean.
  33. if git_status=$(git status --porcelain 2>/dev/null) && [[ -n ${git_status} ]]; then
  34. GIT_TREE_STATE="dirty"
  35. else
  36. GIT_TREE_STATE="clean"
  37. fi
  38. # specify with the tag if the head is tagged.
  39. if GIT_VERSION="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"; then
  40. if git_tag=$(git tag -l --contains HEAD 2>/dev/null | head -n 1 2>/dev/null) && [[ -n ${git_tag} ]]; then
  41. GIT_VERSION="${git_tag}"
  42. fi
  43. fi
  44. # specify to dev if the tree is dirty.
  45. if [[ "${GIT_TREE_STATE:-dirty}" == "dirty" ]]; then
  46. GIT_VERSION="dev"
  47. elif ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
  48. GIT_VERSION="dev"
  49. fi
  50. # respect specified version
  51. GIT_VERSION=${VERSION:-${GIT_VERSION}}
  52. fi
  53. }