forked from MeltwaterArchive/gitflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-hf-hotfix
631 lines (552 loc) · 18.1 KB
/
git-hf-hotfix
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#!/usr/bin/env bash
#
# HubFlow - a fork of the git-flow tools to apply Vincent Driessen's
# branching model to working with GitHub
#
# Original blog post presenting this model is found at:
# http://nvie.com/git-model
#
# The HubFlow documentation is found at:
# http://datasift.github.com/gitflow/
#
# Feel free to contribute to this project at:
# http://github.com/datasift/gitflow
#
# Copyright 2010 Vincent Driessen. All rights reserved.
# Copyright 2012 MediaSift Ltd. 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_hubflow_initialized
hubflow_load_settings
usage() {
echo "usage: git hf hotfix [list] [-v]"
echo " git hf hotfix start <version> [<base>]"
echo " git hf hotfix finish [-sumpk] <version>"
echo " git hf hotfix track <name>"
echo " git hf hotfix pull [-r] [<remote> [<version>]]"
echo " git hf hotfix push [<remote> [<version>]]"
echo " git hf hotfix cancel [-f] <version>"
echo " git hf hotfix rename <new-version>"
echo " git hf hotfix checkout <version>"
echo " git hf hotfix co <version> -- alias to checkout"
}
cmd_default() {
cmd_list "$@"
}
cmd_list() {
DEFINE_boolean verbose false 'verbose (more) output' v
parse_args "$@"
local hotfix_branches
local current_branch
local short_names
hotfix_branches=$(echo "$(git_local_branches)" | grep "^$HOTFIX_PREFIX")
if [ -z "$hotfix_branches" ]; then
warn "No hotfix branches exist."
warn ""
warn "You can start a new hotfix branch:"
warn ""
warn " git hf hotfix start <version> [<base>]"
warn ""
exit 0
fi
current_branch=$(git_current_branch)
short_names=$(echo "$hotfix_branches" | sed "s ^$HOTFIX_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=$HOTFIX_PREFIX$branch
local base=$(git merge-base "$fullname" "$MASTER_BRANCH")
local master_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" = "$master_sha" ]; then
printf "(no commits yet)"
else
local tagname=$(git name-rev --tags --no-undefined --name-only "$base")
local nicename
if [ "$tagname" != "" ]; then
nicename=$tagname
else
nicename=$(git rev-parse --short "$base")
fi
printf "(based on $nicename)"
fi
else
printf "%s" "$branch"
fi
echo
done
}
cmd_help() {
usage
exit 0
}
name_or_current() {
if [ -z "$NAME" ]; then
use_current_hotfix_branch_name
fi
}
parse_args() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# read arguments into global variables
VERSION=$1
HOTFIX_BRANCH=$HOTFIX_PREFIX$VERSION
}
parse_remote_name() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# read arguments into global variables
REMOTE=$1
NAME=$2
HOTFIX_BRANCH=$HOTFIX_PREFIX$VERSION
}
require_version_arg() {
if [ "$VERSION" == "" ]; then
warn "Missing argument <version>"
usage
exit 1
fi
}
require_newversion_arg() {
if [[ -z $VERSION ]] ; then
warn "Missing argument <new-version>"
usage
exit 1
fi
}
require_no_existing_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."
}
use_current_hotfix_branch_name() {
local current_branch=$(git_current_branch)
if startswith "$current_branch" "$HOTFIX_PREFIX"; then
HOTFIX_BRANCH=$current_branch
NAME=${HOTFIX_BRANCH#$HOTFIX_PREFIX}
else
warn "The current HEAD is not a hotfix branch."
warn "Please specify a <name> argument."
exit 1
fi
}
cmd_start() {
DEFINE_boolean fetch true "fetch from $ORIGIN before creating the new branch" F
parse_args "$@"
if hubflow_is_support_branch ; then
BASE=$CURRENT_BRANCH
else
BASE=$MASTER_BRANCH
fi
BASE=${3:-$BASE}
require_version_arg
# sanity checks
require_clean_working_tree
require_remote_available
if flag fetch ; then
hubflow_fetch_latest_changes_from_origin
fi
require_base_is_on_master_or_support
require_no_existing_hotfix_branches
require_branch_absent "$ORIGIN/$HOTFIX_BRANCH"
require_tag_absent "$VERSION_PREFIX$VERSION"
if has "$ORIGIN/$BASE" $(git_remote_branches); then
require_branches_equal "$BASE" "$ORIGIN/$BASE"
fi
# create branch
git checkout -b "$HOTFIX_BRANCH" "$BASE" || die "Unable to create branch '$HOTFIX_BRANCH'"
# remember where to merge it back to
git_config_set_branch "$HOTFIX_BRANCH" hubflowBase "$BASE"
# push it back up to remote repo
hubflow_push_latest_changes_to_origin
echo
echo "Summary of actions:"
echo "- A new branch '$HOTFIX_BRANCH' was created, based on '$BASE'"
echo "- The branch '$HOTFIX_BRANCH' has been pushed up to '$ORIGIN/$HOTFIX_BRANCH'"
echo "- You are now on branch '$HOTFIX_BRANCH'"
echo
echo "Follow-up actions:"
echo "- Bump the version number now!"
echo "- Start committing your hot fixes"
echo "- When done, run:"
echo
echo " git hf hotfix finish '$VERSION'"
echo
echo " and your changes will be merged back into '$BASE' and '$DEVELOP_BRANCH'."
}
cmd_co() {
cmd_checkout "$@"
}
cmd_checkout() {
DEFINE_boolean fetch true "fetch from $ORIGIN before creating the new branch" F
parse_args "$@"
if hubflow_is_support_branch ; then
BASE=$CURRENT_BRANCH
else
BASE=$MASTER_BRANCH
fi
require_version_arg
require_clean_working_tree
require_remote_available
if flag fetch ; then
hubflow_fetch_latest_changes_from_origin
fi
local hotfix_origin="$ORIGIN/$HOTFIX_BRANCH"
require_remote_branch $hotfix_origin
require_tag_absent "$VERSION_PREFIX$VERSION"
git checkout "$HOTFIX_BRANCH"
# remember where to merge it back to
git_config_set_branch "$HOTFIX_BRANCH" hubflowBase "$BASE"
echo
echo "Summary of actions:"
echo "- You have checked out the hotfix branch $HOTFIX_BRANCH"
echo
echo "Follow-up actions:"
echo "- Add any changes needed to this hotfix"
echo "- When done, run:"
echo
echo " git hf hotfix finish '$VERSION'"
echo
echo " and your changes will be merged back into '$BASE' and '$DEVELOP_BRANCH'."
}
cmd_finish() {
DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
DEFINE_boolean sign false "sign the release tag cryptographically" s
DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u
DEFINE_string message "" "use the given tag message" m
DEFINE_boolean automessage true "use the tag name as the message" M
DEFINE_boolean push true "push to $ORIGIN after performing finish" p
DEFINE_boolean keep false "keep branch after performing finish" k
DEFINE_boolean notag false "don't tag this release" n
DEFINE_boolean nobackmerge false "don't back-merge $MASTER_BRANCH to be a parent of $DEVELOP_BRANCH (using tag if applicable)" b parse_args "$@"
parse_args "$@"
if startswith $(git_current_branch) "$HOTFIX_PREFIX"; then
VERSION=$(echo $(git_current_branch) | sed "s/$HOTFIX_PREFIX/" | cut -c 2-)
fi
require_version_arg
# handle flags that imply other flags
if [ "$FLAGS_signingkey" != "" ]; then
FLAGS_sign=$FLAGS_TRUE
fi
# sanity checks
require_branch "$HOTFIX_BRANCH"
require_clean_working_tree
require_remote_available
# update local repo with remote changes first, if asked
if flag fetch; then
# fetch and merge the latest changes from origin
hubflow_merge_latest_changes_from_origin
fi
# push up to origin
if flag push ; then
hubflow_push_latest_changes_to_origin
fi
# where are we merging into?
BASE=$(git_config_get_branch ${HOTFIX_BRANCH} hubflowBase)
if [[ ${BASE} == ${MASTER_BRANCH} ]] ; then
# this is a release made from develop
#
# we must merge it into MASTER and DEVELOP
tag_branch="$MASTER_BRANCH"
additional_branch="$DEVELOP_BRANCH"
final_branch="$DEVELOP_BRANCH"
else
# this is a release made from a support branch
#
# we must merge it back into the support branch
tag_branch="$BASE"
final_branch="$BASE"
fi
# we need to be up to date before we go any further
for merge_branch in $tag_branch $additional_branch ; do
if has "$ORIGIN/$merge_branch" $(git_remote_branches); then
require_branches_equal "$merge_branch" "$ORIGIN/$merge_branch"
fi
done
# try to merge into the branch we want to tag, first
# in case a previous attempt to finish this release branch has failed,
# but the merge into master was successful, we skip it now
hubflow_local_merge_helper "$HOTFIX_BRANCH" "$tag_branch" no_checkout_afterwards no_ff
if noflag notag; then
# try to tag the release
local tagname="$VERSION_PREFIX$VERSION"
if ! git_tag_exists "$tagname"; then
local opts="-a"
flag sign && opts="$opts -s"
[ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
# use the tag as the message
if flag automessage ; then
FLAGS_message="Released $tagname"
fi
# add the tag message if there is one
[ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
git checkout "$tag_branch" || die "Could not checkout $tag_branch."
eval git tag $opts "$tagname" || \
die "Tagging failed. Please run finish again to retry."
fi
# now merge from the tag into the additional_branch, if there
# is one
if [[ -n $additional_branch ]] ; then
if noflag nobackmerge ; then
# merge from the tag down into the DEVELOP branch
#
# this makes the master branch and the new tag a parent
# of the develop branch, which in turn lets you use
# 'git describe' on either branch
#
# we use 'no_ff' because we want a commit for the merge
# to appear in the git history
hubflow_local_merge_helper "$tagname" "$additional_branch" no_ff no_checkout_afterwards
else
# just merge the release branch into the DEVELOP branch
#
# this means that MASTER and DEVELOP continue to diverge
# for the moment
#
# we use 'no_ff' because we want a commit for the merge
# to appear in the git history
hubflow_local_merge_helper "$HOTFIX_BRANCH" "$additional_branch" no_ff no_checkout_afterwards
fi
fi
else
# there's no tag, so just merge into any additional branch
#
# we use 'no_ff' because we want a commit for the merge to appear
# in the git history
if [[ -n $additional_branch ]] ; then
hubflow_local_merge_helper "$HOTFIX_BRANCH" "$additional_branch" no_ff no_checkout_afterwards
fi
fi
# switch to our final branch
hubflow_change_branch "$final_branch"
# make sure the merge worked before we start deleting stuff
for merge_branch in $tag_branch $additional_branch ; do
if [[ $(git rev-list -n2 "$merge_branch..$HOTFIX_BRANCH") ]] ; then
die "hotfix merge into $merge_branch failed"
fi
done
# delete branch
if noflag keep; then
# remove the config setting for our base branch
git_config_unset_branch "$HOTFIX_BRANCH" hubflowBase
# checkout a different branch before we delete
if [ "$HOTFIX_BRANCH" = "$(git_current_branch)" ]; then
hubflow_change_branch "$MASTER_BRANCH"
fi
git branch -d "$HOTFIX_BRANCH"
fi
if flag push; then
# push the changed branches
for merge_branch in $tag_branch $additional_branch ; do
git push "$ORIGIN" "$merge_branch" || \
die "Could not push $merge_branch to $ORIGIN."
done
# push any new tags
if noflag notag; then
git push --tags "$ORIGIN" || \
die "Could not push tags to $ORIGIN."
fi
# delete the release branch
if noflag keep ; then
git push "$ORIGIN" :"$HOTFIX_BRANCH" || \
die "Could not delete the remote $HOTFIX_BRANCH in $ORIGIN."
fi
fi
echo
echo "Summary of actions:"
if flag push ; then
echo "- Latest objects have been fetched from '$ORIGIN'"
fi
echo "- Branch '$HOTFIX_BRANCH' has been merged into $tag_branch"
if noflag notag; then
echo "- The hotfix was tagged '$tagname'"
if [[ -n $additional_branch ]] ; then
echo "- The tag '$tagname' was merged into $additional_branch"
fi
elif [[ -n $additional_branch ]] ; then
echo "- Branch '$HOTFIX_BRANCH' has been merged into $additional_branch"
fi
if flag keep; then
echo "- Branch '$HOTFIX_BRANCH' is still available"
else
echo "- Branch '$HOTFIX_BRANCH' has been deleted"
fi
if flag push; then
echo "- Changed branches and tags have been pushed to '$ORIGIN'"
if noflag keep ; then
echo "- Branch '$HOTFIX_BRANCH' in '$ORIGIN' has been deleted."
fi
fi
echo
}
cmd_cancel() {
DEFINE_boolean fetch true "fetch from $ORIGIN before performing cancel" F
DEFINE_string message "" "use the given tag message" m
DEFINE_boolean push true "push to $ORIGIN after performing cancel" p
DEFINE_boolean keep false "keep branch after performing cancel" k
DEFINE_boolean force false "safety feature; cannot cancel a hotfix without this flag" f
DEFINE_boolean discard true "drop the changes in this hotfix; do not merge back into develop" d
parse_args "$@"
require_version_arg
# has the user chosen the force flag?
if noflag force ; then
warn "To prevent you accidentally cancelling a hotfix, you _must_ use the -f flag"
warn "with this command"
exit 1
fi
# sanity checks
require_branch "$HOTFIX_BRANCH"
require_clean_working_tree
# update local repo with remote changes first, if asked
if flag fetch ; then
# fetch and merge the latest changes from origin
hubflow_merge_latest_changes_from_origin
fi
hubflow_change_branch "$HOTFIX_BRANCH"
if flag push ; then
hubflow_push_latest_changes_to_origin
fi
if has "$ORIGIN/$HOTFIX_BRANCH" $(git_remote_branches); then
require_branches_equal "$HOTFIX_BRANCH" "$ORIGIN/$HOTFIX_BRANCH"
fi
# where did this branch come from?
BASE=$(git_config_get_branch $HOTFIX_BRANCH hubflowBase)
if [[ $BASE == $MASTER_BRANCH ]] ; then
# this is a hotfix made from master
#
# if we are not discarding it, we will merge it into DEVELOP
merge_branch="$DEVELOP_BRANCH"
else
# this is a release made from a support branch
#
# we must merge it back into the support branch
merge_branch="$BASE"
fi
# we only merge into develop if the user hasn't selected the -d flag
if noflag discard ; then
if has "$ORIGIN/$merge_branch" $(git_remote_branches); then
require_branches_equal "$merge_branch" "$ORIGIN/$merge_branch"
fi
# try to merge into develop | support
hubflow_local_merge_helper "$HOTFIX_BRANCH" "$merge_branch" no_ff no_checkout_afterwards
# push back to remote repo
if flag push ; then
hubflow_push_latest_changes_to_origin
fi
fi
# delete the remote branch
if flag push ; then
git push "$ORIGIN" :"$HOTFIX_BRANCH" || \
die "Could not delete the remote $HOTFIX_BRANCH in $ORIGIN."
fi
# delete local branch
if noflag keep ; then
hubflow_change_branch "$merge_branch"
git branch -D "$HOTFIX_BRANCH"
fi
echo
echo "Summary of actions:"
if flag push ; then
echo "- Latest objects have been fetched from '$ORIGIN'"
fi
if noflag discard ; then
echo "- Hotfix branch has been merged into '$merge_branch'"
if flag push ; then
echo "- '$merge_branch' has been pushed to '$ORIGIN'"
fi
fi
if flag push ; then
echo "- Hotfix branch '$HOTFIX_BRANCH' in '$ORIGIN' has been deleted."
fi
if flag keep ; then
echo "- Hotfix branch '$HOTFIX_BRANCH' is still available locally"
else
echo "- Hotfix branch '$HOTFIX_BRANCH' has been deleted locally"
fi
echo
}
cmd_pull() {
git hf pull "$@"
}
cmd_push() {
git hf push "$@"
}
cmd_rename() {
# sort out our args
parse_args "$@"
require_newversion_arg
NEWNAME=$VERSION
use_current_hotfix_branch_name
# make sure the new branch has the hotfix prefix
if ! echo "$NEWNAME" | grep -q "^$HOTFIX_PREFIX" ; then
NEWNAME="$HOTFIX_PREFIX$NEWNAME"
fi
# we need to know the current state of $ORIGIN
hubflow_fetch_latest_changes_from_origin
# sanity checks
require_branch "$HOTFIX_BRANCH"
require_branch_absent "$NEWNAME"
require_branch_absent "$ORIGIN/$NEWNAME"
require_tag_absent "${NEWNAME#$HOTFIX_PREFIX}"
# keep track of what we did
local removed_remote=0
# do it
git branch -m "$HOTFIX_BRANCH" "$NEWNAME" || die "could not rename local branch"
if git_branch_exists "$ORIGIN/$HOTFIX_BRANCH" ; then
git push $ORIGIN ":$HOTFIX_BRANCH" || die "could not delete remote branch '$HOTFIX_BRANCH' at $ORIGIN"
removed_remote=1
fi
git push "$ORIGIN" "$NEWNAME" || die "could not push new branch '$NEWNAME' to $ORIGIN"
# summary
echo
echo "Summary of actions:"
echo "- The local branch '$HOTFIX_BRANCH' has been renamed to '$NEWNAME'."
if [[ $removed_remote == 1 ]] ; then
echo "- The remote branch '$HOTFIX_BRANCH' at $ORIGIN was removed."
fi
echo "- The local branch '$NEWNAME' has been pushed to $ORIGIN."
echo
}