-
Notifications
You must be signed in to change notification settings - Fork 9
/
version
executable file
·73 lines (60 loc) · 1.66 KB
/
version
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/bash
# Automagically calculate project version number and copyright date from git
# Usage: version [version | year | flags]
# Options:
# version - (default) output version info
# year - output copyright year
# flags - output compiler flags to set VERSION and YEAR
# Archive hash, refs and date set by git-archive via export-subst in .gitattributes
# Note: the refs (%d) are truncated to only show the version tag, thus avoiding
# variability caused by moving branch references (like master). This makes archives
# build from tags stable for downstream package distribution. However, versons tags
# are limited to a maximum 9 chars (i.e. v00.00.00) and must start with "v".
ARCHIVED='$Format:archived$'
HASH='$Format:%h$'
REFS='$Format:%<(13,ltrunc)%d$'
DATE='$Format:%aI$'
# Examples expansions:
#ARCHIVED='archived'
#HASH='09f863b'
#REFS='..g: v1.2.3)'
#DATE='1988-01-01...'
VERSION='unknown'
YEAR='????'
case $ARCHIVED in
# When archiving is detected, extract year from date and version from refs, or fall back to hash.
archived)
if [[ $REFS =~ (v[^\)]+)\)$ ]]
then
VERSION=${BASH_REMATCH[1]}
else
VERSION=$HASH
fi
YEAR=${DATE:0:4}
;;
# Default to using git to generate version number and year in working tree
*)
if [[ $(git rev-parse --is-inside-work-tree) == 'true' ]]
then
VERSION=$(git describe --always --tags --dirty)
YEAR=$(git log -1 --date=format:%Y --pretty=format:%ad)
fi
;;
esac
# Strip leading 'v' if found
if [[ ${VERSION:0:1} == 'v' ]]
then
VERSION=${VERSION:1}
fi
# Send output based on option
case ${1:-version} in
version)
echo $VERSION
;;
year)
echo $YEAR
;;
flags)
echo -DVERSION=$VERSION -DYEAR=$YEAR
;;
esac