-
Notifications
You must be signed in to change notification settings - Fork 9
/
git_tag_manager.sh
executable file
·161 lines (144 loc) · 4.49 KB
/
git_tag_manager.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
#!/usr/bin/env bash
# ***************************************************************************************
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ****************************************************************************************
#*****************************************************************************************
# Git tag management script.
#
# This script can be used to tag a gatb-core release. When a tag is added, it is
# automatically pushed to the remote server.
#
# Usage:
# use option -h
#
# Author: Patrick Durand, Inria
# Created: December 2015
#*****************************************************************************************
# --------
# FUNCTION: print a spacer
function printspacer(){
sname=$0 ; length=${#sname} ; ((length+=2))
for ((i=0;i<length;i++)); do
printf " "
done
}
# --------
# FUNCTION: display help message
function help(){
printf "\n$0: a tool to handle git release tag creation using canonical \n"
printspacer
printf "numbering including major, minor and patch numbers.\n\n"
printf "usage: $0 [-h] [-D] [-t <message>] -M <major> -m <minor> -p <patch>\n"
printf "\n"
printf " -t <message> : new tag message (optional). Default message: 'new release'.\n"
printf " -M <tag> : major release number.\n"
printf " -m <tag> : minor release number.\n"
printf " -p <tag> : patch number.\n"
printf " -D : delete existing tag.\n"
printf " -h : this message.\n"
printf "\n"
printf "Using values from '-M A', '-m B' and '-p C' arguments, the script creates a tag\n"
printf "named 'vA.B.C' (without quotes). The tag is automatically pushed to the remote\n"
printf "server.\n"
printf "\n"
printf "Return value is one of:\n"
printf " 0: ok\n"
printf " 1: missing mandatory argument\n"
printf " 2: tag version already exists\n"
printf " 3: failed to create tag version\n"
printf " 4: failed to push tag to remote repository\n"
printf " 5: failed to delete tag\n"
exit 0
}
# Some variables
release_msg=""
curTag=""
MAJOR=""
MINOR=
PATCH=
TAG=""
OUT=""
CMD=""
# Prepare arguments for processing
while getopts hDM:m:p:t: opt
do
case "$opt" in
M) MAJOR="$OPTARG";;
m) MINOR="$OPTARG";;
p) PATCH="$OPTARG";;
t) CREATE_MESSAGE="$OPTARG";;
D) CMD="delete";;
h) help;;
\?) help;;
esac
done
shift `expr $OPTIND - 1`
#Do we have all mandatory arguments ?
mandatory_params=( "-M" "-m" "-p" )
mandatory_values=( "$MAJOR" "$MINOR" "$PATCH" )
for ((i=0;i<${#mandatory_params[@]};++i)); do
if [ -z "${mandatory_values[i]}" ]; then
printf "/!\ Missing mandatory argument: ${mandatory_params[i]}\n" >&2
printf " use option -h to get help.\n" >&2
exit 1
fi
done
# Prepare tag
TAG=$(echo "v${MAJOR}.${MINOR}.${PATCH}")
# Check whether provided tag already exists on repository
curTag=`git tag -l $TAG`
if [ ! -z "$curTag" ]; then
#if tag exists and we do not want to delete it: error
if [ ! "$CMD" == "delete" ]; then
printf "/!\ git tag '$TAG' already exist.\n" >&2
exit 2
fi
#delete tag from local repository...
git tag --delete $curTag
#... then from remote repository
git push --delete origin $curTag
exit 0
fi
#do we have to delete tag?
if [ "$CMD" == "delete" ]; then
printf "/!\ git tag '$TAG' does not exist: nothing to delete.\n" >&2
exit 5
fi
# Prepare message
if [ ! -z "$CREATE_MESSAGE" ]; then
release_msg=$CREATE_MESSAGE
else
release_msg="new release"
fi
# Create tag on local repository and check if it's ok
printf "Tagging git repository with \n"
printf " tag: $TAG\n"
printf " msg: $release_msg\n"
git tag -m "$release_msg" $TAG
OUT=$?
if [ $OUT -eq 0 ];then
printf "git tag '$TAG' created.\n"
else
printf "/!\ unable to create git tag '$TAG'.\n" >&2
exit 3
fi
# Push tag to the remote server and check if it's ok
git push origin $TAG
OUT=$?
if [ $OUT -eq 0 ];then
printf "git tag '$TAG' pushed to remote repository.\n"
exit 0
else
printf "/!\ git tag '$TAG' not pushed to remote repository.\n" >&2
exit 4
fi