-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgit-flow-release
582 lines (477 loc) · 15 KB
/
git-flow-release
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/bin/sh
init() {
require_git_repo
require_gitflow_initialized
gitflow_load_settings
VERSION_PREFIX=$(eval "echo `git config --get gitflow.prefix.versiontag`")
PREFIX=$(git config --get gitflow.prefix.release)
}
usage() {
echo "usage: flow release list [-v]"
echo " flow release start <version> [base]"
echo " flow release push [remoteVersion]"
echo " flow release publish <version>"
echo " flow release track <remoteVersion> [version]"
echo " flow release pull <remote> <remoteVersion>"
echo " flow release finish <remoteVersion>"
}
cmd_default() {
cmd_list "$@"
}
cmd_list() {
DEFINE_boolean verbose false 'verbose (more) output' v
parse_args "$@"
local release_branches
local current_branch
local short_names
release_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
if [ -z "$release_branches" ]; then
warn "No release branches exist."
warn ""
warn "You can start a new release branch:"
warn ""
warn " flow release start <name> [<base>]"
warn ""
exit 0
fi
current_branch=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
short_names=$(echo "$release_branches" | sed "s ^$PREFIX g")
# determine column width first
local width=0
local branch
for branch in $short_names; do
local len=${#branch}
width=$(max $width $len)
done
width=$(($width+3))
local branch
for branch in $short_names; do
local fullname=$PREFIX$branch
local base=$(git merge-base "$fullname" "$MASTER_BRANCH")
local develop_sha=$(git rev-parse "$MASTER_BRANCH")
local branch_sha=$(git rev-parse "$fullname")
if [ "$fullname" = "$current_branch" ]; then
printf "* "
else
printf " "
fi
if flag verbose; then
printf "%-${width}s" "$branch"
if [ "$branch_sha" = "$develop_sha" ]; then
printf "(no commits yet)"
else
local nicename=$(git rev-parse --short "$base")
printf "(based on $nicename)"
fi
else
printf "%s" "$branch"
fi
echo
done
}
cmd_help() {
usage
exit 0
}
parse_args() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# read arguments into global variables
VERSION=$1
BRANCH=$PREFIX$VERSION
}
require_version_arg() {
if [ "$VERSION" = "" ]; then
warn "Missing argument <version>"
usage
exit 1
fi
}
require_version_remote_arg() {
if [ "$VERSION" = "" ]; then
warn "Missing argument <version>"
usage
exit 1
fi
if [ -z "$REMOTE_VERSION" ]; then
warn "Missing argument <remote_version>"
usage
exit 1
fi
}
require_base_is_on_master() {
if ! git_do branch --no-color --contains "$BASE" 2>/dev/null \
| sed 's/[* ] //g' \
| grep -q "^$MASTER_BRANCH\$"; then
die "fatal: Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'."
fi
}
require_no_existing_release_branches() {
local release_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX")
local first_branch=$(echo ${release_branches} | head -n1)
first_branch=${first_branch#$PREFIX}
[ -z "$release_branches" ] || \
die "There is an existing release branch ($first_branch). Finish that one first."
}
parse_version_remote_args(){
# parse options
parse_args "$@"
REMOTE_VERSION=$2
REMOTE_BRANCH=$PREFIX$REMOTE_VERSION
}
parse_push_args(){
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
VERSION=$(echo $(git_current_branch) | awk -F '/' '{print $2}')
BRANCH=$PREFIX$VERSION
REMOTE_VERSION=${1:-$VERSION}
REMOTE_BRANCH=$PREFIX$REMOTE_VERSION
}
parse_track_args() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
REMOTE_NAME=$1
REMOTE_BRANCH=$PREFIX$REMOTE_NAME
# read arguments into global variables
NAME=${2:-$REMOTE_NAME}
BRANCH=$PREFIX$NAME
}
parse_remote_name() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# read arguments into global variables
REMOTE=$ORIGIN
NAME=$2
BRANCH=$PREFIX$NAME
}
name_or_current() {
if [ -z "$NAME" ]; then
use_current_branch_name
fi
}
require_in_current_branch(){
local current_branch=$(git_current_branch)
if [ "$current_branch" != "$BRANCH" ]; then
default_suggestion='yes'
echo
echo "The current branch is $current_branch, not $BRANCH"
printf " Do you want checkout to $BRANCH then contine? [$default_suggestion] "
read answer
answer=${answer:-$default_suggestion}
[ "$answer" != "yes" ] && die 'Please checkout to $BRANCH manually'
echo " Branch $current_branch Will auto checkout to $BRANCH "
git_do checkout -q $BRANCH
fi
}
use_current_branch_name() {
local current_branch=$(git_current_branch)
if startswith "$current_branch" "$PREFIX"; then
BRANCH=$current_branch
NAME=${BRANCH#$PREFIX}
REMOTE_BRANCH=$BRANCH
REMOTE_NAME=$NAME
else
warn "The current HEAD is no $PREFIX branch."
warn "Please specify a <name> argument."
exit 1
fi
}
avoid_accidental_cross_branch_action() {
local current_branch=$(git_current_branch)
if [ "$BRANCH" != "$current_branch" ]; then
warn "Trying to pull from '$BRANCH' while currently on branch '$current_branch'."
warn "To avoid unintended merges,flow aborted."
return 1
fi
return 0
}
cmd_start() {
parse_args "$@"
BASE=${2:-$MASTER_BRANCH}
require_base_is_on_master
require_no_existing_release_branches
# sanity checks
require_clean_working_tree
git_do fetch -q "$ORIGIN" "$MASTER_BRANCH"
require_branch_absent "$BRANCH"
require_tag_absent "$VERSION"
if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then
require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
fi
# create branch
git_do checkout -b "$BRANCH" "$BASE"
echo
echo "Summary of actions:"
echo "- A new branch '$BRANCH' was created, based on '$BASE'"
echo "- You are now on branch '$BRANCH'"
echo
echo "Follow-up actions:"
echo "- Bump the version number now!"
echo "- Start committing last-minute fixes in preparing your release"
echo "- When done, run:"
echo
echo " flow release publish '$VERSION'"
echo
}
cmd_build(){
DEFINE_boolean test false "building on test " t
DEFINE_boolean productTest false "building on product test" p
DEFINE_string message "" "use the given tag message" m
parse_args "$@"
require_version_arg
# handle flags that imply other flags
if [ "$FLAGS_signingkey" != "" ]; then
FLAGS_sign=$FLAGS_TRUE
fi
# sanity checks
require_branch "$BRANCH"
require_clean_working_tree
git_do fetch -q "$ORIGIN" "$MASTER_BRANCH" || \
die "Could not fetch $MASTER_BRANCH from $ORIGIN."
# try to tag the release
# in case a previous attempt to finish this release branch has failed,
# but the tag was set successful, we skip it now
if flag test; then
VERSION_PREFIX="test/"
fi
if flag product_test; then
VERSION_PREFIX="productTest/"
fi
local tagname=$VERSION_PREFIX$VERSION
#get current tag max count value
local count=$(git_do tag -l "$tagname-*"|awk -F '-' '{print $2}'|sort -r|head -1)
if [ -z "$count" ]; then
count=1
else
count=$[$count+1]
fi
tagname="$tagname-$count"
if ! git_tag_exists "$tagname"; then
local opts="-a"
[ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
eval git_do tag $opts "$tagname" "$BRANCH" || \
die "Tagging failed. Please run finish again to retry."
else
die "Tag name: $tagname has exists. Please check."
fi
}
cmd_push() {
parse_push_args "$@"
require_version_remote_arg
# sanity checks
require_branch "$BRANCH"
require_remote_branch "$ORIGIN/$REMOTE_BRANCH"
require_clean_working_tree
git_do fetch -q "$ORIGIN" "$MASTER_BRANCH" || \
die "Could not fetch $MASTER_BRANCH from $ORIGIN."
if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then
require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
fi
# try to merge master into current branch
# in case a previous attempt to finish this release branch has failed,
# but the merge into master was successful, we skip it now
if ! git_is_branch_merged_into "$MASTER_BRANCH" "$BRANCH"; then
git_do merge --no-ff "$MASTER_BRANCH" || \
die "There were merge conflicts."
fi
#try to pull remote changes
git_do pull -q "$ORIGIN" "$REMOTE_BRANCH" || die "Failed to pull from remote '$REMOTE'."
git_do push "$ORIGIN" "$BRANCH":"refs/for/$REMOTE_BRANCH"
if [ $? -ne 0 ]; then
die "git push codeReview failed, Please check it."
fi
echo
echo "Summary of actions:"
echo "- Latest objects have been fetched from '$ORIGIN'"
echo "- '$MASTER_BRANCH' has been merged into branch $BRANCH"
echo "- '$BRANCH' have been pushed to '$ORIGIN $REMOTE_BRANCH'"
echo
}
cmd_publish() {
parse_args "$@"
require_version_arg
require_in_current_branch
# sanity checks
require_clean_working_tree
require_branch "$BRANCH"
#fetch all branch
git_do fetch -q "$ORIGIN"
#make sure local master branch is equal as remote branch
require_branches_absolutely_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
local result=$?
if [ $result -gt 0 ]; then
die "falal: $MASTER_BRANCH isn't equal with $ORIGIN/$MASTER_BRANCH"
fi
#make sure local branch is equal origin branch
require_branches_absolutely_equal "$BRANCH" "$ORIGIN/$BRANCH"
local result=$?
if [ $result -gt 0 ]; then
if [ $result -eq 1 ]; then
git_do merge $ORIGIN/$BRANCH
else
die "falal: $BRANCH isn't equal with $ORIGIN/$BRANCH"
fi
fi
#check master branch has merge into branch
if ! git_is_branch_merged_into "$MASTER_BRANCH" "$BRANCH"; then
die "There were merge conflicts."
fi
# try to tag the release
# but the tag was set successful, we skip it now
git_do fetch -q "$ORIGIN" --tag || \
die "Could not fetch tag from $ORIGIN."
local tagname=$VERSION_PREFIX$VERSION
tagname="$(generate_tag_version "$tagname")"
require_tag_validation "$tagname"
if ! git_tag_exists "$tagname"; then
git_do tag -a "$tagname" "$BRANCH" || \
die "Tagging failed. Please run finish again to retry."
else
die "Tag name: $tagname has exists. Please check."
fi
git_do push "$ORIGIN" $tagname || \
die "Could not push tags to $ORIGIN."
echo
echo "Summary of actions:"
echo "- The release was tagged '$tagname'"
echo "- Tag [$tagname] have been pushed to '$ORIGIN'"
echo
}
require_branches_absolutely_equal() {
require_local_branch "$1"
require_remote_branch "$2"
git_compare_branches "$1" "$2"
local status=$?
if [ $status -gt 0 ]; then
warn "Branches '$1' and '$2' have diverged."
if [ $status -eq 1 ]; then
warn "And branch '$1' may be fast-forwarded."
return 1;
elif [ $status -eq 2 ]; then
# Warn here, since there is no harm in being ahead
warn "And local branch '$1' is ahead of '$2'."
return 2;
else
warn "Branches need merging first."
return 3;
fi
fi
return 0;
}
cmd_finish() {
parse_args "$@"
require_version_arg
require_in_current_branch
require_clean_working_tree
#fetch all remote change
git_do fetch -q "$ORIGIN" || \
die "Could not fetch from $ORIGIN."
#check master must be newest
require_branches_absolutely_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
local result=$?
if [ $result -gt 0 ]; then
die "falal: $MASTER_BRANCH isn't equal with $ORIGIN/$MASTER_BRANCH"
fi
#check BRANCH must be newest
require_branches_absolutely_equal "$BRANCH" "$ORIGIN/$BRANCH"
local result=$?
if [ $result -gt 0 ]; then
die "falal: $BRANCH isn't equal with $ORIGIN/$BRANCH"
fi
#check master branch has merge into branch
if ! git_is_branch_merged_into "$MASTER_BRANCH" "$BRANCH"; then
die "There were merge conflicts."
fi
local TAG_NAME="$(get_latest_tag "$VERSION")"
#check tag is exists
if ! git_tag_exists "$TAG_NAME"; then
die "tag name: $TAG_NAME not exists"
fi
#check current branch is equal as tag name
local result=$(git_do diff --stat "$BRANCH" "$TAG_NAME" | wc -l)
if [ $result -gt 0 ]; then
die "current branch [$BRANCH] is different than tag [$TAG_NAME]"
fi
#merge release into master
git_do checkout -q "$MASTER_BRANCH" || \
die "Can't checkout to $MASTER_BRANCH"
if ! git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then
git_do merge --no-ff "$BRANCH" || \
die "There were merge conflicts."
fi
#push master to origin
git_do push "$ORIGIN" "$MASTER_BRANCH" || \
die "Could not push to $MASTER_BRANCH from $ORIGIN."
git_do branch -D $BRANCH
echo
echo "Summary of actions:"
echo "- The release '$BRANCH' has merged into '$MASTER_BRANCH'"
echo "- Master '$MASTER_BRANCH' have been pushed to '$ORIGIN'"
echo
}
cmd_track() {
parse_track_args "$@"
if [ -z "$NAME" -o -z "$REMOTE_NAME" ]; then
die "Command 'flow release track $@' format is error , Please use 'flow release track <remote_version> [version]'"
fi
# sanity checks
require_clean_working_tree
require_branch_absent "$BRANCH"
git_do fetch -q "$ORIGIN"
require_branch "$ORIGIN/$REMOTE_BRANCH"
# create tracking branch
git_do checkout -b "$BRANCH" "$ORIGIN/$REMOTE_BRANCH"
echo
echo "Summary of actions:"
echo "- A new remote tracking branch '$BRANCH' was created"
echo "- You are now on branch '$BRANCH'"
echo
}
cmd_pull() {
#DEFINE_string prefix false 'alternative remote feature branch name prefix' p
DEFINE_boolean rebase false "pull with rebase" r
parse_remote_name "$@"
if [ -z "$REMOTE" ]; then
die "Name a remote explicitly."
fi
name_or_current
# To avoid accidentally merging different feature branches into each other,
# die if the current feature branch differs from the requested $NAME
# argument.
local current_branch=$(git_current_branch)
if startswith "$current_branch" "$PREFIX"; then
# we are on a local feature branch already, so $BRANCH must be equal to
# the current branch
avoid_accidental_cross_branch_action || die
fi
require_clean_working_tree
if git_branch_exists "$BRANCH"; then
# Again, avoid accidental merges
avoid_accidental_cross_branch_action || die
# we already have a local branch called like this, so simply pull the
# remote changes in
if flag rebase; then
if ! git_do pull --rebase -q "$REMOTE" "$BRANCH"; then
warn "Pull was aborted. There might be conflicts during rebase or '$REMOTE' might be inaccessible."
exit 1
fi
else
git_do pull -q "$remote" "$branch" || die "failed to pull from remote '$remote'."
fi
echo "Pulled $REMOTE's changes into $BRANCH."
else
# setup the local branch clone for the first time
git_do fetch -q "$REMOTE" "$BRANCH" || die "Fetch failed." # stores in FETCH_HEAD
git_do branch --no-track "$BRANCH" FETCH_HEAD || die "Branch failed."
git_do checkout -q "$BRANCH" || die "Checking out new local branch failed."
echo "Created local branch $BRANCH based on $REMOTE's $BRANCH."
fi
echo
echo "Summary of actions:"
echo "- '$BRANCH' has pulled"
echo
}