-
Notifications
You must be signed in to change notification settings - Fork 4
/
git-integration.sh
executable file
·791 lines (680 loc) · 17.3 KB
/
git-integration.sh
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
#!/bin/sh
#
# Copyright (C) 2013-16 John Keeping
# Licensed under the GNU GPL version 2.
# If we haven't been called via Git, see if we can tweak $PATH so
# that we can find the git-sh-setup script when we need it later.
GIT_INTEGRATION_VERSION='@@VERSION@@'
print_version () {
echo "git-integration version $GIT_INTEGRATION_VERSION"
}
if test "$1" = --version
then
print_version
exit
fi
if ! type git-sh-setup >/dev/null 2>&1
then
if ! exec_path=$(git --exec-path)
then
echo >&2 'Git not found!'
exit 1
fi
PATH="$exec_path:$PATH"
export PATH
fi
SUBDIRECTORY_OK=Yes
OPTIONS_KEEPDASHDASH=
OPTIONS_SPEC="\
git integration --create <name> [<base>]
git integration [<action>...] [<branch>]
git integration (--continue | --abort)
--
Actions:
create! create a new integration branch
edit! edit the instruction sheet for a branch
rebuild rebuild an integration branch
cat! show the instruction sheet for a branch
status! show the status of a branch
abort! abort an in-progress rebuild
continue! continue an in-progress rebuild
Inline actions:
add= appends a 'merge <branch>' line to the instruction sheet
Options:
autocontinue continues automatically if rerere resolves all conflicts
prefix= apply a prefix to each branch name when processing it
version! print the version of git-integration
"
. git-sh-setup
set_reflog_action integration
require_work_tree_exists
cd_to_toplevel
if test -n "$GIT_INTEGRATION_DEBUG"
then
set -x
PS4='+ $(basename "$0"):$LINENO: '
fi
LF='
'
_x40='00000'
_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
comment_char=$(git config --get core.commentchar 2>/dev/null | cut -c1)
: ${comment_char:=#}
autocontinue=$(git config --bool integration.autocontinue)
: ${autocontinue:=false}
branch_prefix=$(git config --get integration.prefix 2>/dev/null)
state_dir="$GIT_DIR/integration"
start_file="$state_dir/start-point"
head_file="$state_dir/head-name"
merged_file="$state_dir/merged"
prefix_file="$state_dir/prefix"
autocontinue_file="$state_dir/autocontinue"
insns="$state_dir/git-integration-insn"
# The newest feature we need is the "--comment-lines" option to
# git-stripspace, so if that does not work, tell the user they need to
# upgrade.
git stripspace --comment-lines </dev/null 2>/dev/null ||
die "Your Git is too old. Please upgrade to Git 1.8.2 or newer."
integration_ref () {
local branch
branch=${1#refs/heads/}
echo refs/insns/$branch
}
write_insn_sheet () {
local ref parent insn_blob insn_tree op insn_commit
ref=$1
parent=$(git rev-parse --quiet --verify $ref)
parent_tree=$(git rev-parse --quiet --verify $ref^{tree})
insn_blob=$(git hash-object -w --stdin) ||
die "Failed to write instruction sheet blob object"
insn_tree=$(printf "100644 blob %s\t%s\n" $insn_blob GIT-INTEGRATION-INSN |
git mktree) ||
die "Failed to write instruction sheet tree object"
# If there isn't anything to commit, stop now.
test "$insn_tree" = "$parent_tree" && return
op=${parent:+Update}
: ${op:=Create}
insn_commit=$(echo "$op integration branch $branch" |
git commit-tree ${parent:+-p $parent} $insn_tree) ||
die "Failed to write instruction sheet commit"
git update-ref $ref $insn_commit ${parent:-$_x40} ||
die "Failed to update instruction sheet reference"
}
# Helper function to extract the first word in a (potentially shell quoted)
# string. The normal usage is:
#
# eval first_word $foo
#
# $1 - argument to be echoed
first_word () {
echo "$1"
}
# De-indents its argument based on the indentation of its first content line.
# Leading blank lines are stripped.
#
# $1 - message to be de-indented
#
# Prints to stdout the message after removing indentation.
dedent () {
local message indent
# Strip leading blank lines.
message=$(IFS=
while read -r line
do
test -z "$line" && continue
printf '%s\n' "$line"
break
done
cat
)
indent=$(echo "$message" | sed -n -e '1 {
s/^\([ ]*\).*$/\1/
p
q
}')
echo "$message" | sed -e "s/^$indent//"
}
# Helper function used in for_each_insn. This extracts the command,
# arguments and message from the full command block before calling the
# insn_cmd with each of those as a separate argument.
#
# $1 - insn_cmd to be run for each instruction.
# $2 - message to be parsed and split.
#
# Returns the exit code of insn_cmd.
run_insn_command () {
local IFS
IFS=" $LF"
local insn_cmd first_line message cmd args
insn_cmd=$1
first_line=$(echo "$2" | sed -n -e 1p)
test -n "$first_line" || return 0
local message cmd args
message=$(echo "$2" | sed -e 1d | dedent)
cmd=${first_line%% *}
args=${first_line#* }
$insn_cmd "$cmd" "$args" "$message"
}
# Run a command for each instruction read from stdin. The input is a
# git-integration instruction sheet which is split into instructions.
#
# insn_cmd is called once for each instruction, with the parameters "cmd",
# "args", "message" where:
#
# - cmd is the command in the instruction sheet
# - args is the remainder of the command line
# - message is the indented message following the command
#
# Processing ends early if insn_cmd returns a non-zero exit code for any
# instruction.
#
# $1 - insn_cmd to run for each instruction.
#
# Returns the exit code of insn_cmd.
for_each_insn () {
local cmd
insn_cmd=$1
current_insn=
IFS=
while read -r line
do
case "$line" in
''|' '*|' '*)
: # Blank or indented line.
;;
*)
# New command, finish the previous one.
run_insn_command "$insn_cmd" "$current_insn" || return
current_insn=
;;
esac
current_insn="$current_insn$line$LF"
done
run_insn_command "$insn_cmd" "$current_insn"
}
integration_create () {
branch=$1
base=$2
git update-ref $branch "$branch_prefix$base^{commit}" $_x40 &&
echo "base ${base#$branch_prefix}" |
write_insn_sheet $(integration_ref $branch) &&
git checkout ${branch#refs/heads/} &&
echo "Integration branch $branch created."
}
integration_edit () {
branch=$1
manual_edit=${2=1}
append_branches=$3
ref=$(integration_ref $branch)
edit_file="$GIT_DIR/GIT-INTEGRATION-INSN"
git rev-parse --quiet --verify $ref >/dev/null ||
die "Not an integration branch: ${branch#refs/heads/}"
{
git cat-file blob $ref:GIT-INTEGRATION-INSN &&
if test -n "$append_branches"
then
echo "$append_branches" |
sed -e '/^$/d' -e 's/^/\
merge /'
fi
echo &&
git stripspace --comment-lines <<EOF
Format:
command: args
Indented lines form a comment for certain commands.
For other commands these are ignored.
Lines beginning with $comment_char are stripped.
Commands:
base Resets the branch to the specified state. Every integration
instruction list should begin with a "base" command.
merge Merges the specified branch. Extended comment lines are
added to the commit message for the merge.
. The command is ignored.
EOF
} >"$edit_file"
if test "$manual_edit" != 0
then
git_editor "$edit_file" || die
fi
cat "$edit_file" |
git stripspace --strip-comments |
write_insn_sheet $ref
}
break_integration () {
printf '%s\n%s\n' "$current_insn" "$line" >"$insns".new
cat >>"$insns".new
mv "$insns".new "$insns"
echo "$merged" >"$merged_file"
test $# = 0 || printf '%s\n' "$1" >&2
cat >&2 <<EOF
Once you have resolved this, run:
git integration --continue
NOTE: Any changes to the instruction sheet will not be saved.
EOF
exit 1
}
finish_integration () {
git update-ref -m "$GIT_REFLOG_ACTION: rebuilt" \
"$branch" HEAD $(cat "$start_file" 2>/dev/null) &&
git symbolic-ref -m "$GIT_REFLOG_ACTION: complete" HEAD "$branch" &&
rm -rf "$state_dir" &&
git gc --auto &&
echo "Successfully re-integrated ${branch#refs/heads/}."
}
do_merge () {
local branch_to_merge merge_msg merge_opts
branch_to_merge=$1
shift
test -n "$branch_to_merge" || break_integration
branch_to_merge=$branch_prefix$branch_to_merge
if test "$skip_commit" = "$(git rev-parse --quiet $branch_to_merge)"
then
echo "Merged branch ${branch_to_merge}."
merged="$merged$branch_to_merge$LF"
return
fi
merge_msg=$(
printf '%s\t\tbranch %s' \
$(git rev-parse --quiet --verify $branch_to_merge) \
$branch_to_merge |
git fmt-merge-msg --log --message \
"Merge branch '$branch_to_merge' into ${branch#refs/heads/}" |
git stripspace --strip-comments &&
printf '\n%s\n' "$message"
) || die "failed to create message for merge commit"
merge_opts="--quiet --no-log --no-ff"
if test "$autocontinue" = "true"
then
merge_opts="$merge_opts --rerere-autoupdate"
fi
echo "Merging branch ${branch_to_merge}..."
if ! git merge "$@" $merge_opts -m "$merge_msg" $branch_to_merge
then
if test "$autocontinue" = "true" &&
test -z "$(git ls-files --unmerged)"
then
git commit --no-edit --no-verify || break_integration
else
break_integration
fi
fi
merged="$merged$branch_to_merge$LF"
}
do_base () {
local base
base=$branch_prefix$1
test -z "$merged" || {
echo >&2 "warning: dropping the following branches (resetting to base $base)"
cat "$merged" | sed -e 's/^/warning: /' >&2
}
echo "Resetting to base ${base}..."
GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: resetting to base '$base'" \
git reset --quiet --hard "$base" ||
break_integration "Failed to reset to base $base"
}
do_dot () {
: no-op
}
finalize_command () {
local cmd args message
cmd=$1
args=$2
message=$3
case "$cmd" in
base)
eval "do_base $args"
;;
merge)
eval "do_merge $args"
;;
'.')
eval "do_dot $args"
;;
*)
break_integration "Unknown command: $cmd"
;;
esac
}
run_integration () {
local IFS merged skip_commit
merged=$1
skip_commit=$2
for_each_insn finalize_command
finish_integration
}
integration_rebuild () {
local branch orig_head ref
branch=$1
require_clean_work_tree integrate "Please commit or stash them."
orig_head=$(git rev-parse --quiet --verify "$branch^{commit}") || exit
GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: starting rebuild of $branch" \
git checkout --quiet "$branch^0" || die "could not detach HEAD"
git update-ref ORIG_HEAD $orig_head
ref=$(integration_ref $branch)
git rev-parse --quiet --verify $ref >/dev/null ||
die "Not an integration branch: ${branch#refs/heads/}"
mkdir -p "$state_dir" ||
die "Failed to create state directory"
echo $branch >"$head_file"
echo $autocontinue >"$autocontinue_file"
printf '%s\n' "$branch_prefix" >"$prefix_file"
git rev-parse --quiet --verify "$branch" >"$start_file"
git cat-file blob $ref:GIT-INTEGRATION-INSN >"$insns" ||
die "Failed to read instruction list for branch ${branch#refs/heads/}"
cat "$insns" | run_integration || die
}
integration_abort () {
test $# = 0 || usage
local branch
branch=$(cat "$head_file" 2>/dev/null) ||
die "No integration in progress."
git symbolic-ref -m "$GIT_REFLOG_ACTION: aborted" HEAD $branch &&
git reset --hard $branch &&
rm -rf "$state_dir"
}
integration_continue () {
test $# = 0 || usage
local branch skip_commit merged
branch=$(cat "$head_file" 2>/dev/null) ||
die "No integration in progress."
if test -f "$GIT_DIR/MERGE_HEAD"
then
# We are being called to continue an existing operation,
# without the user having manually committed the result of
# resolving conflicts.
git update-index --ignore-submodules --refresh &&
git diff-files --quiet --ignore-submodules ||
die "You must edit all merge conflicts and then mark them as resolved using git add"
skip_commit=$(cat "$GIT_DIR/MERGE_HEAD")
git commit --no-edit || die
fi
merged=$(cat "$merged_file")
branch_prefix=$(cat "$prefix_file")
autocontinue=$(cat "$autocontinue_file")
cat "$insns" | run_integration "$merged" "$skip_commit"
}
insn_branch_length () {
local cmd args message
cmd=$1
args=$2
message=$3
if test "$cmd" = merge
then
args="$branch_prefix$(eval "first_word $args")"
echo ${#args}
else
echo 0
fi
}
# Show the status of a merge command in the instruction sheet.
#
# $1 - branch to be shown
status_merge () {
local branch_to_merge state verbose_state
branch_to_merge=$1
if test -z "$branch_to_merge"
then
say >&2 "no branch specified with 'merge' command"
return
fi
branch_to_merge="$branch_prefix$branch_to_merge"
test -n "$status_base" || status_base=master
if ! git rev-parse --verify --quiet "$branch_to_merge"^{commit} >/dev/null
then
state="$color_changed."
verbose_state="branch not found"
elif git merge-base --is-ancestor "$branch_to_merge" "$status_base"
then
state="$color_merged+"
verbose_state="merged to $status_base"
elif git-merge-base --is-ancestor "$branch_to_merge" "$branch"
then
state="$color_uptodate*"
verbose_state="up-to-date"
else
state="$color_changed-"
verbose_state="branch changed"
fi
local color
test -z "$use_color" || color='--color=always'
printf '%s %-*s%s(%s)\n' "$state" "$longest_branch" "$branch_to_merge" "$color_reset" "$verbose_state"
test -n "$message" && printf '%s\n' "$message" | sed -e 's/^./ &/'
echo
git --no-pager log --oneline $color --cherry "$status_base...$branch_to_merge" -- 2>/dev/null |
sed -e 's/^/ /' -e '$ a \
'
}
status_dot () {
printf '. %s\n' "$*"
test -n "$message" && printf '%s\n' "$message" | sed -e 's/^./ &/'
echo
}
insn_status () {
local cmd args message
cmd=$1
args=$2
message=$3
case "$cmd" in
base)
status_base="$branch_prefix$args"
;;
merge)
eval "status_merge $args"
;;
'.')
eval "status_dot $args"
;;
*)
say >&2 "unhandled command: $cmd $args"
;;
esac
}
integration_status () {
local branch ref insn_list
branch=$1
ref=$(integration_ref $branch) || die
insn_list=$(git cat-file blob $ref:GIT-INTEGRATION-INSN) ||
die "Failed to read instruction list for branch ${branch#refs/heads/}"
longest_branch=$(
echo "$insn_list" | for_each_insn insn_branch_length | (
max=0
while read -r len
do
test "$len" -gt "$max" && max=$len
done
echo "$max"
)
) || longest_branch=40
# Add two characters separation.
longest_branch=$(($longest_branch + 2))
local use_color color_merged color_uptodate color_changed color_reset
if git config --get-colorbool color.status
then
use_color=true
color_merged=$(git config --get-color color.integration.merged blue)
color_uptodate=$(git config --get-color color.integration.uptodate green)
color_changed=$(git config --get-color color.integration.changed red)
color_reset=$(git config --get-color '' reset)
fi
echo "$insn_list" | for_each_insn insn_status
}
action=
do_create=0
do_edit=0
do_rebuild=auto
do_cat=0
do_status=0
branches_to_add=
need_rebuild=0
total_argc=$#
while test $# != 0
do
case "$1" in
--create)
do_create=1
need_rebuild=1
;;
--edit)
do_edit=1
need_rebuild=1
;;
--rebuild)
do_rebuild=1
;;
--no-rebuild)
do_rebuild=0
;;
--cat)
do_cat=1
;;
--status)
do_status=1
;;
--abort|--continue)
test -z "$action" || usage
test $total_argc -eq 2 || usage
action=${1##--}
;;
--add)
shift
branches_to_add="$branches_to_add$1$LF"
need_rebuild=1
;;
--autocontinue)
autocontinue=true
;;
--no-autocontinue)
autocontinue=false
;;
--prefix)
shift
branch_prefix=$1
;;
--version)
print_version
exit
;;
--)
shift
break
;;
*)
usage
;;
esac
shift
done
if test -n "$action"
then
test $do_create != 1 &&
test $do_edit != 1 &&
test $do_rebuild != 1 &&
test $do_cat != 1 &&
test -z "$branches_to_add" || usage
fi
case $action in
continue)
integration_continue
exit
;;
abort)
integration_abort
exit
;;
'')
: # No-op.
;;
*)
die "internal error: unhandled action: $action"
;;
esac
if test -f "$insns"
then
case $do_create,$do_edit,$do_rebuild,$do_cat,$do_status,"$branches_to_add"
in
0,0,auto,?,?,|0,0,0,?,?,)
: # OK to proceed - cat only
;;
*)
die "Integration already in progress."
;;
esac
fi
if test -n "$branches_to_add"
then
ORIG_IFS=$IFS
IFS=$LF
for branch in $branches_to_add
do
branch="$branch_prefix$branch"
git rev-parse --quiet --verify "$branch^{commit}" >/dev/null ||
die "not a valid commit: $branch"
done
IFS=$ORIG_IFS
fi
test $do_create = 1 ||
test $do_edit = 1 ||
test $do_rebuild = 1 ||
test $do_cat = 1 ||
test -n "$branches_to_add" || do_status=1
branch=
if test $do_create = 1
then
test $# != 0 || usage
branch=$(git check-ref-format --normalize "refs/heads/$1") ||
die "invalid branch name: $1"
shift
if test $# = 0
then
base=master
else
base=$1
shift
git rev-parse --quiet --verify "$base^{commit}" >/dev/null ||
die "no such branch: $base"
fi
test $# = 0 || usage
integration_create "$branch" "$base" || exit
else
if test $# = 0
then
if test -f "$insns"
then
# Integration in progress.
branch=$(cat "$head_file" 2>/dev/null) ||
die "Internal error: instruction file exists but not HEAD file!"
else
branch=$(git symbolic-ref HEAD 2>/dev/null) ||
die "HEAD is detached, could not figure out which integration branch to use"
fi
else
branch=$(git check-ref-format --normalize "refs/heads/${1#refs/heads/}") &&
git rev-parse --quiet --verify "$branch^{commit}" >/dev/null ||
die "no such branch: $1"
shift
fi
git rev-parse --quiet --verify "$(integration_ref "$branch")" >/dev/null ||
die "Not an integration branch: ${branch#refs/heads/}"
fi
test $# = 0 || usage
if test $do_edit = 1 || test -n "$branches_to_add"
then
integration_edit "$branch" $do_edit "$branches_to_add" || exit
fi
if test $do_cat = 1
then
git cat-file blob "$(integration_ref "$branch"):GIT-INTEGRATION-INSN" || exit
fi
if test $do_rebuild = auto && test $need_rebuild = 1
then
test "$(git config --bool --get integration.autorebuild)" = true &&
do_rebuild=1
fi
if test $do_rebuild = 1
then
integration_rebuild "$branch" || exit
fi
if test $do_status = 1
then
integration_status "$branch"
fi