forked from nvie/gitflow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-flow-hotfix
273 lines (244 loc) · 9.39 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
#
# git-flow -- A collection of Git extensions to provide high-level
# repository operations for Vincent Driessen's branching model.
#
# Original blog post presenting this model is found at:
# http://nvie.com/git-model
#
# Feel free to contribute to this project at:
# http://github.com/nvie/gitflow
#
# 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_gitflow_initialized
gitflow_load_settings
VERSION_PREFIX=$(eval "echo `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 [-Fsumpk] <version>"
}
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 "^$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 branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
short_names=$(echo "$hotfix_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" "$PRODUCTION_BRANCH")
local master_sha=$(git rev-parse "$PRODUCTION_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
}
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_base_is_on_master() {
if ! git branch --no-color --contains "$BASE" 2>/dev/null \
| sed 's/[* ] //g' \
| grep -q "^$PRODUCTION_BRANCH\$"; then
die "fatal: Given base '$BASE' is not a valid commit on '$PRODUCTION_BRANCH'."
fi
}
require_no_existing_hotfix_branches() {
local hotfix_branches=$(echo "$(git_local_branches)" | grep "^$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 true "fetch from $ORIGIN before performing finish" F
parse_args "$@"
BASE=${2:-$PRODUCTION_BRANCH}
require_version_arg
require_base_is_on_master
#require_no_existing_hotfix_branches
# sanity checks
require_clean_working_tree
require_branch_absent "$BRANCH"
require_tag_absent "$VERSION_PREFIX$VERSION"
if flag fetch; then
flow_do "Fetching $ORIGIN" "" fetch -q "$ORIGIN"
sync_branch "$PRODUCTION_BRANCH" "nopush" "--first-parent" || die "Unable to synchronize production"
fi
require_branches_equal "$PRODUCTION_BRANCH" "$ORIGIN/$PRODUCTION_BRANCH"
flow_do "Creating branch $BRANCH starting at $BASE" "" checkout -b "$BRANCH" "$BASE"
# create branch
flow_do "Initializing submodules" "" submodule init
flow_do "Updating submodules" "" submodule update
}
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 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
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
if flag fetch; then
flow_do "Fetching $ORIGIN" "" fetch -q "$ORIGIN"
sync_branch "$PRODUCTION_BRANCH" "nopush" "--first-parent" || die "Unable to synchronize production"
sync_branch "$DEVELOP_BRANCH" "nopush" "--first-parent" || die "Unable to synchronize master"
fi
if ! git_is_branch_merged_into "$PRODUCTION_BRANCH" "$BRANCH"; then
if ! git_is_branch_merged_into "$BRANCH" "$PRODUCTION_BRANCH"; then
echo
echo "Cannot finish. $PRODUCTION_BRANCH is not an ancestor of $BRANCH."
echo "This probably means a HOTFIX or RELEASE was finished after you started $BRANCH."
echo "1) Run 'git merge $PRODUCTION_BRANCH'"
echo "2) Test everything again (production will end up different than what you have tested)."
echo "3) Run finish command again"
echo
git checkout $BRANCH > /dev/null 2>&1
exit 1
else
echo
echo "It appears that $BRANCH has already been merged into $PRODUCTION_BRANCH, you're probably retrying a finish after fixing conflicts."
echo
fi
fi
# 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" "$PRODUCTION_BRANCH"; then
flow_do "Checking out $PRODUCTION_BRANCH" "" checkout "$PRODUCTION_BRANCH"
flow_do "Merging $BRANCH into $PRODUCTION_BRANCH" "There were conflicts. Fix them, commit, and finish again" merge --no-ff --log "$BRANCH" -m "[HOTFIX $VERSION] -> $PRODUCTION_BRANCH"
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
local tagname=v`date -u "+%Y.%m.%d_%H.%M"`"_$BRANCH"
if ! git_tag_exists "$tagname"; then
local opts=""
flag sign && opts="$opts -s"
[ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
[ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
flow_do "Tagging $tagname" "" tag $opts "$tagname"
fi
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 "$BRANCH" "$DEVELOP_BRANCH"; then
flow_do "Checking out $DEVELOP_BRANCH" "" checkout "$DEVELOP_BRANCH"
flow_do "Merging $BRANCH into $DEVELOP_BRANCH" "" merge --no-ff --log "$BRANCH" -m "[HOTFIX $VERSION] -> $DEVELOP_BRANCH"
fi
# delete branch
if noflag keep; then
if [ "$BRANCH" = "$(git_current_branch)" ]; then
flow_do "Checking out $DEVELOP_BRANCH" "" checkout "$DEVELOP_BRANCH"
fi
flow_do "Deleting $BRANCH" "" branch -D "$BRANCH"
fi
if flag push; then
flow_do "Pushing $DEVELOP_BRANCH to $ORIGIN" "" push "$ORIGIN" "$DEVELOP_BRANCH"
flow_do "Pushing $PRODUCTION_BRANCH to $ORIGIN" "" push "$ORIGIN" "$PRODUCTION_BRANCH"
if noflag notag; then
flow_do "Pushing tog $tagname to $ORIGIN" "" push "$ORIGIN" "$tagname"
fi
flow_do "Deleting $BRANCH at $ORIGIN" "" push "$ORIGIN" ":refs/heads/$BRANCH"
fi
flow_do "Checking out $PRODUCTION_BRANCH" "" checkout "$PRODUCTION_BRANCH"
}