interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / tools / forkpush.om

585 lines · 14771 bytes Raw Blame History
1om2
2note tools/forkpush.om: the Radicle fork-push script generator, in pure seeded Omega.
3note
4note My personal GitHub account holds branches of stack work that never reached the projects'
5note Radicle homes. stack/FORKS.tsv lists every one of them and where it goes. This program turns
6note that manifest into the POSIX sh script that pushes the Radicle-bound ones home, so the script
7note is generated from data, never written by hand. Build with the seed-born compiler:
8note omc < tools/forkpush.om > forkpush && chmod +x forkpush
9note { printf 'target x86linux\n'; cat tools/forkpush.om; } | omc-x86 > forkpush (native x86-64)
10note Run:
11note forkpush FORKS OUT
12note FORKS read: stack/FORKS.tsv. The header line is exactly
13note github TAB branch TAB head TAB commits TAB date TAB action TAB dest
14note then one branch per line, exactly 7 TAB-separated fields:
15note github owner/name, both parts of letters, digits, . _ -
16note branch letters, digits, . _ / -; not starting with - or /, not ending with /,
17note no .. and no //
18note head 40 lowercase hex digits
19note commits 1 to 20 decimal digits
20note date YYYY-MM-DD, digits
21note action radicle (dest is the project's RID, rad:z then letters and digits),
22note private (dest is -) or si (dest is the path in this repository, the
23note same characters and rules as a branch)
24note A (github, branch) pair appears once. At most 4096 rows and 4 MiB.
25note OUT written: the script. For every radicle row it fetches the branch from
26note https://github.com/<github> into a Radicle working copy of the project, checks the
27note fetched commit is the manifest head, and pushes it to Radicle as fork/<branch>.
28note private and si rows become comment lines only. Every value is single-quoted in the
29note script; validation guarantees none holds a quote.
30note Exit 0 when OUT is written; 2 for the wrong number of arguments; 1 for any refusal, with one
31note stderr line "forkpush: ..." and OUT not opened (the script is built in memory and written
32note once, at the end). The output depends only on the input bytes: no clock, no environment.
33
34note the manifest bytes and the script bytes
35global inp uchar 4194304
36global inn ulong 1
37global ob uchar 4194304
38global obn ulong 1
39note the path named by the current argument
40global pb uchar 4096
41global pbn ulong 1
42note rows, 16 cells each: offset and length of the 7 fields (cells 0..13), then the offset and
43note length of the whole line (14, 15)
44global row ulong 65536
45global nrow ulong 1
46note the action of each row: 1 radicle, 2 private, 3 si
47global act ulong 4096
48note rows per action: radicle, private, si
49global cnt ulong 3
50
51text tpre "forkpush: "
52text tnl "\n"
53text tusage "usage: forkpush FORKS OUT\n"
54text targ "cannot read a command-line argument"
55text topen "cannot open "
56text tread "cannot read "
57text tbig "manifest over the 4 MiB buffer: "
58text twrite "cannot write "
59text toverf "script over the 4 MiB buffer"
60text thdr "github\tbranch\thead\tcommits\tdate\taction\tdest"
61text tbhdr "header line is not github TAB branch TAB head TAB commits TAB date TAB action TAB dest: "
62text tcols "row needs exactly 7 TAB-separated fields: "
63text tmany "more than 4096 rows at: "
64text tbgh "bad github owner/name: "
65text tbbr "bad branch: "
66text tbhd "head is not 40 lowercase hex digits: "
67text tbcm "commits is not 1 to 20 decimal digits: "
68text tbdt "date is not YYYY-MM-DD: "
69text tbac "action is not radicle, private or si: "
70text tbrid "radicle dest is not a RID (rad:z then letters and digits): "
71text tbpr "private dest is not -: "
72text tbsi "bad si dest path: "
73text tdup "duplicate github and branch: "
74text aradi "radicle"
75text apriv "private"
76text asi "si"
77text tradz "rad:z"
78text tdash "-"
79text q1 "fp '"
80text q2 "' '"
81text q3 "'\n"
82text c1 "# private, kept off Radicle: "
83text c2 "# in this repository at "
84text c3 ": "
85text sp " "
86text m1 "# Manifest: "
87text m2 " to push to Radicle, "
88text m3 " kept private, "
89text m4 " in this repository.\n\n"
90
91note the script, paragraph by paragraph
92text h0 "#!/bin/sh\n# fork-push: push the fork-only branches of my personal GitHub forks to their Radicle projects.\n# Generated by tools/forkpush.om from stack/FORKS.tsv; do not edit by hand.\n#\n# For every stack/FORKS.tsv row whose action is radicle, it fetches the branch from\n# https://github.com/<owner>/<repo> into a Radicle working copy of the project, checks that the\n# fetched commit is the head the manifest names, and only then pushes it to Radicle as\n# fork/<branch>. Rows kept private and rows already in this repository are listed below as\n# comments and are never fetched or pushed.\n#\n# Needs git, and rad authenticated as the project's delegate with its node running\n# (rad auth, rad node start). It refuses to run as root.\n# FP_DIR=path the one directory it creates and works in (default ./fork-push); each project\n# is cloned there once, into a directory named after its RID without rad:\n# Exit status: 0 every branch OK, 2 a fetched commit is not the manifest head,\n# 3 a clone, fetch or push failed (3 wins over 2).\n"
93text h1 "set -eu\numask 022\n\nFP_DIR=\"${FP_DIR:-./fork-push}\"\nfp_say() { printf '%s\\n' \"$*\"; }\nfp_die() { printf 'fork-push: %s\\n' \"$*\" >&2; exit 1; }\n\nif [ \"$(id -u)\" = \"0\" ]; then fp_die \"refusing to run as root; run it as your own user\"; fi\ncommand -v git >/dev/null 2>&1 || fp_die \"git is required: https://git-scm.com\"\ncommand -v rad >/dev/null 2>&1 || fp_die \"rad is required: https://radicle.xyz\"\nmkdir -p -- \"$FP_DIR\"\ncd -- \"$FP_DIR\"\nfp_ok=0\nfp_bad=0\nfp_fail=0\n\n"
94text h2 "fp() {\n fp_d=\"${1#rad:}\"\n if [ ! -d \"$fp_d\" ]; then\n if ! rad clone \"$1\" \"$fp_d\" </dev/null; then\n fp_say \"FAILED $1 rad clone\"\n fp_fail=$((fp_fail + 1))\n rm -rf -- \"./$fp_d\"\n return 0\n fi\n fi\n if ! git -C \"$fp_d\" fetch --no-tags --quiet \"https://github.com/$2\" \"+refs/heads/$3:refs/heads/fork/$3\" </dev/null; then\n fp_say \"FAILED $1 fork/$3 fetch from https://github.com/$2\"\n fp_fail=$((fp_fail + 1))\n return 0\n fi\n fp_got=\"$(git -C \"$fp_d\" rev-parse \"refs/heads/fork/$3\" 2>/dev/null || true)\"\n if [ \"$fp_got\" != \"$4\" ]; then\n fp_say \"MISMATCH $1 fork/$3 manifest $4, fetched ${fp_got:-none}\"\n fp_bad=$((fp_bad + 1))\n return 0\n fi\n if git -C \"$fp_d\" push --quiet rad \"refs/heads/fork/$3:refs/heads/fork/$3\" </dev/null; then\n fp_say \"OK $1 fork/$3 $4\"\n fp_ok=$((fp_ok + 1))\n else\n fp_say \"FAILED $1 fork/$3 push\"\n fp_fail=$((fp_fail + 1))\n fi\n}\n\n"
95text h3 "\nfp_say \"\"\nfp_say \"Done in $(pwd): $fp_ok OK, $fp_bad MISMATCH, $fp_fail FAILED.\"\nif [ \"$fp_fail\" -gt 0 ]; then exit 3; fi\nif [ \"$fp_bad\" -gt 0 ]; then exit 2; fi\nexit 0\n"
96
97note die: report m and up to 200 bytes of inp from o on stderr, then exit 1
98fn die m view uchar * o ulong n ulong to void
99k = n
100if gt k 200
101k = 200
102end
103w = write 2 tpre 0 len tpre
104w = write 2 m 0 len m
105if gt k 0
106w = write 2 inp o k
107end
108w = write 2 tnl 0 len tnl
109exit 1
110end
111
112note dief: report m and the current path on stderr, then exit 1
113fn dief m view uchar * to void
114w = write 2 tpre 0 len tpre
115w = write 2 m 0 len m
116w = write 2 pb 0 get pbn 0
117w = write 2 tnl 0 len tnl
118exit 1
119end
120
121note setpath: copy command-line argument ai into pb
122fn setpath ai ulong to void
123n = argread ai pb 0 4096
124if le n 0
125die targ 0 0
126end
127put pbn 0 ulong n
128end
129
130note load: read the file named by argument ai into inp
131fn load ai ulong to void
132setpath ai
133fd = fopen pb 0 get pbn 0 0 0
134if lt fd 0
135dief topen
136end
137p = ulong 0
138note every pass but the last reads at least one byte of the 4 MiB buffer
139while lt p 4194304 bound 4194305
140k = read int fd inp p sub 4194304 p
141if lt k 0
142dief tread
143end
144if eq k 0
145break
146end
147p = add p ulong k
148end
149c = fclose int fd
150if ge p 4194304
151dief tbig
152end
153put inn 0 p
154end
155
156note wout: write ob to the file named by argument ai (created or truncated, mode 0644)
157fn wout ai ulong to void
158setpath ai
159fd = fopen pb 0 get pbn 0 577 420
160if lt fd 0
161dief topen
162end
163n = get obn 0
164p = ulong 0
165while lt p n bound 4194305
166k = write int fd ob p sub n p
167if le k 0
168dief twrite
169end
170p = add p ulong k
171end
172c = fclose int fd
173if ne c 0
174dief twrite
175end
176end
177
178note em: append a text to ob
179fn em t view uchar * to void
180p = get obn 0
181n = len t
182if gt n sub 4194304 p
183die toverf 0 0
184end
185for i ulong n bound 4194304
186put ob add p i get t i
187end
188put obn 0 add p n
189end
190
191note emi: append n bytes of inp from o to ob
192fn emi o ulong n ulong to void
193p = get obn 0
194if gt n sub 4194304 p
195die toverf 0 0
196end
197for i ulong n bound 4194304
198put ob add p i get inp add o i
199end
200put obn 0 add p n
201end
202
203note emb: append one byte to ob
204fn emb c uchar to void
205p = get obn 0
206if ge p 4194304
207die toverf 0 0
208end
209put ob p c
210put obn 0 add p 1
211end
212
213note emu: append v in decimal to ob
214fn emu v ulong to void
215d = new uchar 20 0
216x = v
217put d 0 uchar add rem x 10 48
218k = ulong 1
219x = div x 10
220while gt x 0 bound 20
221put d k uchar add rem x 10 48
222k = add k 1
223x = div x 10
224end
225while gt k 0 bound 20
226k = sub k 1
227emb get d k
228end
229end
230
231note eqx: do the n bytes of inp at o equal the text t
232fn eqx o ulong n ulong t view uchar * to bool
233if ne n len t
234ret false
235end
236for i ulong n bound 4194304
237if ne get inp add o i get t i
238ret false
239end
240end
241ret true
242end
243
244note eqs: are two slices of inp equal
245fn eqs a ulong an ulong b ulong bn ulong to bool
246if ne an bn
247ret false
248end
249for i ulong an bound 4194304
250if ne get inp add a i get inp add b i
251ret false
252end
253end
254ret true
255end
256
257note eol: the end of the line that starts at p (its newline, or e)
258fn eol p ulong e ulong to ulong
259q = p
260while lt q e bound 4194305
261if eq get inp q 10
262ret q
263end
264q = add q 1
265end
266ret e
267end
268
269fn isaln c uchar to bool
270ret or or and ge c 48 le c 57 and ge c 65 le c 90 and ge c 97 le c 122
271end
272
273note isnm: a letter, a digit, or one of . _ -
274fn isnm c uchar to bool
275if isaln c
276ret true
277end
278ret or or eq c 46 eq c 95 eq c 45
279end
280
281fn isdig c uchar to bool
282ret and ge c 48 le c 57
283end
284
285fn ishex c uchar to bool
286ret or and ge c 48 le c 57 and ge c 97 le c 102
287end
288
289note vgh: is inp[o, o+n) owner/name, both parts non-empty letters, digits, . _ -
290fn vgh o ulong n ulong to bool
291sl = ulong 0
292ns = ulong 0
293for i ulong n bound 4194304
294c = get inp add o i
295if eq c 47
296ns = add ns 1
297sl = i
298else
299if not isnm c
300ret false
301end
302end
303end
304if ne ns 1
305ret false
306end
307ret and gt sl 0 lt add sl 1 n
308end
309
310note vpath: is inp[o, o+n) a branch or path: letters, digits, . _ / -, not starting with - or
311note /, not ending with /, no .. and no //
312fn vpath o ulong n ulong to bool
313if eq n 0
314ret false
315end
316c0 = get inp o
317if or eq c0 45 eq c0 47
318ret false
319end
320if eq get inp add o sub n 1 47
321ret false
322end
323pr = uchar 0
324for i ulong n bound 4194304
325c = get inp add o i
326if not or isnm c eq c 47
327ret false
328end
329if and eq c 46 eq pr 46
330ret false
331end
332if and eq c 47 eq pr 47
333ret false
334end
335pr = c
336end
337ret true
338end
339
340note vhead: is inp[o, o+n) 40 lowercase hex digits
341fn vhead o ulong n ulong to bool
342if ne n 40
343ret false
344end
345for i ulong 40
346if not ishex get inp add o i
347ret false
348end
349end
350ret true
351end
352
353note vnum: is inp[o, o+n) 1 to 20 decimal digits
354fn vnum o ulong n ulong to bool
355if or eq n 0 gt n 20
356ret false
357end
358for i ulong n bound 20
359if not isdig get inp add o i
360ret false
361end
362end
363ret true
364end
365
366note vdate: is inp[o, o+n) YYYY-MM-DD in digits
367fn vdate o ulong n ulong to bool
368if ne n 10
369ret false
370end
371for i ulong 10
372c = get inp add o i
373if or eq i 4 eq i 7
374if ne c 45
375ret false
376end
377else
378if not isdig c
379ret false
380end
381end
382end
383ret true
384end
385
386note vrid: is inp[o, o+n) rad:z then at least one letter or digit
387fn vrid o ulong n ulong to bool
388if le n 5
389ret false
390end
391if not eqx o 5 tradz
392ret false
393end
394for i ulong sub n 5 bound 4194304
395if not isaln get inp add add o 5 i
396ret false
397end
398end
399ret true
400end
401
402note fo / fl: offset and length of field k of row r
403fn fo r ulong k ulong to ulong
404ret get row add mul r 16 mul k 2
405end
406
407fn fl r ulong k ulong to ulong
408ret get row add add mul r 16 mul k 2 1
409end
410
411note lineo / linel: offset and length of row r's whole line
412fn lineo r ulong to ulong
413ret get row add mul r 16 14
414end
415
416fn linel r ulong to ulong
417ret get row add mul r 16 15
418end
419
420note bad: refuse row r with message m when ok is false
421fn bad ok bool m view uchar * r ulong to void
422if not ok
423die m lineo r linel r
424end
425end
426
427note split: store the 7 TAB-separated fields of the line [p, q) as row r
428fn split p ulong q ulong r ulong to void
429b = mul r 16
430put row add b 14 p
431put row add b 15 sub q p
432k = ulong 0
433s = p
434t = p
435while lt t q bound 4194305
436if eq get inp t 9
437if ge k 6
438die tcols p sub q p
439end
440put row add b mul k 2 s
441put row add add b mul k 2 1 sub t s
442k = add k 1
443s = add t 1
444end
445t = add t 1
446end
447if ne k 6
448die tcols p sub q p
449end
450put row add b 12 s
451put row add b 13 sub q s
452end
453
454note actof: the action of row r: 1 radicle, 2 private, 3 si; 0 for anything else
455fn actof r ulong to ulong
456o = fo r 5
457n = fl r 5
458if eqx o n aradi
459ret 1
460end
461if eqx o n apriv
462ret 2
463end
464if eqx o n asi
465ret 3
466end
467ret 0
468end
469
470note vdest: check row r's dest against its action a
471fn vdest r ulong a ulong to void
472o = fo r 6
473n = fl r 6
474if eq a 1
475bad vrid o n tbrid r
476end
477if eq a 2
478bad eqx o n tdash tbpr r
479end
480if eq a 3
481bad vpath o n tbsi r
482end
483end
484
485note vrow: check every field of row r, record its action, and refuse a repeated pair
486fn vrow r ulong to void
487bad vgh fo r 0 fl r 0 tbgh r
488bad vpath fo r 1 fl r 1 tbbr r
489bad vhead fo r 2 fl r 2 tbhd r
490bad vnum fo r 3 fl r 3 tbcm r
491bad vdate fo r 4 fl r 4 tbdt r
492a = actof r
493bad gt a 0 tbac r
494vdest r a
495put act r a
496j = sub a 1
497put cnt j add get cnt j 1
498for i ulong r bound 4096
499if eqs fo i 0 fl i 0 fo r 0 fl r 0
500bad not eqs fo i 1 fl i 1 fo r 1 fl r 1 tdup r
501end
502end
503end
504
505note parse: check the header, then split and check every row
506fn parse to void
507e = get inn 0
508q = eol 0 e
509if not eqx 0 q thdr
510die tbhdr 0 q
511end
512p = add q 1
513r = ulong 0
514note every pass consumes at least one byte of the manifest
515while lt p e bound 4194305
516q = eol p e
517if ge r 4096
518die tmany p sub q p
519end
520split p q r
521vrow r
522r = add r 1
523p = add q 1
524end
525put nrow 0 r
526end
527
528note emf: append field k of row r
529fn emf r ulong k ulong to void
530emi fo r k fl r k
531end
532
533note emrow: append row r's line of the script
534fn emrow r ulong to void
535a = get act r
536if eq a 1
537em q1
538emf r 6
539em q2
540emf r 0
541em q2
542emf r 1
543em q2
544emf r 2
545em q3
546ret
547end
548if eq a 2
549em c1
550else
551em c2
552emf r 6
553em c3
554end
555emf r 0
556em sp
557emf r 1
558em tnl
559end
560
561fn main to int
562if ne argcnt 3
563w = write 2 tusage 0 len tusage
564ret 2
565end
566load 1
567parse
568em h0
569em m1
570emu get cnt 0
571em m2
572emu get cnt 1
573em m3
574emu get cnt 2
575em m4
576em h1
577em h2
578n = get nrow 0
579for r ulong n bound 4096
580emrow r
581end
582em h3
583wout 2
584ret 0
585end