forked from MeltwaterArchive/gitflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-hf-chore
670 lines (582 loc) · 18.3 KB
/
git-hf-chore
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#!/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
PREFIX='chore/'
usage() {
echo "usage: git hf chore [list] [-v]"
echo " git hf chore start <name> [<base>]"
echo " git hf chore submit [<name>] [<base>]"
echo " git hf chore finish [-rFkD] [<name|nameprefix>]"
echo " git hf chore diff [<name|nameprefix>]"
echo " git hf chore rebase [-i] [<name|nameprefix>]"
echo " git hf chore checkout <name> [-f]"
echo " git hf chore pull [-r] [<remote> [<name>]]"
echo " git hf chore push [<remote> [<name>]]"
echo " git hf chore cancel [-f] <name>"
echo " git hf chore rename <new-name>"
}
cmd_default() {
cmd_list "$@"
}
cmd_list() {
DEFINE_boolean verbose false 'verbose (more) output' v
parse_args "$@"
local branches=$(git_list_branches_of_type $PREFIX)
branches=$(git_list_branches_of_type $PREFIX)
if [ -z "$branches" ]; then
warn "No chore branches exist."
warn ""
warn "You can start a new chore branch:"
warn ""
warn " git hf chore start <name> [<base>]"
warn ""
exit 0
fi
# determine column width first
local width=0
local branch
for branch in $branches; do
local len=${#branch}
width=$(max $width $len)
done
width=$(($width+3))
local branch
for branch in $branches; do
local fullname=$PREFIX$branch
local remote=
if ! git_local_branch_exists $fullname ; then
fullname="$ORIGIN/$fullname"
remote=" (only at $ORIGIN)"
fi
local base=$(git merge-base "$fullname" "$DEVELOP_BRANCH")
local develop_sha=$(git rev-parse "$DEVELOP_BRANCH")
local branch_sha=$(git rev-parse "$fullname")
local already_merged=$(git_is_branch_merged_into "$fullname" "$DEVELOP_BRANCH")
if [[ $fullname == $CURRENT_BRANCH ]]; then
printf "* "
else
printf " "
fi
if flag verbose; then
printf "%-${width}s" "$branch"
if [ "$branch_sha" = "$develop_sha" ]; then
printf "(no commits yet)"
elif [ "$base" = "$branch_sha" ]; then
printf "(is behind develop, may ff)"
elif [ "$base" = "$develop_sha" ]; then
printf "(based on latest develop)"
elif $already_merged ; then
printf "(already merged; ready for finishing)"
else
printf "(may be rebased)"
fi
printf "%s" "$remote"
else
printf "%s" "$branch"
fi
echo
done
}
cmd_help() {
usage
exit 0
}
require_name_arg() {
if [ "$NAME" = "" ]; then
warn "Missing argument <name>"
usage
exit 1
fi
}
require_newname_arg() {
if [[ -z $NAME ]] ; then
warn "Missing argument <new-name>"
usage
exit 1
fi
}
expand_nameprefix_arg() {
require_name_arg
local expanded_name
local exitcode
expanded_name=$(hubflow_resolve_nameprefix "$NAME" "$PREFIX")
exitcode=$?
case $exitcode in
0) NAME=$expanded_name
BRANCH=$PREFIX$NAME
;;
*) exit 1 ;;
esac
}
use_current_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 not a chore branch."
warn "Please specify a <name> argument."
exit 1
fi
}
expand_nameprefix_arg_or_current() {
if [ "$NAME" != "" ]; then
expand_nameprefix_arg
require_branch "$PREFIX$NAME"
else
use_current_branch_name
fi
}
name_or_current() {
if [ -z "$NAME" ]; then
use_current_branch_name
fi
}
parse_args() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# read arguments into global variables
NAME=$1
BRANCH=$PREFIX$NAME
}
cmd_start() {
DEFINE_boolean fetch true 'fetch from $ORIGIN before creating the new branch' F
parse_args "$@"
BASE=${2:-$DEVELOP_BRANCH}
NEW_BRANCH="$PREFIX$1"
require_name_arg
echo "Base Branch == $BASE"
# sanity checks
require_clean_working_tree
require_remote_available
if flag fetch ; then
hubflow_fetch_latest_changes_from_origin
fi
require_branch_absent "$ORIGIN/$NEW_BRANCH"
# if the origin branch counterpart exists, assert that the local branch
# isn't behind it (to avoid unnecessary rebasing)
if git_branch_exists "$ORIGIN/$BASE"; then
require_branches_equal "$BASE" "$ORIGIN/$BASE"
fi
# create branch
if ! git checkout -b "$NEW_BRANCH" "$BASE"; then
die "Could not create chore branch '$NEW_BRANCH'"
fi
# remember where to merge it back to
git_config_set_branch "$NEW_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 '$NEW_BRANCH' was created, based on '$BASE'"
echo "- The branch '$NEW_BRANCH' has been pushed up to '$ORIGIN/$NEW_BRANCH'"
echo "- You are now on branch '$NEW_BRANCH'"
echo ""
echo "Now, start committing on your chore. When done, create a"
echo "pull request on GitHub. Once that has been merged, use:"
echo ""
echo " git hf chore finish"
echo
echo "to delete the branch both locally and remotely."
}
cmd_submit() {
parse_args "$@"
BASE=$2
name_or_current
# the base is where the chore was created
# most of the time it will be develop branch
# but it could be support branches
if [[ -z $BASE ]]; then
BASE=$(git_config_get_branch $BRANCH hubflowBase)
if [[ -z $BASE ]]; then
BASE="$DEVELOP_BRANCH"
fi
fi
echo "Base Branch == $BASE"
# sanity checks
require_branch "$BRANCH"
require_clean_working_tree
require_github_origin_repo
require_remote_available
hubflow_fetch_latest_changes_from_origin
# push to origin
hubflow_push_latest_changes_to_origin
# pull request details
PR_FILE="./COMMIT_MSG"
PR_TITLE=
PR_DESC=
rm -f "$PR_FILE"
# ask the user for a pull request description
cat <<EOS > "$PR_FILE"
# Please enter the description for your pull request. Lines starting
# with '#' will be ignored, and an empty message aborts the request.
#
# The first line should be a short chore summary, no longer than
# 72 characters.
#
# The subsequent lines should be a longer description of the chore,
# including a summary of backwards-compatibility breaks, wrapped at
# 80 characters.
EOS
# ${VISUAL:-${EDITOR:-vi}} "$PR_FILE"
vi "$PR_FILE"
# extract pull request parameters from description
if [ -r "$PR_FILE" ]; then
PR_TITLE=$(head -n1 "$PR_FILE" | sed -e 's/^[[:space:]]+//' -e 's/"/\\"/g;' )
PR_DESC=$(tail -n+2 "$PR_FILE" | grep -v '^#' | sed -e 's/"/\\"/g;' | tr '\n' '\000' | sed -e 's/\x00/\\n/g;' )
fi
# ensure there's an adequate description
if [ -z "$PR_TITLE" ]; then
die "Aborting submission due to empty summary."
elif [ -z "$PR_DESC" ]; then
warn "You have left the description empty; the review may decide to reject your"
warn "pull request because of this."
fi
# submit pull request to GitHub and
resp=$(github_post \
"/repos/$GITHUB_ORIGIN/pulls" \
"{\"title\":\"$PR_TITLE\",\"body\":\"$PR_DESC\",\"head\":\"$BRANCH\",\"base\":\"$BASE\"}")
# did it succeed?
if echo "$resp" | grep "Validation Failed" > /dev/null ; then
# no, it did not
if echo "$resp" | grep "pull request already exists" > /dev/null ; then
die "A pull request already exists for this chore"
elif echo "$resp" | grep "No commits between" > /dev/null ; then
die "You need to make some commits for this chore before you can make a pull request"
else
warn "An unexpected error was returned from GitHub. Here is the raw response:"
warn
echo "$resp"
exit 1
fi
fi
# parse Pull Request URL from response
PR_URL=$(echo $resp |
awk -F"," '{for(i=1;i<=NF;i++){if($i~/html_url/){print $i"\n"}}}' |
grep 'pull' |
awk -F"\":" '{print $2}' |
awk -F"\"" '{print $2}')
if [ -z "$PR_URL" ]; then
die "Failed to create Pull Request"
else
# if it succeed, delete the pull request file
rm -f "$PR_FILE"
fi
echo
echo "Summary of actions:"
echo "- The branch '$BRANCH' was pushed to '$ORIGIN'"
echo "- A Pull Request from '$BRANCH' to '$BASE' was created at '$PR_URL'"
echo ""
echo "Once the Pull Request has been accepted, cleanup the chore with:"
echo ""
echo " git hf chore finish $NAME"
echo
}
cmd_finish() {
DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F
DEFINE_boolean rebase false "rebase instead of merge" r
DEFINE_boolean keep false "keep branch after performing finish" k
DEFINE_boolean force_delete false "force delete chore branch after finish" D
DEFINE_boolean force_merge false "force merge of chore branch if not merged yet at origin" f
parse_args "$@"
expand_nameprefix_arg_or_current
# where are we merging into?
BASE_BRANCH=$(git_config_get_branch $BRANCH hubflowBase)
echo "Base Branch == $BASE_BRANCH"
# sanity checks
require_branch "$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
if has "$ORIGIN/$BRANCH" $(git_remote_branches); then
require_branches_equal "$BRANCH" "$ORIGIN/$BRANCH"
fi
if has "$ORIGIN/$BASE_BRANCH" $(git_remote_branches); then
require_branches_equal "$BASE_BRANCH" "$ORIGIN/$BASE_BRANCH"
fi
# make sure that the chore branch has been merged into develop
if noflag force_merge ; then
if [[ $(git rev-list -n2 "$BASE_BRANCH..$BRANCH") ]] ; then
echo
echo "Feature branch has not yet been merged into $ORIGIN/$BASE_BRANCH."
echo "Please raise a pull-request via GitHub first, or use the -f flag."
exit 1
fi
fi
# if the user wants to rebase, do that first
if flag rebase; then
if ! git hf chore rebase "$NAME" "$BASE_BRANCH"; then
warn "Finish was aborted due to conflicts during rebase."
warn "Please finish the rebase manually now."
warn "When finished, re-run:"
warn " git hf chore finish '$NAME' '$BASE_BRANCH'"
exit 1
fi
fi
# merge into the base branch
# the merge_helper will not return if there is a merge conflict
# we want to remain on the BASE_BRANCH after the merge is complete
hubflow_local_merge_helper "$BRANCH" "$BASE_BRANCH" no_ff no_checkout_afterwards
# make sure the merge worked
if [[ $(git rev-list -n2 "$BASE_BRANCH..$BRANCH") ]] ; then
die "chore merge failed"
fi
# when no merge conflict is detected, just clean up the chore branch
# delete branch
if flag fetch; then
git push "$ORIGIN" ":refs/heads/$BRANCH"
fi
# switch to the base branch
hubflow_change_branch "$BASE_BRANCH"
# if we merged locally, push those changes up to origin
hubflow_push_latest_changes_to_origin
# delete the local branch if it is no longer needed
if noflag keep; then
# delete the config variable tracking our base branch
git_config_unset_branch $BRANCH hubflowBase
# delete the local branch
if flag force_delete; then
git branch -D "$BRANCH"
else
git branch -d "$BRANCH"
fi
fi
echo
echo "Summary of actions:"
if flag fetch ; then
echo "- The latest changes from '$ORIGIN' were merged into '$MASTER_BRANCH', '$DEVELOP_BRANCH' and any support branches"
fi
echo "- The chore branch '$BRANCH' was merged into '$BASE_BRANCH'"
if flag keep; then
echo "- Feature branch '$BRANCH' is still available"
else
echo "- Feature branch '$BRANCH' has been removed"
fi
if flag fetch; then
echo "- Feature branch '$ORIGIN/$BRANCH' has been removed"
fi
echo "- You are now on branch '$BASE_BRANCH'"
}
cmd_diff() {
parse_args "$@"
if [ "$NAME" != "" ]; then
expand_nameprefix_arg
BASE=$(git merge-base "$DEVELOP_BRANCH" "$BRANCH")
git diff "$BASE..$BRANCH"
else
if ! git_current_branch | grep -q "^$PREFIX"; then
die "Not on a chore branch. Name one explicitly."
fi
BASE=$(git merge-base "$DEVELOP_BRANCH" HEAD)
git diff "$BASE"
fi
}
cmd_checkout() {
DEFINE_boolean fetch true 'fetch from $ORIGIN before creating the new branch' F
parse_args "$@"
if [[ -z "$NAME" ]] ; then
die "Missing argument: <name> - the name of the branch"
fi
# sanity checks
require_clean_working_tree
if flag fetch ; then
hubflow_fetch_latest_changes_from_origin
fi
# thanks to the following folks for this idea:
#
# - https://github.com/bmomberger-reciprocity
# - https://github.com/jhofmeyr
# does the chore already exist?
if git_local_branch_exists "$BRANCH" ; then
# we have a local branch - use that
hubflow_change_branch "$BRANCH"
elif git_remote_branch_exists "$ORIGIN/$BRANCH" ; then
# we have a remote branch - create a local chore branch based
# on the remote branch
git checkout -b "$BRANCH" --track "$ORIGIN/$BRANCH"
else
# the user is really trying to start a new chore
#
# we do not automatically create the chore, in case they have
# mistyped something
warn "The chore '$NAME' does not yet exist locally or remotely."
warn
warn "- Do you need to do a 'git hf update' first?"
warn "- Did you mis-type the name of the chore you want to checkout?"
warn "- Or do you need to use 'git hf chore start $NAME' instead"
warn " to start a new chore?"
exit 1
fi
# branches that are checked out do not get hubflowbase set correctly
# if checking out a branch from a support branch
# one would need to add the hubflow base that is used everywhere
BASE=$(git_config_get_branch $BRANCH hubflowBase)
if [[ -n $2 ]]; then
BASE=$2
git_config_set_branch "$BRANCH" hubflowBase "$BASE"
fi
if [[ -z $BASE ]]; then
BASE=$DEVELOP_BRANCH
git_config_set_branch "$BRANCH" hubflowBase "$BASE"
fi
echo "Base Branch == $BASE"
}
cmd_co() {
# Alias for checkout
cmd_checkout "$@"
}
cmd_rebase() {
DEFINE_boolean interactive false 'do an interactive rebase' i
parse_args "$@"
expand_nameprefix_arg_or_current
warn "Will try to rebase '$NAME'..."
require_clean_working_tree
require_branch "$BRANCH"
git checkout -q "$BRANCH"
local OPTS=
if flag interactive; then
OPTS="$OPTS -i"
fi
git rebase $OPTS "$DEVELOP_BRANCH"
}
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
}
cmd_pull() {
git hf pull "$@"
}
cmd_push() {
git hf push "$@"
}
cmd_cancel() {
DEFINE_boolean fetch true "fetch from $ORIGIN before performing cancel" F
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 chore; cannot cancel a chore without this flag" f
parse_args "$@"
name_or_current
# has the user chosen the force flag?
if noflag force ; then
warn "To prevent you accidentally cancelling a chore, 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 chore branch up to $ORIGIN"
fi
# delete the remote branch
if flag push ; then
git push "$ORIGIN" :"$BRANCH" || \
die "Could not delete the remote $BRANCH in $ORIGIN."
fi
# delete the local branch
if noflag keep ; then
hubflow_change_branch "$DEVELOP_BRANCH"
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 push ; then
echo "- Feature branch '$BRANCH' in '$ORIGIN' has been deleted."
fi
if flag keep ; then
echo "- Feature branch '$BRANCH' is still available locally"
else
echo "- Feature branch '$BRANCH' has been deleted locally"
fi
echo
}
cmd_rename() {
# sort out our args
parse_args "$@"
require_newname_arg
NEWNAME=$NAME
use_current_branch_name
# make sure the new branch has the chore prefix
if ! echo "$NEWNAME" | grep -q "^$PREFIX" ; then
NEWNAME="$PREFIX$NEWNAME"
fi
# we need to know the current state of $ORIGIN
hubflow_fetch_latest_changes_from_origin
# sanity checks
require_branch "$BRANCH"
require_branch_absent "$NEWNAME"
require_branch_absent "$ORIGIN/$NEWNAME"
# keep track of what we did
local removed_remote=0
# do it
git branch -m "$BRANCH" "$NEWNAME" || die "could not rename local branch"
if git_branch_exists "$ORIGIN/$BRANCH" ; then
git push $ORIGIN ":$BRANCH" || die "could not delete remote branch '$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 '$BRANCH' has been renamed to '$NEWNAME'."
if [[ $removed_remote == 1 ]] ; then
echo "- The remote branch '$BRANCH' at $ORIGIN was removed."
fi
echo "- The local branch '$NEWNAME' has been pushed to $ORIGIN."
echo
}