forked from MeltwaterArchive/gitflow
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit-hf-direct-release
More file actions
331 lines (292 loc) · 9.38 KB
/
git-hf-direct-release
File metadata and controls
331 lines (292 loc) · 9.38 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
require_git_repo
require_hubflow_initialized
hubflow_load_settings
VERSION_PREFIX=$(eval "echo `git config --get hubflow.prefix.versiontag`")
PREFIX=$(git config --get hubflow.prefix.release)
usage() {
echo "usage: git hf direct-release [list] [-v]"
echo " git hf direct-release list <version> [<base>]"
echo " git hf direct-release perform <version> [<base>]"
echo " git hf direct-release cancel <version> [<base>]"
}
cmd_default() {
cmd_help
}
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_develop() {
if ! git branch --no-color --contains "$BASE" 2>/dev/null \
| sed 's/[* ] //g' \
| grep -q "^$DEVELOP_BRANCH\$"; then
die "fatal: Given base '$BASE' is not a valid commit on '$DEVELOP_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."
}
cmd_list() {
git tag | tail -n 5
}
cmd_perform() {
start_release "$@"
finish_release "$@"
}
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 release without this flag" f
DEFINE_boolean discard true "drop the changes in this release; 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 release, you _must_ use the -f flag"
warn "with this command"
exit 1
fi
# sanity checks
require_branch "$BRANCH"
require_clean_working_tree
if flag push ; then
git push "$ORIGIN" "$BRANCH" || die "Could not push release branch up to $ORIGIN"
fi
# delete branch
if noflag keep ; then
if [ "$BRANCH" = "$(git_current_branch)" ]; then
git checkout "$DEVELOP_BRANCH"
fi
# do we need to delete remote branch too?
if flag push ; then
git push "$ORIGIN" :"$BRANCH" || \
die "Could not delete the remote $BRANCH in $ORIGIN."
fi
git branch -d "$BRANCH"
fi
echo
echo "Summary of actions:"
if flag push ; then
echo "- Latest objects have been fetched from '$ORIGIN'"
fi
if flag keep ; then
echo "- Release branch '$BRANCH' is still available"
else
echo "- Release branch '$BRANCH' has been deleted"
if flag push ; then
echo "- Release branch '$BRANCH' in '$ORIGIN' has been deleted."
fi
fi
echo
}
start_release() {
DEFINE_boolean fetch true "fetch from $ORIGIN before creating the new branch" F
parse_args "$@"
BASE=${2:-$DEVELOP_BRANCH}
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_develop
require_no_existing_release_branches
# sanity checks
require_clean_working_tree
require_branch_absent "$BRANCH"
require_tag_absent "$VERSION_PREFIX$VERSION"
if noflag nofetch; then
git fetch -q "$ORIGIN" "$DEVELOP_BRANCH"
fi
if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then
require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
fi
# create branch
git checkout -b "$BRANCH" "$BASE"
# push it back up to remote repo
hubflow_push_latest_changes_to_origin
echo
echo "Summary of actions (release start):"
echo "- A new branch '$BRANCH' was created, based on '$BASE'"
echo
}
finish_release() {
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
DEFINE_boolean nobackmerge false "don't back-merge $MASTER_BRANCH to be a parent of $DEVELOP_BRANCH (using tag if applicable)"
# handle flags that imply other flags
if [ "$FLAGS_signingkey" != "" ]; then
FLAGS_sign=$FLAGS_TRUE
fi
# we need to be up to date before we go any further
if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then
require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH"
fi
if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then
require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
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" "$MASTER_BRANCH"; then
git checkout "$MASTER_BRANCH" || \
die "Could not check out $MASTER_BRANCH."
git merge --no-ff --no-edit -srecursive -Xtheirs "$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
local tagname=$VERSION_PREFIX$VERSION
if ! git_tag_exists "$tagname"; then
# local opts="-a"
local opts=""
flag sign && opts="$opts -s"
[ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'"
[ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'"
eval git tag $opts "$tagname" || \
die "Tagging failed. Please run finish again to retry."
fi
fi
# try to merge into develop
if noflag nobackmerge; then
# 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 "$MASTER_BRANCH" "$DEVELOP_BRANCH"; then
git checkout "$DEVELOP_BRANCH" || \
die "Could not check out $DEVELOP_BRANCH."
# merge the master branch back into develop; this makes the master
# branch - and the new tag (if provided) - a parent of the development
# branch, which in turn lets you use 'git describe' on either branch
if noflag notag; then
git merge --no-ff --no-edit "$tagname" || \
die "There were merge conflicts."
else
git merge --no-ff --no-edit "$MASTER_BRANCH" || \
die "There were merge conflicts."
fi
fi
else
# 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
git checkout "$DEVELOP_BRANCH" || \
die "Could not check out $DEVELOP_BRANCH."
# just merge the release branch into the development branch
git merge --no-ff --no-edit "$BRANCH" || \
die "There were merge conflicts."
fi
fi
# delete branch
if noflag keep; then
if [ "$BRANCH" = "$(git_current_branch)" ]; then
git checkout "$MASTER_BRANCH"
fi
git branch -d "$BRANCH"
fi
if flag push; then
git push "$ORIGIN" "$DEVELOP_BRANCH" || \
die "Could not push to $DEVELOP_BRANCH from $ORIGIN."
git push "$ORIGIN" "$MASTER_BRANCH" || \
die "Could not push to $MASTER_BRANCH from $ORIGIN."
if noflag notag; then
git push --tags "$ORIGIN" || \
die "Could not push tags to $ORIGIN."
fi
if noflag keep ; then
git push "$ORIGIN" :"$BRANCH" || \
die "Could not delete the remote $BRANCH in $ORIGIN."
fi
fi
echo
echo "Summary of actions (release finish):"
if flag push ; then
echo "- Latest objects have been fetched from '$ORIGIN'"
fi
echo "- Release branch has been merged into '$MASTER_BRANCH'"
if noflag notag; then
echo "- The release was tagged '$tagname'"
if noflag nobackmerge; then
echo "- Tag '$tagname' has been back-merged into '$DEVELOP_BRANCH'"
fi
fi
if flag nobackmerge; then
echo "- Release branch has been merged into '$DEVELOP_BRANCH'"
else
echo "- Branch '$MASTER_BRANCH' has been back-merged into '$DEVELOP_BRANCH'"
fi
if flag keep; then
echo "- Release branch '$BRANCH' is still available"
else
echo "- Release branch '$BRANCH' has been deleted"
fi
if flag push; then
echo "- '$DEVELOP_BRANCH', '$MASTER_BRANCH' and tags have been pushed to '$ORIGIN'"
if noflag keep ; then
echo "- Release branch '$BRANCH' in '$ORIGIN' has been deleted."
fi
fi
echo
}
parse_remote_name() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# read arguments into global variables
REMOTE=$1
NAME=$2
BRANCH=$PREFIX$NAME
}
name_or_current() {
if [ -z "$NAME" ]; then
use_current_release_branch_name
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, hubflow aborted."
return 1
fi
return 0
}
use_current_release_branch_name() {
local current_branch=$(git_current_branch)
if startswith "$current_branch" "$PREFIX"; then
BRANCH=$current_branch
NAME=${BRANCH#$PREFIX}
else
warn "The current HEAD is no release branch."
warn "Please specify a <name> argument."
exit 1
fi
}