-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-bbflow-hotfix
More file actions
530 lines (433 loc) · 18.1 KB
/
Copy pathgit-bbflow-hotfix
File metadata and controls
530 lines (433 loc) · 18.1 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
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
#
# git-bbflow -- A collection of Git extensions to provide high-level
# repository operations for Blue Billywig's branching model.
#
#
# Copyright (c) 2012 Blue Billywig B.V. All rights reserved.
#
# Based on the work of Vincent Driessen, for which the copyright notice follows.
#
#
# Copyright 2010 Vincent Driessen. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY VINCENT DRIESSEN ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL VINCENT DRIESSEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of Vincent Driessen.
#
require_git_repo
require_gitbbflow_initialized
gitbbflow_load_settings
usage() {
echo "usage: git bbflow hotfix [list] [-v]"
echo " git bbflow hotfix create <version>"
echo " git bbflow hotfix switch [-n] <version>"
echo " git bbflow hotfix abort [-f]"
echo " git bbflow hotfix complete"
echo " git bbflow hotfix rollback"
echo " git bbflow hotfix close"
}
cmd_default() {
cmd_list "$@"
}
cmd_help() {
usage
exit 0
}
cmd_list() {
DEFINE_boolean verbose false 'verbose (more) output' v
parse_args "$@"
git_fetch_origin
local local_hotfix_branches
local remote_hotfix_branches
local short_names
local_release_branches=$(echo "$(git_local_branches)" | grep "^$HOTFIX_PREFIX")
remote_release_branches=$(echo "$(git_remote_branches)" | grep "^$ORIGIN/$HOTFIX_PREFIX")
if [ -z "$local_release_branches" -a -z "$remote_release_branches" ]; then
echo
echo "No hotfix branches exist."
exit 0
fi
short_names=$(git_hotfix_branch_shortnames)
# 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=$HOTFIX_PREFIX$branch
if has "$fullname" $(git_local_branches) && ! has "$ORIGIN/$fullname" $(git_remote_branches); then
if [ "$fullname" = "$BRANCH" ]; then
echo -n "! "
else
echo -n "- "
fi
else
if [ "$fullname" = "$BRANCH" ]; then
echo -n "* "
else
echo -n " "
fi
fi
if flag verbose; then
printf "%-${width}s" "$branch"
if ! has "$fullname" $(git_local_branches); then
echo -n "(remote)"
else
local base=$(git merge-base "$fullname" "$MASTER_BRANCH")
local master_sha=$(git rev-parse "$MASTER_BRANCH")
local branch_sha=$(git rev-parse "$fullname")
if [ "$branch_sha" = "$master_sha" ]; then
echo -n "(no commits yet) "
else
local nicename=$(git rev-parse --short "$base")
echo -n "(based on $nicename) "
fi
if has "$fullname" $(git_local_branches) && ! has "$ORIGIN/$fullname" $(git_remote_branches); then
echo -n "(remote deleted, use 'git bbflow cleanup')"
else
git_compare_branches $fullname "$ORIGIN/$fullname"
local status=$?
if [ $status -gt 0 ]; then
if [ $status -eq 1 ]; then
echo -n "(remote contains updates, use 'git bbflow update' on branch)"
elif [ $status -eq 2 ]; then
echo -n "(changes not pushed to remote yet, use 'git bbflow push' on branch)"
elif [ $status -eq 3 ]; then
echo -n "(remote contains updates, use 'git bbflow update' on branch)"
else
echo -n "(error: remote does not match local branch)"
fi
fi
fi
fi
else
printf "%s" "$branch"
fi
echo
done
}
cmd_create() {
parse_args "$@"
parse_version "$@"
require_version_arg
git_fetch_origin
[ "$ROLE" == "admin" ] || die "Admin role required. Cannot create hotfix."
# sanity checks
require_clean_working_tree
require_no_existing_local_hotfix_branches
require_no_existing_remote_hotfix_branches
# Require release tag to be absent.
if git_tag_exists "$RELEASETAG_PREFIX$VERSION"; then
die "Release $VERSION already exists. Choose another hotfix release version."
fi
require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
# create branch
BRANCH="$HOTFIX_PREFIX$VERSION"
git checkout -q -b "$BRANCH" "$MASTER_BRANCH" || die "Could not create and switch to branch '$BRANCH'."
# publish to origin and track
git push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
git config "branch.$BRANCH.remote" "$ORIGIN"
git config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
echo
echo "Created new hotfix branch '$BRANCH', based on '$MASTER_BRANCH', and published to '$ORIGIN'."
echo "You are now on branch '$BRANCH'."
}
cmd_switch() {
DEFINE_boolean noupdate false "don't update from $ORIGIN" n
parse_args "$@"
parse_version "$@"
require_version_arg
git_fetch_origin
[ "$ROLE" == "admin" ] || die "Admin role required. Cannot switch to hotfix."
BRANCH="$HOTFIX_PREFIX$VERSION"
# sanity checks
require_clean_working_tree
require_branch "$ORIGIN/$BRANCH"
if has "$BRANCH" $(git_local_branches); then
# change to branch
local current_branch=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
if [ "$BRANCH" = "$current_branch" ]; then
die "Already on branch '$BRANCH'."
else
git checkout -q "$BRANCH" || die "Could not switch to branch '$BRANCH'."
if noflag noupdate; then
git_update_from_origin
fi
fi
else
# create tracking branch
git checkout -q -b "$BRANCH" "$ORIGIN/$BRANCH" || die "Could not create and switch to branch '$BRANCH'."
fi
echo
echo "You are now on branch '$BRANCH'."
}
cmd_abort() {
DEFINE_boolean force false "force release abort" f
parse_args "$@"
git_fetch_origin
[ "$ROLE" == "admin" ] || die "Admin role required. Cannot abort hotfix."
# sanity checks
require_clean_working_tree
require_current_hotfix_branch
# Require release tag to be absent.
if git_tag_exists "$RELEASETAG_PREFIX$VERSION"; then
die "Hotfix release $VERSION already completed. Cannot abort this hotfix release. Use 'git bbflow hotfix rollback' if you need to rollback the hotfix release."
fi
if noflag force; then
git_compare_branches "$BRANCH" "$ORIGIN/$BRANCH"
local status=$?
if [ $status -eq 1 -o $status -eq 3 ]; then
die "Remote changes to hotfix release '$VERSION'. Are you sure to abort? Use 'git bbflow hotfix abort -f' to force abort."
fi
git checkout -q "$MASTER_BRANCH"
git branch -d "$BRANCH"
if [ $? -ne 0 ]; then
git checkout -q "$BRANCH"
die "Cannot delete release branch '$BRANCH'. Use 'git bbflow hotfix abort -f' to force abort."
fi
else
git checkout -q "$MASTER_BRANCH"
git branch -D "$BRANCH"
if [ $? -ne 0 ]; then
git checkout -q "$BRANCH"
die "Cannot delete branch '$BRANCH'."
fi
fi
git push "$ORIGIN" ":$BRANCH"
echo
echo "Aborted hotfix release $VERSION."
}
cmd_complete() {
parse_args "$@"
git_fetch_origin
[ "$ROLE" == "admin" ] || die "Admin role required. Cannot complete hotfix."
# sanity checks
require_clean_working_tree
require_current_hotfix_branch
# Check if hotfix branch is up-to-date.
git_compare_branches "$BRANCH" "$ORIGIN/$BRANCH"
local status=$?
[ $status -ne 1 -a $status -ne 3 ] || die "Remote changes to hotfix release $VERSION. Use 'git bbflow update' to update the hotfix."
[ $status -ne 2 ] || die "Local changes to hotfix release $VERSION. Use 'git bbflow push' first to push changes to remote."
[ $status -ne 4 ] || die "Remote and local branch '$BRANCH' unrelated. Unrecoverable error."
# Check if master branch is up-to-date. Update if necessary.
git_compare_branches "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
status=$?
if [ $status -eq 1 -o $status -eq 3 ]; then
echo
echo "Updating '$MASTER_BRANCH' branch from remote '$ORIGIN'."
git checkout -q "$MASTER_BRANCH" || die "Cannot switch to branch '$MASTER_BRANCH'."
git merge "$ORIGIN/$MASTER_BRANCH"
status=$?
if [ $status -eq 1 ]; then
echo
echo "Merge conflict while updating branch '$MASTER_BRANCH' from remote '$ORIGIN'. This should not happen."
echo "Fix conflicts using merge tools and commit using 'git commit',"
echo "or abort the merge resolution by running 'git merge --abort'."
echo
echo "After committing the resolved merge, use:"
echo "- 'git bbflow hotfix switch $VERSION' to switch back to the current hotfix release."
echo "- 'git bbflow hotfix complete' to complete the current hotfix release."
echo
echo "You are now on branch '$MASTER_BRANCH'."
exit 1
fi
git checkout -q "$BRANCH"
fi
# Check if develop branch is up-to-date. Update if necessary.
git_compare_branches "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
status=$?
if [ $status -eq 1 -o $status -eq 3 ]; then
echo
echo "Updating '$DEVELOP_BRANCH' branch from remote '$ORIGIN'."
git checkout -q "$DEVELOP_BRANCH" || die "Cannot switch to branch '$DEVELOP_BRANCH'."
git merge "$ORIGIN/$DEVELOP_BRANCH"
status=$?
if [ $status -eq 1 ]; then
echo
echo "Merge conflict while updating branch '$DEVELOP_BRANCH' from remote '$ORIGIN'. This should not happen."
echo "Fix conflicts using merge tools and commit using 'git commit',"
echo "or abort the merge resolution by running 'git merge --abort'."
echo
echo "After committing the resolved merge, use:"
echo "- 'git bbflow hotfix switch $VERSION' to switch back to the current hotfix release."
echo "- 'git bbflow hotfix complete' to complete the current hotfix release."
echo
echo "You are now on branch '$DEVELOP_BRANCH'."
exit 1
fi
git checkout -q "$BRANCH"
fi
local status
# try to merge into master
# 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 "$BRANCH" "$MASTER_BRANCH"; then
git checkout -q "$MASTER_BRANCH" || die "Could not switch to branch '$MASTER_BRANCH'."
git merge --no-ff "$BRANCH"
status=$?
if [ $status -ne 0 ]; then
echo
echo "Merge conflict while merging hotfix release branch '$BRANCH' into master branch '$MASTER_BRANCH'."
echo "This shouldn't happen while merging the hotfix release branch into the master branch."
echo
echo "You are now on branch '$MASTER_BRANCH'."
exit 1
fi
fi
# Cherry-pick hotfix to release
set +o posix
local hotfix_base_commit=$(diff -u <(git rev-list --first-parent "$MASTER_BRANCH") <(git rev-list --first-parent "$BRANCH") | sed -ne 's/^ //p' | head -1)
# workaround for IntelliJ BashSupport plugin not recognizing process substitution: "
if [ -z "$hotfix_base_commit" ]; then
feature_base_commit="$(git rev-list -n 1 $BRANCH)"
fi
git checkout -q "$DEVELOP_BRANCH" || die "Could not switch to branch '$DEVELOP_BRANCH'."
for commit in $(git cherry "$DEVELOP_BRANCH" "$BRANCH" "$hotfix_base_commit" | grep '^+' | sed -ne 's/^+ //p'); do
git cherry-pick "$commit"
status=$?
if [ $status -ne 0 ]; then
echo
echo "Merge conflict while cherry-picking commit '$commit' of hotfix release $VERSION into branch '$DEVELOP_BRANCH'."
echo "Fix conflicts using merge tools and commit using 'git commit'."
echo
echo "After completing the fix:"
echo "- 'git commit' to commit the fixed cherry-pick."
echo "- 'git bbflow hotfix switch $VERSION' to return to the hotfix."
echo "- 'git bbflow hotfix complete' to continue merging the hotfix into the '$DEVELOP_BRANCH' branch."
echo
echo "You are now on branch '$DEVELOP_BRANCH'."
exit 1
fi
done
# 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
local tagname=$RELEASETAG_PREFIX$VERSION
if ! git_tag_exists "$tagname"; then
git checkout -q "$MASTER_BRANCH" || die "Could not switch to branch '$MASTER_BRANCH'."
git tag -a "$tagname" -m "Hotfix release $VERSION"
status=$?
if [ $status -ne 0 ]; then
git checkout -q "$BRANCH" || warn "Could not switch to branch '$BRANCH'."
echo
echo "Could not tag hotfix release. Run 'git bbflow hotfix complete' again to retry."
exit 1
fi
fi
git push "$ORIGIN" "$MASTER_BRANCH" || warn "Could not push '$MASTER_BRANCH' to '$ORIGIN'."
git push "$ORIGIN" "$DEVELOP_BRANCH" || warn "Could not push '$DEVELOP_BRANCH' to '$ORIGIN'."
git push --tags "$ORIGIN" || warn "Could not push tags to '$ORIGIN'."
git checkout -q "$BRANCH" || die "Could not switch to branch '$BRANCH'."
echo
echo "Merged hotfix release $VERSION into '$MASTER_BRANCH' and '$DEVELOP_BRANCH' branches and tagged it '$tagname'."
}
cmd_close() {
parse_args "$@"
git_fetch_origin
[ "$ROLE" == "admin" ] || die "Admin role required. Cannot close hotfix."
# sanity checks
require_clean_working_tree
require_current_hotfix_branch
require_branches_equal "$BRANCH" "$ORIGIN/$BRANCH"
require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
# Require release tag to be present.
$(git_tag_exists "$RELEASETAG_PREFIX$VERSION") || \
die "Hotfix release $VERSION not yet completed. Cannot close."
# close release branch
git checkout -q "$MASTER_BRANCH" || "Could not switch to branch '$BRANCH'."
git branch -d "$BRANCH" || die "Could not delete branch '$BRANCH'."
# push to remote
git push "$ORIGIN" ":$BRANCH" || warn "Could not push delete '$BRANCH' to '$ORIGIN'."
echo
echo "Closed hotfix release '$VERSION'."
echo
echo "You are now on branch '$MASTER_BRANCH'."
}
cmd_rollback() {
echo "Hotfix release rollback is not implemented yet."
}
parse_args() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
local hotfix_branch=$(echo "$(git_current_branch)" | grep "^$HOTFIX_PREFIX")
if [ ! -z "$hotfix_branch" ]; then
BRANCH="$hotfix_branch"
VERSION="${BRANCH#$HOTFIX_PREFIX}"
fi
}
parse_version() {
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
if [ ! -z $1 ]; then
VERSION=$1
VERSIONARG=$VERSION
local local_hotfix_branch=$(echo "$(git_local_branches)" | grep "^$HOTFIX_PREFIX$VERSION\$")
local remote_hotfix_branch=$(echo "$(git_remote_branches)" | grep "^$ORIGIN/$HOTFIX_PREFIX$VERSION\$")
if [ ! -z $local_hotix_branch ]; then
BRANCH="$local_hotfix_branch"
elif [ ! -z $remote_hotfix_branch ]; then
BRANCH="${remote_hotfix_branch#$ORIGIN/}"
fi
fi
}
require_version_arg() {
if [ "$VERSIONARG" = "" ]; then
warn "Missing argument <version>"
usage
exit 1
fi
}
require_no_existing_local_hotfix_branches() {
local hotfix_branches=$(echo "$(git_local_branches)" | grep "^$HOTFIX_PREFIX")
local first_branch=$(echo ${hotfix_branches} | head -n1)
first_branch=${first_branch#$HOTFIX_PREFIX}
[ -z "$hotfix_branches" ] || \
die "There is an existing hotfix branch ($first_branch). Finish that one first."
}
require_no_existing_remote_hotfix_branches() {
local hotfix_branches=$(echo "$(git_remote_branches)" | grep "^$ORIGIN/$HOTFIX_PREFIX")
local first_branch=$(echo ${hotfix_branches} | head -n1)
first_branch=${first_branch#$HOTFIX_PREFIX}
[ -z "$hotfix_branches" ] || \
die "There is an existing hotfix release branch ($first_branch). Finish that one first."
}
require_current_hotfix_branch() {
local hotfix_branches=$(echo "$(git_local_branches)" | grep "^$HOTFIX_PREFIX")
local current_branch=$(git_current_branch)
if ! has "$current_branch" "$hotfix_branches"; then
die "Not on a hotfix branch."
fi
}
git_hotfix_branch_shortnames() {
{
git_local_branches | grep "^$HOTFIX_PREFIX" | sed "s ^$HOTFIX_PREFIX g"
git_remote_branches | grep "^$ORIGIN/$HOTFIX_PREFIX" | sed "s ^$ORIGIN/$HOTFIX_PREFIX g"
} | sort -u
}