-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack
More file actions
executable file
·2010 lines (1932 loc) · 63.9 KB
/
stack
File metadata and controls
executable file
·2010 lines (1932 loc) · 63.9 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
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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
state_file="$(realpath $(dirname "$0"))/.stack-state"
lock_index="$(realpath $(dirname "$0"))/.lock-index"
cache_index="$(realpath $(dirname "$0"))/.cache-index"
dir="$(realpath $(dirname $0)/ocaml)"
if [[ ! -d $dir ]]; then
echo "Expect OCaml clone in $dir" >&2
exit 1
fi
cd "$dir"
if ! git rev-parse --verify upstream/trunk &> /dev/null; then
echo 'Could not find upstream/trunk' >&2
exit 1
fi
if ! git rev-parse --verify relocatable-locks &> /dev/null; then
if ! git rev-parse --verify origin/relocatable-locks &> /dev/null; then
echo "origin/relocatable-locks not found?!" >&2
exit 1
fi
git branch --track relocatable-locks origin/relocatable-locks
fi
function merge-base
{
local base="$(git merge-base "$1" "$2")"
# There's an error in the branching history for 4.13, possibly caused by PRs
# merged shortly after it was branched. The effect is that there are two
# "last commit before branching 4.13" commits in the history which causes the
# common parent dd7927 to be selected. We manually amend this to 4.13's
# merge-base.
if [[ $base = 'dd7927e156b7cb2f9cb73d2d54a15a9c81921392' ]]; then
base='621753f3eec9de91495a25723de00cef33a9e35a'
fi
echo "$base"
}
# Return status 0 only if the commit specified by $1 is based on a branch which
# is newer than the commit specified by $2 (i.e. cherry-picking $1 onto $2 would
# be a back-port, not a revert)
function is-backport
{
local commit="$1"
local target="$2"
# The multicore merge history (from #10831) causes issues for `git merge-base`
# $pre_10831 is the last commit on trunk before #10831 was merged. If that
# commit is _not_ an ancestor of $target, then we use that instead of the tip
# of trunk to work out the merge-base, as this avoids the complicated rebase
# and merge history in #10831.
local pre_10831='263a2a429c'
if git merge-base --is-ancestor $pre_10831 "$target"; then
local trunk='upstream/trunk'
else
local trunk=$pre_10831
fi
local target_sha="$(git rev-list -n 1 "$target")"
local target_mergebase="$(merge-base "$target" $trunk)"
local mergebase="$(merge-base "$commit" "$target")"
if [[ $mergebase = $target_sha ]]; then
# Commit is based on target!
return
elif [[ $mergebase = $target_mergebase ]]; then
# The common point between $commit and $target is $target's merge-base with
# trunk. That means $commit is newer, as otherwise the common point would
# be the merge-base of $commit and trunk.
return
fi
# This isn't a back-port
return 1
}
# XXX At the moment, caching and locking _almost_ work, but there seem to be
# some instances where rerere files can be missed.
use_caching=1
re_cache=0
declare -A SKIP_CACHE
menu=false
while true; do
case "$1" in
--cache)
shift 1
use_caching=1;;
--re-cache)
shift 1
re_cache=1;;
--no-cache)
shift 1
use_caching=0;;
--skip-cache)
SKIP_CACHE["$2"]=''
shift 2;;
--menu)
shift 1
menu=true
break;;
--is-backport)
if is-backport "$2" "$3"; then
echo -e "$2 is \e[97molder\e[0m than $3"
else
echo -e "$2 is in scope"
fi
exit 0;;
*)
break;;
esac
done
if [[ -n $1 ]]; then
if [[ $1 = ${1#@} ]]; then
if ! git rev-parse --verify "$1" &> /dev/null; then
echo "Lock not found: $1" >&2
exit 1
fi
lock="$(git rev-parse "$1")"
contained="$(git branch relocatable-locks --contains "$lock")"
if [[ -z $contained ]]; then
echo "Lock not found: $1" >&2
exit 1
fi
git --work-tree=../.git/modules/ocaml restore --source=$lock --worktree -- rr-cache
backports="$lock"
lock="git show $lock:branches"
if git show "$backports:config" &>/dev/null; then
while read -r key value; do
case $key in
keep_changes)
keep_changes="$value";;
legacy_empty_commits)
legacy_empty_commits="$value";;
linear_commit_history)
linear_commit_history="$value";;
*)
echo "Unexpected key in lock config file: $key"
exit 1;;
esac
done < <(git show "$backports:config")
if [[ -z $legacy_empty_commits && $keep_changes -eq 1 ]]; then
# Slightly grim hack which affects locks c013d8555a, 590e211336 and
# b5aa73d89c. These locks were committed where an empty commit followed
# by an empty fixup resulted in an empty commit in the series. Newer
# locks record legacy_empty_commits=0 explicitly. Although it affects
# c013d8555a, there aren't any empty+empty-fixup commits in that lock.
legacy_empty_commits=1
fi
else
keep_changes=0
legacy_empty_commits=0
linear_commit_history=0
fi
rm -f ../branches
else
lock="$(realpath ${1#@})"
lock="cat \"$lock\""
rm -f ../branches
cp "${1#@}" ../branches
backports=''
# XXX Hack!
keep_changes=1
legacy_empty_commits=0
linear_commit_history=1
fi
else
lock=''
backports=''
fi
start_time=$(date '+%Y-%m-%d %H:%M:%S')
declare -A HEADS
ALL_TARGETS=(trunk 5.4 5.3 5.2 5.1 5.0 4.14 4.13 4.12 4.11 4.10 4.09 4.08)
function branch
{
if [[ $2 =~ upstream/ ]]; then
BRANCHES+=("$1@${2#upstream/}")
else
BRANCHES+=("$1")
fi
}
function fixup
{
local upstream="$2"
[[ $upstream != 'relocatable-base-trunk' ]] || upstream='upstream/trunk'
if [[ "$3$upstream" =~ ^upstream/ ]]; then
BRANCHES+=("$1@${upstream#upstream/}@fixup")
else
if [[ ${3:-0} -gt 0 ]]; then
BRANCHES+=("$1@fixup~$3")
else
BRANCHES+=("$1@fixup")
fi
fi
}
if [[ -z $lock ]]; then
if [[ ! -e ../menu ]]; then
echo "Menu (../menu) not found!"
exit 1
fi
. ../menu
# The default is to preserve the Changes file
keep_changes=1
legacy_empty_commits=0
linear_commit_history=1
TARGETS=(trunk 5.4 5.3 5.2 5.1 5.0 4.14 4.13 4.12 4.11 4.10 4.09 4.08)
# Check the branches all exist and collect the commits to stack
for instruction in "${BRANCHES[@]}"; do
[[ $instruction != 'stop-backporting' ]] || continue
branch="${instruction%%@*}"
if ! git rev-parse --verify $branch &> /dev/null; then
if ! git rev-parse --verify origin/$branch &> /dev/null; then
echo "Both $branch and origin/$branch do not exist!" >&2
exit 1
else
git branch --track $branch origin/$branch
fi
fi
if [[ -n ${HEADS[$branch]+x} ]]; then
echo "$branch appears more than once in \$BRANCHES" >&2
exit 1
else
HEADS["$instruction"]="$(git rev-parse $branch)"
fi
done
fault=0
for root in "${ALL_TARGETS[@]}"; do
if ! git rev-parse --verify relocatable-base-$root &> /dev/null; then
if ! git rev-parse --verify origin/relocatable-base-$root &> /dev/null; then
echo "Target $root not found" >&2
fault=1
continue
else
git branch relocatable-base-$root origin/relocatable-base-$root
fi
fi
HEADS["relocatable-base-$root"]="$(git rev-parse relocatable-base-$root)"
done
if ((fault)); then
exit 1
fi
for instruction in "${BRANCHES[@]}" "${TARGETS[@]}"; do
if [[ -z ${HEADS[$instruction]} ]]; then
if [[ $instruction = 'stop-backporting' ]]; then
echo "$instruction"
else
echo "relocatable-base-$instruction ${HEADS[relocatable-base-$instruction]}"
fi
else
echo "$instruction ${HEADS[$instruction]}"
fi
done > ../branches
else
TARGETS=()
BRANCHES=()
while read -r branch head; do
if [[ $branch = ${branch#relocatable-base-} ]]; then
BRANCHES+=($branch)
else
TARGETS+=(${branch#relocatable-base-})
fi
[[ -z $head ]] || HEADS[$branch]=$head
done < <(eval "$lock")
fi
# At this point:
# - BRANCHES is an array of the all the branches to be consolidated
# - TARGETS is an array of all the targets to make (trunk, 5.3, etc.)
# - HEADS is an associative array mapping everything in BRANCHES to a sha
declare -A PROVENANCE
declare -A THIS_PROVENANCE
declare -A FIRST_BRANCH
function menu-info
{
if [[ $menu = 'true' ]]; then
if [[ $3 = ${3#* } ]]; then
sha="$3"
else
sha="${3# }"
sha="${sha%% *}"
fi
cmd="\e[34m$1\e[0m (${FIRST_BRANCH["$sha"]}): "
if [[ $2 = 'skip' ]]; then
cmd="$cmd\e[90mSkipping $3 as it's already present"
else
cmd="$cmd$2 \e[97m$3\e[0m"
fi
if [[ $3 = ${3#* } ]]; then
echo -e "$cmd - $(git log --format='%s' -1 $3)"
else
echo -e "$cmd"
for sha in $3; do
echo "${branch//?/ } $(git log --format='%s' -1 $sha)"
done
fi
echo -ne '\e[0m'
elif [[ $2 != 'skip' ]]; then
for sha in $3; do
PROVENANCE[$sha]="$branch"
THIS_PROVENANCE[$sha]="$branch"
done
fi
}
function commit-basis
{
base="$(git merge-base $1 upstream/trunk)"
if [[ -z ${FIRST_BRANCH["$base"]} ]]; then
for target in "${ALL_TARGETS[@]}" failed; do
if [[ $target = 'failed' ]]; then
echo "Unable to work out basis for $1"
exit 1
fi
test_head="${HEADS["relocatable-base-$target"]}"
if [[ -z $test_head ]]; then
test_head="$(git merge-base upstream/$target upstream/trunk)"
fi
if is-backport "$1" "$test_head"; then
base="$target"
break
fi
done
else
base="${FIRST_BRANCH["$base"]}"
fi
for sha in "$@"; do
FIRST_BRANCH["$sha"]="$base"
done
}
declare -A PICKED
declare -A TOUCHED
declare -A TAKEN
TOUCHED['stop-backporting']=1
COMMANDS=()
COMMITS=()
WORKING=()
base_trunk="${HEADS['relocatable-base-trunk']}"
if [[ ! -e $state_file ]]; then
# Remove files leftover by CTRL+C
rm -f _log
rm -rf autom4te.cache
if [[ -n "$(git status --porcelain)" ]]; then
echo 'The working tree is not clean' >&2
exit 1
fi
if [[ -e "$(git rev-parse --git-dir)/REBASE_HEAD" ]]; then
echo 'A rebase appears to be in progress?' >&2
exit 1
elif [[ -e "$(git rev-parse --git-dir)/CHERRY_PICK_HEAD" ]]; then
echo 'A cherry-pick appears to be in progress' >&2
exit 1
elif [[ -e "$(git rev-parse --git-dir)/MERGE_HEAD" ]]; then
echo 'A merge appears to be in progress' >&2
exit 1
fi
git clean -dfX &> /dev/null
if [[ $menu != 'true' ]]; then
echo -n 'Calculating the menu... '
fi
for target in "${ALL_TARGETS[@]}"; do
if [[ -n ${HEADS["relocatable-base-$target"]+x} ]]; then
base="$(git merge-base ${HEADS["relocatable-base-$target"]} upstream/trunk)"
else
base="$(git merge-base upstream/$target upstream/trunk)"
fi
FIRST_BRANCH["$base"]="$target"
done
# Calculate the menu. First, determine the initial commit list from the
# branches.
for entry in "${BRANCHES[@]}"; do
if [[ $entry = 'stop-backporting' ]]; then
COMMITS+=('stop-backporting')
continue
fi
upstream="${entry#*@}"
branch="${entry%%@*}"
if [[ $upstream = $entry ]]; then
# $entry is just a branch name
upstream='trunk'
fi
if [[ $upstream =~ ^fixup(~[0-9]+)?$ ]]; then
# $entry is foo@fixup (single fixup with optional offset)
count="${upstream#fixup~}"
if [[ $count = $upstream ]]; then
count=0
fi
commit="$(git log --format=%h -n 1 "${HEADS[$entry]}")"
commit-basis "$commit"
menu-info "$branch" "adjust $count $entry" "$commit"
COMMITS+=("adjust $count $entry $commit")
elif [[ $upstream =~ @fixup$ ]]; then
# $entry is foo@upstream@fixup (fixups with root + optional Basis)
# upstream refers to upstream/$upstream (at the moment) (at the moment)
upstream="${upstream%@fixup}"
commits=''
count=-1
while read -r entry; do
commit="${entry%% *}"
if [[ ${entry#* } = 'Basis' ]]; then
continue
fi
commits="$commits $commit"
((count++))
done < <(git log upstream/$upstream..${HEADS[$entry]} --format="%h %s" --reverse)
commit-basis $commits
menu-info "$branch" "adjust $count $branch" "$commits"
COMMITS+=("adjust $count $branch$commits")
else
if [[ $upstream = 'trunk' ]]; then
head="${HEADS["$entry"]}"
if [[ $(git merge-base 'upstream/trunk' "$head") = $head ]]; then
# This PR has been merged
merge="$(git log --format='%H %P' --min-parents=2 upstream/trunk | grep -F " $head" | head -n 1 | cut -f1 -d' ')"
if [[ -z $merge ]]; then
echo "Graph interpretation error: $head is merged into upstream/trunk, but cannot find merge commit?">&2
exit 1
fi
upstream="$(git merge-base "$merge~1" "$head")"
#upstream="$base_trunk"
# If this case comes back in being relevant, try commenting it!
# elif [[ -n $base_trunk && $(git merge-base "$base_trunk" "$head") = $base_trunk ]]; then
# upstream="$base_trunk"
else
upstream='upstream/trunk'
fi
else
#upstream="${HEADS["relocatable-base-$upstream"]}"
upstream="upstream/$upstream"
fi
this_basis=''
while read -r entry; do
commit="${entry%% *}"
bootstrap=0
diffs=0
while read -r path; do
case "$path" in
boot/ocaml*) bootstrap=1;;
*) diffs=1;;
esac
done < <(git diff-tree --no-commit-id --name-only -r "$commit")
if [[ -z $this_basis ]]; then
commit-basis "$commit"
this_basis="$base"
else
FIRST_BRANCH["$commit"]="$this_basis"
fi
if [[ $bootstrap -eq 1 ]]; then
if [[ $diffs -eq 0 ]]; then
menu-info "$branch" "bootstrap" "$commit"
COMMITS+=("bootstrap $commit")
else
menu-info "$branch" "coreboot" "$commit"
COMMITS+=("coreboot $commit")
fi
else
if [[ -n ${PICKED["$commit"]} ]]; then
menu-info "$branch" "skip" "$commit"
else
menu-info "$branch" "pick" "$commit"
COMMITS+=("pick $commit")
PICKED["$commit"]='1'
fi
fi
done < <(git log $upstream..${HEADS[$entry]} --format='%h %s' --reverse)
fi
done
if [[ $menu != 'true' ]]; then
echo 'done!'
fi
# Now generate commands for each target branch
for root in "${TARGETS[@]}"; do
COMMANDS+=("branch relocatable-base-$root")
done
# Eliminate the reflog caches, now that we're ready to go
rm -f ../.reflog-all ../.reflog-bootstrap
else
if [[ -n "$(git ls-files --exclude-standard --others)" ]]; then
echo 'Untracked files in the working directory' >&2
exit 1
elif [[ -n "$(git diff --stat)" ]]; then
echo 'Unstaged changes in the working directory' >&2
exit 1
fi
rm -f ../.stack-branches
# Load the stack state
phase=0
while read -r line; do
case ${line%% *} in
pick|bootstrap|coreboot|fixup|adjust|next|stop-backporting)
if ((phase)); then
WORKING+=("$line")
else
COMMITS+=("$line")
fi;;
commit)
COMMANDS+=("$line")
phase=1;;
branch)
COMMANDS+=("$line");;
target)
target="${line#* }";;
target-branch)
target_branch="${line#* }";;
touched)
TOUCHED["${line#* }"]=1;;
taken)
TAKEN["${line#* }"]=1;;
lock)
echo "${line#* }" >> ../.stack-branches;;
cherry-stamp)
cherry_stamp="${line#* }";;
start)
start_time="${line#* }";;
provenance)
line="${line#* }"
PROVENANCE[${line% *}]="${line#* }";;
first)
line="${line#* }"
FIRST_BRANCH[${line% *}]="${line#* }";;
*)
echo "Unrecognised command: $line" >&2
phase=2;;
esac
done < <(cat "$state_file")
if [[ $phase -gt 1 ]]; then
exit 1
fi
if diff -q ../branches ../.stack-branches > /dev/null ; then
rm ../.stack-branches
else
echo "Branches appear to have been altered:"
diff ../.stack-branches ../branches
exit 1
fi
rm "$state_file"
fi
if [[ $menu = 'true' ]]; then
exit 0
fi
rm -f "$lock_index"
if [[ -e ../branches ]]; then
mkdir -p lock-temp
cd lock-temp
mv ../../branches .
GIT_INDEX_FILE="$lock_index" git --work-tree=. add -f branches
echo "keep_changes $keep_changes" > config
echo "legacy_empty_commits $legacy_empty_commits" >> config
echo "linear_commit_history $linear_commit_history" >> config
GIT_INDEX_FILE="$lock_index" git --work-tree=. add -f config
cd ..
rm -rf lock-temp
fi
function abort
{
# echo "Branch: $branch for $target"
echo " $action"
{
for sha in "${!PROVENANCE[@]}"; do
echo "provenance $sha ${PROVENANCE[$sha]}"
done;
for sha in "${!FIRST_BRANCH[@]}"; do
echo "first $sha ${FIRST_BRANCH[$sha]}"
done;
for branch in "${!TOUCHED[@]}"; do
echo "touched $branch"
done;
for branch in "${!TAKEN[@]}"; do
echo "taken $branch"
done;
echo "start $start_time"
echo "cherry-stamp $cherry_stamp"
if ((!backporting)); then
echo 'stop-backporting'
fi
for commit in "${COMMITS[@]}"; do
echo "$commit"
done;
echo "target $target";
echo "target-branch $target_branch";
echo "commit $instr";
for instruction in "${BRANCHES[@]}" "${TARGETS[@]}"; do
if [[ -z ${HEADS[$instruction]} ]]; then
if [[ $instruction = 'stop-backporting' ]]; then
echo "lock $instruction"
else
echo "lock relocatable-base-$instruction ${HEADS[relocatable-base-$instruction]}"
fi
else
echo "lock $instruction ${HEADS[$instruction]}"
fi
done
} > "$state_file"
aborting=1
}
function display-with-provenance
{
local sha="$2"
local provenance="${PROVENANCE["$sha"]}"
if [[ ${provenance#@} = $provenance ]]; then
if [[ -z $provenance ]]; then
echo -e "$1 (\e[5;31mNo - we got no provenance\e[0m) $3"
else
echo -e "$1 (from \e[34m$provenance\e[0m) $3"
fi
else
local via=()
local fixups=()
if [[ ${provenance#*+} != $provenance ]]; then
fixups+=("${provenance#*+}")
provenance="${provenance%+*}"
fi
local chain="$provenance"
local last_provenance=''
if [[ ${PROVENANCE[@$sha]} != $target_branch ]]; then
last_provenance="${PROVENANCE[@$sha]}"
via+=($last_provenance)
fi
while [[ ${provenance#@} != $provenance ]]; do
sha="${provenance#@}"
if [[ ${PROVENANCE[$provenance]} != $last_provenance ]]; then
last_provenance="${PROVENANCE[$provenance]}"
if [[ -n $last_provenance ]]; then
via+=("$last_provenance")
fi
fi
provenance="${PROVENANCE["$sha"]}"
if [[ ${provenance#*+} != $provenance ]]; then
fixups+=("${provenance#*+}")
provenance="${provenance%+*}"
fi
chain="$chain -> $provenance"
done
case ${#via[@]} in
0) via_seg='';;
1) via_seg=" via \e[34m${via[0]}\e[0m";;
2) via_seg=" via \e[34m${via[0]}\e[0m -> \e[34m${via[1]}\e[0m";;
3) via_seg=" via \e[34m${via[0]}\e[0m -> \e[34m${via[1]}\e[0m -> \e[34m${via[2]}\e[0m";;
*)
local count=${#via[@]}
((count-=2))
via_seg=" via \e[34m${via[0]}\e[0m -> ... \e[34m$count more\e[0m ... -> \e[34m${via[-1]}\e[0m";;
esac
if [[ -z $provenance ]]; then
provenance="\e[5;31m<broken provenance chain: $chain>\e[0m"
fi
if [[ ${#via[@]} -le 1 && ${#fixups[@]} -eq 0 ]]; then
echo -e "$1 (from \e[34m$provenance\e[0m$via_seg) $3"
else
echo -e "$1 $3"
case ${#fixups[@]} in
0)
echo -e " from \e[34m$provenance\e[0m$via_seg";;
1)
echo -e " from \e[34m$provenance\e[0m with \e[34m${PROVENANCE[${fixups[0]}]}\e[0m@\e[97m${fixups[0]}\e[0m$via_seg";;
*)
echo -ne " from \e[34m$provenance\e[0m with"
local cycle=1
local sep=' '
for fixup in "${fixups[@]}"; do
if ((cycle % 3 == 0)); then
echo
echo -n ' '
sep='+ '
fi
echo -ne "$sep\e[34m${PROVENANCE[$fixup]}\e[0m@\e[97m$fixup\e[0m"
sep=' + '
((cycle++))
done
echo
if [[ -n $via_seg ]]; then
echo -e " $via_seg"
fi;;
esac
fi
fi
}
stamping=0
function reset-committer
{
info="$(git log -1 --format='%ct')"
current_stamp="${info% *}"
info="$(git log -1 --format='%at %ad' --date='format:%z')"
this_stamp="${info% *}"
this_offset="${info#* }"
info="$(git log -1 --format='%ct %cd' --date='format:%z' HEAD~1)"
last_stamp="${info% *}"
last_stamp=$((last_stamp + 1))
last_offset="${info#* }"
if [[ $stamping -eq 0 ]]; then
stamping=1
last_stamp="$this_stamp"
last_offset="$this_offset"
fi
if [[ $this_stamp -ge $last_stamp ]]; then
last_stamp="$this_stamp"
last_offset="$this_offset"
fi
if [[ $linear_commit_history -eq 1 ]]; then
if [[ $current_stamp != $last_stamp ]]; then
if ! GIT_COMMITTER_DATE="$last_stamp $last_offset" git commit --allow-empty --amend --no-verify --no-edit &> /dev/null; then
GIT_COMMITTER_DATE="$last_stamp $last_offset" git commit --allow-empty --amend --no-verify --no-edit
echo "Resetting the committer date on the last commit has failed" >&2
exit 1
fi
return 1
fi
else
if ! git rebase --committer-date-is-author-date HEAD~1 &> /dev/null; then
git rebase --committer-date-is-author-date HEAD~1
fi
fi
}
declare -A THIS_REMAP
function rebase
{
while [[ ${#WORKING[@]} -gt 0 ]]; do
instr="${WORKING[0]}"
WORKING=("${WORKING[@]:1}")
sha="${instr#* }"
verb="${instr%% *}"
case $verb in
stop-backporting)
last_provenance='_not_a_provenance'
backporting=0;;
next)
if ((backporting)); then
COMMITS+=("$sha")
fi;;
pick|fixup)
if [[ ${PROVENANCE[$sha]} != $last_provenance ]]; then
if [[ ${#SKIPPED} -gt 0 ]]; then
for commit in "${SKIPPED[@]}"; do
if ((backporting)); then
if [[ -z ${TAKEN[${PROVENANCE[$commit]}]} ]]; then
echo -e "Keeping \e[97m$commit\e[0m (from \e[34m$last_provenance\e[0m) as the branch hasn't been used"
THIS_REMAP["$commit"]="$commit"
COMMITS+=("pick $commit")
else
echo -e "Dropping \e[97m$commit\e[0m (from \e[34m$last_provenance\e[0m) as other commits from the branch have been used"
fi
fi
done
fi
last_provenance='_not_a_provenance'
SKIPPED=()
fi
if [[ $verb = 'fixup' ]]; then
this_subject="$(git log -n 1 --format='%s' $sha)"
if [[ $this_subject != "$last_subject" ]]; then
this_subject="\e[33m$this_subject\e[0m (different subject)"
fi
display-with-provenance "fixup \e[97m$sha\e[0m" "$sha" "on \e[97m$(git rev-parse --short $target_branch)\e[0m of $target_branch - $this_subject"
msg="$(git rev-parse --short HEAD)"
amend='--amend'
next=''
else
current_pick="$sha"
last_subject="$(git log -n 1 --format='%s' $sha)"
display-with-provenance "cherry-pick \e[97m$sha\e[0m" "$sha" "to $target_branch - $last_subject"
msg="$sha"
amend=''
next='pick $head'
fi
if cherry-pick "$sha"; then
commit "$msg" "$next" "$instr" "$amend"
fi;;
bootstrap)
current_pick="$sha"
last_provenance='_not_a_provenance'
SKIPPED=()
bootstrap_msg="Bootstrap \e[97m$sha\e[0m to $target_branch ($(git rev-parse --short HEAD))"
cherry_stamp="$(date '+%Y-%m-%d %H:%M:%S.%N')"
display-with-provenance "Bootstrap \e[97m$sha\e[0m" "$sha" "to $target_branch ($(git rev-parse --short HEAD))"
if [[ ! -e ../.reflog-bootstrap ]]; then
echo -n 'Scouring reflog for previous bootstraps: '
git reflog --format='%h %p' -- boot/ocamlc > ../.reflog-bootstrap
echo 'done!'
fi
cached="$(grep " $(git rev-parse --short HEAD)$" ../.reflog-bootstrap | head -n 1 | cut -f1 -d' ')"
if [[ -n $cached ]] && git diff-tree --no-commit-id --name-only -r "$cached" | grep -Fq 'boot/ocaml'; then
git merge --ff-only $cached &> /dev/null
if [[ -s boot/ocamlc ]]; then
echo "Re-using previous bootstrap computation from $cached"
if ! reset-committer; then
new="$(git rev-parse --short HEAD)"
echo " Reset committer information on $cached to give $new"
cached="$new"
fi
if ((backporting)); then
THIS_REMAP["$current_pick"]="$cached"
COMMITS+=("bootstrap $cached")
PROVENANCE["$cached"]="@$sha"
PROVENANCE["@$cached"]="$target_branch"
THIS_PROVENANCE["$cached"]="@$sha"
THIS_PROVENANCE["@$cached"]="$target_branch"
fi
else
git reset --hard HEAD~1 &> /dev/null
commit "$sha" 'bootstrap $head' "$instr"
fi
else
BOOTSTRAPS+=("$bootstrap_msg")
echo " Bootstrapping..."
if ! ./configure --disable-native-compiler --disable-ocamldoc --disable-ocamltest --disable-debugger > _log 2>&1; then
cat _log
rm _log
abort
echo "configure failed: either fix or erase $state_file"
elif ! make -j coldstart > _log 2>&1; then
cat _log
rm _log
abort
echo "make coldstart failed: either fix or erase $state_file"
echo 'Do _not_ bootstrap'
elif ! make -j coreall > _log 2>&1; then
cat _log
rm _log
abort
echo "make coreall failed: either fix or erase $state_file"
echo 'Do _not_ bootstrap'
elif ! make -j bootstrap > _log 2>&1; then
cat _log
rm _log
abort
git reset -- boot/ocaml* &> /dev/null
git checkout -- boot/ocaml* &> /dev/null
echo "make bootstrap failed: either fix or erase $state_file"
echo 'Do _not_ bootstrap'
else
rm _log
git clean -dfX &> /dev/null
# OCaml 4.06 and earlier
if [[ -e boot/ocamldep ]]; then
git add -- boot/ocamldep
fi
git add -- boot/ocamlc boot/ocamllex
if [[ -z "$(git status --porcelain)" ]]; then
# Bootstrap with no change - record a special commit to cache this
truncate --size 0 boot/ocamlc
git add -- boot/ocamlc
git commit -m 'Bootstrap (empty - for caching)' --no-verify &> /dev/null
git reset --hard HEAD~1 &> /dev/null
fi
commit "$sha" 'bootstrap $head' "$instr"
fi
fi;;
coreboot)
if [[ -z $current_pick ]]; then
current_pick="$sha"
fi
last_provenance='_not_a_provenance'
SKIPPED=()
coreboot_msg="Coreboot \e[97m$sha\e[0m to $target_branch ($(git rev-parse --short HEAD)) - $(git log -n 1 --format='%s' $sha)"
display-with-provenance "Coreboot \e[97m$sha\e[0m" "$sha" "to $target_branch ($(git rev-parse --short HEAD)) - $(git log -n 1 --format='%s' $sha)"
if cherry-pick "$sha" 'boot/ocaml*' 'Do _not_ bootstrap'; then
next="${WORKING[0]}"
if [[ ${next%% *} = 'fixup' ]]; then
echo " Deferring coreboot - next command is a fixup"
deferred_coreboot="$current_pick"
if ! git commit --reuse-message="$sha" &> _log; then
cat _log
rm _log
abort
echo "Commit failed - please fix and re-run $0"
echo 'Do _not_ bootstrap'
else
rm _log
reset-committer
head="$(git rev-parse --short HEAD)"
#PROVENANCE["$head"]="@$sha"
#PROVENANCE["@$head"]="$target_branch"
if ((backporting)); then
COMMITS+=("coreboot $head")
PROVENANCE["$head"]="@$sha"
THIS_PROVENANCE["$head"]="@$sha"
fi
fi
else
coreboot "$sha" "$coreboot_msg"
fi
fi;;
*)
echo "Internal error: unrecognised command $instr" >&2
exit 1;;
esac
if ((aborting)); then
for instr in "${WORKING[@]}"; do
echo "$instr" >> "$state_file"
done
WORKING=()
fi
done
}
function reconfigure
{
reconfigure=0
while read -r item; do
case $item in
configure)
if [[ $reconfigure -eq 0 ]]; then
reconfigure=1
fi;;
configure.ac|aclocal.m4|build-aux/*)
if grep -q '^<<<<<' $item; then
reconfigure=-1
elif [[ $reconfigure -eq 0 ]]; then
reconfigure=1
fi;;
esac
done < <(git diff --name-only --cached)
if [[ reconfigure -eq 1 ]]; then
echo -e " \e[90mconfigure.ac has been changed - regenerating configure\e[0m"
if [[ -e tools/autogen ]]; then
autogen=tools/autogen
else
autogen=./autogen
fi
rm -f configure
if [[ $(sed -ne 's/^AC_PREREQ(\[\(.*\)\])$/\1/p' configure.ac) =~ ^(2.69)?$ ]]; then
if grep -q '^autoconf ' $autogen; then
restore="sed -i -e 's/^\${1-autoconf}/autoconf/' $autogen"
sed -i -e 's/^autoconf/${1-autoconf}/' $autogen
elif grep -q '[^$]autoconf -' $autogen; then
restore="sed -i -e 's/\${1-autoconf}/autoconf/' $autogen"
sed -i -e 's/autoconf -/${1-autoconf} -/' $autogen
else
restore=''
fi
autogen="$autogen autoconf2.69"
fi
if $autogen; then
eval $restore
if [[ -e utils/config.mlp ]]; then
if ! grep -q '^<<<<<' utils/config.mlp ; then
git add -- utils/config.mlp
fi
fi
if [[ -e utils/config.common.ml ]]; then
if [[ -z "$(git status --porcelain | fgrep 'DU utils/config.common.ml')" ]]; then
if ! grep -q '^<<<<<' utils/config.common.ml ; then
git add -- utils/config.common.ml
rm -f utils/config.common.ml.in
fi
fi
fi
if [[ -e utils/config.common.ml.in ]]; then
if [[ -z "$(git status --porcelain --no-renames | grep '\(DU\|A \) utils/config.common.ml.in')" ]]; then
case "$target" in
relocatable-base-trunk|relocatable-base-5.4|relocatable-base-5.3|relocatable-base-5.2)
;;
*)
echo $target
git status --porcelain
exit 1;;
esac
if ! grep -q '^<<<<<' utils/config.common.ml.in ; then
git add -- utils/config.common.ml.in
fi
fi
fi
git add -- configure VERSION