gunk-dev/the-valley

status.go

sourceddc9a2fd850emain's published tip history
8.3 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 matches a pattern against the tail of a ref name, so every
68 // ref is compared whole here.
69 var attestations []string
70 for _, line := range strings.Split(snapshot, "\n") {
71 fields := strings.Fields(line)
72 if len(fields) < 2 {
73 continue
74 }
75 oid, ref := fields[0], fields[1]
76 if ref == "refs/heads/"+target {
77 s.tip = oid
78 }
79 if ref == "refs/heads/"+branch {
80 s.head = oid
81 }
82 switch {
83 case ref == s.requestRef:
84 s.request = oid
85 case ref == s.outcomeRef:
86 s.outcome = oid
87 case strings.HasPrefix(ref, attestationRefs):
88 attestations = append(attestations, ref+" "+oid)
89 }
90 }
91 if s.tip == "" {
92 return nil, failure("origin has no main branch; integration status is unknown")
93 }
94 if s.head == "" && s.request == "" && s.outcome == "" {
95 return nil, failure(branch + ": no remote branch or recorded integration request on origin")
96 }
97 fetch := []string{"fetch", "--quiet", "--no-tags", "--no-write-fetch-head", "origin", s.tip}
98 for _, oid := range []string{s.head, s.request, s.outcome} {
99 if oid != "" {
100 fetch = append(fetch, oid)
101 }
102 }
103 if _, err := g.output(false, fetch...); err != nil {
104 return nil, failure("could not fetch the status objects; origin may have changed. Try valley status " +
105 shellQuote(branch) + " again")
106 }
107
108 if s.outcome != "" {
109 record, err := g.output(true, "cat-file", "blob", s.outcome)
110 if err != nil {
111 record = ""
112 }
113 s.memo = parseMemo(record)
114 }
115 // A deleted topic can still have a pending request or a recorded
116 // result. The memo's head stands in only when it is an object id: any
117 // other text would be resolved as a revision of this checkout.
118 if s.head == "" {
119 s.head = s.request
120 }
121 if s.head == "" && isObjectID(s.memo.head) {
122 s.head = s.memo.head
123 }
124 s.state = decide(g, s, evidenceFingerprint(attestations))
125 return s, nil
126}
127
128// memo is the integrator's outcome record for a request, one line: the
129// head it judged, the target tip it judged that head against, the
130// fingerprint of the evidence it saw, and its decision. A failed pass
131// adds when it failed (integrator/refs.go writes it). The first three are
132// what the integrator compares to decide whether to judge a request again.
133type memo struct {
134 head, tip, evidence string
135 decision string // land, stale, reject, failed, or unknown
136 failedAt string // set for failed alone
137}
138
139// parseMemo reads an outcome record. Anything it does not recognise is
140// unknown, never a decision: a damaged record, or one in a format newer
141// than this reader, must not read as a landing or as any other answer the
142// integrator did not give. Fields are separated by spaces or tabs, and
143// trailing newlines are dropped, as bin/valley read it with read(1); a
144// second line, a NUL, or a sixth field makes the record unknown.
145func parseMemo(record string) memo {
146 line := strings.TrimRight(record, "\n")
147 fields := strings.FieldsFunc(line, func(r rune) bool { return r == ' ' || r == '\t' })
148 field := func(i int) string {
149 if i < len(fields) {
150 return fields[i]
151 }
152 return ""
153 }
154 m := memo{head: field(0), tip: field(1), evidence: field(2), decision: field(3), failedAt: field(4)}
155 if strings.ContainsAny(line, "\n\x00") || len(fields) > 5 ||
156 !isObjectID(m.head) || !isObjectID(m.tip) || !isHex(m.evidence, 16) {
157 m.decision = "unknown"
158 }
159 switch m.decision {
160 case "land", "stale", "reject":
161 if m.failedAt != "" {
162 m.decision = "unknown"
163 }
164 case "failed":
165 if m.failedAt == "" {
166 m.decision = "unknown"
167 }
168 default:
169 m.decision = "unknown"
170 }
171 if m.decision != "failed" {
172 m.failedAt = ""
173 }
174 return m
175}
176
177// isObjectID is a full SHA-1 or SHA-256 object id, lowercase.
178func isObjectID(s string) bool {
179 return isHex(s, 40) || isHex(s, 64)
180}
181
182func isHex(s string, n int) bool {
183 if len(s) != n {
184 return false
185 }
186 for _, c := range []byte(s) {
187 if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
188 return false
189 }
190 }
191 return true
192}
193
194// evidenceFingerprint is the evidence state the integrator records in a
195// memo: the first 16 hex digits of the SHA-256 over every attestation ref
196// on origin, one "<ref> <oid>" line each, in byte order. A memo with a
197// different fingerprint was written about other evidence.
198func evidenceFingerprint(attestations []string) string {
199 sorted := append([]string(nil), attestations...)
200 sort.Strings(sorted)
201 h := sha256.New()
202 for _, line := range sorted {
203 h.Write([]byte(line + "\n"))
204 }
205 return hex.EncodeToString(h.Sum(nil))[:16]
206}
207
208// decide names the change's state, one per answer status gives.
209//
210// main is authoritative for a landing, so ancestry is read first: main can
211// advance before the integrator records its outcome, and a failure
212// storing evidence afterwards must not hide an integration already there.
213// A delta applied onto a moved tip lands as a different commit, so a land
214// memo for this head is the next answer. Only after both does a request or
215// a recorded decision speak for the change.
216func decide(g git, s *status, evidence string) string {
217 if s.head != "" && g.succeeds(true, "cat-file", "-e", s.head+"^{commit}") &&
218 g.succeeds(false, "merge-base", "--is-ancestor", s.head, s.tip) {
219 return "integrated"
220 }
221 decision := s.memo.decision // "" when there is no outcome record
222 switch {
223 case decision == "land" && s.memo.head == s.head && (s.request == "" || s.request == s.head):
224 return "integrated-recorded"
225 case s.request != "" && s.request != s.head:
226 return "outdated-request"
227 case decision == "unknown":
228 return "outcome-unreadable"
229 case s.request == "":
230 return "not-requested"
231 case decision == "" || s.memo.head != s.head:
232 return "awaiting-decision"
233 case s.memo.tip != s.tip || s.memo.evidence != evidence:
234 return "awaiting-new-decision"
235 }
236 switch decision {
237 case "stale":
238 return "stale"
239 case "reject":
240 return "rejected"
241 case "failed":
242 return "failed"
243 }
244 // A land memo for this head with the request at this head was
245 // integrated-recorded above, and no other decision parses.
246 panic("valley-status: no state for decision " + decision)
247}