-
Notifications
You must be signed in to change notification settings - Fork 0
/
gittag.sh
executable file
·72 lines (60 loc) · 1.46 KB
/
gittag.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
#!/bin/bash
usage () {
echo "usage: <script> $0 [major|minor|patch]"
exit 1
}
if [ "$#" -ne 1 ]; then
usage
fi
if [[ "$1" =~ ^(major|minor|patch)$ ]]; then
type=$1
else
echo "Unsupported option: $1"
echo
usage
fi
#get highest tag number
VERSION=`git describe --abbrev=0 --tags 2>/dev/null`
if [ -z $VERSION ];then
NEW_TAG="1.0.0"
echo "No tag present."
echo "Creating tag: $NEW_TAG"
git tag $NEW_TAG
git push --tags
echo "Tag created and pushed: $NEW_TAG"
exit 0;
fi
#replace . with space so can split into an array
VERSION_BITS=(${VERSION//./ })
#get number parts and increase last one by 1
VNUM1=${VERSION_BITS[0]}
VNUM2=${VERSION_BITS[1]}
VNUM3=${VERSION_BITS[2]}
case $type in
major)
VNUM1=$((VNUM1+1))
VNUM2=0
VNUM3=0
;;
minor)
VNUM2=$((VNUM2+1))
VNUM3=0
;;
patch)
VNUM3=$((VNUM3+1))
;;
esac
#create new tag
NEW_TAG="${VNUM1}.${VNUM2}.${VNUM3}"
#get current hash and see if it already has a tag
GIT_COMMIT=`git rev-parse HEAD`
CURRENT_COMMIT_TAG=`git describe --contains $GIT_COMMIT 2>/dev/null`
# only tag if no tag already (would be better if the git describe command above could have a silent option)
if [ -z "$CURRENT_COMMIT_TAG" ]; then
echo "Updating $VERSION to $NEW_TAG"
git tag $NEW_TAG
git push --tags
echo "Tag created and pushed: $NEW_TAG"
else
echo "This commit is already tagged as: $CURRENT_COMMIT_TAG"
fi