pre-commit 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env bash
  2. # Copyright The OpenTelemetry Authors
  3. # SPDX-License-Identifier: Apache-2.0
  4. set -eo pipefail
  5. if [[ ! -w "$(pwd)/sdk/src/version/version.cc" && ! -w "$(pwd)/api/include/opentelemetry/version.h" ]]; then
  6. echo "Error: Version file(s) are not writable. Check permissions and try again."
  7. exit 1
  8. fi
  9. # format: "v<MAJOR>.<MINOR>.<PATCH>-<PRERELEASE>+<BUILDMETADATA>-<NUMBER_OF_NEW_COMMITS>-g<LAST_COMMIT_HASH>"
  10. semver_regex="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-([0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*))?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?-([0-9]+)-g([0-9|a-z]+)$"
  11. git_tag=$(git describe --tags --long 2>/dev/null) || true
  12. if [[ ! -z $git_tag ]] && [[ $git_tag =~ $semver_regex ]]; then
  13. major="${BASH_REMATCH[1]}"
  14. minor="${BASH_REMATCH[2]}"
  15. patch="${BASH_REMATCH[3]}"
  16. pre_release="${BASH_REMATCH[5]}" #optional
  17. build_metadata="${BASH_REMATCH[7]}" #optional
  18. count_new_commits="${BASH_REMATCH[9]}"
  19. latest_commit_hash="${BASH_REMATCH[10]}"
  20. if [[ -z ${major} ]] || [[ -z ${minor} ]] || [[ -z ${patch} ]] || [[ -z ${count_new_commits} ]] || [[ -z ${latest_commit_hash} ]]; then
  21. echo "Error: Incorrect tag format received. Exiting.."
  22. exit 1
  23. fi
  24. else
  25. major=0 && minor=0 && patch=0 && pre_release="" && build_metadata="" && count_new_commits=0
  26. latest_commit_hash="$(git rev-parse --short HEAD)"
  27. fi
  28. : ${pre_release:="NONE"} # use default string if not defined
  29. : ${build_metadata:="NONE"} # use default string if not defined
  30. latest_commit_hash=$(git rev-parse ${latest_commit_hash}) # get full hash from short
  31. if [[ -z ${latest_commit_hash} ]]; then
  32. echo "Error: Incorrect short hash received. Exiting.."
  33. exit 1
  34. fi
  35. short_version="${major}.${minor}.${patch}"
  36. full_version="${short_version}-${pre_release}-${build_metadata}"
  37. # Update api version.h
  38. sed -i "/^\#define OPENTELEMETRY_VERSION /c\#define OPENTELEMETRY_VERSION \"${short_version}\"" "$(pwd)/api/include/opentelemetry/version.h"
  39. sed -i "/^\#define OPENTELEMETRY_VERSION_MAJOR /c\#define OPENTELEMETRY_VERSION_MAJOR ${major}" "$(pwd)/api/include/opentelemetry/version.h"
  40. sed -i "/^\#define OPENTELEMETRY_VERSION_MINOR /c\#define OPENTELEMETRY_VERSION_MINOR ${minor}" "$(pwd)/api/include/opentelemetry/version.h"
  41. sed -i "/^\#define OPENTELEMETRY_VERSION_PATCH /c\#define OPENTELEMETRY_VERSION_PATCH ${patch}" "$(pwd)/api/include/opentelemetry/version.h"
  42. git add "$(pwd)/api/include/opentelemetry/version.h"
  43. # Update sdk version.cc
  44. cat > "$(pwd)/sdk/src/version/version.cc" <<END
  45. // Copyright The OpenTelemetry Authors
  46. // SPDX-License-Identifier: Apache-2.0
  47. // Please DONOT touch this file.
  48. // Any changes done here would be overwritten by pre-commit git hook
  49. #include "opentelemetry/sdk/version/version.h"
  50. OPENTELEMETRY_BEGIN_NAMESPACE
  51. namespace sdk
  52. {
  53. namespace version
  54. {
  55. const int major_version = ${major};
  56. const int minor_version = ${minor};
  57. const int patch_version = ${patch};
  58. const char *pre_release = "${pre_release}";
  59. const char *build_metadata = "${build_metadata}";
  60. const char *short_version = "${short_version}";
  61. const char *full_version = "${full_version}";
  62. const char *build_date = "$(date -u)";
  63. } // namespace version
  64. } // namespace sdk
  65. OPENTELEMETRY_END_NAMESPACE
  66. END
  67. git add "$(pwd)/sdk/src/version/version.cc"
  68. # Update documentation version
  69. sed -i "/^release =/crelease = \"${short_version}\"" "$(pwd)/docs/public/conf.py"
  70. git add "$(pwd)/docs/public/conf.py"