1#!/usr/bin/env bash
2
3set -e
4
5# parse the current git commit hash
6COMMIT=`git rev-parse HEAD`
7
8# check if the current commit has a matching tag
9TAG=$(git describe --exact-match --abbrev=0 --tags ${COMMIT} 2> /dev/null || true)
10SUFFIX=''
11DETAIL=''
12
13# use the matching tag as the version, if available
14if [ -z "$TAG" ]; then
15  TAG=$(git describe --abbrev=0)
16  COMMITS=$(git --no-pager log ${TAG}..HEAD --oneline)
17  COMMIT_COUNT=$(echo -e "${COMMITS}" | wc -l)
18  COMMIT_COUNT_PADDING=$(printf %03d $COMMIT_COUNT)
19  SHORT_COMMIT_ID=$(git rev-parse --short HEAD)
20
21  SUFFIX='-dev'
22  DETAIL=".${COMMIT_COUNT_PADDING}.${SHORT_COMMIT_ID}"
23fi
24
25if [ -n "$(git diff --shortstat 2> /dev/null | tail -n1)" ]; then
26  SUFFIX="-dirty"
27fi
28
29
30VERSION="${TAG}${SUFFIX}${DETAIL}"
31echo $VERSION
32