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