-
Notifications
You must be signed in to change notification settings - Fork 13
/
umpf
executable file
·1911 lines (1713 loc) · 48.2 KB
/
umpf
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
# We ignore the check SC2155 "Declare and assign separately to avoid masking
# return values", as this reduced legibility and return values are ignored.
# shellcheck disable=SC2155
#
# MIT License
#
# Copyright (c) 2022 Pengutronix
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Universal Magic Patch Functionator
GIT_RELATIVE=""
GIT_REMOTE=""
GIT_FALLBACK_REMOTE=""
SERIES=""
BASE=""
NAME=""
FLAGS=""
PATCH_DIR="umpf-patches"
IDENTICAL=false
STABLE=false
FORCE=false
UPDATE=false
VERBOSE=false
VERSION_SEPARATOR=-
VERSION=1
AUTO_RERERE=false
BLOCK_SIZE=100
BB=false
NIX=false
declare -A OVERRIDES
# Create a pristine environment to minimize unnecessary fuzz when different
# users use umpf on the same patch stack. That is, don't load any config files,
# and pin down environment variables which could influence git's behaviour or
# patch output.
PRISTINE_GIT_PARAMS=( -c gc.auto=0 -c core.abbrev=12 -c core.editor=:
-c sequence.editor=: )
# Maybe we need to save name and e-mail from the user config…
# Note from git-commit-tree(1): if set in the user's environment, GIT_AUTHOR_*
# and GIT_COMMITTER_* still take precedence over the user.* config variables.
PRISTINE_GIT_AUTHOR=$(git config --get user.name)
PRISTINE_GIT_EMAIL=$(git config --get user.email)
if [ -n "${PRISTINE_GIT_AUTHOR}" ]; then
PRISTINE_GIT_PARAMS+=( -c user.name="${PRISTINE_GIT_AUTHOR}" )
fi
if [ -n "${PRISTINE_GIT_EMAIL}" ]; then
PRISTINE_GIT_PARAMS+=( -c user.email="${PRISTINE_GIT_EMAIL}" )
fi
pristine_git() {
# Notes from the git(1) manpage:
# - GIT_DIFF_OPTS takes takes precedence over -U command line parameter
# - GIT_EDITOR takes precedence over core.editor config variable
HOME=/nonexistent \
XDG_CONFIG_HOME=/nonexistent \
GIT_CONFIG_NOSYSTEM=true \
GIT_DIFF_OPTS="-u3" \
GIT_EDITOR=: \
git "${PRISTINE_GIT_PARAMS[@]}" "$@"
}
GIT="pristine_git"
GIT_DIR="${GIT_DIR:-$(${GIT} rev-parse --git-dir 2> /dev/null)}"
GIT_DIR="${GIT_DIR:-.git}"
STATE="${GIT_DIR}/umpf"
### flags ####
SUPPORTED_FLAGS=("extraversion" "upstreamstatus")
declare -A flag_extraversion
flag_extraversion[val]="replace"
flag_extraversion[values]="replace conflictfree"
declare -A flag_upstreamstatus
flag_upstreamstatus[val]="none"
flag_upstreamstatus[values]="none insert"
##############
info() {
if [ -z "$*" ]; then
echo >&2
else
echo "umpf: $*" >&2
fi
}
bailout() {
if [ -d "${STATE}" ]; then
echo "${line}" > "${STATE}/failed"
cat <<- EOF > "${STATE}/config"
PATCH_DIR="${PATCH_DIR}"
GIT_REMOTE="${GIT_REMOTE}"
SERIES="${SERIES}"
IDENTICAL="${IDENTICAL}"
STABLE="${STABLE}"
UPDATE="${UPDATE}"
FLAGS="${FLAGS}"
VERSION_SEPARATOR="${VERSION_SEPARATOR}"
VERSION="${VERSION}"
EOF
if ${FORCE}; then
echo "FORCE=true" >> "${STATE}/config"
fi
declare -p OVERRIDES >> "${STATE}/config"
for flag in "${SUPPORTED_FLAGS[@]}"; do
declare -p "flag_${flag}" >> "${STATE}/config"
done
fi
if [ -n "$*" ]; then
info "$*"
info
fi
info "Resolve issues and call 'umpf continue', or call 'umpf abort' to start over."
exit 1
}
abort() {
if [ -n "$*" ]; then
info "$*"
fi
if [[ -d "${GIT_DIR}/rebase-apply" || -d "${GIT_DIR}/rebase-merge" ]]; then
${GIT} rebase --abort
fi
if [ -f "${GIT_DIR}/CHERRY_PICK_HEAD" ]; then
${GIT} cherry-pick --abort
fi
if [ -f "${GIT_DIR}/MERGE_HEAD" ]; then
${GIT} merge --abort
fi
undo_persistent
cleanup
exit 1
}
cleanup() {
if [ -n "${series_out}" ]; then
exec {series_out}<&-
unset series_out
fi
if [ -n "${series_in}" ]; then
exec {series_in}<&-
unset series_in
fi
rm -rf "${GIT_DIR}/refs/umpf"
rm -rf "${STATE}"
}
usage() {
cat <<- EOF
usage: $0 [<options>] [--] <command>
Mandatory arguments to long options are mandatory for short options too.
--auto-rerere automatically try to use rerere after conflicts
--bb with format-patch: write patch series for bitbake
--nix with format-patch: write patch series nix
-h, --help
-f, --force
--flags specify/override umpf-flags
-i, --identical use exact commit hashes, not tip of branches
-s, --stable create a 'stable' tag from a branch based on an
existing tag. Implies '--identical'.
-b, --base <base> specify/override base name [default: merge base]
-n, --name <name> specify/override umpf name
-p, --patchdir <path> with format-patch: write patches to <path>/
--relative <path> create patches relative to <path>
-r, --remote <remote> use <remote> to resolve branch names
--override <topic>[=<commit-ish>]
use commit-ish instead for the topic. May be
specified more than once. If only <topic> is
specified, it's interpreted as
<topic>=<remote>/<topic>
-u, --update with --patchdir: update existing patches in <path>
-v, --version <version> with tag: overwrite version number [default: 1]
Commands:
diff <commit-ish> show patches not in any topic branch (not
upstream) and patches missing locally
show <commit-ish> show an useries file from an umpf
tag <commit-ish> generate a utag from an umerge
format-patch <utag> generate a useries file and patch stack
from a utag
merge <commit-ish> [<name>]merge one a topic branches into current
head, like git merge, but umpf compatible
Use <name> as topic name if specified.
Otherwise <commit-ish> must be the correct
topic name. The remote is removed if necessary
build <umpf> build an umerge from another umpf
distribute <commit-ish> push patches not yet in any topic branch
upstream
continue continue a previously interrupted umpf command
abort abort a previously started umpf command
init <seriesfile> manually create an umpf series file by
answering questions
EOF
}
### setup ###
setup() {
local tmp args o l topic commitish
if tmp="$(git config umpf.patch-dir)"; then
PATCH_DIR="${tmp}"
fi
if tmp="$(git config umpf.block-size)"; then
BLOCK_SIZE="${tmp}"
fi
if tmp="$(git config umpf.remote)"; then
GIT_REMOTE="${tmp}"
fi
if tmp="$(git config umpf.fallback-remote)"; then
GIT_FALLBACK_REMOTE="${tmp}"
fi
o="fhisub:n:p:r:v:"
l="auto-rerere,bb,nix,flags:,force,help,identical,stable,update,base:,name:,patchdir:,relative:,override:,remote:,version:"
if ! args="$(getopt -n umpf -o "${o}" -l "${l}" -- "${@}")"; then
usage
exit 1
fi
eval set -- ${args}
while [ ${#} -gt 0 ]; do
OPT="${1}"
shift
case "${OPT}" in
--auto-rerere)
if [[ "$(${GIT} config --get rerere.enabled)" = "true" || -d "$(${GIT} rev-parse --git-common-dir)/rr-cache" ]]; then
AUTO_RERERE=true
else
bailout "--auto-rerere is given, but rerere is not enabled in current repo."
fi
;;
--bb)
BB=true
;;
--nix)
NIX=true
;;
-f|--force)
FORCE=true
;;
--flags)
FLAGS="${1}"
shift
;;
-h|--help)
usage
exit
;;
-i|--identical)
IDENTICAL=true
;;
-s|--stable)
STABLE=true
IDENTICAL=true
;;
-u|--update)
UPDATE=true
;;
-b|--base)
BASE="${1}"
shift
;;
-n|--name)
NAME="${1}"
shift
;;
-p|--patchdir)
PATCH_DIR="${1}"
shift
;;
--relative)
GIT_RELATIVE="${1}"
shift
;;
-r|--remote)
GIT_REMOTE="${1}"
shift
;;
--override)
IFS="=" read -r topic commitish <<< "${1}"
if [ -n "${commitish}" ]; then
OVERRIDES[$topic]="${commitish}"
else
OVERRIDES[$topic]=""
fi
shift
;;
-v|--version)
VERSION="${1}"
shift
;;
--)
break
;;
esac
done
COMMAND="${1}"
shift
ARGS=( "${@}" )
if [[ -n "${GIT_RELATIVE}" && ! "${COMMAND}" =~ ^(init|build|tag)$ ]]; then
bailout "--relative is only valid for 'init', 'build' and 'tag'"
fi
test -n "${PATCH_DIR}" || bailout "Patch directory not defined"
if [ "${PATCH_DIR}" == "${PATCH_DIR#/}" ]; then
PATCH_DIR="$PWD/${PATCH_DIR}"
fi
PATCH_DIR="${PATCH_DIR%/}"
if [ -n "${GIT_REMOTE}" ]; then
GIT_REMOTE="${GIT_REMOTE}/"
fi
if [ -n "${GIT_FALLBACK_REMOTE}" ]; then
GIT_FALLBACK_REMOTE="${GIT_FALLBACK_REMOTE}/"
fi
for topic in "${!OVERRIDES[@]}"; do
local rev
if [ -z "${OVERRIDES[$topic]}" ]; then
OVERRIDES[$topic]="${GIT_REMOTE}/${topic}"
fi
if ! rev="$(${GIT} rev-parse "${OVERRIDES[$topic]}^{}" 2> /dev/null)"; then
echo "Revision '${OVERRIDES[$topic]}' not found for topic '${topic}'"
exit 1
fi
OVERRIDES[$topic]="${rev}"
done
}
prepare() {
[ -d "${GIT_DIR}" ] || bailout "Not a git repository."
SERIES="${1:-HEAD}"
}
prepare_series() {
local s
if [ -f "${SERIES}" ]; then
cp "${SERIES}" "${STATE}/series"
return
fi
if ${GIT} rev-parse --verify -q "${SERIES}" > /dev/null; then
import_series "${SERIES}" &&
return
fi
if ! s="$(${GIT} tag | sort -r | grep -m1 "^${SERIES}/[0-9]\{8\}$")"; then
abort "Unknown file or revision '${SERIES}'"
fi
import_series "${s}"
}
prepare_persistent() {
prepare "${2}"
test -d "${STATE}" && bailout "umpf in progress!"
mkdir "${STATE}"
echo "${1}" > "${STATE}/command"
prepare_series
check_series
(${GIT} symbolic-ref -q --short HEAD || ${GIT} rev-parse HEAD) > "${STATE}/head-name"
}
undo_persistent() {
if test -e "${STATE}/head-name"; then
head_name=$(<"${STATE}/head-name")
${GIT} checkout -q "${head_name}"
fi
}
### helper ###
nice_branch() {
read -r -a replies < <(sed -r -n 's,^(remotes/([^/]*)/|heads/)?(.*),\3 \2,p' <<< "${1}")
}
find_branch_rev() {
local name branch remote
local -a branches replies
name="${1}"
if [ -n "${OVERRIDES[$name]}" ]; then
reply="${OVERRIDES[$name]}"
return
fi
if ${IDENTICAL}; then
reply="${2}"
return
fi
remote="${GIT_REMOTE}"
mapfile -t branches < <(${GIT} show-ref | sed -n -r "s:[^ ]* refs/((heads|remotes/[^ ]*)/${name})\$:\1:p" | sort -r)
while [ -z "${GIT_REMOTE}" ] || ! reply="$(${GIT} rev-parse "${remote}${name}^{}" 2> /dev/null)"; do
if [ "${#branches[@]}" -eq 0 ]; then
break
fi
if [ -z "${GIT_REMOTE}" ]; then
info "Remote undefined. Choose the branch with the correct remote:"
else
info "Branch not found for '${remote}'. Choose alternative branch:"
fi
local i=0 def=0
for branch in "${branches[@]}"; do
nice_branch "${branch}"
echo "${i}) ${replies[1]:+${replies[1]}/}${replies[0]}"
if [ "${GIT_FALLBACK_REMOTE}" == "${replies[1]}/" ]; then
def="${i}"
fi
i=$((i+1))
done
read -e -i ${def} -p "branch number: " i
nice_branch "${branches[$i]}"
remote="${replies[1]:-refs/heads}/"
if [ -z "${GIT_REMOTE}" ]; then
GIT_REMOTE="${remote}"
info "Using remote '${GIT_REMOTE}'."
fi
done
if [ -z "${reply}" ]; then
if [ "$(${GIT} rev-parse "${name}")" == "${name}" ]; then
reply="${name}"
fi
fi
if [ -z "${reply}" ]; then
bailout "Failed to find '${name}'"
fi
for branch in "${branches[@]}"; do
local b="$(${GIT} rev-parse --verify "${branch}^{}")"
if [ "${reply}" == "${b}" ]; then
continue
fi
if [ "$(${GIT} merge-base "${reply}" "${branch}")" == "${reply}" ]; then
info "Warning: The following commits are in '${branch}' but not in '${remote}${name}':"
GIT_PAGER="" ${GIT} log --oneline "${reply}...${b}"
if tty -s; then
local choice=""
read -e -i n -p "Use ${branch} instead? [y/n]: " choice
if [ "${choice}" == "y" ]; then
reply="${b}"
fi
fi
fi
done
}
list_branch_names() {
rev="${1}"
filter="${2}"
${GIT} show-ref --head | sed -n -r -e "s:${rev} refs/(${filter}):\1:p"
}
find_branch_name() {
local head merge mergelog candidate
local -a branches replies
head="${1}"
merge="${2}"
mergelog=$(${GIT} log --oneline -1 "${head}")
name="$(${GIT} log -1 ${head} | sed -n -r 's,^ umpf-merge-topic: (.*)$,\1,p')"
if [ -n "${name}" ]; then
if ${GIT} log -1 ${head} | grep -q '^ umpf-squash-topic$' ; then
squash=1
fi
return
fi
if [ -n "${GIT_REMOTE}" ]; then
mapfile -t branches < <(list_branch_names "${merge}" "remotes/${GIT_REMOTE}")
fi
if [ ${#branches[@]} -ne 1 ]; then
mapfile -t branches < <(list_branch_names "${merge}")
fi
case "${#branches[@]}" in
0)
info "No branch found for ${mergelog}"
candidate=$(sed -n "s/^[0-9a-f]* Merge.* '\([^ ]*\)' .*/\1/p" <<< "${mergelog}")
read -e -i "${candidate}" -p "topic: " name
;;
1)
nice_branch "${branches[0]}"
name="${replies[0]}"
;;
*)
info "Multiple branches found for $mergelog"
local i=0
for branch in "${branches[@]}"; do
nice_branch "${branch}"
info "${i}) ${replies[1]:+[${replies[1]}]} ${replies[0]}"
i=$((i+1))
done
while [ -z "${name}" ]; do
read -e -p "branch number: " i
nice_branch "${branches[$i]}"
name="${replies[0]}"
done
if [ -z "${GIT_REMOTE}" ]; then
GIT_REMOTE="${replies[1]:-refs/heads}/"
info "Using remote '${GIT_REMOTE}'."
fi
esac
}
#
# needs ${branches[@]} and ${branch_names[@]}
#
show_diff() {
local head base i
local -a all
head="${1}"
base="${2}"
all=( "${head}" "^${base}" "${branches[@]/#/^}" )
if [ -n "$(${GIT} rev-list --no-merges "${all[@]}")" ]; then
info
info "These patches are not in any topic branch:"
${GIT} rev-list --no-merges --oneline "${all[@]}"
${GIT} rev-list --no-merges --reverse "${all[@]}" > "${STATE}/diff"
info
fi
for i in $(seq 0 $((${#branches[@]}-1))); do
all=( "^${head}" "^${base}" "${branches[i]}" )
if [ -n "$(${GIT} rev-list --no-merges "${all[@]}")" ]; then
info
info "These patches are in ${branch_names[i]} but not in '${head}':"
${GIT} rev-list --no-merges --oneline "${all[@]}"
info
fi
done
}
topic_from_rev() {
local rev="$(${GIT} rev-parse --symbolic-full-name "${1}")"
if [ -z "${rev}" ]; then
# not a branch name, so try to find one
rev="$(${GIT} describe --all "${1}")"
rev="$(${GIT} rev-parse --symbolic-full-name "${rev}")"
fi
local -a remotes
mapfile -t remotes < <(${GIT} remote)
remotes=( "${remotes[@]/#/refs\/remotes\/}" "refs/heads" )
local pattern="${remotes[*]}"
pattern="^(${pattern// /|})/"
sed -r "s,${pattern},," <<< "${rev}" || echo "${1}"
}
import_series() {
local series="${STATE}/series"
local import="${1}"
local names base_rev
local -A tmp_branches
for base_rev in $(${GIT} rev-parse ${import}) $(${GIT} rev-list --merges -1 "${import}"); do
${GIT} log -1 "${base_rev}" | sed -n '/^ # umpf-base/,/^ # umpf-end/p' > "${series}"
if [ "$(wc -l < "${series}")" -gt 2 ]; then
break
fi
done
if [ "$(wc -l < "${series}")" -gt 2 ]; then
info "Using series from commit message..."
${GIT} log -1 --format='%b' "${base_rev}" | sed '$d' > "${series}"
if ${STABLE}; then
local topic rev
rev="$(${GIT} rev-parse ${import})"
if ! topic="$(${GIT} symbolic-ref -q --short "${import}")"; then
topic="$(topic_from_rev "${import}")"
fi
s="# umpf-topic: ${topic}\n# umpf-hashinfo: ${rev}\n"
sed -i "s;^# umpf-end;${s}\0;" "${series}"
fi
${GIT} rev-parse "${base_rev}^" > "${STATE}/flat"
if ${VERBOSE}; then
info
cat "${series}"
fi
return
fi
info "Creating series from merges..."
local -a merges branches branch_names
local -A branch_squashes
base_rev="$(${GIT} merge-base "${base_rev}^1" "${base_rev}^2")"
exec {revlistfd}< <(${GIT} rev-list --merges --topo-order --parents "${base_rev}...${import}")
while read head base merges <&${revlistfd}; do
local magic base name
exec {localfd}< <(${GIT} notes show "${head}" 2>/dev/null)
# Reading git notes must be in sync with run_build()
while read magic value <&${localfd}; do
case "${magic}" in
umpf-build-note:)
BASE="${BASE:-${value% *}}"
NAME="${NAME:-${value#* }}"
;;
umpf-build-flags:)
FLAGS="${FLAGS:-${value}}"
;;
umpf-build-relative:)
GIT_RELATIVE="${GIT_RELATIVE:-${value}}"
;;
esac
done
exec {localfd}<&-
if [[ -n "${BASE}" && -n "${NAME}" ]]; then
break
fi
done
exec {revlistfd}<&-
if [ -z "${BASE}" ]; then
BASE="$(${GIT} describe "${base_rev}" 2>/dev/null)"
read -e -i "${BASE}" -p "base: " BASE
fi
echo "# umpf-base: ${BASE}" >> "${series}"
if [ -n "${GIT_RELATIVE}" ]; then
echo "# umpf-relative: ${GIT_RELATIVE}" >> "${series}"
fi
if [ -z "${NAME}" ]; then
if ! NAME="$(${GIT} symbolic-ref -q --short "${import}")"; then
if ${GIT} show-ref --tags --heads -q "${import}"; then
NAME="${import}"
fi
fi
NAME="${NAME##${GIT_REMOTE}}"
if [ -z "${NAME}" ]; then
read -e -p "name: " NAME
fi
fi
echo "# umpf-name: ${NAME}" >> "${series}"
mapfile -t merges < <(${GIT} rev-list --merges --reverse --topo-order --parents "${BASE}...${import}" | {
while read head base merges; do
for merge in ${merges}; do
echo "${head}:${merge}"
done
done } )
names="#"
for merge in "${merges[@]}"; do
local squash=
head="${merge%:*}"
branch="${merge#*:}"
find_branch_name "${head}" "${branch}"
test -z "${name}" && continue
if ! grep -q "#${name}#" <<< "${names}"; then
branch_names[${#branch_names[@]}]="${name}"
branch_squashes[${name}]="${squash}"
names="${names}${name}#"
fi
tmp_branches[${name}]="${branch}"
done
for name in "${branch_names[@]}"; do
branch="${tmp_branches[${name}]}"
if [ "${branch_squashes[${name}]}" = 1 ]; then
echo "# umpf-squash-topic: ${name}" >> "${series}"
else
echo "# umpf-topic: ${name}" >> "${series}"
fi
echo "# umpf-hashinfo: ${branch}" >> "${series}"
branches[${#branches[@]}]="${branch}"
done
echo "# umpf-end" >> "${series}"
if ${VERBOSE}; then
info
cat "${series}"
show_diff "${import}" "${BASE}"
fi
}
merge() {
local rev topic into squash_topic
rev="${1}"
topic="${2}"
if [ -z "${topic}" ]; then
topic="$(topic_from_rev "${rev}")"
fi
if [ -z "${topic}" ]; then
info "Failed to derive topic name from '${rev}'"
if [ "${COMMAND}" = "merge" ]; then
info
info "Use 'umpf merge <commit-ish> <name>' to specify the topic name".
info
fi
return 1
fi
if [ -f "${STATE}/name" ]; then
into=" into $(<${STATE}/name)"
elif ! into=" into $(${GIT} symbolic-ref -q --short HEAD)"; then
into=""
fi
if [ "${squash}" = 1 ]; then
squash_topic="umpf-squash-topic"
fi
info "merging '${topic}'..."
${GIT} merge --no-ff -m "
Merge '${topic}'${into}
umpf-merge-topic: ${topic}
${squash_topic}
" "${rev}"
if [ $? -ne 0 ]; then
${AUTO_RERERE} &&
info "merge failed. Trying to continue with rerere solutions..." &&
test -z "$(${GIT} rerere remaining)" &&
info "Looks like rerere was successful." &&
${GIT} add -u &&
${GIT} commit
fi
}
### namespace: check ###
verify_topic() {
if [ ! -e "${STATE}/check" ]; then
return
fi
if ${IDENTICAL} && ! grep -q hashinfo "${STATE}/check"; then
abort "Cannot run identical '$(<${STATE}/command)' without 'hashinfo' (${line})!"
fi
if [ "$(<${STATE}/command)" = "format_patch" ] && ! grep -q topic-range "${STATE}/check"; then
abort "Cannot run '$(<${STATE}/command)' without a 'topic-range' for each 'topic'!" \
"Maybe you need to 'tag' first?"
fi
}
check_topic() {
verify_topic
echo "topic" > "${STATE}/check"
}
check_release() {
verify_topic
echo "release" > "${STATE}/check"
# release has no hashinfo. Add it to satisfy verify_topic in check_end
echo "hashinfo" >> "${STATE}/check"
}
check_hashinfo() {
if [ ! -e "${STATE}/check" ] || grep -q hashinfo "${STATE}/check"; then
abort "'hashinfo' without 'topic'!"
fi
echo "hashinfo" >> "${STATE}/check"
}
check_topic_range() {
if [ ! -e "${STATE}/check" ] || grep -q topic-range "${STATE}/check"; then
abort "'topic-range' without 'topic'!"
fi
echo "topic-range" >> "${STATE}/check"
}
verify_flag() {
local flag validval=0
flag=flag_$flagname
declare -p ${flag} &>/dev/null || abort "Unknown umpf-flag: ${flagname} (supported flags: ${SUPPORTED_FLAGS[*]})"
declare -n flag
for v in ${flag[values]}; do
[ "${v}" != "${flagval}" ] && continue
# shellcheck disable=SC2154
flag[val]="${v}"
validval=1
done
[ ${validval} -eq 1 ] || abort "Invalid umpf-flag value: ${flagval} (allowed values: ${flag[values]})!"
}
check_flags() {
local flagval flagname
for keyval in ${content}; do
flagname=${keyval%%=*}
flagval=${keyval#*=}
verify_flag
done
}
check_end() {
verify_topic
echo "end" > "${STATE}/check"
}
check_series() {
mv "${STATE}/series" "${STATE}/series.in"
parse_series check "${STATE}/series.in"
if [ ! -f "${STATE}/check" ]; then
abort "Empty series!"
fi
if ! grep -q end "${STATE}/check"; then
echo "# umpf-end" >> "${STATE}/done"
fi
# done contains the series with name/base fixups
cp "${STATE}/done" "${STATE}/series"
rm -f "${STATE}/check" "${STATE}/series.in"
# cleanup so parse_series can be run again
rm -f "${STATE}/name" "${STATE}/version" "${STATE}/tag" "${STATE}/base" "${STATE}/done"
}
### parse ###
parse_setup() {
line="$1"
cmd="$(sed -n -r "s/# umpf-([a-z-]*)(:.*)?\$/\1/p" <<< "$line")"
content="$(sed -n -r "s/# umpf-[a-z-]*: *(.*)\$/\1/p" <<< "$line")"
}
do_exec() {
local func="${namespace}_${1}"
shift
if declare -F | grep " ${func}$" &>/dev/null; then
"${func}" "${@}"
fi
}
parse() {
local line cmd content
parse_setup "$@"
grep -q "^${line}$" "${STATE}/done" 2> /dev/null && return
if [ -e "${STATE}/base" ]; then
has_base=true
else
has_base=false
fi
if grep -q "^${line}$" "${STATE}/failed" 2>/dev/null; then
has_failed=true
else
has_failed=false
fi
case "$cmd" in
"")
do_exec other
;;
base)
local reply baserev
${has_base} && bailout "duplicate base line"
content="${BASE:-${content}}"
line="# umpf-base: ${content}"
test -n "${content}" || bailout "${cmd} line without value"
echo "${content}" > "${STATE}/base-name"
if ! baserev="$(${GIT} rev-parse --verify -q "refs/tags/${content}^{}" 2>/dev/null)"; then
find_branch_rev "${content}" || abort "invalid base: '${content}'"
baserev="${reply}"
fi
echo "${baserev}" > "${STATE}/base"
do_exec base
if [ -n "${GIT_RELATIVE}" ]; then
echo "${line}" >> "${STATE}/done"
content="${GIT_RELATIVE}"
line="# umpf-relative: ${content}"
do_exec relative
fi
if [ -n "${FLAGS}" ]; then
echo "${line}" >> "${STATE}/done"
content="${FLAGS}"
line="# umpf-flags: ${content}"
do_exec flags
fi
;;
flags)
${has_base} || bailout "${cmd} before base"
test -n "${content}" || bailout "${cmd} line without value"
if [ -z "${FLAGS}" ]; then
do_exec flags
fi
;;
relative)
${has_base} || bailout "${cmd} before base"
test -n "${content}" || bailout "${cmd} line without value"
if [ -z "${GIT_RELATIVE}" ]; then
GIT_RELATIVE="${content}"
do_exec relative
fi
;;
name)
${has_base} || bailout "${cmd} before base"
content="${NAME:-${content}}"
line="# umpf-name: ${content}"
test -n "${content}" || bailout "${cmd} line without value"
test -e "${STATE}/name" && bailout "duplicate name line"
local version="$(date +%Y%m%d ${SOURCE_DATE_EPOCH:+--date=@${SOURCE_DATE_EPOCH}})${VERSION_SEPARATOR}${VERSION}"
local name="${content}"
local tagname="${name}/${version}"
echo "${name}" > "${STATE}/name"
echo "${version}" > "${STATE}/version"
echo "${tagname}" > "${STATE}/tag"
do_exec name
;;
version)
${has_base} || bailout "${cmd} before base"
test -n "${content}" || bailout "${cmd} line without value"
if ${STABLE}; then
local name="$(<"${STATE}/name")"
local version="$(sed "s;${name}/\([0-9]\{8\}\)${VERSION_SEPARATOR}.*;\1${VERSION_SEPARATOR}${VERSION};" <<< "${content}")"
local tagname="${name}/${version}"
echo "${version}" > "${STATE}/version"
echo "${tagname}" > "${STATE}/tag"
fi
do_exec version
;;
release)
${has_base} || bailout "${cmd} before base"
test -n "${content}" || bailout "${cmd} line without value"
do_exec release
;;
topic|squash-topic)
case $cmd in
topic) squash= ;;
squash-topic) squash=1;;
esac
${has_base} || bailout "${cmd} before base"
test -n "${content}" || bailout "${cmd} line without value"
do_exec topic
;;
hashinfo)
${has_base} || bailout "${cmd} before base"
test -n "${content}" || bailout "${cmd} line without value"
do_exec hashinfo
;;
topic-range)
${has_base} || bailout "${cmd} before base"
test -n "${content}" || bailout "${cmd} line without value"
do_exec topic_range
;;
end)
${has_base} || bailout "${cmd} before base"
do_exec end
rm "${STATE}/base"
;;
esac
rm -f "${STATE}/failed"
echo "${line}" >> "${STATE}/done"
}
parse_series() {
local namespace="${1}"
local series="${2}"
local squash
exec {series_in}< "${series}"
while read line <&${series_in}; do
parse "$line"
done
exec {series_in}<&-
unset series_in
}
### namespace: rebase ###
rebase_other() {
if $has_base; then
: # skip
else
printf "%s\n" "$line" >&${series_out}
fi
}
rebase_base() {
echo "$line" >&${series_out}
echo 1 > "${STATE}/num"
${GIT} checkout -q "${baserev}" || abort "checkout '${content}' failed"
if grep -sq "project('libcamera'," "meson.build"; then
project="libcamera"