gunk-dev/the-valley

status.go

source7ff002a27ee8main's published tip history
8.4 KiB
1package main
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "sort"
7 "strings"
8)
9
10const (
11 target = "main"
12 requestPrefix = "refs/the-valley/integration-requests/" + target + "/"
13 outcomePrefix = "refs/the-valley/integration-outcomes/" + target + "/"
14 attestationRefs = "refs/the-valley/attestations/"
15)
16
17// status is what one read of origin found for a change, and the state
18// decide made of it.
19type status struct {
20 branch string // the change as given, without origin/
21 change string // the request's name for it (changeName)
22 requestRef string
23 outcomeRef string
24
25 tip string // origin's main
26 head string // the change's head as this read knows it; "" for none
27 request string // the commit the integration request names; "" for none
28 outcome string // the outcome record's blob; "" for none
29 memo memo // the outcome record, read; meaningful when outcome is set
30
31 state string
32}
33
34// changeName is the change segment of a request ref. The integrator reads
35// a request as <prefix>/<target>/<change> and refuses a change segment
36// holding a slash (integrator/refs.go), so a branch name with one is
37// written with dashes instead. bin/valley's change_name is the same
38// mapping, and the request approve files is named by it.
39func changeName(branch string) string {
40 return strings.ReplaceAll(branch, "/", "-")
41}
42
43// read takes one snapshot of origin and decides the change's state from
44// it. A status read is a snapshot of origin, not a replay of this
45// checkout's cached refs: one ls-remote lists every ref the decision
46// reads, and the objects they name are fetched by id, so the decision is
47// about one moment of origin and never about refs/remotes. Nothing local
48// is pruned, so evidence this checkout produced survives the read.
49func read(g git, branch string) (*status, error) {
50 if !g.succeeds(true, "rev-parse", "--git-dir") {
51 return nil, failure("not a git repository")
52 }
53 if !g.succeeds(true, "check-ref-format", "refs/heads/"+branch) {
54 return nil, failure("not a branch name: " + branch)
55 }
56 s := &status{branch: branch, change: changeName(branch)}
57 s.requestRef = requestPrefix + s.change
58 s.outcomeRef = outcomePrefix + s.change
59
60 snapshot, err := g.output(false, "ls-remote", "--refs", "origin",
61 "refs/heads/"+branch, "refs/heads/"+target, s.requestRef, s.outcomeRef,
62 attestationRefs+"*")
63 if err != nil {
64 return nil, failure("could not read origin; integration status is unknown. Try valley status " +
65 shellQuote(branch) + " again")
66 }
67 // ls-remote prints "<oid>\t<ref>", and a ref name may hold any space
68 // but ASCII's, so the line is split at the tab alone. ls-remote matches
69 // a pattern against the tail of a ref name, so every ref is compared
70 // whole here.
71 var attestations []string
72 for _, line := range strings.Split(snapshot, "\n") {
73 oid, ref, ok := strings.Cut(line, "\t")
74 if !ok || ref == "" {
75 continue
76 }
77 if ref == "refs/heads/"+target {
78 s.tip = oid
79 }
80 if ref == "refs/heads/"+branch {
81 s.head = oid
82 }
83 switch {
84 case ref == s.requestRef:
85 s.request = oid
86 case ref == s.outcomeRef:
87 s.outcome = oid
88 case strings.HasPrefix(ref, attestationRefs):
89 attestations = append(attestations, ref+" "+oid)
90 }
91 }
92 if s.tip == "" {
93 return nil, failure("origin has no main branch; integration status is unknown")
94 }
95 if s.head == "" && s.request == "" && s.outcome == "" {
96 return nil, failure(branch + ": no remote branch or recorded integration request on origin")
97 }
98 fetch := []string{"fetch", "--quiet", "--no-tags", "--no-write-fetch-head", "origin", s.tip}
99 for _, oid := range []string{s.head, s.request, s.outcome} {
100 if oid != "" {
101 fetch = append(fetch, oid)
102 }
103 }
104 if _, err := g.output(false, fetch...); err != nil {
105 return nil, failure("could not fetch the status objects; origin may have changed. Try valley status " +
106 shellQuote(branch) + " again")
107 }
108
109 if s.outcome != "" {
110 record, err := g.output(true, "cat-file", "blob", s.outcome)
111 if err != nil {
112 record = ""
113 }
114 s.memo = parseMemo(record)
115 }
116 // A deleted topic can still have a pending request or a recorded
117 // result. The memo's head stands in only when it is an object id: any
118 // other text would be resolved as a revision of this checkout.
119 if s.head == "" {
120 s.head = s.request
121 }
122 if s.head == "" && isObjectID(s.memo.head) {
123 s.head = s.memo.head
124 }
125 s.state = decide(g, s, evidenceFingerprint(attestations))
126 return s, nil
127}
128
129// memo is the integrator's outcome record for a request, one line: the
130// head it judged, the target tip it judged that head against, the
131// fingerprint of the evidence it saw, and its decision. A failed pass
132// adds when it failed (integrator/refs.go writes it). The first three are
133// what the integrator compares to decide whether to judge a request again.
134type memo struct {
135 head, tip, evidence string
136 decision string // land, stale, reject, failed, or unknown
137 failedAt string // set for failed alone
138}
139
140// parseMemo reads an outcome record. Anything it does not recognise is
141// unknown, never a decision: a damaged record, or one in a format newer
142// than this reader, must not read as a landing or as any other answer the
143// integrator did not give. Fields are separated by spaces or tabs, and
144// trailing newlines are dropped, as bin/valley read it with read(1); a
145// second line, a NUL, or a sixth field makes the record unknown.
146func parseMemo(record string) memo {
147 line := strings.TrimRight(record, "\n")
148 fields := strings.FieldsFunc(line, func(r rune) bool { return r == ' ' || r == '\t' })
149 field := func(i int) string {
150 if i < len(fields) {
151 return fields[i]
152 }
153 return ""
154 }
155 m := memo{head: field(0), tip: field(1), evidence: field(2), decision: field(3), failedAt: field(4)}
156 if strings.ContainsAny(line, "\n\x00") || len(fields) > 5 ||
157 !isObjectID(m.head) || !isObjectID(m.tip) || !isHex(m.evidence, 16) {
158 m.decision = "unknown"
159 }
160 switch m.decision {
161 case "land", "stale", "reject":
162 if m.failedAt != "" {
163 m.decision = "unknown"
164 }
165 case "failed":
166 if m.failedAt == "" {
167 m.decision = "unknown"
168 }
169 default:
170 m.decision = "unknown"
171 }
172 if m.decision != "failed" {
173 m.failedAt = ""
174 }
175 return m
176}
177
178// isObjectID is a full SHA-1 or SHA-256 object id, lowercase.
179func isObjectID(s string) bool {
180 return isHex(s, 40) || isHex(s, 64)
181}
182
183func isHex(s string, n int) bool {
184 if len(s) != n {
185 return false
186 }
187 for _, c := range []byte(s) {
188 if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
189 return false
190 }
191 }
192 return true
193}
194
195// evidenceFingerprint is the evidence state the integrator records in a
196// memo: the first 16 hex digits of the SHA-256 over every attestation ref
197// on origin, one "<ref> <oid>" line each, in byte order. A memo with a
198// different fingerprint was written about other evidence.
199func evidenceFingerprint(attestations []string) string {
200 sorted := append([]string(nil), attestations...)
201 sort.Strings(sorted)
202 h := sha256.New()
203 for _, line := range sorted {
204 h.Write([]byte(line + "\n"))
205 }
206 return hex.EncodeToString(h.Sum(nil))[:16]
207}
208
209// decide names the change's state, one per answer status gives.
210//
211// main is authoritative for a landing, so ancestry is read first: main can
212// advance before the integrator records its outcome, and a failure
213// storing evidence afterwards must not hide an integration already there.
214// A delta applied onto a moved tip lands as a different commit, so a land
215// memo for this head is the next answer. Only after both does a request or
216// a recorded decision speak for the change.
217func decide(g git, s *status, evidence string) string {
218 if s.head != "" && g.succeeds(true, "cat-file", "-e", s.head+"^{commit}") &&
219 g.succeeds(false, "merge-base", "--is-ancestor", s.head, s.tip) {
220 return "integrated"
221 }
222 decision := s.memo.decision // "" when there is no outcome record
223 switch {
224 case decision == "land" && s.memo.head == s.head && (s.request == "" || s.request == s.head):
225 return "integrated-recorded"
226 case s.request != "" && s.request != s.head:
227 return "outdated-request"
228 case decision == "unknown":
229 return "outcome-unreadable"
230 case s.request == "":
231 return "not-requested"
232 case decision == "" || s.memo.head != s.head:
233 return "awaiting-decision"
234 case s.memo.tip != s.tip || s.memo.evidence != evidence:
235 return "awaiting-new-decision"
236 }
237 switch decision {
238 case "stale":
239 return "stale"
240 case "reject":
241 return "rejected"
242 case "failed":
243 return "failed"
244 }
245 // A land memo for this head with the request at this head was
246 // integrated-recorded above, and no other decision parses.
247 panic("valley-status: no state for decision " + decision)
248}