forked from foreverneverer/pegasus-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carrot
executable file
·312 lines (281 loc) · 8.37 KB
/
carrot
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
# Carrot: A Release veRsiOn Tool for java client
# By WeijieSun ([email protected])
staging_branch="thrift-0.11.0-inlined"
project_name="pegasus-java-client"
command_decorator="verify"
function git_current_branch()
{
echo `git branch | fgrep "*" | cut -d " " -f 2`
}
function java_client_get_current_version()
{
pomfile_version=`grep version pom.xml | grep $staging_branch | tr -d '[:space:]'`
prefix="<version>"
postfix="</version>"
pomfile_version=${pomfile_version#$prefix}
pomfile_version=${pomfile_version%$postfix}
version_id=`echo $pomfile_version | cut -d - -f 1 | sed -e 's/\./ /g'`
versions=($version_id)
case $1 in
major)
echo ${versions[0]}-$staging_branch
;;
minor)
echo ${versions[0]}.${versions[1]}-$staging_branch
;;
patch)
echo $version_id-$staging_branch
;;
*)
echo "Invalid current version type"
exit -1
;;
esac
}
function java_client_get_next_version()
{
pomfile_version=`grep version pom.xml | grep $staging_branch | tr -d '[:space:]'`
prefix="<version>"
postfix="</version>"
pomfile_version=${pomfile_version#$prefix}
pomfile_version=${pomfile_version%$postfix}
version_id=`echo $pomfile_version | cut -d - -f 1 | sed -e 's/\./ /g'`
versions=($version_id)
case $1 in
major)
versions[0]=$[ ${versions[0]} + 1 ]
;;
minor)
versions[1]=$[ ${versions[1]} + 1 ]
;;
patch)
if [ ${versions[2]} == "SNAPSHOT" ]; then
versions[2]="0"
else
versions[2]=$[ ${versions[2]} + 1 ]
fi
;;
*)
echo "Invalid next version type"
exit -1
;;
esac
if [ ${versions[2]} == "SNAPSHOT" ]; then
echo ${versions[0]}.${versions[1]}-$staging_branch-SNAPSHOT
else
echo ${versions[0]}.${versions[1]}.${versions[2]}-$staging_branch
fi
}
function get_branch_type()
{
if [ $1 = $staging_branch ]; then
echo "staging"
else
echo "release"
fi
}
function verify_command()
{
answer=""
echo -n -e "\033[31mExecuting command: $@, y/N?\033[0m"
read answer
if [ -z $answer ] || [ $answer = "y" ]; then
eval "$@"
else
return -1
fi
return $?
}
function verbose_command()
{
echo -e "\033[31mExec Command: $@ \033[0m"
eval "$@"
return $?
}
function carrot_execute()
{
case $command_decorator in
silence)
eval $1
;;
verbose)
verbose_command $1
;;
verify)
verify_command $1
;;
simulate)
echo -e "\033[32m$1\033[0m"
;;
*)
echo "invalid command decorator"
exit -1
;;
esac
if [ $? -ne 0 ]; then
echo "error in execute command $1, simulate the remaining commands"
command_decorator="simulate"
fi
}
#
# patch -b|--branch branch_name -p|--commit_point commit_point -s|--start_from_this -d|--decorate decorate_type
#
function usage_patch
{
echo "carrot patch -- apply patch to specific branch, and release a new patch version"
echo " -h|--help, print this help"
echo " -b|--branch BRANCH_NAME, the target branch. For current branch if not set"
echo " -p|--commit_point GIT_COMMIT_ID, cherry-pick this to the target"
echo " -s|--start_from_this. If set, cherry-pick from [GIT_COMMIT_ID, HEAD] to the target"
echo " -d|--decorate TYPE. [silence|verbose|verify|simulate], default is verify"
}
function make_patch
{
branch_name=""
commit_point=""
recent_commit=""
starting_flag="false"
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_patch
exit 0
;;
-b|--branch)
branch_name=$2
shift
;;
-p|--commit_point)
commit_point=$2
shift;;
-s|--start_from_this)
starting_flag="true"
;;
-d|--decorate)
command_decorator=$2
shift
;;
*)
usage_patch
exit -1
;;
esac
shift
done
old_branch=`git_current_branch`
old_branch_type=`get_branch_type $old_branch`
# only in staging branch, we try to calcuate the -s flag, AND
# only in staging branch, we try to get the recent commit point in log
if [ $old_branch_type == "staging" ]; then
if [ ! -z $commit_point ]; then
if [ $starting_flag == "true" ]; then
recent_commit=`git log | sed -n "1p" | cut -d" " -f 2`
fi
else
commit_point=`git log | sed -n "1p" | cut -d" " -f 2`
fi
fi
current_branch=$old_branch
# we don't apply the patch unless we are in a release tag
if [ ! -z $branch_name ]; then
carrot_execute "git checkout $branch_name"
current_branch=$branch_name
if [ ! -z $recent_commit ]; then
carrot_execute "git cherry-pick $commit_point^..$recent_commit"
elif [ -n $commit_point ]; then
carrot_execute "git cherry-pick $commit_point"
fi
elif [ $old_branch_type == "staging" ]; then
echo "Please checkout to a release branch, or give a release branch name by -b"
exit -1
fi
new_version=`java_client_get_next_version patch`
carrot_execute "mvn versions:set -DnewVersion=$new_version"
carrot_execute "mvn versions:commit"
carrot_execute "git commit -am \"Release $project_name $new_version\""
carrot_execute "git tag -a $new_version-release -m \"Release $project_name $new_version\""
carrot_execute "git push -u origin $current_branch"
carrot_execute "git push --tags"
if [ $current_branch != $old_branch ]; then
carrot_execute "git checkout $old_branch"
fi
}
#
# minor-release -d|--decorate decorate_type
#
function usage_release_minor
{
echo "carrot minor-release"
echo " -h|--help, print this help "
echo " -d|--decorate TYPE. [silence|verbose|verify|simulate], default is verify"
}
function release_minor
{
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_release_minor
exit 0
;;
-d|--decorate)
command_decorator=$2
shift
;;
esac
shift
done
this_branch=`git_current_branch`
branch_type=`get_branch_type $this_branch`
if [ $branch_type != "staging" ]; then
echo "when release minor, we need to be in staging branch, currently in a $branch_type branch $this_branch"
exit -1
fi
this_version=`java_client_get_current_version minor`
# create new branch and push
carrot_execute "git checkout -b $this_version-release"
# from a.b.SNAPSHOT -> a.b.0
new_version=`java_client_get_next_version patch`
# commit the release version
carrot_execute "mvn versions:set -DnewVersion=$new_version"
carrot_execute "mvn versions:commit"
carrot_execute "git commit -am \"Release $project_name $new_version\""
carrot_execute "git push -u origin $this_version-release"
# then make tag
carrot_execute "git tag -a $new_version-release -m \"Release $project_name $new_version\""
carrot_execute "git push --tags"
# update the staging branch's version
carrot_execute "git checkout $this_branch"
# from a.b.SNAPSHOT -> a.b+1.SNAPSHOT
new_version=`java_client_get_next_version minor`
carrot_execute "mvn versions:set -DnewVersion=$new_version"
carrot_execute "mvn versions:commit"
carrot_execute "git commit -am \"Bump version to $new_version\""
}
function usage_carrot
{
echo "carrot -- Carrot is A Release veRsiOn Tool"
echo " help print the help"
echo " patch Make patch"
echo " minor-release Release a minor version"
}
action=$1
case $action in
help)
usage_carrot ;;
patch)
shift
make_patch $*
;;
minor-release)
shift
release_minor $*
;;
*)
echo "ERROR: unknown command $cmd"
echo
usage_carrot
exit -1
esac