interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / tools / bring / src / 50patch.om

760 lines · 14584 bytes Raw Blame History
1note tools/bring/src/50patch.om: interplane patch [--base REF] [--title T] [--message-file F] [--yes]
2note OWNER/REPO. Run inside a git work tree. It resolves the base (default: the merge base of HEAD
3note and refs/remotes/origin/main), builds the mbox with /usr/bin/git format-patch (an argument list,
4note no shell, an empty environment), shows what would be sent and sends only after --yes or the
5note exact "yes" typed on /dev/tty: the key (made once, as bring makes it), the statement signed with
6note namespace interplane-patch, the sign-in of bring, one POST to <base>/OWNER/REPO/patches/new.
7note Exit 0 sent (the patch page is printed), 1 nothing sent, 2 the server refused it (its reason
8note is printed). The key never leaves this computer; the forge never signs for anyone.
9text tqprog "/usr/bin/git"
10text tqrp "rev-parse"
11text tqiw "--is-inside-work-tree"
12text tqtrue "true"
13text tqver "--verify"
14text tqqt "--quiet"
15text tqeoo "--end-of-options"
16text tqcmt "^{commit}"
17text tqhd "HEAD"
18text tqmb "merge-base"
19text tqorg "refs/remotes/origin/main"
20text tqfp "format-patch"
21text tqso "--stdout"
22text tqo1 "--no-color"
23text tqo2 "--no-ext-diff"
24text tqo3 "--no-textconv"
25text tqo4 "--no-attach"
26text tqo5 "--no-thread"
27text tqo6 "--no-cover-letter"
28text tqo7 "--src-prefix=a/"
29text tqo8 "--dst-prefix=b/"
30text tqo9 "--no-signature"
31text tqo10 "--no-relative"
32text tqo11 "--no-notes"
33text tqrl "rev-list"
34text tqcnt "--count"
35text tqdf "diff"
36text tqnmo "--name-only"
37text tqz "-z"
38text tqlog "log"
39text tqm1 "-1"
40text tqfs "--format=%s"
41text tqdots ".."
42text tqmbox "mbox"
43text tqgout "gout"
44text tqfb "--base"
45text tqft "--title"
46text tqfm "--message-file"
47text tqfy "--yes"
48text tqns "interplane-patch"
49text tqst1 "interplane-patch 1\n"
50text tqslash "/"
51text tqpnew "/patches/new"
52text tqfrepo "&repo="
53text tqfbase "&base="
54text tqfttl "&title="
55text tqfdsc "&description="
56text tqfmbx "&mbox="
57text tqfsig "&sig="
58text tqpath "path "
59text tqrefu "refused: "
60text tqusage "usage: interplane patch [--base REF] [--title T] [--message-file F] [--yes] OWNER/REPO"
61text tqnowt "this folder is not inside a git work tree"
62text tqnohd "this work tree has no commit at HEAD"
63text tqnobs "cannot find the remote's main (refs/remotes/origin/main); name the base with --base REF"
64text tqbadb "the --base value does not name a commit here"
65text tqnone "there is no commit between the base and HEAD: nothing to send"
66text tqmany "more than 100 commits between the base and HEAD: a patch holds at most 100"
67text tqbig "the patch is larger than 2097152 bytes: nothing sent"
68text tqgit "git could not build the patch"
69text tqttlb "the title must be 1 to 256 bytes on one line; give one with --title"
70text tqdscb "the message file must hold at most 60000 bytes"
71text tqdscf "cannot read the message file"
72text tqs1 "Repository: "
73text tqs2 "Base: "
74text tqs3 "Commits: "
75text tqs4 "Files changed: "
76text tqs5 "Bytes: "
77text tqs6 "Title: "
78text tqs7 "Send to: "
79text tqask "Type yes to send this patch, anything else to stop:\n"
80text tqpage "Patch page: "
81text tqhave "Interplane already has this patch.\n"
82text tqref "Interplane refused the patch: "
83text tqno "Nothing sent.\n"
84text tqsetp "/settings"
85global gqrepo uchar 256
86global gqrepol ulong 1
87global gqbref uchar 4096
88global gqbrefl ulong 1
89global gqmsgf uchar 4096
90global gqmsgfl ulong 1
91global gqbase uchar 40
92global gqttl uchar 256
93global gqttll ulong 1
94global gqdsc uchar 60000
95global gqdscl ulong 1
96global gqyes ulong 1
97global gqncm ulong 1
98global gqnfl ulong 1
99global gqbytes ulong 1
100note ---- arguments
101fn ptrepoc c uchar to bool
102if isaln c
103ret true
104end
105ret or or eq c 45 eq c 46 eq c 95
106end
107note ptrepo: OWNER/REPO: two names of letters, digits, '.', '-' and '_' around one slash
108fn ptrepo a view uchar * to void
109sl = ulong 0
110ns = ulong 0
111for i ulong len a bound 256
112c = get a i
113if eq c 47
114ns = add ns 1
115sl = i
116else
117if not ptrepoc c
118die tqusage
119end
120end
121end
122if or ne ns 1 eq sl 0
123die tqusage
124end
125if or eq add sl 1 len a eq get a 0 46
126die tqusage
127end
128if eq get a add sl 1 46
129die tqusage
130end
131for i ulong len a bound 256
132put gqrepo i get a i
133end
134put gqrepol 0 len a
135end
136fn ptflag a view uchar * t view uchar * to bool
137ret beq a t
138end
139note ptval i ac w: the value of the flag at argument i (the next argument) into its global: w 0
140note --base (no leading '-'), 1 --title (1..256 bytes), 2 --message-file
141fn ptval i ulong ac ulong w ulong to void
142a = new uchar 4096 0
143if ge add i 1 ac
144die tqusage
145end
146n = getarg add i 1 a
147if eq n 0
148die tqusage
149end
150if eq w 0
151if eq get a 0 45
152die tqbadb
153end
154for k ulong n bound 4096
155put gqbref k get a k
156end
157put gqbrefl 0 n
158end
159if eq w 1
160if gt n 256
161die tqttlb
162end
163for k ulong n bound 256
164put gqttl k get a k
165end
166put gqttll 0 n
167end
168if eq w 2
169for k ulong n bound 4096
170put gqmsgf k get a k
171end
172put gqmsgfl 0 n
173end
174end
175note ptargs: ac counts the program and "patch". The tool takes at most 8 arguments after "patch"
176note (--base REF, --title T, --message-file F, --yes, OWNER/REPO): more is the usage line (exit 1),
177note answered before the loop, so the loop runs at most 8 times whatever the argument count.
178fn ptargs ac ulong to void
179if gt ac 10
180die tqusage
181end
182a = new uchar 4096 0
183got = false
184i = ulong 2
185while lt i ac bound 8
186n = getarg i a
187w = ulong 9
188if ptflag part a 0 n tqfb
189w = ulong 0
190end
191if ptflag part a 0 n tqft
192w = ulong 1
193end
194if ptflag part a 0 n tqfm
195w = ulong 2
196end
197if ptflag part a 0 n tqfy
198w = ulong 3
199end
200if lt w 3
201ptval i ac w
202i = add i 1
203end
204if eq w 3
205put gqyes 0 1
206end
207if eq w 9
208if or got eq n 0
209die tqusage
210end
211if or eq get a 0 45 gt n 200
212die tqusage
213end
214ptrepo part a 0 n
215got = true
216end
217i = add i 1
218end
219if not got
220die tqusage
221end
222end
223note ---- git: an argument list, standard output into the temp file gout (or mbox), no shell
224note gitline: the first line of gout into o; its length
225fn gitline o edit uchar 4096 to ulong
226fd = topen tqgout 0
227n = read fd o 0 4096
228pl_close fd
229if lt n 0
230die terrio
231end
232k = ulong 0
233while lt k ulong n bound 4096
234if eq get o k 10
235break
236end
237k = add k 1
238end
239ret k
240end
241fn ishex40 o view uchar * to bool
242if ne len o 40
243ret false
244end
245for i ulong 40
246c = get o i
247h = or isdig c and ge c 97 le c 102
248if not h
249ret false
250end
251end
252ret true
253end
254note gitcommit ref eoo: rev-parse --verify --quiet [--end-of-options] <ref>^{commit}: the 40 hex
255note digits into o; false when ref names no commit
256fn gitcommit ref view uchar * eoo bool o edit uchar 40 to bool
257a = new uchar 4096 0
258q = new uchar 4200 0
259l = new uchar 4096 0
260avinit a
261avadd a tqprog
262avadd a tqrp
263avadd a tqver
264avadd a tqqt
265if eoo
266avadd a tqeoo
267end
268n = cpy q 0 ref
269n = cpy q n tqcmt
270avadd a part q 0 n
271if ne runprog a tqgout 0
272ret false
273end
274k = gitline l
275if not ishex40 part l 0 k
276ret false
277end
278for i ulong 40
279put o i get l i
280end
281ret true
282end
283note ptrange: "<base>..HEAD" into q; its length
284fn ptrange q edit uchar 64 to ulong
285for i ulong 40
286put q i get gqbase i
287end
288n = cpy q 40 tqdots
289ret cpy q n tqhd
290end
291fn ptbase to void
292b = new uchar 40 0
293a = new uchar 4096 0
294l = new uchar 4096 0
295r = new uchar 4096 0
296if gt get gqbrefl 0 0
297n = get gqbrefl 0
298for i ulong n bound 4096
299put r i get gqbref i
300end
301if not gitcommit part r 0 n true b
302die tqbadb
303end
304else
305avinit a
306avadd a tqprog
307avadd a tqmb
308avadd a tqhd
309avadd a tqorg
310if ne runprog a tqgout 0
311die tqnobs
312end
313k = gitline l
314if not ishex40 part l 0 k
315die tqnobs
316end
317for i ulong 40
318put b i get l i
319end
320end
321for i ulong 40
322put gqbase i get b i
323end
324end
325note ptcount: the commits of base..HEAD (1..100)
326fn ptcount to void
327a = new uchar 4096 0
328l = new uchar 4096 0
329q = new uchar 64 0
330v = new ulong 1 0
331avinit a
332avadd a tqprog
333avadd a tqrl
334avadd a tqcnt
335n = ptrange q
336avadd a part q 0 n
337if ne runprog a tqgout 0
338die tqgit
339end
340k = gitline l
341if not pdec part l 0 k v
342die tqgit
343end
344if eq get v 0 0
345die tqnone
346end
347if gt get v 0 100
348die tqmany
349end
350put gqncm 0 get v 0
351end
352note ptmbox: git format-patch --stdout base..HEAD into the temp file mbox; its size in gqbytes
353fn ptmbox to void
354a = new uchar 4096 0
355q = new uchar 64 0
356st = new uchar 256 0
357avinit a
358avadd a tqprog
359avadd a tqfp
360avadd a tqso
361avadd a tqo1
362avadd a tqo2
363avadd a tqo3
364avadd a tqo4
365avadd a tqo5
366avadd a tqo6
367avadd a tqo7
368avadd a tqo8
369avadd a tqo9
370avadd a tqo10
371avadd a tqo11
372n = ptrange q
373avadd a part q 0 n
374if ne runprog a tqmbox 0
375die tqgit
376end
377fd = topen tqmbox 0
378if lt pl_fstat fd st 0
379die terrio
380end
381pl_close fd
382sz = pl_size st
383if gt sz 2097152
384die tqbig
385end
386put gqbytes 0 sz
387end
388note ptfiles: the files base and HEAD differ in (the names of git diff --name-only -z, counted)
389fn ptfiles to void
390a = new uchar 4096 0
391b = new uchar 65536 0
392avinit a
393avadd a tqprog
394avadd a tqdf
395avadd a tqnmo
396avadd a tqz
397for i ulong 40
398put b i get gqbase i
399end
400avadd a part b 0 40
401avadd a tqhd
402if ne runprog a tqgout 0
403die tqgit
404end
405fd = topen tqgout 0
406c = ulong 0
407while true bound 9223372036854775807
408r = read fd b 0 65536
409if eq r -4
410continue
411end
412if lt r 0
413die terrio
414end
415if eq r 0
416break
417end
418for i ulong ulong r bound 65536
419if eq get b i 0
420c = add c 1
421end
422end
423end
424pl_close fd
425put gqnfl 0 c
426end
427note pttitle: the title: --title, else the subject of the HEAD commit; 1..256 bytes, no control byte
428fn pttitle to void
429a = new uchar 4096 0
430l = new uchar 4096 0
431if eq get gqttll 0 0
432avinit a
433avadd a tqprog
434avadd a tqlog
435avadd a tqm1
436avadd a tqfs
437avadd a tqhd
438if ne runprog a tqgout 0
439die tqgit
440end
441k = gitline l
442if or eq k 0 gt k 256
443die tqttlb
444end
445for i ulong k bound 256
446put gqttl i get l i
447end
448put gqttll 0 k
449end
450for i ulong get gqttll 0 bound 256
451c = get gqttl i
452if or lt c 32 eq c 127
453die tqttlb
454end
455end
456end
457note ptdesc: the description from --message-file (none: empty): carriage returns dropped, blanks
458note and line feeds at the end trimmed, at most 60000 bytes
459fn ptdesc to void
460p = new uchar 4096 0
461b = new uchar 65536 0
462put gqdscl 0 0
463n = get gqmsgfl 0
464if eq n 0
465ret
466end
467for i ulong n bound 4096
468put p i get gqmsgf i
469end
470fd = pl_open part p 0 n 4 0
471if lt fd 0
472die tqdscf
473end
474k = ulong 0
475while true bound 9223372036854775807
476r = read int fd b 0 65536
477if eq r -4
478continue
479end
480if lt r 0
481die tqdscf
482end
483if eq r 0
484break
485end
486for i ulong ulong r bound 65536
487c = get b i
488if ne c 13
489if ge k 60000
490die tqdscb
491end
492put gqdsc k c
493k = add k 1
494end
495end
496end
497pl_close int fd
498while gt k 0 bound 60000
499c = get gqdsc sub k 1
500if and and ne c 10 ne c 32 ne c 9
501break
502end
503k = sub k 1
504end
505put gqdscl 0 k
506end
507note ---- what would be sent
508fn ptshow to void
509t = new uchar 4096 0
510n = get gqrepol 0
511for i ulong n bound 256
512put t i get gqrepo i
513end
514say tqs1
515say part t 0 n
516say tnl
517for i ulong 40
518put t i get gqbase i
519end
520say tqs2
521say part t 0 40
522say tnl
523say tqs3
524sayd get gqncm 0
525say tnl
526say tqs4
527sayd get gqnfl 0
528say tnl
529say tqs5
530sayd get gqbytes 0
531say tnl
532n = get gqttll 0
533for i ulong n bound 256
534put t i get gqttl i
535end
536say tqs6
537sayp part t 0 n
538say tnl
539n = gcopy 0 t
540say tqs7
541say part t 0 n
542say tnl
543end
544note ---- the statement: "interplane-patch 1\n<owner>/<repo>\n<base>\n<sha256 of the mbox>\n<sha256 of
545note title, line feed, description>\n" into the temp file stmt
546fn ptstmt to void
547d = new uchar 32 0
548x = new uchar 64 0
549t = new uchar 60300 0
550fd = topen tqmbox 0
551shafd fd d
552pl_close fd
553k = hexw d x 0
554fd = tmpfd tstmt
555mustwrite fd tqst1
556n = get gqrepol 0
557for i ulong n bound 256
558put t i get gqrepo i
559end
560mustwrite fd part t 0 n
561mustwrite fd tnl
562for i ulong 40
563put t i get gqbase i
564end
565mustwrite fd part t 0 40
566mustwrite fd tnl
567mustwrite fd x
568mustwrite fd tnl
569n = get gqttll 0
570for i ulong n bound 256
571put t i get gqttl i
572end
573put t n 10
574m = get gqdscl 0
575for i ulong m bound 60000
576put t add add n 1 i get gqdsc i
577end
578y = sha2_digest 256 part t 0 add add n 1 m d
579k = hexw d x 0
580mustwrite fd x
581mustwrite fd tnl
582pl_close fd
583end
584note ---- the form: csrf, repo, base, title, description, mbox (base64url, no padding), sig
585fn ptfdesc fd int to void
586b = new uchar 4096 0
587n = get gqdscl 0
588p = ulong 0
589while lt p n bound 60000
590k = sub n p
591if gt k 4096
592k = ulong 4096
593end
594for i ulong k bound 4096
595put b i get gqdsc add p i
596end
597formenc fd part b 0 k
598p = add p k
599end
600end
601fn ptfmbox fd int to void
602b = new uchar 45000 0
603e = new uchar 60000 0
604src = topen tqmbox 0
605while true bound 9223372036854775807
606k = ulong 0
607while lt k 45000 bound 9223372036854775807
608r = read src b k sub 45000 k
609if eq r -4
610continue
611end
612if lt r 0
613die terrio
614end
615if eq r 0
616break
617end
618k = add k ulong r
619end
620if eq k 0
621break
622end
623el = b64u part b 0 k e
624mustwrite fd part e 0 el
625if lt k 45000
626break
627end
628end
629pl_close src
630end
631fn ptform to void
632t = new uchar 4096 0
633fd = tmpfd tform
634csrfform fd
635mustwrite fd tqfrepo
636n = get gqrepol 0
637for i ulong n bound 256
638put t i get gqrepo i
639end
640formenc fd part t 0 n
641mustwrite fd tqfbase
642for i ulong 40
643put t i get gqbase i
644end
645mustwrite fd part t 0 40
646mustwrite fd tqfttl
647n = get gqttll 0
648for i ulong n bound 256
649put t i get gqttl i
650end
651formenc fd part t 0 n
652mustwrite fd tqfdsc
653ptfdesc fd
654mustwrite fd tqfmbx
655ptfmbox fd
656mustwrite fd tqfsig
657formsig fd tstsig
658pl_close fd
659end
660note ptsend: POST the form. A 429 (the write gate: 30 writes a minute) sends the same form again at
661note most 3 times, after 2, 4 and 8 seconds (4 POSTs and 14 seconds of waiting at most; the tool
662note reads no Retry-After); a 429 after that is a refusal like any other code. 201 and 200 print the
663note patch page and return; anything else prints the server's reason in one line and leaves with
664note status 2.
665fn ptsend to void
666p = new uchar 4096 0
667v = new uchar 4096 0
668e = new uchar 24 0
669n = cpy p 0 tqslash
670m = get gqrepol 0
671for i ulong m bound 256
672put p add n i get gqrepo i
673end
674n = cpy p add n m tqpnew
675c = http part p 0 n true tbody
676w = ulong 2
677while and eq c 429 le w 8 bound 3
678for i ulong w bound 8
679pl_sleep
680end
681c = http part p 0 n true tbody
682w = mul w 2
683end
684if or eq c 201 eq c 200
685k = mustval tqpath 10 v
686if eq c 200
687say tqhave
688end
689say tqpage
690m = gcopy 0 p
691say part p 0 m
692say part v 0 k
693say tnl
694ret
695end
696r = fval tbody tqrefu 10
697warn tpfx
698if ge r 0
699for i ulong ulong r bound 4096
700put v i get gval i
701end
702warn tqref
703warn part v 0 ulong r
704else
705k = decw c e 0
706warn thttpbad
707warn part e 0 k
708end
709warn tnl
710cleanup
711exit 2
712end
713note patchflow: the whole command; ac is the argument count. Returns the exit status 0 or 1.
714fn patchflow ac ulong to int
715pk = new uchar 32 0
716a = new uchar 4096 0
717l = new uchar 4096 0
718h = new uchar 40 0
719ptargs ac
720tmpsetup
721setbase
722avinit a
723avadd a tqprog
724avadd a tqrp
725avadd a tqiw
726if ne runprog a tqgout 0
727die tqnowt
728end
729k = gitline l
730if not beq part l 0 k tqtrue
731die tqnowt
732end
733if not gitcommit tqhd false h
734die tqnohd
735end
736ptbase
737ptcount
738ptmbox
739ptfiles
740pttitle
741ptdesc
742ptshow
743if eq get gqyes 0 0
744if not consent tqask
745say tqno
746cleanup
747ret 1
748end
749end
750makekey
751readpk pk
752ptstmt
753sign tstmt tqns
754signin0 pk
755csrfof tqsetp
756ptform
757ptsend
758cleanup
759ret 0
760end