-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-versioning.sh
More file actions
executable file
·123 lines (109 loc) · 2.12 KB
/
git-versioning.sh
File metadata and controls
executable file
·123 lines (109 loc) · 2.12 KB
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
#!/bin/sh
# git-versioning.sh
info()
{
cat << EOF
To start versioning on git you need to do this:
1. git tag -a -m "init" 1.0
2. git push --tags
3. this script will do everything else :-)
Examples:
short version - 2.1.9
long version - 2.1.9-abee12c-new_cool_feature
Major and minor will be taken from tag, patch = commits count from tag point
EOF
}
usage()
{
cat << EOF
usage: $0 options
OPTIONS:
-i info
-s short version (major.minor.patch)
-l long version (major.minor.patch-sha1-branch)
-p package version (short or long version depends on RC branch)
-b branch name
EOF
}
current_tag()
{
CURRENT_TAG=$(git describe --tags --always)
if [[ ! "$CURRENT_TAG" =~ "-" ]]; then
CURRENT_TAG=${CURRENT_TAG}-0-000000
fi
echo ${CURRENT_TAG}
}
current_version()
{
echo $(current_tag | cut -d'-' -f 1,2 | sed 's/-/\./g')
}
current_sha1()
{
echo $(current_tag | cut -d'-' -f 3,3)
}
current_branch()
{
read -a CURRENT_BRANCHES <<< $(git for-each-ref --format='%(objectname) %(refname:short)' refs \
| grep `git rev-parse HEAD` \
| grep -v HEAD \
| cut -d' ' -f 2 \
| cut -d'/' -f 2)
for CURRENT_BRANCH in "${CURRENT_BRANCHES[@]}"
do
if [[ ${CURRENT_BRANCH} == rc-* ]]; then
break
else
continue
fi
done
echo ${CURRENT_BRANCH}
}
short_version()
{
echo $(current_version)
}
long_version()
{
echo $(current_version)-$(current_sha1)-$(current_branch)
}
package_version()
{
if [[ $(current_branch) == rc-* ]]; then
short_version
else
long_version
fi
}
if [[ $# -eq 0 ]]; then
usage
exit
fi
while getopts "slpbih?" OPTION
do
case $OPTION in
i)
info
exit
;;
s)
short_version
exit
;;
l)
long_version
exit
;;
p)
package_version
exit
;;
b)
current_branch
exit
;;
*)
usage
exit
;;
esac
done