Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # If run inside a git repository will return a valid semver based on
  4. # the semver formatted tags. For example if the current HEAD is tagged
  5. # at 0.0.1, then the version echoed will simply be 0.0.1. However, if
  6. # the tag is say, 3 patches behind, the tag will be in the form
  7. # `0.0.1+build.3.0ace960`. This is basically, the current tag a
  8. # monotonically increasing commit (the number of commits since the
  9. # tag, and then a git short ref to identify the commit.
  10. #
  11. # You may also pass this script the a `release` argument. If that is
  12. # the case it will exit with a non-zero value if the current head is
  13. # not tagged.
  14. #
  15. set -e
  16.  
  17. commit_count=
  18. version_candidate=
  19. version_tag=
  20. vsn=
  21.  
  22. get-version-candidate()
  23. {
  24. local ver_regex='tag: (v([^,\)]+)|([0-9]+(\.[0-9]+)*))'
  25. local tag_lines=`git log --oneline --decorate | fgrep "tag: "`
  26.  
  27. if [[ $tag_lines =~ $ver_regex ]]; then
  28. if [[ ${BASH_REMATCH[1]:0:1} = "v" ]]; then
  29. version_tag=${BASH_REMATCH[1]}
  30. version_candidate=${BASH_REMATCH[2]}
  31. else
  32. version_tag=${BASH_REMATCH[3]}
  33. version_candidate=${BASH_REMATCH[3]}
  34. fi
  35. else
  36. version_tag=""
  37. version_candidate="0.0.0"
  38. fi
  39. }
  40.  
  41. get-commit-count()
  42. {
  43. if [[ $version_tag = "" ]]; then
  44. commit_count=`git rev-list HEAD | wc -l`
  45. else
  46. commit_count=`git rev-list ${version_tag}..HEAD | wc -l`
  47. fi
  48. commit_count=`echo $commit_count | tr -d ' 't`
  49. }
  50.  
  51. build-version()
  52. {
  53. if [[ $commit_count = 0 ]]; then
  54. vsn=$version_candidate
  55. else
  56. local ref=`git log -n 1 --pretty=format:'%h'`
  57. vsn="${version_candidate}+build.${commit_count}.${ref}"
  58. fi
  59. }
  60.  
  61. check-tag()
  62. {
  63. if [[ "$1" = "release" ]]; then
  64. if [[ $commit_count != 0 ]]; then
  65. echo "The current head *must* be tagged"
  66. exit 127
  67. fi
  68. fi
  69. }
  70.  
  71. get-version-candidate
  72. get-commit-count
  73. build-version
  74. check-tag "$1"
  75.  
  76. echo $vsn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement