1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#!/bin/sh
# optionally take commit-ish as argument for debugging purposes
head=$1
gitrepo=true
toplevel="$(git rev-parse --show-toplevel 2> /dev/null)"
if test -z "${toplevel}" ; then
# failed to get toplevel directory, we are not inside a git
# repo?
gitrepo=false
toplevel=$PWD
fi
if test -f "${toplevel}/VERSION" ; then
cat "${toplevel}/VERSION"
exit 0
fi
# no VERSION file
if ! ${gitrepo} ; then
echo "E: Not inside a git repository and no VERSION file found. Abort." 1>&2
exit 1
fi
# gitdesc extracts the closest version tag, removing the leading "v" and
# returning the rest
gitdesc() {
git describe --match='v[0-9]*' --always "$@" 2>/dev/null |
sed -e 's,^v,,'
}
closest_tag=$(gitdesc --abbrev=0 ${head})
current_commit=$(gitdesc ${head})
if test -n "${head}" ; then
# cannot pass --dirty when using commit-ish
dirty_info=${current_commit}
else
dirty_info=$(gitdesc --dirty)
fi
# First figure out if the current commit matches a tag:
#
# v3.4.1
# v3.4.1-rc.1
if [ "${closest_tag}" = "${current_commit}" ] ; then
# We are on a tag, leave it alone, maybe including the dirty
# mark.
#
# This works even if we ever use a tag like v3.4.1+pl1 (which we
# will hopefully never have).
echo "${dirty_info}"
exit 0
fi
# We have something which doesn't exactly match a tag:
#
# v3.4.1-315-g21e6d6765
# v3.4.1-rc.1-315-g21e6d6765
#
# Use the -315-g21e6d6765 part as metadata, using + to indicate that
# this is 3.4.1 with 315 additional changes. Keep the g21e6d6765 part to
# have a record of the corresponding git commit hash.
#
# Transform the above into:
#
# v3.4.1+315-g21e6d6765
# v3.4.1-rc.1+315-g21e6d6765
echo "${dirty_info}" | sed -e "s,^\(${closest_tag}\)-,\1+,"
|