Skip to content

Commit e8345b5

Browse files
committed
getversion: use commit tag if possible; update documentation
In order to support a tag checkout, the version string detection now starts with checking a possible tag of the current commit. In all other cases the previous behaviour is unchanged (except for the detached HEAD state being handled more explicitly now). The documentation header now reflects the current processing flow.
1 parent a0b9a9b commit e8345b5

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

getversion

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
# This is in case we're building from a tarball.
77
#
88
# * If we're inside a git tree:
9-
# - For the "master" branch, use "git describe".
10-
# - For "detached" branches (mostly for tag checkouts), also use "git describe".
11-
# - For other branches, use a "$VERSION-$NBCOMMITS-$BRANCH-$DATE-g$COMMIT"
12-
# sortof version.
9+
# - For a current tagged commit, return this tag.
10+
# - For the "master/devel/stable-*" branches, use "git describe".
11+
# - For other branches or detached HEADs, return a
12+
# "$VERSION-$NBCOMMITS-$BRANCH-$DATE-g$COMMIT" version string
13+
# (with "BRANCH=detached" for a detached HEAD)
1314
#
1415
# * Try to make it up from the directory name (munin-2.0.1 -> 2.0.1)
1516
#
@@ -23,7 +24,7 @@
2324
# shellcheck disable=SC2006
2425

2526

26-
current_git_branch() {
27+
get_current_git_branch_name() {
2728
# hide stderr (for detached HEADs)
2829
GB=`LANG='' git branch --points-at HEAD 2>/dev/null | awk '$1 == "*" {print $2}'`
2930
if echo "$GB" | grep -q "^("; then
@@ -36,22 +37,33 @@ current_git_branch() {
3637

3738

3839
generate_version_string() {
39-
branch=`current_git_branch`
40-
case "$branch" in
41-
""|master|devel|stable-*)
42-
git describe
43-
;;
44-
*)
45-
branch=`echo "$branch" | sed -e 's/[^0-9A-Za-z\.\-\_]/_/g'`
46-
# "foo | read VAR" does *not* work (variable is changed only in subshell)
47-
# workaround stolen from http://www.etalabs.net/sh_tricks.html
48-
read -r VERSION COMMITS <<EOF
40+
# try the obvious: is our current commit tagged?
41+
head_tag=`git tag --points-at HEAD`
42+
if [ -n "$head_tag" ]; then
43+
echo "$head_tag"
44+
else
45+
branch_name=`get_current_git_branch_name`
46+
case "$branch_name" in
47+
master|devel|stable-*)
48+
# public branches or detached HEADs
49+
git describe
50+
;;
51+
*)
52+
if [ -n "$branch_name" ]; then
53+
clean_branch_name=`echo "$branch_name" | sed -e 's/[^0-9A-Za-z\.\-\_]/_/g'`
54+
else
55+
clean_branch_name="detached"
56+
fi
57+
# "foo | read VAR" does *not* work (variable is changed only in subshell)
58+
# workaround stolen from http://www.etalabs.net/sh_tricks.html
59+
read -r VERSION COMMITS <<EOF
4960
`git describe --long | perl -lne 'print "$1 $2" if m/(.*)-(\d+)-g\w+/'`
5061
EOF
51-
# As git describe, we also use the "-g" magic string to denote a git hash
52-
git log -n 1 --pretty="${VERSION}-${branch}-%ad-c${COMMITS}-g%h" --date=short
53-
;;
54-
esac
62+
# As git describe, we also use the "-g" magic string to denote a git hash
63+
git log -n 1 --pretty="${VERSION}-${clean_branch_name}-%ad-c${COMMITS}-g%h" --date=short
64+
;;
65+
esac
66+
fi
5567
}
5668

5769

0 commit comments

Comments
 (0)