-
Notifications
You must be signed in to change notification settings - Fork 2
/
gitdesc.sh
executable file
·181 lines (150 loc) · 4.07 KB
/
gitdesc.sh
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
#?bin/sh
# Peter Hyman, [email protected]
# December 2020, April 2021
# Free to use
# No warranties
# Attribution appreciated as well as enhancements or fixes!
# gitdesc.sh
# This program will return commit references based on Tags and Annotated Tags from git describe
# customize init() function to extract variables to return
# ideal use to provide input for M4 defines in configure.ac
# or run standalone to get output for some other purpose like packaging
usage() {
cat >&2 <<EOF
$(basename $0) command [-r]
all - entire git describe
commit - commit, omitting g
tagrev - tag revision count
tagdate - date tag committed
major - major release version
minor - minor release version
micro - micro release version
version - M.m.c
-r -- get release tag only
EOF
exit 1
}
# showw message and usage
die() {
echo "$1"
usage
}
# return variables
# everything, with leading `v' and leading `g' for commits
describe_tag=
# abbreviated commit
commit=
# count of commits from last tag
tagrev=
# date tag committed
tagdate=
# major version
major=
# minor version
minor=
# micro version
micro=
# get release or tag?
# if -r option is used, tagopt will be null
tagopt="--tags"
# how long shhow commit string be? 7 is default
commit_length=7
# get whole commit and parse
# if tagrev > 0 then add it and commit to micro version
# Expected tag format is:
##-##-##
# v#.#.# - adjust as required
##-##-##
# git describe will return
# vTAG-R-gC
# describe_tag variable will hold
# TAG-R-C
# with leading v and commit leadint g removed
init() {
local realtag
if [ -d '.git' ] ; then
# git describe raw format
describe_tag=$(git describe $tagopt --long --abbrev=$commit_length)
# grab real tag version in case we need it for git-show
realtag=$(echo $describe_tag | cut -d- -f1)
# if tag has a leading `v' this will remove
# if some other tag format is used, change or omit
describe_tag=${describe_tag/v/}
# git describe prefixes commit with the letter `g'
# this substitution removes the g. If the letter `g' is part of tag, this logic
# will need revision, such as reversing $describe_tag and then reverting
# echo $describe | rev, for example, and get variables right to left
describe_tag=${describe_tag/g/}
# assign commit, tag revision, and version to variables using `-' separator
commit=$(echo $describe_tag | cut -d- -f3)
tagrev=$(echo $describe_tag | cut -d- -f2)
version=$(echo $describe_tag | cut -d- -f1)
# if tag date is needed
# alter date format per strftime formats
# YYYY-mm-dd
tagdate=$(git show -n1 -s --date=format:"%Y-%m-%d" --format="%cd" $realtag)
# (Other examples)
# dd mmm YYYY
# tagdate=$(git show -n1 -s --date=format:"%d %b %Y" --format="%cd" $version)
# dd MMMMMMM YYYY
# tagdate=$(git show -n1 -s --date=format:"%d %B %Y" --format="%cd" $version)
# set micro version or full micro version if tag revision > 0
micro=$(echo $version | cut -d. -f3)
[ $tagrev -gt 0 ] && micro=$micro-$tagrev-$commit
# assign minor version
minor=$(echo $version | cut -d. -f2)
# assign major version
major=$(echo $version | cut -d. -f1)
elif [ -r VERSION ] ; then
# if no .git directory, then look for a file named VERSION
major=$(awk '/Major: / {printf "%s",$2; exit}' VERSION)
minor=$(awk '/Minor: / {printf "%s",$2; exit}' VERSION)
micro=$(awk '/Micro: / {printf "%s",$2; exit}' VERSION)
else
# if no .git directory and no file VERSION, then we can't go on
echo "Cannot find .git or VERSION file. Aborting"
exit 1
fi
}
[ ! $(which git) ] && die "Something very wrong: git not found."
[ $# -eq 0 ] && die "Must provide a command and optional argument."
# are we getting a release only?
if [ $# -eq 2 ]; then
if [ $2 = "-r" ]; then
tagopt=""
else
die "Invalid option. Must be -r or nothing."
fi
fi
init
case "$1" in
"all" )
retval=$describe_tag
;;
"commit" )
retval=$commit
;;
"tagrev" )
retval=$tagrev
;;
"tagdate" )
retval=$tagdate
;;
"version" )
retval=$version
;;
"major" )
retval=$major
;;
"minor" )
retval=$minor
;;
"micro" )
retval=$micro
;;
* )
die "Invalid command."
;;
esac
echo $retval
exit 0