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