| 1 | package main |
| 2 | |
| 3 | // valley review with no change: a walk through what needs the operator |
| 4 | // (review.go). |
| 5 | // |
| 6 | // The walk reads the queue once, as valley pending reads it |
| 7 | // (operatorQueue, queue.go), across every project the serving host |
| 8 | // publishes, or the one --repo names, and steps through its rows that wait |
| 9 | // on an approval in landing order. A project that did not read, and a |
| 10 | // discovery that failed and left the walk with the current project alone, |
| 11 | // are said before the first step and again in the summary, and nothing |
| 12 | // says that nothing needs the operator while either stands. |
| 13 | // |
| 14 | // Each step prints a header naming the row's project, handle, version, |
| 15 | // stack and checks, then calls the single-change review (review.go) with |
| 16 | // a reviewStep for the row: the git handle on the row's repository root, |
| 17 | // the current checkout's top level or the cache clone a project other |
| 18 | // than the current one is read through (cacheRepo, list.go), with |
| 19 | // cacheEnvironment's environment; the change as the queue held it, its ID, |
| 20 | // version and head; the walk's -v and --build-here; the walk's terminal |
| 21 | // and streams; and the walk's signer source. Every process the step starts |
| 22 | // runs in that root and environment, unless it targets a tree of its own, |
| 23 | // and nothing is set in the walk's own environment or directory. A cache |
| 24 | // clone's configuration is its own, so the step's environment gives it |
| 25 | // the current checkout's valley.principal, user.name and user.email |
| 26 | // through GIT_CONFIG_COUNT, for each key the clone's own configuration |
| 27 | // does not set, and nothing is written into the clone. |
| 28 | // |
| 29 | // The walk's signer source trusts the integrator of the valley the |
| 30 | // current checkout's main names. A step in a cache clone whose project's |
| 31 | // main names another valley, or none, is not reviewed with it |
| 32 | // (walkValley): the step ends in error, saying which valley each names, |
| 33 | // unless a flag or the environment gives what the walk trusts. |
| 34 | // |
| 35 | // The review returns how the step ended (stepOutcome), and the walk goes |
| 36 | // on to the next row whatever it was, an error included. Each review a |
| 37 | // step runs is recorded as an untimed review command of its own, from the |
| 38 | // step's repository and environment, so a step in a cache clone is its |
| 39 | // project's. |
| 40 | // |
| 41 | // Once the queue is first read, until the process exits, the walk |
| 42 | // catches SIGINT and SIGTERM, and each step's review takes them from the |
| 43 | // walk's catch while it listens (review.go): SIGINT and SIGTERM end the |
| 44 | // step interrupted, SIGTERM sent to the walk alone included, and SIGHUP |
| 45 | // ends it with 129. The review ends the walk through the step's end path: |
| 46 | // once the step has cleaned up, the summary so far is printed (not for |
| 47 | // SIGHUP), the step and the walk are recorded, and the process exits with |
| 48 | // the review's status. One that arrives while no review listens, between |
| 49 | // steps or while a step cleans up, ends the walk before the next step: |
| 50 | // the step under way ends as it would and is recorded, the summary so far |
| 51 | // is printed, and the walk exits 130. One that arrives while the queue is |
| 52 | // read again ends the walk with 130 once the read has ended. |
| 53 | // |
| 54 | // The summary is one line per row walked, then what still needs the |
| 55 | // operator, read again from every project, and what either read did not |
| 56 | // cover. |
| 57 | |
| 58 | import ( |
| 59 | "errors" |
| 60 | "fmt" |
| 61 | "io" |
| 62 | "os" |
| 63 | "os/signal" |
| 64 | "slices" |
| 65 | "strconv" |
| 66 | "strings" |
| 67 | "syscall" |
| 68 | "time" |
| 69 | |
| 70 | "the-valley/change" |
| 71 | ) |
| 72 | |
| 73 | // walkConfigKeys are the settings a review in a cache clone borrows from |
| 74 | // the current checkout: who approves and rebases, and who commits. |
| 75 | var walkConfigKeys = []string{"valley.principal", "user.name", "user.email"} |
| 76 | |
| 77 | // walkQueue is one read of the queue for a walk. |
| 78 | type walkQueue struct { |
| 79 | repos []listed |
| 80 | h handleMap |
| 81 | rows, moving []*entry |
| 82 | gaps []string // what the read did not cover, one line each |
| 83 | drafts, branches int |
| 84 | } |
| 85 | |
| 86 | // walkStep is one row walked and how its step ended. |
| 87 | type walkStep struct { |
| 88 | row *entry |
| 89 | outcome stepOutcome |
| 90 | } |
| 91 | |
| 92 | // runWalk walks the queue. end ends the process when a signal ends a step, |
| 93 | // after the walk has said what it walked; it does not return. |
| 94 | func runWalk(g git, args []string, stdout, stderr io.Writer, end func(int)) int { |
| 95 | l := listing{all: true} |
| 96 | var flags []string // the flags each step's review is recorded with |
| 97 | var verbose, buildHere bool |
| 98 | for i := 0; i < len(args); i++ { |
| 99 | switch a := args[i]; { |
| 100 | case a == "-v" || a == "--build-here": |
| 101 | flags = append(flags, a) |
| 102 | verbose, buildHere = verbose || a == "-v", buildHere || a == "--build-here" |
| 103 | case a == "--repo" && i+1 < len(args): |
| 104 | i++ |
| 105 | l.repo = args[i] |
| 106 | case strings.HasPrefix(a, "--repo="): |
| 107 | l.repo = strings.TrimPrefix(a, "--repo=") |
| 108 | default: |
| 109 | fmt.Fprintf(stderr, "valley: walk does not take %s\n", a) |
| 110 | return 2 |
| 111 | } |
| 112 | } |
| 113 | if l.repo != "" && !change.ValidPrincipal(l.repo) { |
| 114 | fmt.Fprintf(stderr, "valley: --repo %s: not a project name\n", l.repo) |
| 115 | return 1 |
| 116 | } |
| 117 | q, err := readWalkQueue(g, l, stderr) |
| 118 | if err != nil { |
| 119 | fmt.Fprintf(stderr, "valley: %s\n", err) |
| 120 | return 1 |
| 121 | } |
| 122 | defer closeSources(q.repos) |
| 123 | warnUnverified(q.repos, stderr) |
| 124 | t := terminalOf(stdout) |
| 125 | p := func(format string, args ...any) { fmt.Fprintf(stdout, format, args...) } |
| 126 | if len(q.rows) == 0 { |
| 127 | for _, line := range q.gapLines(t) { |
| 128 | p("%s\n", line) |
| 129 | } |
| 130 | if len(q.gaps) > 0 { |
| 131 | return 1 |
| 132 | } |
| 133 | p("%s\n", q.nothing()) |
| 134 | return 0 |
| 135 | } |
| 136 | p("needs you (%d, in landing order)\n", len(q.rows)) |
| 137 | for _, line := range q.gapLines(t) { |
| 138 | p("%s\n", line) |
| 139 | } |
| 140 | |
| 141 | top, _ := g.output(true, "rev-parse", "--show-toplevel") |
| 142 | repos := map[string]listed{} |
| 143 | for _, r := range q.repos { |
| 144 | repos[r.name] = r |
| 145 | } |
| 146 | own := &walkValley{g: g, given: verification.given(), published: map[string]publication{}} |
| 147 | var walked []walkStep |
| 148 | summary := func() int { |
| 149 | status := 0 |
| 150 | p("\nwalked (%d of %d)\n", len(walked), len(q.rows)) |
| 151 | for _, s := range walked { |
| 152 | p("%s\n", s.line(t, q.h)) |
| 153 | if s.outcome.kind == "error" { |
| 154 | status = 1 |
| 155 | } |
| 156 | } |
| 157 | return status |
| 158 | } |
| 159 | // Never stopped: the process exits holding it. |
| 160 | interrupts := make(chan os.Signal, 1) |
| 161 | signal.Notify(interrupts, os.Interrupt, syscall.SIGTERM) |
| 162 | interrupted := false |
| 163 | for i, e := range q.rows { |
| 164 | if interrupted = interrupted || caught(interrupts); interrupted { |
| 165 | break |
| 166 | } |
| 167 | p("\n%s\n", stepHeader(t, i+1, len(q.rows), e)) |
| 168 | r := repos[e.repo] |
| 169 | step := git{env: g.env, stderr: g.stderr, dir: top} |
| 170 | if r.g.dir != "" { |
| 171 | step.dir, step.env = r.g.dir, withConfig(r.g.env, borrowedConfig(g, r.g)) |
| 172 | } |
| 173 | if step.dir == "" { |
| 174 | walked = append(walked, walkStep{row: e, outcome: stepOutcome{kind: "error", reason: "no work tree here to review it in"}}) |
| 175 | continue |
| 176 | } |
| 177 | if why := own.elsewhere(r.g); why != "" { |
| 178 | fmt.Fprintf(stderr, "valley: %s: %s\n", e.repo, why) |
| 179 | walked = append(walked, walkStep{row: e, outcome: stepOutcome{kind: "error", reason: why}}) |
| 180 | continue |
| 181 | } |
| 182 | inv := &invocation{args: append(append([]string{"review"}, flags...), e.id), since: time.Now(), version: versionStamp} |
| 183 | left := phase("steps") |
| 184 | o := reviewChange(reviewStep{g: step, queued: &queuedChange{id: e.id, version: e.version, head: e.head}, |
| 185 | verbose: verbose, buildHere: buildHere, stdin: os.Stdin, stdout: stdout, stderr: stderr, |
| 186 | signers: verification, interrupts: interrupts, |
| 187 | end: func(status int, o stepOutcome) { |
| 188 | walked = append(walked, walkStep{row: e, outcome: o}) |
| 189 | if status == interruptedStatus { |
| 190 | summary() |
| 191 | } |
| 192 | inv.record(step, status, false) |
| 193 | closeSources(q.repos) |
| 194 | verification.done() |
| 195 | end(status) |
| 196 | }}, e.id) |
| 197 | left() |
| 198 | inv.record(step, o.status, false) |
| 199 | walked = append(walked, walkStep{row: e, outcome: o}) |
| 200 | interrupted = caught(interrupts) |
| 201 | } |
| 202 | |
| 203 | status := summary() |
| 204 | if interrupted { |
| 205 | return interruptedStatus |
| 206 | } |
| 207 | again, err := readWalkQueue(g, l, io.Discard) |
| 208 | if caught(interrupts) { |
| 209 | closeSources(again.repos) |
| 210 | return interruptedStatus |
| 211 | } |
| 212 | if err != nil { |
| 213 | fmt.Fprintf(stderr, "valley: the queue did not read again: %s\n", err) |
| 214 | return 1 |
| 215 | } |
| 216 | closeSources(again.repos) |
| 217 | // The summary repeats what the first read did not cover, since the walk |
| 218 | // left it out, and adds what the second read did not. |
| 219 | for _, gap := range again.gaps { |
| 220 | if !slices.Contains(q.gaps, gap) { |
| 221 | q.gaps = append(q.gaps, gap) |
| 222 | } |
| 223 | } |
| 224 | p("\n") |
| 225 | for _, line := range q.gapLines(t) { |
| 226 | p("%s\n", line) |
| 227 | } |
| 228 | switch { |
| 229 | case len(again.rows) > 0: |
| 230 | var names []string |
| 231 | for _, e := range again.rows { |
| 232 | names = append(names, e.repo+" "+e.ref) |
| 233 | } |
| 234 | p("still needs you (%d): %s\n", len(again.rows), strings.Join(names, ", ")) |
| 235 | case len(again.gaps) == 0: |
| 236 | p("%s\n", again.nothing()) |
| 237 | } |
| 238 | if len(q.gaps) > 0 { |
| 239 | status = 1 |
| 240 | } |
| 241 | return status |
| 242 | } |
| 243 | |
| 244 | // walkValley is the valley the walk's checkout's main names, whose |
| 245 | // publication the walk's signer source reads, set against the valley each |
| 246 | // step's cache clone names (instanceOf). A step whose project names |
| 247 | // another valley, or none, is not verified with the walk's source: the |
| 248 | // integrator that signs its transfer statements is its own valley's. |
| 249 | type walkValley struct { |
| 250 | g git // the walk's checkout |
| 251 | given bool // a flag or the environment gives what the walk trusts |
| 252 | |
| 253 | read bool |
| 254 | instance string |
| 255 | err error |
| 256 | published map[string]publication // what each instance URL read publishes |
| 257 | } |
| 258 | |
| 259 | // publication is the signers tree an instance repository publishes, "" |
| 260 | // for none, or the error of reading it. |
| 261 | type publication struct { |
| 262 | tree string |
| 263 | err error |
| 264 | } |
| 265 | |
| 266 | // elsewhere is why a step in the cache clone c is not reviewed with the |
| 267 | // walk's signer source, or "" when its project's main names the walk's |
| 268 | // valley. Two URLs are one valley when they are the same string, or when |
| 269 | // both read the same signers publication, which holds for two spellings |
| 270 | // of one repository and makes the trust the same either way. When the |
| 271 | // operator gave what the walk trusts, every step is reviewed with it. |
| 272 | func (v *walkValley) elsewhere(c git) string { |
| 273 | if v.given || c.dir == "" { |
| 274 | return "" |
| 275 | } |
| 276 | const there = " · review it from a checkout of its own valley" |
| 277 | theirs, err := instanceOf(c) |
| 278 | var none noInstance |
| 279 | switch { |
| 280 | case errors.As(err, &none): |
| 281 | return "its main names no valley" + there |
| 282 | case err != nil: |
| 283 | return fmt.Sprintf("could not read which valley its main names: %v", err) |
| 284 | } |
| 285 | if !v.read { |
| 286 | v.read = true |
| 287 | v.instance, v.err = instanceOf(v.g) |
| 288 | } |
| 289 | if v.err != nil { |
| 290 | return fmt.Sprintf("could not read which valley this checkout's main names: %v", v.err) |
| 291 | } |
| 292 | if theirs == v.instance { |
| 293 | return "" |
| 294 | } |
| 295 | if a, b := v.publication(theirs), v.publication(v.instance); a.err == nil && b.err == nil && a.tree == b.tree { |
| 296 | return "" |
| 297 | } |
| 298 | return fmt.Sprintf("its main names the valley %s, not this checkout's %s%s", theirs, v.instance, there) |
| 299 | } |
| 300 | |
| 301 | // publication is what the instance repository at url publishes, read once. |
| 302 | func (v *walkValley) publication(url string) publication { |
| 303 | if p, ok := v.published[url]; ok { |
| 304 | return p |
| 305 | } |
| 306 | tree, err := remoteRef(v.g.restricted(), url, signersRef) |
| 307 | p := publication{tree: tree, err: err} |
| 308 | v.published[url] = p |
| 309 | return p |
| 310 | } |
| 311 | |
| 312 | // caught reports whether a signal is waiting on c, and takes it. |
| 313 | func caught(c chan os.Signal) bool { |
| 314 | select { |
| 315 | case <-c: |
| 316 | return true |
| 317 | default: |
| 318 | return false |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // readWalkQueue reads the queue across the repositories l covers, and |
| 323 | // what the read did not cover. closeSources removes what it read. |
| 324 | func readWalkQueue(g git, l listing, stderr io.Writer) (walkQueue, error) { |
| 325 | list, _, gap, err := findSources(g, l) |
| 326 | if err != nil { |
| 327 | return walkQueue{}, err |
| 328 | } |
| 329 | q := walkQueue{repos: readSources(list, newVerifier())} |
| 330 | q.h = handles(q.repos) |
| 331 | if gap != nil { |
| 332 | q.gaps = append(q.gaps, fmt.Sprintf("discovery is unavailable: %v · walking %s only", gap, list[0].name)) |
| 333 | } |
| 334 | for _, r := range q.repos { |
| 335 | if r.err != nil { |
| 336 | q.gaps = append(q.gaps, fmt.Sprintf("%s did not read: %v", r.name, r.err)) |
| 337 | } |
| 338 | } |
| 339 | q.rows, q.moving = operatorQueue(q.repos, q.h, l, newCosts(), stderr, func(_ listed, view jsonRepo) { |
| 340 | q.drafts, q.branches = q.drafts+len(*view.Drafts), q.branches+len(view.Branches) |
| 341 | }) |
| 342 | return q, nil |
| 343 | } |
| 344 | |
| 345 | // gapLines are the read's gaps, one failed line each. |
| 346 | func (q walkQueue) gapLines(t terminal) []string { |
| 347 | var lines []string |
| 348 | for _, gap := range q.gaps { |
| 349 | lines = append(lines, t.tint(colourRefused, "✗")+" "+fit(gap, t.width-2)) |
| 350 | } |
| 351 | return lines |
| 352 | } |
| 353 | |
| 354 | // nothing is the line of a queue with no row, with the counts valley |
| 355 | // pending gives of what else there is. |
| 356 | func (q walkQueue) nothing() string { |
| 357 | parts := []string{"nothing needs you"} |
| 358 | if n := len(q.moving); n > 0 { |
| 359 | parts = append(parts, fmt.Sprintf("%d moving", n)) |
| 360 | } |
| 361 | if q.drafts > 0 { |
| 362 | parts = append(parts, counted(q.drafts, "draft")) |
| 363 | } |
| 364 | switch q.branches { |
| 365 | case 0: |
| 366 | case 1: |
| 367 | parts = append(parts, "1 branch no change is on") |
| 368 | default: |
| 369 | parts = append(parts, fmt.Sprintf("%d branches no change is on", q.branches)) |
| 370 | } |
| 371 | return strings.Join(parts, " · ") |
| 372 | } |
| 373 | |
| 374 | // stepHeader is the line that opens step n of all: the row's project, its |
| 375 | // change, the stack it tops and its checks, as the queue read them. |
| 376 | func stepHeader(t terminal, n, all int, e *entry) string { |
| 377 | parts := []string{fmt.Sprintf("[%d/%d] %s", n, all, e.repo), t.strong(e.name)} |
| 378 | if len(e.below) > 0 { |
| 379 | parts = append(parts, "stack of "+strconv.Itoa(len(e.below)+1)) |
| 380 | } |
| 381 | if c := checkCount(e.state); c != "" { |
| 382 | parts = append(parts, c) |
| 383 | } |
| 384 | return strings.Join(parts, " · ") |
| 385 | } |
| 386 | |
| 387 | // borrowedConfig is the configuration a review in the cache clone at |
| 388 | // clone takes from the current checkout here: each of walkConfigKeys that |
| 389 | // here sets and the clone's own configuration does not. |
| 390 | func borrowedConfig(here, clone git) [][2]string { |
| 391 | var kv [][2]string |
| 392 | for _, k := range walkConfigKeys { |
| 393 | v, err := here.output(true, "config", "--get", k) |
| 394 | if err != nil || v == "" || clone.succeeds(true, "config", "--local", "--get", k) { |
| 395 | continue |
| 396 | } |
| 397 | kv = append(kv, [2]string{k, v}) |
| 398 | } |
| 399 | return kv |
| 400 | } |
| 401 | |
| 402 | // withConfig is env with each pair added to git's configuration through |
| 403 | // GIT_CONFIG_COUNT, after the entries env gives it already. A count git |
| 404 | // would refuse has failed every git the walk ran before this. |
| 405 | func withConfig(env []string, kv [][2]string) []string { |
| 406 | if len(kv) == 0 { |
| 407 | return env |
| 408 | } |
| 409 | n := 0 |
| 410 | var kept []string |
| 411 | for _, e := range env { |
| 412 | if v, ok := strings.CutPrefix(e, "GIT_CONFIG_COUNT="); ok { |
| 413 | n, _ = strconv.Atoi(v) |
| 414 | continue |
| 415 | } |
| 416 | kept = append(kept, e) |
| 417 | } |
| 418 | for i, p := range kv { |
| 419 | kept = append(kept, fmt.Sprintf("GIT_CONFIG_KEY_%d=%s", n+i, p[0]), fmt.Sprintf("GIT_CONFIG_VALUE_%d=%s", n+i, p[1])) |
| 420 | } |
| 421 | return append(kept, fmt.Sprintf("GIT_CONFIG_COUNT=%d", n+len(kv))) |
| 422 | } |
| 423 | |
| 424 | // line is the summary's line for a step: its glyph, the row's project, |
| 425 | // handle and version, and what became of it. |
| 426 | func (s walkStep) line(t terminal, h handleMap) string { |
| 427 | e, o := s.row, s.outcome |
| 428 | glyph, colour, says := "◇", colourWaiting, o.kind |
| 429 | switch o.kind { |
| 430 | case "approved": |
| 431 | glyph, colour = "✓", colourDone |
| 432 | id, handle, version := o.id, o.handle, o.version |
| 433 | if known := h[handleKey{e.repo, recordTarget, id}]; known != "" { |
| 434 | handle = known |
| 435 | } |
| 436 | switch { |
| 437 | case id != e.id: |
| 438 | says = "approved " + handle + " v" + version + ", a layer under it" |
| 439 | case version != strconv.Itoa(e.version): |
| 440 | says = "approved v" + version |
| 441 | } |
| 442 | case "rejected": |
| 443 | glyph, colour, says = "✓", colourDone, "rejected · branch deleted" |
| 444 | case "kept": |
| 445 | says = "kept · rejection not confirmed" |
| 446 | case "refused": |
| 447 | glyph, colour, says = "⊘", colourRefused, "refused, then skipped" |
| 448 | case "moved": |
| 449 | says = "moved while shown" |
| 450 | case "not-reviewed": |
| 451 | glyph, colour, says = "⊘", colourRefused, "not reviewed · "+o.reason |
| 452 | case "error": |
| 453 | glyph, colour = "✗", colourRefused |
| 454 | if o.reason != "" { |
| 455 | says += " · " + o.reason |
| 456 | } |
| 457 | } |
| 458 | lead := " " + glyph + " " + e.repo + " " |
| 459 | return " " + t.tint(colour, glyph) + " " + e.repo + " " + t.strong(e.name) + " " + |
| 460 | fit(says, t.width-columns(lead)-columns(e.name)-2) |
| 461 | } |