1#!/bin/sh
2VERSION="unknown"
3
4DIRTY=""
5git status | grep -q clean || DIRTY='.dirty'
6
7# Special environment variable to signal that we are building a release, as this
8# has consequenses for the version number.
9if [ "${IS_RELEASE}" = "YES" ]; then
10  TAG="$(git describe --tags --exact-match 2> /dev/null | cut -d- -f 2-)"
11  if [ -n "${TAG}" ]; then
12    # We're on a tag
13    echo "${TAG}${DIRTY}" | tee .version
14    exit 0
15  fi
16  echo 'This is not a tag, either tag this commit or do not set $IS_RELEASE' >&2
17  exit 1
18fi
19
20#
21# Generate the version number based on the branch
22#
23if [ ! -z "$(git rev-parse --abbrev-ref HEAD 2> /dev/null)" ]; then
24  GIT_VERSION="$(git describe --tags)"
25  LAST_TAG="$(echo ${GIT_VERSION} | cut -d- -f1)"
26  COMMITS_SINCE_TAG="$(echo ${GIT_VERSION} | cut -d- -f2)"
27  GIT_HASH="$(echo ${GIT_VERSION} | cut -d- -f3)"
28  BRANCH=".$(git rev-parse --abbrev-ref HEAD | perl -p -e 's/-//g;')"
29  [ "${BRANCH}" = ".master" ] && BRANCH=''
30
31  TAG="$(git describe --tags --exact-match 2> /dev/null | cut -d- -f 2-)"
32  if [ -n "${TAG}" ]; then # We're exactly on a tag
33    COMMITS_SINCE_TAG="0"
34    GIT_HASH="g$(git show --no-patch --format=format:%h HEAD)"
35  fi
36
37  VERSION="${LAST_TAG}+${COMMITS_SINCE_TAG}${BRANCH}.${GIT_HASH}${DIRTY}"
38  echo "$VERSION" > .version
39elif [ -f .version ]; then
40  VERSION="$(cat .version)"
41fi
42
43printf $VERSION
44