-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack
executable file
·1440 lines (1367 loc) · 48.1 KB
/
stack
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"
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
}
menu=false
case "$1" in
--menu)
shift 1
menu=true;;
--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;;
esac
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"
rm -f ../branches
else
lock="$(realpath ${1#@})"
lock="cat \"$lock\""
rm -f ../branches
cp "${1#@}" ../branches
backports=''
fi
else
lock=''
backports=''
fi
start_time=$(date '+%Y-%m-%d %H:%M:%S')
declare -A HEADS
if [[ -z $lock ]]; then
# fix-autogen appears first, since it affects any commit touching configure.
# Part of #10254 allows overriding the autoconf command in autogen.
# They don't need to go in branches, however.
BRANCHES=([email protected] [email protected])
# Existing patches in maintenance branches and in opam-repository
# - Compatibility with -fno-common default in GCC 10 (#9180; 4.09.1)
# opam-repository carries a different patch; passing -fcommon to configure
BRANCHES+=([email protected])
# - SIGSTKSZ change in glibc 2.34 (#10266 and #10726; 4.13.0, 4.14.0)
# opam-repository carries this patch already
BRANCHES+=(sigaltstack sigaltstack-4.09@fixup)
# - Correct configure script for 4.09.1
# configure wasn't regenerated; opam-repository carries this patch already
BRANCHES+=([email protected])
# Existing patch in maintenance branch and proposed for opam-repository
# - Fix installing tools links when bytecode programs disabled
# (#8858; 4.09.1, 4.10.0)
# Permits the disabling of bytecode tool installation on 4.08+
BRANCHES+=([email protected])
# Existing patches in maintenance branches and proposed for opam-repository
# - Allow the reconfigure target on Windows (#8996; 4.09.1, 4.10.0)
# **Critical**: graphics.4.08.0 and graphics.4.08.1 require it
BRANCHES+=([email protected])
# - Compatibility mingw-w64's ANSI stdio.h changes
# (#9939; 4.10.2, 4.11.2, 4.12.0)
# **Critical**: OCaml doesn't build with mingw-w64 headers 8.0.0+ otherwise
BRANCHES+=([email protected])
# Proposed back-ports to opam-repository only
# - SUBST_STRING macro in utils/Makefile (from #8650; 4.10.0)
# Dependency of #9285
# - Link all DLLs with -static-libgcc on mingw32 (#9285 and #10046; 4.12.0)
# **Critical**: i686 mingw-w64 OCaml requires a runtime DLL not available by
# default
# - Update config.sub and config.guess (no PR; 4.12.0)
# Proposed back-ports to maintenance branches and opam-repository:
# - Compatibility with binutils 2.36+ on mingw-w64 x64
# (#10351; 4.12.1, 4.13.0)
# **Critical**: fix in 4.12.1 is too high risk to back-port, this alters the
# linker flags instead. mingw-w64 x64 is unusable without this
BRANCHES+=([email protected])
# - Compatibility with mingw-w64 headers 8.0.0 (#10062; 4.12.0)
# **Critical**: both mingw-w64 ports require this tweak for the headers to
# behave correctly if user-code loads <stdio.h> (or other
# headers pulling in mingw.h) before <caml/config.h>
BRANCHES+=([email protected])
# - Fix parallel generation of header programs (#2267 + #8626; 4.09.0)
# Prerequisite for faster-flexdll on 4.08 (and also fixes Cygwin compilation
# on 4.08)
BRANCHES+=([email protected])
# - FlexDLL bootstrapping overhaul (#10135; 4.13.0)
# Vastly improves the build performance and reliability on Windows.
# Simplifies the packaging for ocaml-base-compiler.
# - Add --disable-stdlib-manpages to configure (#8835 and #9335; 4.11.0)
# --disable-stdlib-manpages was added in 4.10.0 but broken by the time of
# release. The build is very time-consuming, especially on Windows, so this
# is recommended to allow the Windows configuration to use it
BRANCHES+=([email protected] [email protected])
# - Add --disable-ocamltest to configure (#9250; 4.11.0)
# ocamltest is never installed, but the build uses time and resources.
# Propose back-porting this to save time on older compiler builds.
BRANCHES+=([email protected])
# These all simplify the testing infrastructure, but aren't required:
# - Add --enable-warn-error (from #9625; 4.12.0)
# - Fix warn-error issues with win32graph (ocaml/graphics#28)
# - Allow make's default target to build the compiler (from #8951; 4.10.0)
# - Revert the Wdeclaration-after-statement dev-error
# (from #1176; 4.11.0 - back-ports part of #11051; 5.0.0)
# - Don't define _INTEGRAL_MAX_BITS in Windows stat implementation
# (#9686; 4.12.0)
# - autoconf tweaks (#8639; 4.11.0)
# - fix tools/check-symbol-names (#9260; 4.11.0)
BRANCHES+=([email protected] [email protected] [email protected] [email protected] [email protected] [email protected] autoconf-tweak-4.09@fixup~1 [email protected])
# - CI tweaks and backports
BRANCHES+=([email protected] ocamltest [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] ocamltest-4.09@fixup~2 [email protected] [email protected] [email protected])
# This slightly simplifies the patch rebasing infrastructure, as it stops
# configure from being completely rejected
# - Ensure that configure can be patched (#9847; 4.12.0)
BRANCHES+=([email protected])
# Back-ports
# NB These back-ports have been selected to ease the back-porting of the main
# PRs. Some of the back-ports will want tweaking after the PRs are finalised.
# For example, back-porting #1941 and #8992 makes the -set-global-string
# portion of enable-relative rebase more easily, but the better back-port
# would be to add the argument using the pre-4.10 mechanisms.
# - Original long shebang "fix" (#8622; 4.10.0)
# - Hardening of -use-runtime for spaces and symbols (#11112; 5.0.0)
# - Expose Config.ext_exe (from #9652; 4.12.0)
# - Caml_inline macro (from #1176; 4.11.0)
# - _T / T macro (#2075; 4.09.0)
# - ocamlrun -config (#9284; 4.13.0)
# - Eliminate tools/ocamlmklibconfig.ml (#10204; 4.13.0)
# - Add HAS_REALPATH to s.h (from #10047; 4.13.0)
# - Add runtime/build_config.h (from #10451; 4.13.0)
# - Simplification of ocamlcp/ocamloptp processing (#1941; 4.09.0)
# - Share argument implementations (from #8992; 4.10.0)
BRANCHES+=([email protected] [email protected] [email protected] [email protected] [email protected] use-runtime-evil-fixup@fixup [email protected] [email protected] [email protected] [email protected] build_config-4.11@fixup build_config-4.08@fixup [email protected] [email protected] [email protected])
# Branches
# - misc-win-fixes
# - windows-ln
# - one-camlheader
BRANCHES+=(misc-win-fixes windows-ln windows-ln-5.0@fixup windows-ln-4.11@fixup one-camlheader one-camlheader-4.13@fixup~1)
# - target-bindir
BRANCHES+=(target-bindir target-bindir-5.0@fixup)
# - ld.conf-CRLF
# - ld.conf-search
BRANCHES+=(ld.conf-CRLF ld.conf-search)
# - ld.conf-relative
BRANCHES+=(ld.conf-relative [email protected])
# - compiled-primitives
BRANCHES+=(compiled-primitives compiled-primitives-5.0@fixup compiled-primitives-4.12@fixup)
# - enable-relative
BRANCHES+=(enable-relative unified-enable-relative@fixup~1 unified-enable-relative-5.0@fixup~1 [email protected]@fixup [email protected]@fixup cmmgen-4.09@fixup~1 [email protected]@fixup)
# - ld-warning
BRANCHES+=(ld-warning ld-warning-5.0@fixup)
# - runtime-id
BRANCHES+=(runtime-id runtime-id-5.0@fixup runtime-id-4.14@fixup runtime-id-4.13@fixup runtime-id-4.12@fixup runtime-id-4.11@fixup runtime-id-4.10@fixup runtime-id-4.09@fixup runtime-id-4.08@fixup)
# - runtime-suffixing
BRANCHES+=(runtime-suffixing unified-runtime-suffixing@trunk@fixup [email protected]@fixup [email protected]@fixup unified-runtime-suffixing-4.12@fixup unified-runtime-suffixing-4.11@fixup~1)
# - camlheader-search
BRANCHES+=(camlheader-search unified-camlheader-search@fixup~1 unified-camlheader-search-5.0@fixup~1 unified-camlheader-search-4.14@fixup~1 unified-camlheader-search-4.13@fixup~1 unified-camlheader-search-4.12@fixup~1 unified-camlheader-search-4.11@fixup~1 unified-camlheader-search-4.10@fixup~1)
TARGETS=(trunk 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
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 "${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
echo "relocatable-base-$instruction ${HEADS[relocatable-base-$instruction]}"
else
echo "$instruction ${HEADS[$instruction]}"
fi
done > ../branches
else
while read -r branch head; do
if [[ $branch = ${branch#relocatable-base-} ]]; then
BRANCHES+=($branch)
else
TARGETS+=(${branch#relocatable-base-})
fi
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
function menu-info
{
if [[ $menu = 'true' ]]; then
cmd="\e[34m$1\e[0m: "
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"
done
fi
}
declare -A PICKED
declare -A TOUCHED
COMMANDS=()
COMMITS=()
WORKING=()
base_trunk="${HEADS['relocatable-base-trunk']}"
if [[ ! -e $state_file ]]; then
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
# Calculate the menu. First, determine the initial commit list from the
# branches.
for entry in "${BRANCHES[@]}"; do
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]}")"
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}"
cmd=''
count=-1
while read -r entry; do
commit="${entry%% *}"
if [[ $count -eq -1 && ${entry#* } = 'Basis' ]]; then
continue
fi
cmd="$cmd $commit"
((count++))
done < <(git log upstream/$upstream..${HEADS[$entry]} --format="%h %s" --reverse)
menu-info "$branch" "adjust $count $branch" "$cmd"
COMMITS+=("adjust $count $branch$cmd")
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"
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
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 [[ $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
# Now generate commands for each target branch
for root in "${TARGETS[@]}"; do
COMMANDS+=("branch relocatable-base-$root")
done
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)
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;;
lock)
echo "${line#* }" >> ../.stack-branches;;
cherry-stamp)
cherry_stamp="${line#* }";;
start)
start_time="${line#* }";;
provenance)
line="${line#* }"
PROVENANCE[${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
rm -f branches
mv ../branches .
GIT_INDEX_FILE="$lock_index" git add -f branches
rm branches
fi
function abort
{
# echo "Branch: $branch for $target"
echo " $action"
{
for sha in "${!PROVENANCE[@]}"; do
echo "provenance $sha ${PROVENANCE[$sha]}"
done;
for branch in "${!TOUCHED[@]}"; do
echo "touched $branch"
done;
echo "start $start_time"
echo "cherry-stamp $cherry_stamp"
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
echo "lock relocatable-base-$instruction ${HEADS[relocatable-base-$instruction]}"
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
}
function reset-committer
{
if ! git rebase --committer-date-is-author-date HEAD~1 &> /dev/null; then
git rebase --committer-date-is-author-date HEAD~1
echo "Resetting the committer date on the last commit has failed" >&2
exit 1
fi
}
function rebase
{
while [[ ${#WORKING[@]} -gt 0 ]]; do
instr="${WORKING[0]}"
WORKING=("${WORKING[@]:1}")
sha="${instr#* }"
verb="${instr%% *}"
case $verb in
next)
COMMITS+=("$sha");;
pick|fixup)
if [[ ${PROVENANCE[$sha]} != $last_provenance ]]; then
if [[ ${#SKIPPED} -gt 0 ]]; then
for commit in "${SKIPPED[@]}"; do
echo -e "Keeping \e[97m$commit\e[0m (from \e[34m$last_provenance\e[0m) as it hasn't yet been used"
COMMITS+=("pick $commit")
done
fi
last_provenance='_not_a_provenance'
SKIPPED=()
fi
if [[ $verb = 'fixup' ]]; then
display-with-provenance "fixup \e[97m$sha\e[0m" "$sha" "on $target_branch - $(git log -n 1 --format='%s' $sha)"
msg="$(git rev-parse --short HEAD)"
amend='--amend'
next=''
else
display-with-provenance "cherry-pick \e[97m$sha\e[0m" "$sha" "to $target_branch - $(git log -n 1 --format='%s' $sha)"
msg="$sha"
amend=''
next='pick $head'
fi
if cherry-pick "$sha"; then
commit "$msg" "$next" "$instr" "$amend"
fi;;
bootstrap)
last_provenance='_not_a_provenance'
SKIPPED=()
bootstrap_msg="Bootstrap \e[97m$sha\e[0m to $target_branch ($(git rev-parse --short HEAD))"
display-with-provenance "Bootstrap \e[97m$sha\e[0m" "$sha" "to $target_branch ($(git rev-parse --short HEAD))"
cached="$(git reflog --format='%h %p' | grep " $(git rev-parse --short HEAD)$" | head -n 1 | cut -f1 -d' ')"
if [[ -n $cached ]] && git diff-tree --no-commit-id --name-only -r "$cached" | grep -Fq 'boot/ocaml'; then
echo "Re-using previous bootstrap computation from $cached"
git merge --ff-only $cached &> /dev/null
COMMITS+=("bootstrap $cached")
PROVENANCE["$cached"]="@$sha"
PROVENANCE["@$cached"]="$target_branch"
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
commit "$sha" 'bootstrap $head' "$instr"
fi
fi;;
coreboot)
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"
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"
COMMITS+=("coreboot $head")
PROVENANCE["$head"]="@$sha"
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
git add -- configure VERSION
else
eval $restore
abort
echo "Regenerating configure failed - please fix and re-run $0"
return 1
fi
fi
}
function rerere-hash
{
# This facility is crude, but it should be adequate to compute rerere hashes
awk -f- "$1" <<'EOF'
/^(<<<<<<<|=======)/ {
in_hunk++;
next;
}
/^>>>>>>>/ {
if (one > two) {
printf("%s\0%s\0", two, one);
} else {
printf("%s\0%s\0", one, two);
}
in_hunk=0;
one="";
two="";
next;
}
{
if (in_hunk == 1) {
one = one $0 "\n";
} else if (in_hunk == 2) {
two = two $0 "\n";
}
}
EOF
}
function cherry-pick
{
unset RERERE
declare -gA RERERE
local conflicts=0
cherry_stamp="$(date '+%Y-%m-%d %H:%M:%S.%N')"
# Initially cherry-pick without using rerere
git -c rerere.enabled=false cherry-pick --no-commit "$1" &> /dev/null
# Now scan the status for any files with conflicts and, if found, compute
# the rerere hash
while read -r status; do
file=${status:3}
case ${status:0:2} in
UU|AA)
if grep -q '^<<<<<' "$file" ; then
rerere_hash=$(rerere-hash "$file" | sha1sum | cut -f1 -d' ')
if [[ -n ${RERERE[$rerere_hash]} ]]; then
# Conflicting IDs - assume stronger matching here
RERERE[$rerere_hash]='*'
else
RERERE[$rerere_hash]=$file
fi
conflicts=1
fi;;
esac
done < <(git status --porcelain 2>/dev/null)
# Only if there were conflicts, repeat the cherry-pick, this time with rerere
# turned on.
if ((conflicts)); then
git reset --hard HEAD &> /dev/null
sleep 0.01
git cherry-pick --no-commit "$1" &> /dev/null
fi
git reset -- Changes $2 &> /dev/null
git checkout -- Changes $2 &> /dev/null
if reconfigure; then
while read -r status; do
case ${status:0:2} in
DU)
git reset -- "${status:3}" &> /dev/null
TO_DELETE+=("${status:3}");;
UU|AA)
if ! grep -q '^<<<<<' "${status:3}" ; then
git add -- "${status:3}"
else
git reset -- "${status:3}" &>/dev/null
fi;;
esac
done < <(git status --porcelain)
if [[ -n $(git diff --stat) ]]; then
abort
git status
echo
echo "Please resolve changes, add to the index and re-run $0"
if [[ -n $3 ]]; then
echo "$3"
fi
return 1
else
return 0
fi
else
return 1
fi
}
function coreboot
{
head_sha="$(git rev-parse --short HEAD)"
# Check the cache
if [[ -z "$(git status --porcelain)" ]]; then
echo " Commit skipped - it's empty"
return
fi
if ! git commit --reuse-message="$1" &> _log; then
cat _log
rm _log
abort
echo "Commit failed - please fix and re-run $0"
else
rm _log
reset-committer
code_sha="$(git rev-parse --short HEAD)"
show-rr-resolutions "$cherry_stamp" "$head"
cached="$(git reflog --format='%h %p' | grep " ${code_sha}\$" | head -n 1 | cut -f1 -d' ')"
if [[ -n $cached ]] && git diff-tree --no-commit-id --name-only -r "$cached" | grep -Fq 'boot/ocaml'; then
# This code commit has a bootstrap parent - so we can search for a unified commit
combined=''
while read -r commit; do
if git diff --quiet $cached $commit; then
combined="$commit"
break;
fi
done < <(git reflog --format='%h %p' | grep " ${head_sha}\$" | cut -f1 -d' ' | sort | uniq)
if [[ -n $combined ]] && git diff-tree --no-commit-id --name-only -r "$combined" | grep -Fq 'boot/ocaml'; then
echo " Re-using previous coreboot computation from $combined"
git reset --hard $combined &> /dev/null
if [[ $(git log -1 --format='%cn <%ce> on %cd') != $(git log -1 --format='%an <%ae> on %ad') ]]; then
reset-committer
new="$(git rev-parse --short HEAD)"
echo " Reset committer information on $combined to give $new"
combined="$new"
fi
COMMITS+=("coreboot $combined")
PROVENANCE["$combined"]="@$1"
PROVENANCE["@$combined"]="$target_branch"
return
fi
fi
git reset --hard $head_sha &> /dev/null
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"
return
fi
BOOTSTRAPS+=("$2")
echo ' Warming up...'
if ! make -j coldstart > _log 2>&1; then
cat _log
rm _log
abort
echo "make coldstart failed: either fix or erase $state_file"
return
fi
rm -f _log
echo ' Building...'
if ! git merge --ff-only $code_sha &> /dev/null || \
! { if git diff-tree --no-commit-id --name-only -r "$code_sha" | grep -Fxq configure; then \
./configure --disable-native-compiler --disable-ocamldoc --disable-ocamltest --disable-debugger; \
else \
./config.status;
fi; \
make -j coreall ; } > _log 2>&1 ; then