Guest User

Untitled

a guest
Feb 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #!/usr/bin/sh
  2. # git-distance-based SEMVER
  3. #
  4. # Optional Flag: -t to cause the script to actually tag the github repo
  5. # Using -t will cause the original behavior of Jerry's version.
  6. # Not using -t will not touch the github repo or tags. Just will set the VERSION output and VERSION file
  7. #
  8. # Optional Argument: path to VERSION file. Defaults to VERSION in the current director
  9.  
  10. # major and minor, once released, are designated by add RELEASE-vx.y tag and the patch will be derived based on distance to that tag
  11. # if we don't find such a tag, v0.0 is assumed and we we'll use the distance to FIRST_COMMIT
  12. #
  13. # for master branch, it will tag it like "v0.1.5"
  14. # for other branches, e.g. feature-xyz, it will tag it like "feature-xyz-v0.1.5
  15. #
  16.  
  17. # The VERSION output will be just the SEMVER with no v prefix so that other
  18. # processes can use just the version. But a v will be appended to the git tag
  19. # that is created
  20. #
  21.  
  22. # REQUIREMENTS:
  23. # 1. Have the FIRST_COMMIT tag created on, well, the first commit.
  24. # This gives us a reference point. (the reason we may not figure out by doing "git rev-list" is that in some situations,
  25. # our repo is a shallowed git clone)
  26. # 2. Repo being operated on needs to be writeable by the process running this script. Having mist-ci-bot associated with the repo will works
  27. #
  28.  
  29. function parse_git_dirty {
  30. [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working tree clean" ]] && echo "*"
  31. }
  32.  
  33. while getopts ":t" opt; do
  34. case $opt in
  35. t)
  36. DO_GIT_TAG=1
  37. shift $((OPTIND-1))
  38. ;;
  39. \?)
  40. echo "Invalid option: -$OPTARG" >&2
  41. exit 1
  42. ;;
  43. esac
  44. done
  45.  
  46. if [ -z "$1" ]; then
  47. VERSION_FILE=VERSION
  48. else
  49. VERSION_FILE=$1
  50. fi
  51.  
  52.  
  53. #if [ "`git rev-list HEAD | tail -n 1`" != "`git log FIRST_COMMIT --format=%H`" ] ; then git fetch --unshallow ; fi
  54. git fetch --prune --tags
  55.  
  56. # looking for the nearest RELEASE-vx.y tag and extract vx.y
  57. MAJORMINOR=$(git rev-list HEAD --pretty="format:%d" | grep -v commit | sed -n 's/.*RELEASE-v\([0-9]*\.[0-9]*\).*/\1/p' | head -n 1)
  58. # use this RELEASE-vx.y to calculate the distance
  59. if [ "$MAJORMINOR" = "" ] ; then
  60. MAJORMINOR="0.0"
  61. DISTANCE=$(git rev-list FIRST_COMMIT..HEAD --count)
  62. else
  63. DISTANCE=$(git rev-list RELEASE-v$MAJORMINOR..HEAD --count)
  64. fi
  65.  
  66. # use SEMVER for master
  67. # master: v0.1.5
  68. # feature-xyz: feature-xyz-v0.1.5
  69. BRANCH=`git rev-parse --abbrev-ref HEAD`
  70. VERSION="${MAJORMINOR}.${DISTANCE}"
  71. if [ "${BRANCH}" != "master" ] ; then
  72. VERSION="${BRANCH}-${VERSION}"
  73. fi
  74.  
  75.  
  76. dirty=`parse_git_dirty`
  77. if [ "${dirty}" = "*" ]; then
  78. VERSION="${VERSION}-dirty"
  79. fi
  80.  
  81. echo "VERSION: ${VERSION}"
  82.  
  83. ## END of git-distance-based SEMVER¯
Add Comment
Please, Sign In to add comment