forked from RasmusVoss/gitflow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-flow-fix
937 lines (805 loc) · 28.1 KB
/
git-flow-fix
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
#
# 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.
#
init() {
COMMAND="git flow fix"
require_git_repo
require_gitflow_initialized
gitflow_load_settings
PREFIX_RELEASE=$(git config --get gitflow.prefix.release)
VERSION_PREFIX=$(eval "echo `git config --get gitflow.prefix.versiontag`")
PREFIX=$(git config --get gitflow.prefix.fix)
}
cmd_test() {
echo "PREFIX = $PREFIX"
echo "DEVELOP = $RELEASE_BRANCH"
echo "VERSION = $VERSION"
}
cmd_version() {
VERSION=$1
require_and_store_version_arg
}
usage() {
echo "usage: $COMMAND [list] [-v]"
echo " $COMMAND version <version>"
echo " $COMMAND start [-F] <name> [<base>]"
echo " $COMMAND delete [<name|nameprefix>]"
echo " $COMMAND publish <name>"
echo " $COMMAND submit <name>"
# echo " $COMMAND fromreview <name>"
echo " $COMMAND track <name>"
echo " $COMMAND diff [<name|nameprefix>]"
echo " $COMMAND fix checkout [<name|nameprefix>]"
# echo " $COMMAND fix check"
# echo " $COMMAND fix pull [-r] <remote> [<name>]"
# echo " $COMMAND fix rebase [-i] [<name|nameprefix>]"
# echo " $COMMAND fix finish [-rFkDS] [<name|nameprefix>]"
echo " "
echo "Type '$COMMAND help <command>' for further information"
}
init_restore_branch() {
trap "git checkout -q $(git_symbolic_head)" INT TERM EXIT
}
cancel_restore_branch() {
trap - INT TERM EXIT
}
cmd_default() {
cmd_list "$@"
}
cmd_list() {
DEFINE_boolean verbose false 'verbose (more) output' v
parse_args "$@"
VERSION=$1
[[ -z "VERSION" ]] && PREFIX_VERSION=$VERSION/
local releasefix_branches
local current_branch
local short_names
releasefix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX$PREFIX_VERSION")
if [ -z "$releasefix_branches" ]; then
warn "No fix branches exist."
warn ""
warn "You can start a new fix branch:"
warn ""
warn " $COMMAND start <version> <name>"
warn ""
exit 0
fi
current_branch=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
short_names=$(echo "$releasefix_branches" | sed "s ^$PREFIX$PREFIX_VERSION \1 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$PREFIX_VERSION$branch
local base=$(git merge-base "$fullname" "$RELEASE_BRANCH")
local release_sha=$(git rev-parse "$RELEASE_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" = "$release_sha" ]; then
printf "(no commits yet)"
elif [ "$base" = "$branch_sha" ]; then
printf "(is behind $RELEASE_BRANCH, may ff)"
elif [ "$base" = "$release_sha" ]; then
printf "(based on latest $RELEASE_BRANCH)"
else
printf "(may be rebased)"
fi
else
printf "%s" "$branch"
fi
echo
done
}
cmd_help() {
echo " "
case "$1" in
list)
echo "$COMMAND [list] [<version>] [-v]"
echo " List all the fix you have local"
echo " Use -v for a more verbose output"
;;
start)
echo "git flow feature start [-F] <name> <base>"
echo " Start a new feature branch with <name>."
echo " Branch is created from <base>."
echo " If -F is given git will fetch current version from remote."
;;
delete)
echo "git flow feature delete <name|prefix>"
echo " Delete feature branch(es) with <name> or prefix."
;;
publish)
echo "git flow feature publish <name>"
echo " Push feature branch <name> to remote."
echo " Allows others to participate using track <name>."
;;
submit)
echo "git flow feature submit <name>"
echo " Push the feature branch <name> as single commit to gerrit for review"
;;
track)
echo "git flow feature track <name>"
echo " Pulls a remote existing feature branch so you can participate"
;;
diff)
echo "git flow feature diff <name>"
echo " Shows the diff for the feature branch <name>"
;;
check)
echo "git flow feature check"
echo " Checks your gerrit access data"
;;
*) echo "currently no future information is available for the selected command"
echo "this means maybe you should not use it"
echo " "
usage ;;
esac
echo " "
exit 0
}
require_name_arg() {
if [ "$NAME" = "" ]; then
warn "Missing argument <name>"
usage
exit 1
fi
}
require_and_store_version_arg() {
if [ "$VERSION" = "" ]; then
warn "Missing argument <version>"
usage
exit 1
else
if [ "$VERSION" != "$(get_stored_version)" ]; then
git_do config gitflow.fix.version $VERSION
echo "Stored current VERSION=$VERSION"
fi
fi
}
get_stored_version() {
echo $(git config --get gitflow.fix.version)
}
expand_nameprefix_arg() {
require_name_arg
require_and_store_version_arg
local expanded_name
local exitcode
expanded_name=$(gitflow_resolve_nameprefix "$NAME" "$PREFIX$VERSION/")
exitcode=$?
case $exitcode in
0) NAME=$expanded_name
BRANCH=$PREFIX$VERSION/$NAME
;;
*) exit 1 ;;
esac
REMOTE_BRANCH=$(get_tracked_branch)
}
use_current_fix_branch_name() {
local current_branch=$(git_current_branch)
if startswith "$current_branch" "$PREFIX$VERSION/"; then
BRANCH=$current_branch
NAME=${BRANCH#$PREFIX$VERSION/}
else
warn "The current HEAD is no feature branch."
warn "Please specify a <name> argument."
exit 1
fi
REMOTE_BRANCH=$(get_tracked_branch)
}
expand_nameprefix_arg_or_current() {
if [ "$NAME" != "" ]; then
expand_nameprefix_arg
require_branch "$PREFIX$VERSION/$NAME"
else
use_current_fix_branch_name
fi
}
name_or_current() {
if [ -z "$NAME" ]; then
use_current_fix_branch_name
fi
}
get_tracked_branch() {
[ "$(git config "branch.$BRANCH.remote")" == "$ORIGIN" ] || return 1
local remote_branch=$(git config "branch.$BRANCH.merge" | sed -n 's,^refs/heads/,,p')
[ "$remote_branch" ] || return 1
has "$ORIGIN/$remote_branch" $(git_remote_branches) || return 1
echo "$remote_branch"
}
has_tracked_branch() {
get_tracked_branch >/dev/null
}
parse_args() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# read arguments into global variables
VERSION=${2:-$(get_stored_version)}
NAME=$1
BRANCH=$PREFIX$VERSION/$NAME
RELEASE_BRANCH=$PREFIX_RELEASE$VERSION
REMOTE_BRANCH=$(get_tracked_branch)
}
parse_args_version() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# read arguments into global variables
VERSION=${2:-$(get_stored_version)}
NAME=$1
BRANCH=$PREFIX$VERSION/$NAME
RELEASE_BRANCH=$PREFIX_RELEASE$VERSION
REMOTE_BRANCH=$(get_tracked_branch)
}
parse_remote_name() {
# parse options
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
# read arguments into global variables
REMOTE=$1
NAME=$2
BRANCH=$PREFIX$NAME
}
gerrit_push() {
local err
err=$(git push "$ORIGIN" "$1" 2>&1) || {
echo "Gerrit Error: " >&2
echo "$err" | egrep '(error|!)' >&2
return 1
}
}
cmd_check() {
echo "Checking gerrit access data"
[ "$(gerrit_check)" == "0" ] && die "Cannot locate gerrit connection information"
local test_request=$(gerrit_query "is:open")
local own=$(echo "$test_request" | gitflow-JSON.sh | grep -c '\[[0-9+]\]')
echo "You currently own $own open reviews"
}
cmd_start() {
DEFINE_boolean fetch false 'fetch from origin before performing local operation' F
parse_args "$@"
BASE=${3:-$RELEASE_BRANCH}
[ "$BASE" ] || fail "Could not determine which 'release' to branch from. Specify a base explicitly."
require_name_arg
require_and_store_version_arg
# sanity checks
require_branch_absent "$BRANCH"
# update the local repo with remote changes, if asked
if flag fetch; then
git_do fetch -q "$ORIGIN" "$PREFIX_RELEASE$VERSION"
fi
# if the origin branch counterpart exists, assert that the local branch
# isn't behind it (to avoid unnecessary rebasing)
if git_branch_exists "$ORIGIN/$PREFIX_RELEASE$VERSION"; then
require_branches_equal "$PREFIX_RELEASE$VERSION" "$ORIGIN/$PREFIX_RELEASE$VERSION"
fi
echo git checkout -b "$BRANCH" "$BASE"
# create branch
if ! git checkout -b "$BRANCH" "$BASE"; then
die "Could not create feature branch '$BRANCH'"
fi
echo
echo "Summary of actions:"
echo "- A new branch '$BRANCH' was created, based on '$BASE'"
echo "- You are now on branch '$BRANCH'"
echo ""
echo "Now, start committing on your feature. When done, use:"
echo ""
echo " $COMMAND submit $VERSION $NAME"
echo ""
echo "If you want to colaborate on the feature, use:"
echo ""
echo " $COMMAND publish $VERSION $NAME"
echo ""
echo "If you want to abandon the feature, or if it has been merged into develop, use:"
echo ""
echo " $COMMAND delete $VERSION $NAME"
echo
}
cmd_publish() {
parse_args "$@"
expand_nameprefix_arg_or_current
# sanity checks
require_clean_working_tree
require_branch "$BRANCH"
local err=$(git fetch -q "$ORIGIN" "refs/heads/$BRANCH:refs/remotes/$ORIGIN/$BRANCH" 2>&1 | grep "Couldn't find remote ref")
if [ "$err" ]; then
rm $DOT_GIT_DIR/refs/remotes/$ORIGIN/$BRANCH 2>/dev/null
fi
if has "$ORIGIN/$BRANCH" $(git_all_branches); then
err=$(git push $ORIGIN "$BRANCH:refs/heads/$BRANCH" 2>&1)
echo "$err"
if [ "$(echo "$err" | grep "non-fast-forward updates were rejected")" ]; then
warn ""
warn "*** ERROR: Couldn't push your branch."
warn ""
if [ "$(git config --get "branch.$BRANCH.remote")" ]; then
warn " The remote feature branch could not be fast-forwarded."
warn ""
warn " Use 'git pull --rebase' to get the latest changes from the server."
else
warn " A feature branch named '$BRANCH' already exists on the server."
warn ""
warn " To select a different name for your branch, use:"
warn " git branch -m ${BRANCH} ${PREFIX}<newname>"
fi
die ""
fi
exit 0
fi
# create remote branch
git push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH"
git fetch -q "$ORIGIN" "refs/heads/$BRANCH:refs/remotes/$ORIGIN/$BRANCH"
# configure remote tracking
git config "branch.$BRANCH.remote" "$ORIGIN"
git config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
git config "branch.$BRANCH.rebase" true
git checkout "$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 '$BRANCH'"
echo
}
cmd_track() {
parse_args "$@"
require_name_arg
# sanity checks
require_clean_working_tree
require_branch_absent "$BRANCH"
git fetch -q "$ORIGIN" "refs/heads/$BRANCH:refs/remotes/$ORIGIN/$BRANCH"
require_branch "$ORIGIN/$BRANCH"
# create tracking branch
git checkout -b "$BRANCH" "$ORIGIN/$BRANCH"
git config "branch.$BRANCH.remote" "$ORIGIN"
git config "branch.$BRANCH.merge" "refs/heads/$BRANCH"
git config "branch.$BRANCH.rebase" true
echo
echo "Summary of actions:"
echo "- A new remote tracking branch '$BRANCH' was created"
echo "- You are now on branch '$BRANCH'"
echo
}
cmd_checkout() {
parse_args "$@"
if [ "$NAME" != "" ]; then
expand_nameprefix_arg
git_do checkout "$BRANCH"
else
die "Name a feature branch explicitly."
fi
}
cmd_co() {
# Alias for checkout
cmd_checkout "$@"
}
fail() {
echo >&2
echo >&2 "*** ERROR: $1"
exit 128
}
commit_message () {
git cat-file commit "$1" | sed "1,/^$/d"
}
git_get_first_fix_branch_commit () {
git rev-list --first-parent "$1" ^"$ORIGIN/$RELEASE_BRANCH" | tail -n 1
}
git_is_fix_branch_empty () {
if [ -z $(git_get_first_fix_branch_commit "$1") ]; then
return 0
else
return 1
fi
}
git_is_commit_on_branch () { # commit branch
if test "$(git merge-base $2 $1)" == "$(git rev-parse $1)"; then
return 0
else
return 1
fi
}
require_fix_branch() {
require_branch "$1"
first_fix_commit=$(git_get_first_fix_branch_commit "$1")
if [ -z "$first_fix_commit" ]; then
if ! git_is_commit_on_branch "$1" "$ORIGIN/$RELEASE_BRANCH"; then
die "Current branch did NOT initially branch from '$ORIGIN/$RELEASE_BRANCH'"
fi
else
if [[ "`git rev-list $first_fix_commit^ ^"$ORIGIN/$RELEASE_BRANCH" 2>&1 | wc -l`" != "0" \
&& "`git rev-parse $first_fix_commit^`" != "`git rev-parse $ORIGIN/$RELEASE_BRANCH^`" ]]; then
die "Current branch did NOT initially branch from '$ORIGIN/$RELEASE_BRANCH'"
fi
fi
}
git_change_id() {
local change_id=$(commit_message "$1" | sed -n 's/^Change-Id:\s\+\(I[a-fA-F0-9]\+\)\s*$/\1/p' | tr "A-F" "a-f")
[ "$change_id" ] || return 1
echo "$change_id"
}
git_manipulate_change_id() {
local change_id=$2
local tr=
case "$1" in
review) tr=1032547698badcfe ;; # xor 11..
message) tr=23016745ab89efcd ;; # xor 22..
*) fail "derived_change_id: wrong type" ;;
esac
echo "$change_id" | tr "0123456789abcdef" "$tr"
}
git_derived_change_id() {
local change_id=$(git_change_id "$2") || return 1
echo $(git_manipulate_change_id $1 "$change_id")
}
replace_change_id() {
sed '/^Change-Id:/d' | sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba';
[ "$1" ] && echo && echo "Change-Id: $1"
}
git_symbolic_head() {
local HEAD=$(git symbolic-ref HEAD 2>/dev/null | sed -n 's,refs/heads/,,p')
[ "$HEAD" ] || HEAD=$(git rev-parse HEAD)
echo "$HEAD"
}
git_is_clean_merge() {
git merge --no-commit --no-ff "$1" >/dev/null 2>/dev/null
local status=$?
git reset -q --hard
return $status
}
ask() {
local default=$(echo "$1" | tr 'NY' 'ny')
has $default n y || fail "ask: No default."
shift
local question="$@"
local answer=
until has "$answer" n y; do
echo -n "$question"
[ "$default" == "y" ] && echo -n " [Y/n] "
[ "$default" == "n" ] && echo -n " [y/N] "
read answer || die "Interrupted."
[ -z "$answer" ] && answer=$default
answer=$(echo "$answer" | tr 'NY' 'ny')
done
test "$answer" == "y"
}
nth_string () {
case "$1" in
*1[0-9]|*[04-9]) echo "$1"th;;
*1) echo "$1"st;;
*2) echo "$1"nd;;
*3) echo "$1"rd;;
esac
}
generate_squash_message() {
local commits=$(git log --reverse --no-merges --first-parent --pretty=format:%H "$BRANCH" ^"$ORIGIN/$RELEASE_BRANCH")
local count=$(echo "$commits" | wc -l)
[ "$count" -eq 0 ] && return 1
[ "$count" -eq 1 ] && {
commit_message "$commits" | sed '/^Change-Id:/d'
return 0
}
echo "# This is a combination of $count commits."
local i=0
while [ "$i" -lt "$count" ]; do
((i++))
echo "# The $(nth_string $i) commit's message is:"
echo
commit_message "$(echo "$commits" | sed -n "$i p")" | sed '/^Change-Id:/d'
done
if has_tracked_branch; then
echo "# You are on a remote tracked branch, the branch info is provided here"
echo ""
echo "Fix-Branch: $BRANCH"
fi
# If more than one commits are squashed, add stripped commit template in the end.
local template=$(git config commit.template)
if [ -f "$template" ]; then
echo
cat "$template" | sed '/^\s*#/d;/^\s*$/d'
fi
}
get_fix_commit_message() {
# Find the first commit of the feature branch
local first_fix_commit=$(git_get_first_fix_branch_commit "$BRANCH")
[ "$first_fix_commit" ] || die "Aborting: $BRANCH contains no commits on top of $ORIGIN/$RELEASE_BRANCH."
# The Gerrit Change-Id for the whole feature is _derived_ from the first commit
local first_change_id=$(git_change_id "$first_fix_commit") || fail "First commit in feature branch doesn't contain a Change-Id"
local feature_change_id=$(git_derived_change_id review "$first_fix_commit")
local message_change_id=$(git_derived_change_id message "$first_fix_commit")
# If there is a commit in the feature branch with 'message_change_id', use that for the squash commit message
local feature_message_commit=$(git log -1 --pretty=format:%H --grep="^Change-Id: $message_change_id" "$BRANCH" ^"$ORIGIN/$RELEASE_BRANCH")
if [ "$feature_message_commit" ]; then
local commit_message=$(commit_message "$feature_message_commit")
else
# Commit message has not been edited yet -> generate from all commits in feature branch
local commit_message=$(generate_squash_message)
fi
echo "$commit_message" | replace_change_id "$feature_change_id"
}
edit_fix_commit_message() {
local first_fix_commit=$(git_get_first_fix_branch_commit "$BRANCH")
[ "$first_fix_commit" ] || die "Aborting: $BRANCH contains no commits on top of $RELEASE_BRANCH."
# The Gerrit Change-Id for the whole feature is _derived_ from the first commit
local first_change_id=$(git_change_id "$first_fix_commit") || fail "First commit in feature branch doesn't contain a Change-Id"
local feature_change_id=$(git_derived_change_id review "$first_fix_commit")
local message_change_id=$(git_derived_change_id message "$first_fix_commit")
local initial_commit_message=$(get_fix_commit_message | replace_change_id)
( echo "# This is the commit message for the entire fix branch you are submitting to Gerrit."
echo "# Changes made to this message will be recorded as a new commit in your fix branch."
echo ""
echo "$initial_commit_message") > $DOT_GIT_DIR/COMMIT_EDITMSG
# Create a commit with the initial commit message
git commit --quiet --allow-empty --file=$DOT_GIT_DIR/COMMIT_EDITMSG --edit || die "Submit aborted due to empty commit message."
if [ "$(commit_message HEAD | replace_change_id)" == "$initial_commit_message" ]; then
# Remove commit if nothing changes
git reset -q --hard HEAD^ # Commit message not changed, delete empty commit
else
# Replace change ID with message change id
commit_message HEAD | replace_change_id "$message_change_id" | git commit --amend --quiet --allow-empty --file=-
fi
return 0
}
cmd_message() {
parse_args "$@"
expand_nameprefix_arg_or_current
require_fix_branch "$BRANCH"
require_clean_working_tree
init_restore_branch
git checkout -q "$BRANCH"
edit_fix_commit_message
}
cmd_info() {
parse_args "$@"
expand_nameprefix_arg_or_current
require_fix_branch "$BRANCH"
[ "$(gerrit_check)" == "0" ] && die "Cannot locate gerrit connection information"
local first_fix_commit=$(git_get_first_fix_branch_commit "$BRANCH")
[ "$first_fix_commit" ] || die "Aborting: $BRANCH contains no commits on top of $RELEASE_BRANCH."
# The Gerrit Change-Id for the whole fix is _derived_ from the first commit
local fix_change_id=$(git_derived_change_id review "$first_fix_commit")
info=$(gerrit_query "$fix_change_id") \
|| fail "Couldn't contact Gerrit to get the change url"
echo "change $fix_change_id"
echo "----------------------------------------------"
echo "$info"
}
cmd_ts() {
parse_args "$@"
[ "$(gerrit_check)" == "0" ] && die "Cannot locate gerrit connection information"
expand_nameprefix_arg_or_current
require_fix_branch "$BRANCH"
require_clean_working_tree
# Ensure we return to the HEAD we were called from after this command completes (or fails)
HEAD=$(git_symbolic_head)
trap "git checkout -q $HEAD" INT TERM EXIT
git checkout -q "$BRANCH"
# Edit message
edit_fix_commit_message || exit 1
}
cmd_submit() {
parse_args "$@"
[ "$(gerrit_check)" == "0" ] && die "Cannot locate gerrit connection information"
expand_nameprefix_arg_or_current
require_fix_branch "$BRANCH"
require_clean_working_tree
# Ensure we return to the HEAD we were called from after this command completes (or fails)
HEAD=$(git_symbolic_head)
trap "git checkout -q $HEAD" INT TERM EXIT
git checkout -q "$BRANCH"
# If we are tracking a remote branch, require that they are equal
if has_tracked_branch; then
git fetch -q "$ORIGIN" "refs/heads/$REMOTE_BRANCH:refs/remotes/$ORIGIN/$REMOTE_BRANCH"
git_compare_branches "$BRANCH" "remotes/$ORIGIN/$REMOTE_BRANCH"
if [[ $? -ne 0 && $? -ne 2 ]]; then
warn "The $ORIGIN/$BRANCH has changes that are not in the local branch."
warn "You should probably pull to get these changes before submitting."
ask Y "Abort now?" && die "Ok."
fi
fi
if has "$ORIGIN/$RELEASE_BRANCH" $(git_remote_branches); then
git fetch -q "$ORIGIN" "refs/heads/$RELEASE_BRANCH:refs/remotes/$ORIGIN/$RELEASE_BRANCH" || \
fail "Couldn't fetch latest $RELEASE_BRANCH from Gerrit"
fi
# Check if merge into develop would be clean, otherwise request to merge first
if has "$ORIGIN/$RELEASE_BRANCH" $(git_remote_branches); then
if ! git_is_clean_merge "remotes/$ORIGIN/$RELEASE_BRANCH"; then
warn "Merge with the latest develop would FAIL: Merge needed before submitting."
ask Y "Should I start the merge now so you can solve conflicts?" \
|| die "Submit cancelled. Please merge manually before submitting to Gerrit."
warn
warn "Merging with $ORIGIN/$RELEASE_BRANCH"
warn " - When merge is completed, run '$COMMAND submit' again"
warn " - To abort the merge, run 'git reset --hard HEAD'"
warn
trap - INT TERM EXIT
git merge "remotes/$ORIGIN/$RELEASE_BRANCH"
exit $?
fi
fi
# Edit message
edit_fix_commit_message || exit 1
# Move to detached head mode so our squash commit doesn't go on any branch
git checkout -q `git rev-parse "$BRANCH"` || fail "Failed to checkout detached head"
# Reset to the last merge point with develop branch, but keeping the differences staged
git reset --soft `git merge-base "remotes/$ORIGIN/$RELEASE_BRANCH" "$BRANCH"` || fail "Failed to create change commit"
git_is_clean_working_tree && fail "Fix branch contains no changes compared to '$ORIGIN/$RELEASE_BRANCH'"
# Commit squashed changes
get_fix_commit_message | git commit --quiet --cleanup=strip --file=-
detached_head=$(git rev-parse HEAD)
git checkout -q "$BRANCH"
# Push commit to gerrit for review
gerrit_push "$detached_head:refs/for/$RELEASE_BRANCH" || fail "Failed to submit change to gerrit"
# Print Summary of actions
change_info=$(gerrit_query "commit:${detached_head}")
change_number=$(echo "$change_info" | gerrit_query_filter _number)
change_id=$(echo "$change_info" | gerrit_query_filter id)
base_url=$(gerrit_url)
echo
echo "Summary of actions:"
echo "- All commits on you fix branch were squashed into one,"
echo " and the resulting commit was pushed to Gerrit for review:"
echo " Commit: $(git log -1 --no-color --pretty="format:(%h..) %s" "${detached_head}")"
echo " Change-Id: ${change_id}"
echo " Review: ${base_url}/${change_number}"
echo
}
cmd_review() {
parse_args "$@"
[ "$(gerrit_check)" == "0" ] && die "Cannot locate gerrit connection information"
expand_nameprefix_arg_or_current
require_fix_branch "$BRANCH"
commit_head=$(git rev-parse HEAD)
# sanity checks
git fetch -q "$ORIGIN" "refs/heads/$REMOTE_BRANCH:refs/remotes/$ORIGIN/$REMOTE_BRANCH"
# is the remote branch present on the server
if ! has "$ORIGIN/$REMOTE_BRANCH" $(git_all_branches); then
warn "The $ORIGIN/$REMOTE_BRANCH does not exist on Gerrit."
warn "You should probably use $COMMAND publish."
warn "Aborting "
die
fi
git checkout -q "$BRANCH"
# If we are tracking a remote branch, require that they are equal
if has_tracked_branch; then
git fetch -q "$ORIGIN" "refs/heads/$BRANCH:refs/remotes/$ORIGIN/$REMOTE_BRANCH"
git_compare_branches "$BRANCH" "remotes/$ORIGIN/$REMOTE_BRANCH"
output_compare_branches=$?
if [[ $output_compare_branches == 0 ]]; then
warn "Local branch and remote branch are the same"
warn "No need to push for review"
warn "Aborting"
die
fi
if [[ $output_compare_branches -ne 0 && $output_compare_branches -ne 2 ]]; then
warn "The $ORIGIN/$REMOTE_BRANCH has changes that are not in the local branch."
warn "You should probably pull to get these changes before submitting."
ask Y "Abort now?" && die "Ok."
fi
fi
# Push commit to gerrit for review
gerrit_push "HEAD:refs/for/$REMOTE_BRANCH" || fail "Failed to submit change to gerrit"
# Display link to change
change_info=$(gerrit_query "commit:${commit_head}")
change_url=$(echo "$change_info" | gerrit_query_filter _number)
base_url=$(gerrit_url)
git log -1 --no-color --pretty="format:Commit: %Cgreen%h%Creset %s" "${commit_head}"
echo " ${base_url}/${change_url}"
echo
}
delete_gerrit_branch() {
# Check if remote branch exists
has_tracked_branch || return 0
# Cleanup remote branches which don't exist on $ORIGIN anymore
git remote prune $ORIGIN >/dev/null || "Failed to connect to $ORIGIN."
# Silently return if the tracking branch didn't exist anymore
has_tracked_branch || return 0
warn "This feature branch also seems to exist on Gerrit."
ask N "Also delete branch from Gerrit?" || return 1
git fetch -q "$ORIGIN" "$REMOTE_BRANCH" || fail "Failed to fetch from $ORIGIN."
gerrit_branch_point=$(git_get_first_fix_branch_commit FETCH_HEAD)
local_branch_point=$(git_get_first_fix_branch_commit "$BRANCH")
if [ "$gerrit_branch_point" != "$local_branch_point" ]; then
fail "Local and remote branch have different branch points. Refusing to delete remote branch."
fi
gerrit_push ":refs/heads/$REMOTE_BRANCH" || fail "Failed to delete remote branch."
return 2
}
cmd_delete() {
parse_args "$@"
expand_nameprefix_arg_or_current
require_clean_working_tree
require_fix_branch "$BRANCH"
# Check if change was merged into develop branch
if has "$ORIGIN/$RELEASE_BRANCH" $(git_remote_branches); then
if ! git_is_fix_branch_empty "$BRANCH"; then
first_fix_commit=$(git_get_first_fix_branch_commit "$BRANCH")
fix_change_id=$(git_derived_change_id review "$first_fix_commit") || fail "First commit in feature branch doesn't contain a Change-Id"
git fetch -q "$ORIGIN" "$RELEASE_BRANCH" || {
warn "Failed to check whether change has been merged into $RELEASE_BRANCH."
warn "Couldn't fetch branch from $ORIGIN."
ask N "Really delete fix branch?" || die "Aborting."
}
fix_merge_commit=$(git log -1 --pretty=format:%H --grep="^Change-Id: $fix_change_id" FETCH_HEAD)
[ "$fix_merge_commit" ] || {
warn "The fix doesn't seem to have been merged into $ORIGIN/$RELEASE_BRANCH!"
warn "Maybe the Gerrit review was not yet completed?"
ask N "Really delete fix branch?" || die "Aborting."
}
fi
fi
# If we are tracking a remote gerrit branch, we might want to delete it from gerrit
delete_gerrit_branch
remote_deleted=$?
[[ "$BRANCH" == "$(git_symbolic_head)" ]] && \
(git checkout -q "$RELEASE_BRANCH" || fail "Couldn't checkout '$RELEASE_BRANCH'.")
# delete local branch
git branch -D "$BRANCH" >/dev/null || fail "Couldn't delete '$BRANCH'"
echo
echo "Summary of actions:"
[ $remote_deleted -eq 1 ] && echo "- The remote branch '$BRANCH' was kept on the server."
[ $remote_deleted -eq 2 ] && echo "- The remote branch '$BRANCH' was deleted"
echo "- The local branch '$BRANCH' was deleted"
echo "- You are now on branch '$(git_symbolic_head)'"
echo
}
cmd_diff() {
parse_args "$@"
expand_nameprefix_arg_or_current
shift
local BASE=$(git merge-base "$ORIGIN/$RELEASE_BRANCH" "$BRANCH")
git diff "$BASE..$BRANCH" $@
}
cmd_log() {
parse_args "$@"
expand_nameprefix_arg_or_current
shift
require_fix_branch "$BRANCH"
git log "$BRANCH" ^"$ORIGIN/$RELEASE_BRANCH" $@
}