Skip to content

Instantly share code, notes, and snippets.

@eSlider
Created July 14, 2021 15:30
Show Gist options
  • Save eSlider/8baefbcbfdc1a3cb69e10840d23cfc2f to your computer and use it in GitHub Desktop.
Save eSlider/8baefbcbfdc1a3cb69e10840d23cfc2f to your computer and use it in GitHub Desktop.
semiver.sh
#!/bin/bash
# https://engineering.taboola.com/calculating-git-version/
# Maybe we would use https://gist.github.com/OleksandrKucherenko/9fb14f81a29b46886ccd63b774c5959f
# Depends on https://semver.org/spec/v2.0.0.html
branch=$(git rev-parse --abbrev-ref HEAD)
latest=$(git tag -l --merged master --sort='-*authordate' | head -n1)
semver_parts=(${latest//./ })
major=${semver_parts[0]}
minor=${semver_parts[1]}
patch=${semver_parts[2]}
version=""
case $branch in
"master")
version=${major}.${minor}.$((patch + 1))
;;
"feature/*")
branch_parts=(${latest//./ })
# echo $branch | grep -Pio "(feature|bug)/\K.+?$"
count=$(git rev-list HEAD ^${latest} --ancestry-path ${latest} --count)
version=${major}.${minor}.${patch}-${branch}-${count}
;;
*)
echo >&2 "unsupported branch type"
exit 1
;;
esac
echo "${version}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment