gunk-dev/the-valley

refs.go

source57064e55f18fmain's published tip history
25.0 KiB
1package main
2
3// The refs transport: a reconciliation loop over the integration requests
4// durable in the served repository.
5//
6// It polls, deliberately. The bus is unauthenticated (bd-d853d9c), which
7// gates automated consumers, and the requests are durable in git with the
8// bus as a projection over them (architecture.md). So the controller
9// level-triggers over the request refs themselves and converges: each pass
10// recomputes the verdict for every request against the current tip, acts
11// only where the answer has changed since the last pass, and needs no
12// subscription at all. It publishes outcomes to the bus exactly as the
13// post-receive hook publishes ref updates. Consuming events arrives in a
14// later phase.
15//
16// # The request ref
17//
18// A request is a ref:
19//
20// refs/the-valley/integration-requests/<target>/<change> -> commit
21//
22// The target segment names the protected branch, so the change's target
23// stream is refs/heads/<target>; the last segment names the change. The
24// commit is the change's head, its tree is what the attestations are over,
25// and the delta is the merge base with the target's tip to that head.
26// Nothing else is encoded: a change is a diff targeting a stream
27// (ida-93e4f91), and every part of that is already in git. A request
28// whose last segment is c-<change ID> is the request of the change with a
29// record under that ID, and the records decide whether it may land at all
30// (records.go).
31
32import (
33 "the-valley/change"
34 "the-valley/integrator/verdict"
35
36 "fmt"
37 "sort"
38 "strings"
39 "time"
40)
41
42const (
43 requestPrefix = "refs/the-valley/integration-requests"
44 outcomePrefix = "refs/the-valley/integration-outcomes"
45)
46
47// request is one integration request as the refs transport found it.
48type request struct {
49 ref string
50 change string
51 target string
52 head string
53}
54
55// requests lists the pending requests, in ref order so a pass is
56// deterministic, along with the refs under the namespace that are not
57// requests at all.
58//
59// Any holder of the approve grant can write this namespace, and anyone at all
60// where a repository is served without the pre-receive hook, so a name that
61// does not parse is an ordinary thing to find there and not a reason to stop: one
62// unreadable ref must not hold up every readable one. It is reported and
63// skipped.
64func (in *integrator) requests() ([]request, []string, error) {
65 out, err := git(in.repo, "for-each-ref", "--format=%(refname) %(objectname)", requestPrefix)
66 if err != nil {
67 return nil, nil, err
68 }
69 var found []request
70 var malformed []string
71 for _, line := range strings.Split(strings.TrimSpace(out), "\n") {
72 if line == "" {
73 continue
74 }
75 ref, head, ok := strings.Cut(line, " ")
76 if !ok {
77 continue
78 }
79 rest := strings.TrimPrefix(ref, requestPrefix+"/")
80 target, change, ok := strings.Cut(rest, "/")
81 if !ok || strings.Contains(change, "/") || target == "" || change == "" {
82 malformed = append(malformed, ref)
83 continue
84 }
85 found = append(found, request{ref: ref, change: change, target: "refs/heads/" + target, head: head})
86 }
87 sort.Slice(found, func(i, j int) bool { return found[i].ref < found[j].ref })
88 sort.Strings(malformed)
89 return found, malformed, nil
90}
91
92// reconcile is one pass: judge every request against the current tip of its
93// target, and act where the answer has changed.
94//
95// The change records are read once, before any request is judged. A pass
96// that cannot read them judges nothing: which heads are versions of
97// changes that are not ready is what keeps those heads from landing. The
98// requests a withdrawn change voids are deleted before the rest are
99// judged.
100func (in *integrator) reconcile() error {
101 requests, malformed, err := in.requests()
102 if err != nil {
103 return err
104 }
105 for _, ref := range malformed {
106 fmt.Fprintf(in.out, "%s: not a request ref; one is %s/<target>/<change>. Skipped.\n", ref, requestPrefix)
107 }
108 if len(requests) == 0 {
109 return nil
110 }
111 idx, err := in.readRecords()
112 if err != nil {
113 return err
114 }
115 for _, u := range idx.unreadable {
116 fmt.Fprintf(in.out, "WARNING: %s is a change record that does not read: %s. Requests are judged as though it did not exist, so a head it names is not held back by it. The hook refuses such a record, so the repository's storage is corrupt; restore the record or delete it.\n", u.ref, u.why)
117 }
118 requests = in.voidWithdrawn(requests, idx)
119 for _, r := range requests {
120 if err := in.handle(r, idx); err != nil {
121 // One bad request must not stop the pass: the integrator is a
122 // controller, and a request it cannot judge is one request's
123 // problem.
124 fmt.Fprintf(in.out, "%s: %v\n", r.change, err)
125 }
126 }
127 return nil
128}
129
130// handle judges one request and acts on the verdict. A decision it
131// reports is timed, and its timing line follows the report and the
132// recorded outcome; a refusal said before, and so not said again, is not
133// timed again either.
134func (in *integrator) handle(r request, idx *recordIndex) error {
135 watch := newStopwatch()
136 tip, ok, err := resolveRef(in.repo, r.target)
137 if err != nil {
138 return err
139 } else if !ok {
140 return fmt.Errorf("no target %s to integrate onto", r.target)
141 }
142 // Level-triggering: the verdict is a function of the request's head,
143 // the target's tip, and the evidence stored for the change, so an
144 // unchanged answer is one where none of the three has moved.
145 // Re-announcing an unchanged answer would be a retry storm; missing a
146 // moved one would strand a contributor who pushed the attestation a
147 // previous pass said was absent.
148 evidence, err := in.evidenceState()
149 if err != nil {
150 return err
151 }
152 memo := fmt.Sprintf("%s %s %s", r.head, tip, evidence)
153 last, err := in.readMemo(r)
154 if err != nil {
155 return err
156 }
157 if in.answered(last, memo) {
158 return nil
159 }
160 // From here on the request has been taken up, so whatever happens is
161 // this pass's answer to it — including a failure. Recording that is
162 // what keeps the next tick from asking the identical question.
163 done := watch.stage(stageRefusal)
164 outcome, err := in.refuse(r, idx, tip, last, memo)
165 done()
166 if err == nil && outcome == "" {
167 outcome, err = in.act(r, tip, watch)
168 }
169 if err != nil {
170 printTiming := in.timing(r, tip, failedMemo, watch)
171 memoErr := in.writeMemo(r, memo+" "+failedMemo+" "+in.now().Format(time.RFC3339))
172 printTiming()
173 if memoErr != nil {
174 return fmt.Errorf("%v (and the failure could not be recorded: %v)", err, memoErr)
175 }
176 return err
177 }
178 if last == memo+" "+string(outcome) {
179 return nil
180 }
181 printTiming := in.timing(r, tip, string(outcome), watch)
182 err = in.writeMemo(r, memo+" "+string(outcome))
183 printTiming()
184 return err
185}
186
187// refuse applies the rules that refuse a request before it is judged
188// (records.go), and reports a refusal. It reports one only when it is new:
189// a refusal is decided again on every pass, and saying the same one every
190// tick would bury it.
191func (in *integrator) refuse(r request, idx *recordIndex, tip, last, memo string) (verdict.Outcome, error) {
192 outcome, why, err := in.recordRefusal(r, idx, tip)
193 if err == nil && outcome == "" {
194 outcome, why, err = in.mergeRefusal(r, tip)
195 }
196 if err != nil {
197 return "", err
198 }
199 if outcome != "" && last != memo+" "+string(outcome) {
200 in.reportRefusal(r, tip, outcome, why)
201 }
202 // A record that does not read may name a commit this request brings,
203 // and whether it holds that commit back cannot be told. That is said
204 // with the request, whatever is decided about it.
205 if outcome == "" || last != memo+" "+string(outcome) {
206 for _, u := range in.uncheckable(r, idx, tip) {
207 fmt.Fprintf(in.out, "WARNING: %s may name a commit %s brings, and does not read, so whether it holds that commit back cannot be checked\n", u.ref, r.change)
208 }
209 }
210 return outcome, nil
211}
212
213// reportRefusal prints a refusal the way report prints a verdict.
214func (in *integrator) reportRefusal(r request, tip string, outcome verdict.Outcome, why string) {
215 fmt.Fprintf(in.out, "%s -> %s @ %s: %s\n", r.change, r.target, verdict.Short(tip), outcome)
216 fmt.Fprintf(in.out, " %s\n", why)
217 fmt.Fprintf(in.out, " no event: a refusal is not staleness, and the request stays where it is\n")
218}
219
220// failedMemo marks a memo written for a pass that could not act on its own
221// verdict, and retryAfter is how long that answer stands.
222//
223// A failure here is a function of the same three things the verdict is, so
224// the identical attempt fails identically until one of them moves — and
225// moving any of them already re-judges the request, whatever the memo says.
226// The window exists for the other kind of failure: a bus, a disk, a lock,
227// something outside the three that will be different later. Minutes rather
228// than ticks, because a controller that reports the same refusal every
229// fifteen seconds has buried the report it was making.
230const (
231 failedMemo = "failed"
232 retryAfter = 10 * time.Minute
233)
234
235// answered says whether this pass already has this request's answer, given
236// the last memo written for it. An outcome stands until the request, the
237// tip, or the evidence moves; a recorded failure stands the same way, and
238// additionally expires, so a transient one is retried without being
239// retried at tick rate. A refusal never stands: what refused it can change
240// while the three stay where they are, as when a draft is readied again,
241// so it is decided again on every pass. Nor does a voiding: a request
242// filed again at the same head is a fresh approval.
243func (in *integrator) answered(last, memo string) bool {
244 rest, ok := strings.CutPrefix(last, memo+" ")
245 if !ok {
246 return false
247 }
248 switch verdict.Outcome(rest) {
249 case refusedNotReady, refusedMerge, voidedWithdrawn:
250 return false
251 }
252 when, failed := strings.CutPrefix(rest, failedMemo+" ")
253 if !failed {
254 return true
255 }
256 at, err := time.Parse(time.RFC3339, when)
257 if err != nil {
258 return false
259 }
260 return in.now().Sub(at) < retryAfter
261}
262
263// act judges one request and does what the verdict says, returning the
264// outcome so the memo can record it.
265func (in *integrator) act(r request, tip string, watch *stopwatch) (verdict.Outcome, error) {
266 ch, snap, landed, err := in.materialize(r, tip, watch)
267 if err != nil {
268 return "", err
269 }
270 v := verdict.Decide(ch, snap)
271 in.report(r, tip, v)
272 if landed.composition != "" {
273 fmt.Fprintf(in.out, " policy %s\n", landed.composition)
274 }
275
276 switch v.Outcome {
277 case verdict.Land:
278 outcome, err := in.land(ch, v, r, tip, landed, watch)
279 if outcome != "" || err != nil {
280 if outcome == "" {
281 outcome = v.Outcome
282 }
283 return outcome, err
284 }
285 case verdict.Stale:
286 done := watch.stage(stagePublish)
287 in.publishStale(ch, v, tip)
288 done()
289 case verdict.Reject:
290 // There is no rejection event. The failure model is unified on
291 // staleness (architecture.md), and a change whose evidence is
292 // forged is not stale — so rather than stretch either event to
293 // cover it, the vocabulary stays as it is and the refusal is the
294 // integrator's own output. The request stays put; nothing about
295 // it will change on its own.
296 fmt.Fprintf(in.out, " no event: a forged attestation is not staleness, and the vocabulary has no other word\n")
297 }
298 return v.Outcome, nil
299}
300
301// landing is the commit a verdict would move the target to, and what the
302// verdict was computed under.
303type landing struct {
304 commit string // the commit that would be the target's new tip
305 digest string // the valley-tree-v1 digest of the resulting tree
306 policy string // a digest of the composed policy the verdict used
307
308 // composition is what the pass has to say about how the policy layers
309 // resolved, or empty when both were where they are configured to be.
310 composition string
311
312 // notes are the contributor's attestations as stored, by check name.
313 // The verdict does not read them; what lands countersigns them.
314 notes map[string][]byte
315}
316
317// materialize gathers everything the verdict is a function of: the applied
318// delta, the required checks, the recomputed closures, and what intervening
319// landings touched.
320func (in *integrator) materialize(r request, tip string, watch *stopwatch) (verdict.Change, verdict.Snapshot, landing, error) {
321 var land landing
322 done := watch.stage(stageCheckout)
323 wt, err := addWorktree(in.repo, tip)
324 done()
325 if err != nil {
326 return verdict.Change{}, verdict.Snapshot{}, land, err
327 }
328 defer wt.remove()
329
330 base, err := gitLine(in.repo, "merge-base", tip, r.head)
331 if err != nil {
332 return verdict.Change{}, verdict.Snapshot{}, land, fmt.Errorf("%s and %s share no history", verdict.Short(r.head), r.target)
333 }
334 done = watch.stage(stageEvidence)
335 attested, err := in.treeDigest(wt, r.head)
336 if err != nil {
337 done()
338 return verdict.Change{}, verdict.Snapshot{}, land, err
339 }
340 ch := verdict.Change{ID: r.change, Target: r.target, Base: base, Head: r.head, Attested: attested}
341 ch.Evidence, land.notes, err = in.collectEvidence(wt, r.head, attested)
342 done()
343 if err != nil {
344 return ch, verdict.Snapshot{}, land, err
345 }
346
347 // The policy is read from the integrator's own side and never from the
348 // tree being gated: the project layer from this checkout of the target
349 // tip, the floor from the instance repository's integrated tip. Both
350 // are resolved once and held for the whole pass.
351 done = watch.stage(stagePolicy)
352 pol, closePolicy, err := in.policy.open(wt)
353 if err != nil {
354 done()
355 return ch, verdict.Snapshot{}, land, err
356 }
357 defer closePolicy()
358 land.composition = pol.note
359 cat, policyDigest, err := pol.catalogue()
360 if err != nil {
361 done()
362 return ch, verdict.Snapshot{}, land, err
363 }
364 land.policy = policyDigest
365 derived, err := pol.derive(wt, base, r.head)
366 if err != nil {
367 done()
368 return ch, verdict.Snapshot{}, land, err
369 }
370 required, err := pol.required(derived, cat)
371 done()
372 if err != nil {
373 return ch, verdict.Snapshot{}, land, err
374 }
375
376 snap := verdict.Snapshot{Required: required, Touched: map[string]bool{}, Now: in.now()}
377
378 // Apply the delta. A base that is already the tip is a fast-forward:
379 // the head lands as it stands and its tree is the tree that was
380 // attested.
381 if base == tip {
382 land.commit = r.head
383 land.digest = attested
384 snap.Apply = verdict.Applied{Clean: true, Tree: attested}
385 } else {
386 done = watch.stage(stageApply)
387 tree, conflict, err := mergeDelta(in.repo, base, tip, r.head)
388 if err != nil {
389 done()
390 return ch, snap, land, err
391 }
392 if conflict != "" {
393 done()
394 snap.Apply = verdict.Applied{Clean: false, Conflict: conflict}
395 return ch, snap, land, nil
396 }
397 if land.commit, err = in.commitTree(tree, tip, r); err != nil {
398 done()
399 return ch, snap, land, err
400 }
401 land.digest, err = in.treeDigest(wt, tree)
402 done()
403 if err != nil {
404 return ch, snap, land, err
405 }
406 snap.Apply = verdict.Applied{Clean: true, Tree: land.digest}
407
408 // What intervening landings touched, in the policy's own terms:
409 // the classes the deriver matches over base..tip. Asking the
410 // deriver rather than matching globs here keeps one implementation
411 // of what a class covers.
412 done = watch.stage(stagePolicy)
413 intervening, err := pol.derive(wt, base, tip)
414 done()
415 if err != nil {
416 return ch, snap, land, err
417 }
418 snap.Touched = intervening.classes
419 }
420
421 // The closures only have to be recomputed when the tree moved: an
422 // unmoved tree presents the identical computation by construction.
423 if land.digest != attested {
424 done = watch.stage(stageClosures)
425 snap.Closures, snap.ClosureErrors = in.closures(wt, land.commit, required)
426 done()
427 }
428 return ch, snap, land, nil
429}
430
431// committerEmail is the address on the commits the integrator makes. git
432// wants an address and there is none to give: the integrator is a service,
433// not a correspondent. .invalid never resolves, which says so in the address
434// itself. Which integrator committed is carried by the name beside it.
435const committerEmail = "integrator@the-valley.invalid"
436
437// commitTree turns the merged tree into the commit that would become the
438// target's new tip. The tip is its only parent, so the ref write is a
439// fast-forward; the change's authorship is carried over from its head.
440//
441// Both identities are stated here rather than left to git. The author is the
442// asker's, because re-parenting a delta is not authoring it. The committer is
443// the integrator, under the name it signs its transfer statement with: the
444// commit and the statement about it then name the same actor. Leaving either
445// to git would ask git to guess an identity from the environment, and under
446// the unit there is nothing to guess from — which is a landing that fails for
447// a reason that has nothing to do with the change.
448func (in *integrator) commitTree(tree, tip string, r request) (string, error) {
449 message, err := git(in.repo, "log", "-1", "--format=%B", r.head)
450 if err != nil {
451 return "", err
452 }
453 author, err := git(in.repo, "log", "-1", "--format=%an%n%ae%n%aI", r.head)
454 if err != nil {
455 return "", err
456 }
457 fields := strings.Split(strings.TrimRight(author, "\n"), "\n")
458 if len(fields) != 3 {
459 return "", fmt.Errorf("cannot read the authorship of %s", verdict.Short(r.head))
460 }
461 cmd := []string{"commit-tree", tree, "-p", tip, "-m", strings.TrimRight(message, "\n")}
462 // The committer date is the author's, so that the commit id is a function
463 // of what the pass decided from — the tree, the tip, the message and the
464 // authorship — and of nothing else. A pass that dies between here and the
465 // ref write recomputes the identical commit next time round instead of
466 // leaving another unreferenced one behind. Nothing reads this date: when
467 // the landing happened is recorded, signed, in the transfer statement.
468 out, err := in.gitEnv(map[string]string{
469 "GIT_AUTHOR_NAME": fields[0],
470 "GIT_AUTHOR_EMAIL": fields[1],
471 "GIT_AUTHOR_DATE": fields[2],
472 "GIT_COMMITTER_NAME": in.signerName,
473 "GIT_COMMITTER_EMAIL": committerEmail,
474 "GIT_COMMITTER_DATE": fields[2],
475 }, cmd...)
476 return strings.TrimSpace(out), err
477}
478
479// land moves the protected ref, stores the evidence, and says so on the
480// bus. The ref write is a compare-and-swap against the tip the verdict was
481// computed over, so a request that lost a race to another lands nothing and
482// is judged again next pass against the tip that won.
483//
484// The order is the commit point's. Everything that can fail while the
485// integrator is still deciding happens first, and the ref write is the
486// commit. Composing the transfer statement — vetting it, writing it down,
487// signing it — is part of deciding: a claim the integrator cannot write
488// down is a claim it has not made, and a stream moved on a decision that
489// cannot be stated is a landing nothing records. A failure before the
490// commit point leaves the stream where it was and the request where it was,
491// which is a transaction abandoned before it committed and costs one pass
492// to redo.
493//
494// Past the commit point the landing is a fact in the stream, and what is
495// left is recording it: handing the ref move to the host to publish,
496// storing the evidence, consuming the request, and saying so on the bus.
497// None of those may be skipped because an earlier one failed, and
498// consuming the request in particular may not be reachable only through
499// something that can fail — a request left pending for a change
500// already in the stream is re-judged against an empty delta on every pass,
501// forever. So each step runs, each failure is reported, and the landing is
502// reported as having happened, because it did.
503func (in *integrator) land(ch verdict.Change, v verdict.Verdict, r request, tip string, l landing, watch *stopwatch) (verdict.Outcome, error) {
504 done := watch.stage(stageSign)
505 transfer, err := in.composeTransfer(ch, v, l.digest, l.commit, tip, l.policy)
506 if err != nil {
507 done()
508 return "", err
509 }
510 evidence, err := in.countersign(ch, v, l, transfer)
511 done()
512 if err != nil {
513 return "", err
514 }
515
516 // Judging and signing can take minutes, and a change can be unreadied
517 // or withdrawn meanwhile, or a record can come to name a commit this
518 // request brings. So every record is read again here, as the last
519 // thing before the commit point, and a refusal lands nothing. A record
520 // written between this read and the ref write is still missed: refs
521 // offer no way to make the two one step. What lands then is still the
522 // head the operator approved.
523 done = watch.stage(stageRefusal)
524 outcome, why, err := in.finalRefusal(r, tip)
525 done()
526 if err != nil || outcome != "" {
527 if outcome != "" {
528 in.reportRefusal(r, tip, outcome, why)
529 }
530 return outcome, err
531 }
532
533 // The commit point.
534 done = watch.stage(stageLand)
535 _, err = git(in.repo, "update-ref", ch.Target, l.commit, tip)
536 done()
537 if err != nil {
538 return "", fmt.Errorf("%s moved under the verdict; nothing landed: %w", ch.Target, err)
539 }
540 fmt.Fprintf(in.out, " landed %s %s -> %s\n", ch.Target, verdict.Short(tip), verdict.Short(l.commit))
541 done = watch.stage(stageRecord)
542 in.queueRefUpdate(ch.Target, tip, l.commit)
543
544 var unrecorded []string
545 refs, err := in.store(evidence)
546 if err != nil {
547 unrecorded = append(unrecorded, fmt.Sprintf("the evidence was not stored (%v)", err))
548 }
549 for _, ref := range refs {
550 fmt.Fprintf(in.out, " evidence %s\n", ref)
551 }
552 if why := in.consume(r); why != "" {
553 unrecorded = append(unrecorded, why)
554 }
555 done()
556 // The bus comes last. Everything above is the landing's own record; an
557 // event is a best-effort notice of it, and a bus that fails or hangs
558 // must not leave any of that record undone.
559 done = watch.stage(stagePublish)
560 in.publishRefUpdated(ch.Target, tip, l.commit)
561 in.publishLanded(ch, v, tip, l.commit)
562 done()
563 if len(unrecorded) > 0 {
564 return "", fmt.Errorf("%s landed as %s and the landing is not fully recorded: %s",
565 ch.ID, verdict.Short(l.commit), strings.Join(unrecorded, "; "))
566 }
567 return "", nil
568}
569
570// consume deletes the request that landed, and returns why it was not
571// consumed, or "" when it was or when it moved.
572//
573// The request is consumed only if it still names the head that landed.
574// A second approval can replace it while this pass works — the asker's
575// write is leased against what it read, and nothing stops it reading the
576// request this pass is about to land — and a delete that did not compare
577// would take the new request with the old one. A request that moved is a
578// new request, and it stands for the next pass. A request that is gone
579// did not move, whatever a branch named like it holds.
580func (in *integrator) consume(r request) string {
581 _, err := git(in.repo, "update-ref", "-d", r.ref, r.head)
582 if err == nil {
583 return ""
584 }
585 if now, found, _ := resolveRef(in.repo, r.ref); found && now != r.head {
586 fmt.Fprintf(in.out, " request %s moved to %s while %s landed; it stands for the next pass\n",
587 r.ref, verdict.Short(now), verdict.Short(r.head))
588 return ""
589 }
590 return fmt.Sprintf("the request ref %s was not consumed (%v)", r.ref, err)
591}
592
593// report prints the verdict, per check, in the order it was judged.
594func (in *integrator) report(r request, tip string, v verdict.Verdict) {
595 fmt.Fprintf(in.out, "%s -> %s @ %s: %s\n", r.change, r.target, verdict.Short(tip), v.Outcome)
596 fmt.Fprintf(in.out, " %s\n", v.Reason)
597 for _, cv := range v.Checks {
598 if cv.Transferred {
599 fmt.Fprintf(in.out, " check %-24s transferred (%s)\n", cv.Name, cv.Rule)
600 continue
601 }
602 fmt.Fprintf(in.out, " check %-24s %s\n", cv.Name, cv.Reason)
603 }
604}
605
606// The memo is the level-trigger's memory: what this integrator last decided
607// about a request, keyed by the request. It is durable in git like
608// everything else the transport reads, so a restarted controller does not
609// re-announce a judgement it has already made.
610func (in *integrator) memoRef(r request) string {
611 return outcomePrefix + "/" + strings.TrimPrefix(r.ref, requestPrefix+"/")
612}
613
614// readMemo reads the memo of a request, or "" when it has none. The memo
615// ref is read by its exact name, so a tag or branch named like a missing
616// memo is no memo.
617func (in *integrator) readMemo(r request) (string, error) {
618 oid, ok, err := resolveRef(in.repo, in.memoRef(r))
619 if err != nil || !ok {
620 return "", err
621 }
622 out, ok := gitTry(in.repo, "cat-file", "blob", oid)
623 if !ok {
624 return "", nil
625 }
626 return strings.TrimSpace(out), nil
627}
628
629func (in *integrator) writeMemo(r request, value string) error {
630 blob, err := in.writeBlob([]byte(value + "\n"))
631 if err != nil {
632 return err
633 }
634 _, err = git(in.repo, "update-ref", in.memoRef(r), blob)
635 return err
636}
637
638// evidenceState digests the attestation namespace: every ref under it and
639// the object it points at, as change.EvidenceFingerprint reads them.
640// Attestation refs are create-only, so this moves exactly when evidence
641// arrives.
642//
643// It is deliberately the whole namespace rather than the refs for one
644// change's subject digest, which would take a checkout to compute and would
645// have to be recomputed before the memo could rule the work out. So an
646// attestation for one change re-judges every pending request. That is the
647// conservative direction — a pass that runs and changes nothing costs a
648// pass; a verdict never revisited strands a change — and it is bounded by
649// the number of requests pending.
650func (in *integrator) evidenceState() (string, error) {
651 out, err := git(in.repo, "for-each-ref", "--format=%(refname) %(objectname)", attestationPrefix)
652 if err != nil {
653 return "", err
654 }
655 var lines []string
656 if out = strings.TrimSuffix(out, "\n"); out != "" {
657 lines = strings.Split(out, "\n")
658 }
659 return change.EvidenceFingerprint(lines), nil
660}
661
662func (in *integrator) now() time.Time {
663 if !in.clock.IsZero() {
664 return in.clock
665 }
666 return time.Now().UTC()
667}