This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
/
git-flow-hotfix
450 lines (394 loc) · 14.7 KB
/
git-flow-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
#
# git-flow -- A collection of Git extensions to provide high-level
# repository operations for Vincent Driessen's branching model.
#
# A blog post presenting this model is found at:
# http://blog.avirtualhome.com/development-workflow-using-git/
#
# Feel free to contribute to this project at:
# http://github.com/petervanderdoes/gitflow
#
# Authors:
# Copyright 2012 Peter van der Does. All rights reserved.
#
# Original Author:
# 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER 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.
#
init() {
require_git_repo
require_gitflow_initialized
gitflow_load_settings
VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag)
PREFIX=$(git config --get gitflow.prefix.hotfix)
}
usage() {
echo "usage: git flow hotfix [list] [-v]"
echo " git flow hotfix start [-F] <version> [<base>]"
echo " git flow hotfix finish [-Fsumpknbf] <version>"
echo " git flow hotfix publish <version>"
echo " git flow hotfix delete [-fr] <version>"
}
cmd_default() {
cmd_list "$@"
}
cmd_list() {
DEFINE_boolean verbose false 'verbose (more) output' v
parse_args "$@"
local hotfix_branches
local current_branch
hotfix_branches=$(git_local_branches_prefixed "$PREFIX")
if [ -z "$hotfix_branches" ]; then
warn "No hotfix branches exist."
warn ""
warn "You can start a new hotfix branch:"
warn ""
warn " git flow hotfix start <version> [<base>]"
warn ""
exit 0
fi
current_branch=$(git_current_branch)
# Determine column width first
local width=0
local branch
for branch in $hotfix_branches; do
local len=${#branch}
width=$(max $width $len)
done
width=$(($width+3-${#PREFIX}))
local branch
for branch in $hotfix_branches; do
local base=$(git merge-base "$branch" "$MASTER_BRANCH")
local master_sha=$(git rev-parse "$MASTER_BRANCH")
local branch_sha=$(git rev-parse "$branch")
if [ "$branch" = "$current_branch" ]; then
printf "* "
else
printf " "
fi
if flag verbose; then
printf "%-${width}s" "${branch#$PREFIX}"
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#$PREFIX}"
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_no_existing_hotfix_branches() {
local hotfix_branches=$(git_local_branches_prefixed "$PREFIX")
local first_branch=$(echo ${hotfix_branches} | head -n1)
first_branch=${first_branch#$PREFIX}
[ -z "$hotfix_branches" ] || die "There is an existing hotfix branch '$first_branch'. Finish that one first."
}
cmd_start() {
DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F
parse_args "$@"
eval set -- "${FLAGS_ARGV}"
BASE=${2:-$MASTER_BRANCH}
# No need to continue if not clean
require_clean_working_tree
# Update the local repo with remote changes, if asked
if flag fetch; then
git_fetch_branch "$ORIGIN" "$MASTER_BRANCH"
fi
# Run filter on the version
VERSION=$(run_filter_hook hotfix-start-version $VERSION)
# As VERSION might have changed reset BRANCH with new VERSION
BRANCH=$PREFIX$VERSION
gitflow_require_version_arg
git_is_ancestor "$BASE" "$MASTER_BRANCH" || die "Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'."
require_no_existing_hotfix_branches
# Sanity checks
require_branch_absent "$BRANCH"
require_tag_absent "$VERSION_PREFIX$VERSION"
if git_remote_branch_exists "$ORIGIN/$MASTER_BRANCH"; then
require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
fi
run_pre_hook "$VERSION_PREFIX$VERSION" "$ORIGIN" "$BRANCH" "$BASE"
# Create branch
git checkout -b "$BRANCH" "$BASE" || die "Could not create hotfix branch '$BRANCH'."
run_post_hook "$VERSION_PREFIX$VERSION" "$ORIGIN" "$BRANCH" "$BASE"
echo
echo "Summary of actions:"
echo "- A new branch '$BRANCH' was created, based on '$BASE'"
echo "- You are now on branch '$(git_current_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 flow hotfix finish '$VERSION'"
echo
}
cmd_publish() {
parse_args "$@"
gitflow_require_version_arg
# Sanity checks
require_clean_working_tree
require_branch "$BRANCH"
git fetch -q "$ORIGIN" || die "Could not fetch branch '$BRANCH' from remote '$ORIGIN'."
require_branch_absent "$ORIGIN/$BRANCH"
run_pre_hook "$VERSION_PREFIX$VERSION" "$ORIGIN" "$BRANCH"
# Create remote branch
git push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
git fetch -q "$ORIGIN" || die "Could not fetch branch '$BRANCH' from '$ORIGIN'."
# Configure remote tracking
git config "branch.$BRANCH.remote" "$ORIGIN"
git config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
git checkout "$BRANCH" || die "Could not check out branch '$BRANCH'."
run_post_hook "$VERSION_PREFIX$VERSION" "$ORIGIN" "$BRANCH"
echo
echo "Summary of actions:"
echo "- A new remote branch '$BRANCH' was created"
echo "- The local branch '$BRANCH' was configured to track the remote branch"
echo "- You are now on branch '$(git_current_branch)'"
echo
}
cmd_finish() {
DEFINE_boolean fetch false "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_string messagefile "" "use the contents of the given file as tag message" f
DEFINE_boolean push false "push to $ORIGIN after performing finish" p
DEFINE_boolean keep false "keep branch after performing finish" k
DEFINE_boolean keepremote false "keep the remote branch"
DEFINE_boolean keeplocal false "keep the local branch"
DEFINE_boolean notag false "don't tag this release" n
DEFINE_boolean nobackmerge false "don't back-merge $MASTER_BRANCH, or tag if applicable, in $DEVELOP_BRANCH " b
parse_args "$@"
gitflow_require_version_arg
# Handle flags that imply other flags
if [ "$FLAGS_signingkey" != "" ]; then
FLAGS_sign=$FLAGS_TRUE
fi
# Keeping both branches implies the --keep flag to be true.
if flag keepremote && flag keeplocal; then
FLAGS_keep=$FLAGS_TRUE
fi
# Sanity checks
require_branch "$BRANCH"
require_clean_working_tree
# We always fetch the Branch from Origin
# This is done to avoid possible commits on the remote that are not
# merged into the local branch
if git_remote_branch_exists "$ORIGIN/$BRANCH"; then
git_fetch_branch "$ORIGIN" "$BRANCH"
fi
# Update local branches with remote branches
if flag fetch; then
git_fetch_branch "$ORIGIN" "$MASTER_BRANCH"
git_fetch_branch "$ORIGIN" "$DEVELOP_BRANCH"
fi
# Check if the local branches have all the commits from the remote branches
if git_remote_branch_exists "$ORIGIN/$BRANCH"; then
require_branches_equal "$BRANCH" "$ORIGIN/$BRANCH"
fi
if git_remote_branch_exists "$ORIGIN/$MASTER_BRANCH"; then
require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
fi
if git_remote_branch_exists "$ORIGIN/$DEVELOP_BRANCH"; then
require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
fi
# Check if the hotfix branch:
# - has commits: No reason to finish a hotfix without commits
# - Is ahead of the master: If it's not a good idea to merge
# - Can be merged: If there's no common ancestor we can't merge the hotfix
git_compare_refs "$BRANCH" "$MASTER_BRANCH"
case $? in
0) die "You need some commits in the hotfix branch '$BRANCH'"
;;
1) die "The hotfix branch '$BRANCH' is not ahead of branch '$MASTER_BRANCH'"
;;
4) die "The hotfix branch '$BRANCH' has no common ancestor with branch '$MASTER_BRANCH'"
;;
*) ;;
esac
run_pre_hook "$VERSION_PREFIX$VERSION" "$ORIGIN" "$BRANCH"
# 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 "$MASTER_BRANCH" || die "Could not check out branch '$MASTER_BRANCH'."
git merge --no-ff "$BRANCH" || die "There were merge conflicts."
# TODO: What do we do now?
fi
if noflag notag; then
# 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 ! git_tag_exists "$VERSION_PREFIX$VERSION"; then
if [ "$FLAGS_message" != "" ] && [ "$FLAGS_messagefile" != "" ]; then
die "Use either -m,--message or -f,--messagefile. Can not use both options at the same time"
fi
local opts="-a"
flag sign && opts="$opts -s"
[ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
[ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
[ "$FLAGS_messagefile" != "" ] && opts="$opts -F '$FLAGS_messagefile'"
eval git tag $opts "$VERSION_PREFIX$VERSION" || die "Tagging failed. Please run finish again to retry."
fi
fi
# By default we back-merge the $MASTER_BRANCH unless the user explicitly
# stated not to do a back-merge, in that case we use the $BRANCH.
if noflag nobackmerge; then
MERGE_BRANCH="$MASTER_BRANCH"
else
MERGE_BRANCH="$BRANCH"
fi
# Try to merge into develop.
# In case a previous attempt to finish this release branch has failed,
# but the merge into develop was successful, we skip it now
if ! git_is_branch_merged_into "$MERGE_BRANCH" "$DEVELOP_BRANCH"; then
git checkout "$DEVELOP_BRANCH" || die "Could not check out branch '$DEVELOP_BRANCH'."
if noflag nobackmerge; then
# Accounting for 'git describe', if a release is tagged
# we use the tag commit instead of the branch.
if noflag notag; then
commit="$VERSION_PREFIX$VERSION"
else
commit="$MASTER_BRANCH"
fi
else
commit="$BRANCH"
fi
git merge --no-ff "$commit" || die "There were merge conflicts."
# TODO: What do we do now?
fi
run_post_hook "$VERSION_PREFIX$VERSION" "$ORIGIN" "$BRANCH"
# Delete branch
if noflag keep; then
if noflag keeplocal; then
git branch -d "$BRANCH"
fi
if noflag keepremote;then
local remotebranch=0
if git_remote_branch_exists "$ORIGIN/$BRANCH"; then
remotebranch=1
git_remote_branch_delete "$BRANCH"
fi
fi
fi
if flag push; then
git push "$ORIGIN" "$DEVELOP_BRANCH" || die "Could not push branch '$DEVELOP_BRANCH' to remote '$ORIGIN'."
git push "$ORIGIN" "$MASTER_BRANCH" || die "Could not push branch '$MASTER_BRANCH' to remote '$ORIGIN'."
if noflag notag; then
git push --tags "$ORIGIN" || die "Could not push tags to remote '$ORIGIN'."
fi
fi
echo
echo "Summary of actions:"
if flag fetch; then
echo "- Latest objects have been fetched from '$ORIGIN'"
fi
echo "- Hotfix branch '$BRANCH'has been merged into '$MASTER_BRANCH'"
if noflag notag; then
echo "- The hotfix was tagged '$VERSION_PREFIX$VERSION'"
fi
[ "$commit" = "$MASTER_BRANCH" ] && echo "- Master branch '$MASTER_BRANCH' has been back-merged into '$DEVELOP_BRANCH'"
[ "$commit" = "$VERSION_PREFIX$VERSION" ] && echo "- Hotfix tag '$VERSION_PREFIX$VERSION' has been back-merged into '$DEVELOP_BRANCH'"
[ "$commit" = "$BRANCH" ] && echo "- Hotfix branch '$BRANCH' has been merged into '$DEVELOP_BRANCH'"
if noflag keep; then
local keepmsg="'has been deleted"
if flag keeplocal; then
keepmsg="is locally still available."
if [ $remotebranch -eq 1 ]; then
keepmsg=$keepmsg" It is deleted from '$ORIGIN'."
fi
fi
if flag keepremote ; then
if git_remote_branch_exists "$ORIGIN/$BRANCH"; then
keepmsg="is remotely still available. It is deleted locally."
fi
fi
else
keepmsg="is still available"
fi
echo "- Hotfix branch '$BRANCH' "$keepmsg
if flag push; then
echo "- '$DEVELOP_BRANCH', '$MASTER_BRANCH' and tags have been pushed to '$ORIGIN'"
fi
echo "- You are now on branch '$(git_current_branch)'"
echo
}
cmd_delete() {
DEFINE_boolean force false "force deletion" f
DEFINE_boolean remote false "delete remote branch" r
parse_args "$@"
gitflow_require_version_arg
# Sanity checks
require_branch "$BRANCH"
run_pre_hook "$VERSION" "$ORIGIN" "$BRANCH"
local current_branch=$(git_current_branch)
# We can't delete a branch we are on, switch to the master branch.
if [ "$BRANCH" = "$current_branch" ]; then
require_clean_working_tree
git checkout "$MASTER_BRANCH" || die "Could not check out branch '$MASTER_BRANCH'."
fi
if ( git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH" && git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH" ); then
git branch -d "$BRANCH" || die "Could not delete the $BRANCH."
if flag remote; then
git push "$ORIGIN" :"$BRANCH" || die "Could not delete the remote $BRANCH in $ORIGIN."
fi
else
if flag force; then
git branch -D "$BRANCH" || die "Could not delete the $BRANCH."
if flag remote; then
git push "$ORIGIN" :"$BRANCH" || die "Could not delete the remote $BRANCH in $ORIGIN."
fi
else
die "Hotfix branch '$BRANCH' has been not been merged in branch '$MASTER_BRANCH' and/or branch '$DEVELOP_BRANCH'. Use -f to force the deletion."
fi
fi
run_post_hook "$VERSION" "$ORIGIN" "$BRANCH"
echo
echo "Summary of actions:"
echo "- Hotfix branch '$BRANCH' has been deleted."
flag remote && echo "- Hotfix branch '$BRANCH' in '$ORIGIN' has been deleted."
echo "- You are now on branch '$(git_current_branch)'"
echo
}