The following will give you the latest git tag name (if the HEAD is tagged) or latest commit ID otherwise. After execution the X_VERSION
variable holds the end value.
I use it to name packages of my builds and other versioning purposes. See example here.
X_GIT_COMMITID=`git rev-parse --short HEAD | tr -d "\n" | sed 's/ //g'`
X_GIT_TAG=`git tag --points-at $X_GIT_COMMITID | tr -d "\n" | sed 's/ //g'`
if [ -n "$X_GIT_TAG" ]; then
X_VERSION="$X_GIT_TAG"
else
X_VERSION="git-$X_GIT_COMMITID"
fi
echo $X_VERSION