1
+ #! /bin/bash
2
+
3
+ function version::get_version_vars() {
4
+ BUILD_DATE=$( date -u ' +%Y-%m-%dT%H:%M:%SZ' )
5
+ GIT_TREE_STATE=" unknown"
6
+ GIT_COMMIT=" unknown"
7
+ GIT_VERSION=" unknown"
8
+
9
+ # get the git tree state if the source was exported through git archive.
10
+ # shellcheck disable=SC2016,SC2050
11
+ if [[ ' $Format:%%$' == " %" ]]; then
12
+ GIT_TREE_STATE=" archive"
13
+ GIT_COMMIT=' $Format:%H$'
14
+ # when a 'git archive' is exported, the '$Format:%D$' below will look
15
+ # something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: '
16
+ # can be extracted from it.
17
+ if [[ ' $Format:%D$' =~ tag:\ (v[^ ,]+) ]]; then
18
+ GIT_VERSION=" ${BASH_REMATCH[1]} "
19
+ else
20
+ GIT_VERSION=" ${GIT_COMMIT: 0: 7} "
21
+ fi
22
+ # respect specified version.
23
+ GIT_VERSION=" ${VERSION:- ${GIT_VERSION} } "
24
+ return
25
+ fi
26
+
27
+ # return directly if not found git client.
28
+ if [[ -z " $( command -v git) " ]]; then
29
+ # respect specified version.
30
+ GIT_VERSION=${VERSION:- ${GIT_VERSION} }
31
+ return
32
+ fi
33
+
34
+ # find out git info via git client.
35
+ if GIT_COMMIT=$( git rev-parse " HEAD^{commit}" 2> /dev/null) ; then
36
+ # specify as dirty if the tree is not clean.
37
+ if git_status=$( git status --porcelain 2> /dev/null) && [[ -n ${git_status} ]]; then
38
+ GIT_TREE_STATE=" dirty"
39
+ else
40
+ GIT_TREE_STATE=" clean"
41
+ fi
42
+
43
+ # specify with the tag if the head is tagged.
44
+ if GIT_VERSION=" $( git rev-parse --abbrev-ref HEAD 2> /dev/null) " ; then
45
+ if git_tag=$( git tag -l --contains HEAD 2> /dev/null | head -n 1 2> /dev/null) && [[ -n ${git_tag} ]]; then
46
+ GIT_VERSION=" ${git_tag} "
47
+ fi
48
+ fi
49
+
50
+ # specify to dev if the tree is dirty.
51
+ if [[ " ${GIT_TREE_STATE:- dirty} " == " dirty" ]]; then
52
+ GIT_VERSION=" dev"
53
+ elif ! [[ " ${GIT_VERSION} " =~ ^v([0-9]+)\. ([0-9]+)(\. [0-9]+)? (-[0-9A-Za-z.-]+)? (\+ [0-9A-Za-z.-]+)? $ ]]; then
54
+ GIT_VERSION=" dev"
55
+ fi
56
+
57
+ # respect specified version
58
+ GIT_VERSION=${VERSION:- ${GIT_VERSION} }
59
+ fi
60
+ }
0 commit comments