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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#!/bin/bash
### Description
#
# Return the version representing the state of the working directory.
#
# Expected output variants:
# - 2.3.1
# - 2.3.1-dirty
# - 2.3.1-146-COMMITID
# - 2.3.1-146-COMMITID-dirty
#
# The "-dirty" suffix is added whenever there are uncommitted changes in the
# working directory.
#
# The "-xxx-COMMITID" part is added whenever the currently checked out commit
# does not have an associated git tag. "xxx" represents the number of commits
# since the latest tag. "COMMITID" is a hash representing the current commit.
### Shell configuration and error handler
#
set -e
set -u
_fatalError() {
MSG="$1"
echo "ERROR($0): $MSG" 1>&2
exit 1
}
### Check the runtime environment
#
if [ ! -d dev-tools ]; then
_fatalError "This script must be run from the root of either git repository or uncompressed distribution package directory"
fi
### Define the help method
#
_showHelp()
{
cat <<EOF
Purpose:
Return the version of Snoopy code in the source tree
Supported CLI arguments:
-i Ignore a dirty working directory - do not add '-dirty' suffix to the version.
Only applicable in 'git' mode (and when 'autoreconf' mode uses git too).
-m MODE Specify the mode of operation ('git', 'changelog', or 'autoreconf' [default]).
Mode 'autoreconf' uses 'git' mode if available, and falls back on 'changelog'
mode (i.e. when used within a distribution tarball).
-h/--help Show this help.
Supported environment variables:
SNOOPY_RELEASE_VERSION_IGNORE_DIRTY
If set, dirty working tree is not reported as such in the returned version.
Useful for fixing up (refreshed) release tooling on-the-fly while releasing
a package.
Usage:
Get the software release version:
$0
Get the version that ignores the "-dirty" git tag:
$0 -i
EOF
}
_showHelpAndExit()
{
_showHelp
exit
}
### Parse the CLI arguments
#
if [[ $@ =~ [-][-]help ]]; then
_showHelpAndExit
fi
IGNORE_DIRTY="false"
MODE="autoreconf"
while getopts ":him:" opt; do
case "$opt" in
i)
IGNORE_DIRTY="true"
;;
m)
MODE="$OPTARG"
;;
h)
_showHelpAndExit
;;
?)
_fatalError "Unsupported argument: '-$OPTARG'. Run '$0 -h' to list supported arguments." $LINENO
;;
*)
_fatalError "Internal error (opt=$opt)" $LINENO
;;
esac
done
### Parse ENV arguments
#
if [ ! -z ${SNOOPY_RELEASE_VERSION_IGNORE_DIRTY+x} ]; then
echo "[$0] WARNING: Environment varible SNOOPY_RELEASE_VERSION_IGNORE_DIRTY set, ignoring potentially dirty working directory." 1>&2
IGNORE_DIRTY=true
fi
### Check arguments
#
case $MODE in
'autoreconf')
;;
'git')
;;
'changelog')
;;
*)
_fatalError "Invalid run mode: '$MODE'. Only 'git', 'changelog' or 'autoreconf' (default) options are supported."
;;
esac
#### First get current commit
##
#CURRENT_COMMIT_HASH=`git rev-parse HEAD`
#
#
#
#### Check if current commit has corresponding tag
##
#if git show-ref --tags | grep "^$CURRENT_COMMIT_HASH refs/tags" > /dev/null; then
# CURRENT_TAG=`git show-ref --tags | grep "^$CURRENT_COMMIT_HASH refs/tags" | awk '{print $2}' | sed -e 's#^refs/tags##'`
# CURRENT_PACKAGE_VERSION=`echo "$CURRENT_TAG" | sed -e 's/snoopy-//'`
#
# ### Is tree dirty?
# #
# if git describe --tags --dirty | grep -- '-dirty$' > /dev/null; then
# CURRENT_PACKAGE_VERSION="${CURRENT_PACKAGE_VERSION}-dirty"
# fi
#else
# # Here '-dirty' gets appended automatically
# CURRENT_PACKAGE_VERSION=`git describe --tags --dirty`
#fi
### Get current Snoopy version from git, if applicable
#
# Used for building from git, and for making RC packages
#
if [ "$MODE" == "autoreconf" -o "$MODE" == "git" ]; then
if [ -d .git ]; then
if [ "$IGNORE_DIRTY" == "true" ]; then
SNOOPY_RELEASE_VERSION=`git describe --tags --always | sed -e 's/^snoopy-//'`
else
SNOOPY_RELEASE_VERSION=`git describe --tags --always --dirty | sed -e 's/^snoopy-//'`
fi
echo $SNOOPY_RELEASE_VERSION
exit 0
fi
fi
### Otherwise get it from ChangeLog
#
# Used only if ./bootstrap.sh is run from distribution package, not from git
#
if [ "$MODE" == "autoreconf" -o "$MODE" == "changelog" ]; then
if [ -f ChangeLog ]; then
SNOOPY_RELEASE_VERSION=`cat ChangeLog | grep -E '^[-0-9]+ - Version [0-9]+\.[0-9]+\.[0-9]+(rc[0-9]+)?$' | head -n1 | awk '{print $4}'`
echo $SNOOPY_RELEASE_VERSION
exit 0
fi
fi
### Signal error
#
_fatalError "Unable to determine Snoopy version (mode=$MODE)"
|