| 1 | package main |
| 2 | |
| 3 | // valley review: a change, or a stack layer by layer, paged for the |
| 4 | // operator to approve, reject or skip. With no change it walks what needs |
| 5 | // the operator (walk.go), and each step of the walk is this review of one |
| 6 | // change, called with the step's repository. bin/valley hands the verb to |
| 7 | // this program. |
| 8 | // |
| 9 | // A change is resolved before origin is fetched, as approve resolves it, |
| 10 | // and shown at the head its record names, or a plain branch at its tip on |
| 11 | // origin. A stack is read first (stack.go), and reviewed one layer at a |
| 12 | // time (reviewstack.go). Each page is set against the change's approved |
| 13 | // version (compare.go), paged through the configured diff pager into |
| 14 | // less, and read back for notes (reviewpage.go). The prompt is verdict.go's. |
| 15 | // [a]pprove is approve's attempt (approve.go), [b]ase onto main is the |
| 16 | // rebase verb's (rebase.go), and before either acts, and before |
| 17 | // [r]eject, the change is read again (standing.go, carry.go). |
| 18 | // |
| 19 | // A review is given a reviewStep: the repository it runs in, whose |
| 20 | // directory and environment every process it starts is given, unless the |
| 21 | // process targets a tree of its own; what a walk's queue held of the |
| 22 | // change; the terminal it reads answers from, a byte at a time, and the |
| 23 | // streams it writes to; the signer source its reads verify a transfer |
| 24 | // statement with; the caller's catch of SIGINT and SIGTERM; and the path |
| 25 | // an interrupt ends it by. It returns how it ended as a stepOutcome, the |
| 26 | // same for a walk's step as for valley review <change>, which turns it |
| 27 | // into the exit status. |
| 28 | // |
| 29 | // The caller catches SIGINT and SIGTERM from before the review starts |
| 30 | // until the process exits, so neither ever gets the default handling. The |
| 31 | // review takes them from the caller's catch while it listens, until its |
| 32 | // cleanup, and acts on each: it ends the review with status 130, its |
| 33 | // outcome interrupted, through the step's end path, which ends the |
| 34 | // process: once the command is recorded for valley review <change>, and |
| 35 | // once the walk's summary so far is printed for a step (walk.go). One that |
| 36 | // arrives while a program it runs in the foreground has the terminal, such |
| 37 | // as less or the editor less starts, ends it once that program has ended: |
| 38 | // less takes Ctrl-C itself. During [a]pprove's attempt, SIGINT is the |
| 39 | // attempt's to act on: it stops the checks, and the attempt ends the |
| 40 | // review through the same path. One that arrives during the cleanup stays |
| 41 | // in the caller's catch, and the caller acts on it once the review has |
| 42 | // returned: valley review <change> then exits 130. SIGHUP ends the review |
| 43 | // at once while it listens, with status 129. Every other ending returns, a |
| 44 | // failure included, so a walk goes on to its next step. However it ends, |
| 45 | // the review leaves nothing of its own behind: its pages and the |
| 46 | // background read of the checks. |
| 47 | |
| 48 | import ( |
| 49 | "bytes" |
| 50 | "errors" |
| 51 | "fmt" |
| 52 | "io" |
| 53 | "os" |
| 54 | "os/signal" |
| 55 | "strconv" |
| 56 | "strings" |
| 57 | "sync" |
| 58 | "syscall" |
| 59 | |
| 60 | "the-valley/change" |
| 61 | ) |
| 62 | |
| 63 | const reviewUsage = "usage: valley review [-v] [--build-here] [<change> | --repo <name>]" |
| 64 | |
| 65 | // reviewStep is what one review of a change is given: by valley review |
| 66 | // <change> for the current checkout, and by the walk for each step. |
| 67 | type reviewStep struct { |
| 68 | // g is the repository the review runs in, at its root for a walk's |
| 69 | // step, with the environment every process the review starts is given. |
| 70 | g git |
| 71 | queued *queuedChange // what the walk's queue held of the change; nil outside a walk |
| 72 | verbose, buildHere bool |
| 73 | stdin *os.File // the terminal: read a byte at a time, and given to the programs that read it |
| 74 | stdout, stderr io.Writer |
| 75 | signers *signerSource // what the review's reads verify a transfer statement with |
| 76 | // interrupts is the caller's catch of SIGINT and SIGTERM, which it |
| 77 | // holds until the process exits. The review takes from it only while |
| 78 | // it listens. |
| 79 | interrupts <-chan os.Signal |
| 80 | // end ends the process once a signal has ended the review and the |
| 81 | // review has cleaned up, with the status and how the review ended. It |
| 82 | // does not return. |
| 83 | end func(status int, o stepOutcome) |
| 84 | } |
| 85 | |
| 86 | // queuedChange is a change as the walk's queue held it. |
| 87 | type queuedChange struct { |
| 88 | id string |
| 89 | version int |
| 90 | head string |
| 91 | } |
| 92 | |
| 93 | // stepOutcome is how a review ended. kind is approved, rejected, kept, |
| 94 | // skipped, refused (an answer was refused, then the change skipped), |
| 95 | // moved (it moved while it was shown), not-reviewed, interrupted or error. |
| 96 | // status is the exit status valley review <change> gives it. |
| 97 | type stepOutcome struct { |
| 98 | kind string |
| 99 | id, handle, version, head string // approved: what was filed, the head alone for a plain branch |
| 100 | branch string // rejected and kept: the branch |
| 101 | reason string // not-reviewed, and an error that says why |
| 102 | status int |
| 103 | } |
| 104 | |
| 105 | // shownChange is the change review acts on: the one it was given, or a |
| 106 | // layer of its stack (useLayer). For a change with a record, id, handle, |
| 107 | // version, head, state and owner are its record's, with head the active |
| 108 | // version's. A plain branch has a name and a branch alone. |
| 109 | type shownChange struct { |
| 110 | name, id, handle, version, head, branch, state, owner string |
| 111 | } |
| 112 | |
| 113 | // review is one review of a change and the stack under it. |
| 114 | type review struct { |
| 115 | step reviewStep |
| 116 | g git |
| 117 | stdin *os.File |
| 118 | stdout, stderr io.Writer |
| 119 | verbose, buildHere bool |
| 120 | |
| 121 | c shownChange |
| 122 | approveHead, approveVersion string // what [a]pprove files (bind) |
| 123 | carriedVersion, carriedHead string // the version the answer carried to, which lands, and its head |
| 124 | checksFailedAt string // the head [a]pprove's checks failed at |
| 125 | checksClause string // the checks line for the prompt's header |
| 126 | topName string // the name review was given |
| 127 | layers []reviewLayer |
| 128 | notes []string // taken in the pages since the last layer began |
| 129 | does doers |
| 130 | checks *checksRead |
| 131 | |
| 132 | said *stepOutcome // how the review ended, once it says; the last said stands |
| 133 | refused bool // an answer was refused |
| 134 | attempt func(*approval) // runs [a]pprove's attempt: approval.run |
| 135 | |
| 136 | mu sync.Mutex |
| 137 | scratch string // the directory holding the page being shown |
| 138 | exiting bool |
| 139 | foreground int // programs the review waits on with signals held |
| 140 | pending bool // a signal held until they end |
| 141 | } |
| 142 | |
| 143 | // reviewEnded is a review that cannot go on, having said why. status is |
| 144 | // the exit status valley review <change> gives it. |
| 145 | type reviewEnded struct{ status int } |
| 146 | |
| 147 | func (e reviewEnded) Error() string { return "the review ended with status " + strconv.Itoa(e.status) } |
| 148 | |
| 149 | // endedStatus is the exit status of a review err ended. |
| 150 | func endedStatus(err error) int { |
| 151 | var ended reviewEnded |
| 152 | if errors.As(err, &ended) { |
| 153 | return ended.status |
| 154 | } |
| 155 | return 1 |
| 156 | } |
| 157 | |
| 158 | func runReview(g git, args []string, stdout, stderr io.Writer, end func(int)) int { |
| 159 | var name, repo string |
| 160 | var verbose, buildHere, repoGiven bool |
| 161 | for i := 0; i < len(args); i++ { |
| 162 | switch a := args[i]; { |
| 163 | case a == "-v": |
| 164 | verbose = true |
| 165 | case a == "--build-here": |
| 166 | buildHere = true |
| 167 | case a == "--repo": |
| 168 | if i+1 >= len(args) { |
| 169 | fmt.Fprintf(stderr, "valley: %s\n", reviewUsage) |
| 170 | return 1 |
| 171 | } |
| 172 | repo, repoGiven = args[i+1], true |
| 173 | i++ |
| 174 | case strings.HasPrefix(a, "-") || name != "": |
| 175 | fmt.Fprintf(stderr, "valley: %s\n", reviewUsage) |
| 176 | return 1 |
| 177 | default: |
| 178 | name = a |
| 179 | } |
| 180 | } |
| 181 | if name == "" { |
| 182 | var walk []string |
| 183 | if verbose { |
| 184 | walk = append(walk, "-v") |
| 185 | } |
| 186 | if buildHere { |
| 187 | walk = append(walk, "--build-here") |
| 188 | } |
| 189 | if repoGiven { |
| 190 | walk = append(walk, "--repo", repo) |
| 191 | } |
| 192 | if err := fetchOrigin(g, stdout, stderr); err != nil { |
| 193 | return exitCode(err) |
| 194 | } |
| 195 | return runWalk(g, walk, stdout, stderr, end) |
| 196 | } |
| 197 | if repoGiven { |
| 198 | fmt.Fprintf(stderr, "valley: %s\n", reviewUsage) |
| 199 | return 1 |
| 200 | } |
| 201 | commandTimed = false |
| 202 | // Never stopped: the process exits holding it. |
| 203 | interrupts := make(chan os.Signal, 1) |
| 204 | signal.Notify(interrupts, os.Interrupt, syscall.SIGTERM) |
| 205 | o := reviewChange(reviewStep{g: g, verbose: verbose, buildHere: buildHere, stdin: os.Stdin, |
| 206 | stdout: stdout, stderr: stderr, signers: verification, interrupts: interrupts, |
| 207 | end: func(status int, _ stepOutcome) { |
| 208 | verification.done() |
| 209 | end(status) |
| 210 | }}, name) |
| 211 | if caught(interrupts) { |
| 212 | return interruptedStatus |
| 213 | } |
| 214 | return o.status |
| 215 | } |
| 216 | |
| 217 | // reviewChange is one review of the change named, and how it ended. Only |
| 218 | // a signal ends it any other way: through s.end. |
| 219 | func reviewChange(s reviewStep, name string) stepOutcome { |
| 220 | return newReview(s).review(name) |
| 221 | } |
| 222 | |
| 223 | // newReview is a review given s. |
| 224 | func newReview(s reviewStep) *review { |
| 225 | return &review{step: s, g: s.g, stdin: s.stdin, stdout: s.stdout, stderr: s.stderr, |
| 226 | verbose: s.verbose, buildHere: s.buildHere, attempt: func(a *approval) { a.run() }} |
| 227 | } |
| 228 | |
| 229 | // review reviews the change named, and returns how it ended. It stops |
| 230 | // listening before it cleans up: a signal taken during the cleanup would |
| 231 | // find the review already ending and leave the listener parked in exit, |
| 232 | // and stop would wait for it forever. A signal that arrives during the |
| 233 | // cleanup stays in the caller's catch instead. |
| 234 | func (r *review) review(name string) stepOutcome { |
| 235 | stop := r.listen() |
| 236 | defer stop() // for an end that leaves this goroutine rather than the process |
| 237 | status := 0 |
| 238 | if err := r.run(name); err != nil { |
| 239 | status = endedStatus(err) |
| 240 | } |
| 241 | stop() |
| 242 | o, _ := r.cleanup(status) |
| 243 | return o |
| 244 | } |
| 245 | |
| 246 | // run is the review of the change named. An error is a review that ended |
| 247 | // without a verdict, having said why. |
| 248 | func (r *review) run(name string) error { |
| 249 | if err := r.resolve(name); err != nil { |
| 250 | return r.die(err.Error()) |
| 251 | } |
| 252 | if err := fetchOrigin(r.g, r.stdout, r.stderr); err != nil { |
| 253 | return reviewEnded{exitCode(err)} |
| 254 | } |
| 255 | if !r.queued() { |
| 256 | return nil |
| 257 | } |
| 258 | // Notes are kept under the branch, and [r]eject deletes it. approve |
| 259 | // files the head alone, so it approves such a change. |
| 260 | if r.c.id != "" && r.c.branch == "" { |
| 261 | return r.die(fmt.Sprintf("change %s v%s records no branch, and review acts on a change's branch. Approve its head alone: valley approve --head %s %s", |
| 262 | r.c.handle, r.c.version, r.c.head, r.c.handle)) |
| 263 | } |
| 264 | branch := r.c.branch |
| 265 | r.topName = name |
| 266 | if r.ref("refs/remotes/origin/"+branch) == "" { |
| 267 | return r.die("no such branch on origin: " + branch) |
| 268 | } |
| 269 | r.bind() |
| 270 | // The verdict disposes of the change; the notes are what the human made |
| 271 | // of it, so they are printed whatever the verdict was. A stack that |
| 272 | // cannot be read is reviewed whole against main, as a change with |
| 273 | // nothing under it is. |
| 274 | if !r.readStack() { |
| 275 | fmt.Fprintf(r.stderr, "valley: warning: the stack under %s could not be read; reviewing it whole, against main\n", name) |
| 276 | r.layers = nil |
| 277 | } |
| 278 | r.readDoes() |
| 279 | if len(r.layers) > 1 { |
| 280 | if err := r.reviewStack(); err != nil { |
| 281 | return err |
| 282 | } |
| 283 | r.printStackNotes() |
| 284 | return nil |
| 285 | } |
| 286 | if err := r.showReview(branch); err != nil { |
| 287 | return err |
| 288 | } |
| 289 | if err := r.decide(branch, "", 0); err != nil { |
| 290 | return err |
| 291 | } |
| 292 | r.printNotes(branch, r.notes) |
| 293 | return nil |
| 294 | } |
| 295 | |
| 296 | // fetchOrigin brings this checkout's remote-tracking refs up to date. |
| 297 | func fetchOrigin(g git, stdout, stderr io.Writer) error { |
| 298 | fetch := g.command("fetch", "--prune", "--quiet", "origin") |
| 299 | fetch.Stdout, fetch.Stderr = stdout, stderr |
| 300 | processStarted() |
| 301 | return fetch.Run() |
| 302 | } |
| 303 | |
| 304 | // listen takes SIGINT and SIGTERM from the caller's catch, and SIGHUP, |
| 305 | // until the stop it returns, which returns once every signal taken has |
| 306 | // been acted on: one that ends the review ends the process, so nothing of |
| 307 | // the review acts on a signal once stop has returned. A SIGINT or SIGTERM |
| 308 | // that arrives after is left in the caller's catch. |
| 309 | func (r *review) listen() (stop func()) { |
| 310 | hup := make(chan os.Signal, 1) |
| 311 | signal.Notify(hup, syscall.SIGHUP) |
| 312 | quit, done := make(chan struct{}), make(chan struct{}) |
| 313 | go func() { |
| 314 | defer close(done) |
| 315 | for { |
| 316 | select { |
| 317 | case s := <-r.step.interrupts: |
| 318 | r.signalled(s) |
| 319 | case s := <-hup: |
| 320 | r.signalled(s) |
| 321 | case <-quit: |
| 322 | if caught(hup) { |
| 323 | r.signalled(syscall.SIGHUP) |
| 324 | } |
| 325 | return |
| 326 | } |
| 327 | } |
| 328 | }() |
| 329 | var once sync.Once |
| 330 | return func() { |
| 331 | once.Do(func() { |
| 332 | signal.Stop(hup) |
| 333 | close(quit) |
| 334 | <-done |
| 335 | }) |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // signalled acts on a signal: SIGHUP ends the review at once, and another |
| 340 | // ends it now, or once the programs in the foreground have ended. |
| 341 | func (r *review) signalled(s os.Signal) { |
| 342 | if s == syscall.SIGHUP { |
| 343 | r.exit(128 + int(syscall.SIGHUP)) |
| 344 | } |
| 345 | r.mu.Lock() |
| 346 | held := r.foreground > 0 |
| 347 | r.pending = r.pending || held |
| 348 | r.mu.Unlock() |
| 349 | if !held { |
| 350 | r.exit(interruptedStatus) |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // inForeground runs f, which runs a program that has the terminal or |
| 355 | // handles signals itself, holding any signal until it returns. |
| 356 | func (r *review) inForeground(f func()) { |
| 357 | r.mu.Lock() |
| 358 | r.foreground++ |
| 359 | r.mu.Unlock() |
| 360 | f() |
| 361 | r.mu.Lock() |
| 362 | r.foreground-- |
| 363 | act := r.foreground == 0 && r.pending |
| 364 | r.mu.Unlock() |
| 365 | if act { |
| 366 | r.exit(interruptedStatus) |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // cleanup removes what the review leaves outside the repository, and |
| 371 | // returns how it ended with status: what it said, or else interrupted for |
| 372 | // a signal's status and error for any other. Only the first call acts: it |
| 373 | // reports false to any other, whose caller leaves the ending to the first. |
| 374 | func (r *review) cleanup(status int) (stepOutcome, bool) { |
| 375 | r.mu.Lock() |
| 376 | if r.exiting { |
| 377 | r.mu.Unlock() |
| 378 | return stepOutcome{}, false |
| 379 | } |
| 380 | r.exiting = true |
| 381 | o := stepOutcome{kind: "error"} |
| 382 | if r.said != nil { |
| 383 | o = *r.said |
| 384 | } else if status == interruptedStatus { |
| 385 | o.kind = "interrupted" |
| 386 | } |
| 387 | o.status = status |
| 388 | scratch, checks := r.scratch, r.checks |
| 389 | r.scratch, r.checks = "", nil |
| 390 | r.mu.Unlock() |
| 391 | if scratch != "" { |
| 392 | os.RemoveAll(scratch) |
| 393 | } |
| 394 | checks.stop() |
| 395 | return o, true |
| 396 | } |
| 397 | |
| 398 | // exit ends the review and the process, from anywhere, with status. |
| 399 | func (r *review) exit(status int) { |
| 400 | o, first := r.cleanup(status) |
| 401 | if !first { |
| 402 | select {} // the ending under way ends the process |
| 403 | } |
| 404 | r.step.end(status, o) |
| 405 | } |
| 406 | |
| 407 | // die says what ends the review: nothing is left to offer. It returns the |
| 408 | // error that ends it, with status 1. |
| 409 | func (r *review) die(msg string) error { |
| 410 | fmt.Fprintf(r.stderr, "valley: %s\n", msg) |
| 411 | return reviewEnded{1} |
| 412 | } |
| 413 | |
| 414 | // refuse reports what was not done, without leaving the review: the |
| 415 | // change is still under review and the operator can still answer. It |
| 416 | // returns false. |
| 417 | func (r *review) refuse(msg string) bool { |
| 418 | fmt.Fprintf(r.stderr, "valley: %s\n", msg) |
| 419 | return false |
| 420 | } |
| 421 | |
| 422 | // newScratch makes the directory the next page is written to. |
| 423 | func (r *review) newScratch() (string, error) { |
| 424 | dir, err := os.MkdirTemp("", "valley-review-") |
| 425 | if err != nil { |
| 426 | return "", r.die(fmt.Sprintf("no scratch directory for the review: %v", err)) |
| 427 | } |
| 428 | r.mu.Lock() |
| 429 | if r.exiting { |
| 430 | r.mu.Unlock() |
| 431 | os.RemoveAll(dir) |
| 432 | select {} |
| 433 | } |
| 434 | r.scratch = dir |
| 435 | r.mu.Unlock() |
| 436 | return dir, nil |
| 437 | } |
| 438 | |
| 439 | // dropScratch removes the page's directory. |
| 440 | func (r *review) dropScratch() { |
| 441 | r.mu.Lock() |
| 442 | dir := r.scratch |
| 443 | r.scratch = "" |
| 444 | r.mu.Unlock() |
| 445 | if dir != "" { |
| 446 | os.RemoveAll(dir) |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | // outcome says how the review ended, unless it is already ending and |
| 451 | // says so itself. The last one said stands. |
| 452 | func (r *review) outcome(o stepOutcome) { |
| 453 | r.mu.Lock() |
| 454 | if !r.exiting { |
| 455 | r.said = &o |
| 456 | } |
| 457 | r.mu.Unlock() |
| 458 | } |
| 459 | |
| 460 | // skipped is the outcome of a review that was skipped or stopped: refused |
| 461 | // when an answer was refused first. |
| 462 | func (r *review) skipped() { |
| 463 | if r.refused { |
| 464 | r.outcome(stepOutcome{kind: "refused"}) |
| 465 | } else { |
| 466 | r.outcome(stepOutcome{kind: "skipped"}) |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // queued compares a walk's step with what the walker's queue held of it. |
| 471 | // A change that landed, is no longer ready or has no branch on origin is |
| 472 | // not reviewed, and the step ends; one at another version is reviewed at |
| 473 | // it, saying so first. It reports false when the step ends here. |
| 474 | func (r *review) queued() bool { |
| 475 | q := r.step.queued |
| 476 | if q == nil { |
| 477 | return true |
| 478 | } |
| 479 | s, _ := r.readStanding() |
| 480 | why := "" |
| 481 | switch { |
| 482 | case s.landed != "": |
| 483 | why = "it landed as " + s.landed |
| 484 | case r.c.state != "ready": |
| 485 | why = "it is " + strings.Replace(r.c.state, "draft", "a draft", 1) |
| 486 | case r.c.branch == "" || r.ref("refs/remotes/origin/"+r.c.branch) == "": |
| 487 | why = "its branch is gone from origin" |
| 488 | } |
| 489 | if why != "" { |
| 490 | fmt.Fprintf(r.stdout, "%s is not reviewed: %s\n", r.c.handle, why) |
| 491 | r.outcome(stepOutcome{kind: "not-reviewed", reason: why}) |
| 492 | return false |
| 493 | } |
| 494 | if q.id != r.c.id || strconv.Itoa(q.version) != r.c.version || q.head != r.c.head { |
| 495 | fmt.Fprintf(r.stdout, "%s moved to v%s since the queue was read\n", r.c.handle, r.c.version) |
| 496 | } |
| 497 | return true |
| 498 | } |
| 499 | |
| 500 | // resolve reads the name as approve and rebase read a change's name: a |
| 501 | // change's ID, handle, or the branch its active version names, or else a |
| 502 | // plain branch. |
| 503 | func (r *review) resolve(name string) error { |
| 504 | r.c = shownChange{name: name} |
| 505 | if !r.g.succeeds(true, "rev-parse", "--git-dir") { |
| 506 | return failure("not a git repository") |
| 507 | } |
| 508 | res, err := resolveRecorded(r.g, name) |
| 509 | if err != nil { |
| 510 | return err |
| 511 | } |
| 512 | r.c.branch = res.branch |
| 513 | if res.id != "" { |
| 514 | r.c.id, r.c.handle, r.c.version, r.c.head = res.id, res.handle, strconv.Itoa(res.version), res.head |
| 515 | r.c.state, r.c.owner = res.state, res.owner |
| 516 | } |
| 517 | return nil |
| 518 | } |
| 519 | |
| 520 | // verifier is what the review's reads verify a transfer statement with: |
| 521 | // the step's signer source. |
| 522 | func (r *review) verifier() verifier { return r.step.signers.verifier() } |
| 523 | |
| 524 | // ref is the object ID a ref holds, read by its exact name, or "". |
| 525 | func (r *review) ref(name string) string { |
| 526 | oid, ok, err := r.g.ref(name) |
| 527 | if err != nil || !ok { |
| 528 | return "" |
| 529 | } |
| 530 | return oid |
| 531 | } |
| 532 | |
| 533 | func (r *review) short(oid string) string { |
| 534 | s, _ := r.g.output(false, "rev-parse", "--short", oid) |
| 535 | return s |
| 536 | } |
| 537 | |
| 538 | // changeHead is the commit review acts on: the recorded head for a change |
| 539 | // with a record, and otherwise the branch's head on origin. |
| 540 | func (r *review) changeHead(branch string) string { |
| 541 | if r.c.head != "" { |
| 542 | return r.c.head |
| 543 | } |
| 544 | return r.ref("refs/remotes/origin/" + branch) |
| 545 | } |
| 546 | |
| 547 | // fastForwards reports whether commit fast-forwards from main's tip as the |
| 548 | // fetch left it. A main that cannot be read is not one it fast-forwards |
| 549 | // from. |
| 550 | func (r *review) fastForwards(commit string) bool { |
| 551 | tip := r.ref("refs/remotes/origin/main") |
| 552 | return tip != "" && r.g.succeeds(false, "merge-base", "--is-ancestor", tip, commit) |
| 553 | } |
| 554 | |
| 555 | // bind binds [a]pprove to the active version's head of a change with a |
| 556 | // record. A plain branch is bound to the head review pages, when it pages |
| 557 | // it (showReview, useLayer). |
| 558 | func (r *review) bind() { |
| 559 | r.approveHead, r.approveVersion = "", "" |
| 560 | if r.c.id != "" { |
| 561 | r.approveHead, r.approveVersion = r.c.head, r.c.version |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | // readStanding reads where the change under review stands now. A plain |
| 566 | // branch has no record, and reads as nothing. It reports false, having |
| 567 | // said why, when origin cannot be read. |
| 568 | func (r *review) readStanding() (standing, bool) { |
| 569 | if r.c.id == "" { |
| 570 | return standing{}, true |
| 571 | } |
| 572 | s, err := standingOf(r.g, r.c.id) |
| 573 | if err != nil { |
| 574 | fmt.Fprintf(r.stderr, "valley: %s\n", err) |
| 575 | return standing{}, false |
| 576 | } |
| 577 | return s, true |
| 578 | } |
| 579 | |
| 580 | // notReady is the line review shows for a change whose record is not |
| 581 | // ready, above its diff and in its prompt, or "" for a ready change and a |
| 582 | // plain branch. A draft is often a change an agent is still working on, |
| 583 | // so the prompt then offers only [v]iew diff and [s]kip: [a]pprove would |
| 584 | // be refused, and [r]eject would delete the branch its owner may still be |
| 585 | // working on. |
| 586 | func notReady(handle, version, state, owner string) string { |
| 587 | switch state { |
| 588 | case "draft": |
| 589 | return fmt.Sprintf("%s v%s is a draft: its owner (%s) hasn't marked it ready, so it can't be approved yet.", handle, version, owner) |
| 590 | case "withdrawn": |
| 591 | return handle + " is withdrawn: there is nothing to approve." |
| 592 | } |
| 593 | return "" |
| 594 | } |
| 595 | |
| 596 | // answer reads one answer from the terminal, a byte at a time so that |
| 597 | // nothing after it is taken from whatever reads the input next, with the |
| 598 | // spaces and tabs around it trimmed. ok is false when the input ends |
| 599 | // before the line does. |
| 600 | func (r *review) answer() (string, bool) { |
| 601 | var line []byte |
| 602 | b := make([]byte, 1) |
| 603 | for { |
| 604 | n, err := r.stdin.Read(b) |
| 605 | if n == 1 { |
| 606 | if b[0] == '\n' { |
| 607 | return strings.Trim(string(line), " \t"), true |
| 608 | } |
| 609 | line = append(line, b[0]) |
| 610 | } |
| 611 | if err != nil { |
| 612 | return "", false |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | // mustAnswer is answer, ending the review when the input has ended. |
| 618 | func (r *review) mustAnswer() (string, error) { |
| 619 | choice, ok := r.answer() |
| 620 | if !ok { |
| 621 | return "", r.die("stdin closed before an answer; nothing done") |
| 622 | } |
| 623 | return choice, nil |
| 624 | } |
| 625 | |
| 626 | // prompt prints review's prompt: the header's clauses, the empty ones |
| 627 | // left out, and the keys. |
| 628 | func (r *review) prompt(header []string, keys ...string) { |
| 629 | var shown []string |
| 630 | for _, h := range header { |
| 631 | if h != "" { |
| 632 | shown = append(shown, h) |
| 633 | } |
| 634 | } |
| 635 | fmt.Fprint(r.stdout, promptLines(terminalOf(r.stdout), shown, keys)) |
| 636 | } |
| 637 | |
| 638 | // decide takes the verdict on a change alone, and on the top of a stack |
| 639 | // of n, named name. Its prompt is one header line, the change and what |
| 640 | // stands in its way, and the keys it takes. A change is named by its |
| 641 | // handle and version, a plain branch by its name, and a change alone |
| 642 | // carries the checks line review read for it. The top of a stack says that |
| 643 | // approving it lands all n layers. [v]iew diff pages the change's whole |
| 644 | // diff, or the top layer's. |
| 645 | // |
| 646 | // A change that is not ready says so in its header, and is offered only |
| 647 | // [v]iew diff and [s]kip, or [s]top. The top of a stack with a layer under |
| 648 | // it that is not ready says why it can't be approved (layerHeld), as does |
| 649 | // a change whose checks failed at the head shown, and neither is offered |
| 650 | // [a]pprove. A change that can be approved and does not fast-forward from |
| 651 | // main says so (behindMain). Before [a]pprove and [r]eject act, moved |
| 652 | // reads the change again, and ends its review when it moved. |
| 653 | func (r *review) decide(branch, name string, n int) error { |
| 654 | approve, reject, stop := "[a]pprove", "[r]eject", "[s]kip" |
| 655 | stopped := "skipped " + branch + "; nothing done" |
| 656 | header := []string{branch} |
| 657 | own, held, behind := "", "", "" |
| 658 | if r.c.id != "" { |
| 659 | header = []string{r.c.handle + " v" + r.c.version} |
| 660 | switch r.c.state { |
| 661 | case "draft": |
| 662 | own = "draft (its owner, " + r.c.owner + ", has not readied it)" |
| 663 | case "withdrawn": |
| 664 | own = "withdrawn" |
| 665 | } |
| 666 | } |
| 667 | if n > 0 { |
| 668 | header = append(header, fmt.Sprintf("stack of %d", n)) |
| 669 | approve, reject, stop = fmt.Sprintf("[a]pprove all %d", n), "[r]eject "+name, "[s]top" |
| 670 | stopped = fmt.Sprintf("stopped reviewing %s at change %d of %d; nothing done", name, n, n) |
| 671 | held = r.layerHeld(n - 1) |
| 672 | } else if r.checksClause != "" { |
| 673 | header = append(header, r.checksClause) |
| 674 | } |
| 675 | if own == "" && held == "" { |
| 676 | behind = r.behindMain(branch) |
| 677 | } |
| 678 | for { |
| 679 | var clause, answers string |
| 680 | var keys []string |
| 681 | failed := "" |
| 682 | switch { |
| 683 | case own != "": |
| 684 | clause, keys, answers = own, []string{"[v]iew diff", stop}, "v or s" |
| 685 | case held != "": |
| 686 | clause, keys, answers = held, []string{"[v]iew diff", reject, stop}, "v, r, or s" |
| 687 | default: |
| 688 | if failed = r.approveFailedAt(branch); failed != "" { |
| 689 | clause, keys, answers = "checks failed at "+failed, []string{"[v]iew diff", reject, stop}, "v, r, or s" |
| 690 | } else { |
| 691 | clause, keys, answers = behind, []string{approve, "[v]iew diff", reject, stop}, "a, v, r, or s" |
| 692 | } |
| 693 | } |
| 694 | r.prompt(append(append([]string{}, header...), clause), keys...) |
| 695 | // A refusal from approve is not a verdict: it says what it did not |
| 696 | // do and the prompt comes back, so the branch can still be rejected |
| 697 | // or skipped. When the checks failed, approving the same head again |
| 698 | // is no longer offered, nor is approving a change that is not ready. |
| 699 | // A change that moved since it was shown is not acted on, and its |
| 700 | // review ends. |
| 701 | choice, err := r.mustAnswer() |
| 702 | if err != nil { |
| 703 | return err |
| 704 | } |
| 705 | switch choice { |
| 706 | case "a": |
| 707 | if failed != "" || own != "" || held != "" { |
| 708 | fmt.Fprintln(r.stdout, "answer "+answers) |
| 709 | continue |
| 710 | } |
| 711 | moved, err := r.moved(true) |
| 712 | if err != nil { |
| 713 | return err |
| 714 | } |
| 715 | if moved || r.approve(branch) { |
| 716 | return nil |
| 717 | } |
| 718 | case "v": |
| 719 | var err error |
| 720 | if n > 0 { |
| 721 | err = r.viewLayer(n - 1) |
| 722 | } else { |
| 723 | err = r.viewFull(branch) |
| 724 | } |
| 725 | if err != nil { |
| 726 | return err |
| 727 | } |
| 728 | case "r": |
| 729 | if own != "" { |
| 730 | fmt.Fprintln(r.stdout, "answer "+answers) |
| 731 | continue |
| 732 | } |
| 733 | moved, err := r.moved(false) |
| 734 | if err != nil { |
| 735 | return err |
| 736 | } |
| 737 | if moved { |
| 738 | return nil |
| 739 | } |
| 740 | if done, err := r.reject(branch); err != nil || done { |
| 741 | return err |
| 742 | } |
| 743 | case "s": |
| 744 | fmt.Fprintln(r.stdout, stopped) |
| 745 | r.skipped() |
| 746 | return nil |
| 747 | default: |
| 748 | fmt.Fprintln(r.stdout, "answer "+answers) |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | // approveFailedAt is the head the checks failed at in this review, short, |
| 754 | // while the change is still at it, or "". Approving that head again would |
| 755 | // run the same checks over the same tree and fail the same way, so the |
| 756 | // prompt stops offering [a]pprove until the change has a new head. |
| 757 | func (r *review) approveFailedAt(branch string) string { |
| 758 | if r.checksFailedAt == "" || r.checksFailedAt != r.changeHead(branch) { |
| 759 | return "" |
| 760 | } |
| 761 | return r.short(r.checksFailedAt) |
| 762 | } |
| 763 | |
| 764 | // behindMain is the clause review's prompt shows for a change that does |
| 765 | // not fast-forward from main, or "". Being behind main asks nothing of the |
| 766 | // operator: the integrator applies the change's delta onto main as main |
| 767 | // stands, with the evidence for the head approved, and the host's |
| 768 | // restacker rebases ready changes itself. When the restacker reports that |
| 769 | // rebasing the change conflicted, the clause says so instead, and |
| 770 | // [a]pprove stays offered: the integrator decides whether the change |
| 771 | // applies. |
| 772 | func (r *review) behindMain(branch string) string { |
| 773 | if r.fastForwards(r.changeHead(branch)) { |
| 774 | return "" |
| 775 | } |
| 776 | if s, _ := r.readStanding(); s.conflict == "conflict" { |
| 777 | return "conflicts with main (owner resolving)" |
| 778 | } |
| 779 | return "behind main (integrator merges)" |
| 780 | } |
| 781 | |
| 782 | // moved reads a change with a record again before review acts on an |
| 783 | // answer about it: another principal may return it to draft, ready a new |
| 784 | // version, or withdraw it while the operator reads, and it may land. When |
| 785 | // it is no longer what review showed, moved says in one line what changed |
| 786 | // and what happens next, and the review of that change ends: the prompt's |
| 787 | // answers were about what was shown, so it is not asked again. A new |
| 788 | // version the answer carries to does not end it (carried), nor does |
| 789 | // approving the new version in place, which moved offers for an approval. |
| 790 | // It reports false when the change is still as shown, the answer carries |
| 791 | // or the new version is to be approved, and for a plain branch, which |
| 792 | // approve and reject check themselves. |
| 793 | func (r *review) moved(approving bool) (bool, error) { |
| 794 | h := r.c.handle |
| 795 | r.carriedVersion, r.carriedHead = "", "" |
| 796 | if r.c.id == "" { |
| 797 | return false, nil |
| 798 | } |
| 799 | s, ok := r.readStanding() |
| 800 | switch { |
| 801 | case !ok: |
| 802 | fmt.Fprintf(r.stdout, "could not read %s again from origin; nothing done\n", h) |
| 803 | r.outcome(stepOutcome{kind: "error"}) |
| 804 | return true, nil |
| 805 | case s.state == "withdrawn": |
| 806 | fmt.Fprintf(r.stdout, "%s changed while you were reviewing: it was withdrawn (since %s), so there is nothing to approve.\n", h, s.since) |
| 807 | case s.state == "draft": |
| 808 | fmt.Fprintf(r.stdout, "%s changed while you were reviewing: it was returned to draft (since %s), so there is nothing to approve now. It comes back when it is ready again.\n", h, s.since) |
| 809 | case s.landed != "": |
| 810 | fmt.Fprintf(r.stdout, "%s landed as %s while you were reviewing; there is nothing left to approve.\n", h, s.landed) |
| 811 | case s.version == r.c.version && s.head == r.c.head: |
| 812 | return false, nil |
| 813 | default: |
| 814 | carried, err := r.carriedTo(s, approving) |
| 815 | if err != nil || carried { |
| 816 | return false, err |
| 817 | } |
| 818 | } |
| 819 | r.outcome(stepOutcome{kind: "moved"}) |
| 820 | return true, nil |
| 821 | } |
| 822 | |
| 823 | // carriedTo reports whether the answer about the version review showed |
| 824 | // carries to the version s the change moved to, as carry decides |
| 825 | // (carry.go). When it does, it says so in one line, with carry's reason, |
| 826 | // and the answer stays bound to the head shown: an approval names what the |
| 827 | // reviewer read, and the integrator applies its delta onto main as main |
| 828 | // stands. The version moved to is what lands, so the approval's verdict |
| 829 | // names it (carriedVerdict, approve.go). When it does |
| 830 | // not, it says why in one line and offers to page only what changed since |
| 831 | // the version shown. Answering an approval, it also offers to approve the |
| 832 | // version the change moved to, when carry says: at once, or after the |
| 833 | // page. [a] binds the approval to that version's head, and the attempt |
| 834 | // reads the record again before it files. It reports false unless the |
| 835 | // answer carries or [a] is taken. |
| 836 | func (r *review) carriedTo(s standing, approving bool) (bool, error) { |
| 837 | h := r.c.handle |
| 838 | r.bind() |
| 839 | dir, err := r.newScratch() |
| 840 | if err != nil { |
| 841 | return false, err |
| 842 | } |
| 843 | defer r.dropScratch() |
| 844 | var out bytes.Buffer |
| 845 | verdict, reason, offer, text := "", "", "", "" |
| 846 | if err := runCarry(r.g, []string{r.c.id, r.c.version, s.version}, &out); err != nil { |
| 847 | fmt.Fprintf(r.stderr, "valley: %s\n", err) |
| 848 | } else { |
| 849 | first, rest, _ := strings.Cut(out.String(), "\n") |
| 850 | f := strings.Fields(first) |
| 851 | if len(f) > 0 { |
| 852 | verdict = f[0] |
| 853 | } |
| 854 | if t := strings.Split(first, "\t"); len(t) > 1 { |
| 855 | reason = t[1] |
| 856 | if len(t) > 2 { |
| 857 | offer = t[2] |
| 858 | } |
| 859 | } |
| 860 | text = rest |
| 861 | } |
| 862 | if !approving { |
| 863 | offer = "none" |
| 864 | } |
| 865 | switch verdict { |
| 866 | case "carries": |
| 867 | answer := "rejection" |
| 868 | if approving { |
| 869 | answer = "approval" |
| 870 | } |
| 871 | fmt.Fprintf(r.stdout, "%s moved to v%s while you were reviewing. %s, so your %s covers it.\n", h, s.version, reason, answer) |
| 872 | r.carriedVersion, r.carriedHead = s.version, s.head |
| 873 | return true, nil |
| 874 | case "review": |
| 875 | keys := "[v]iew what changed since v" + r.c.version + ", [s]kip?" |
| 876 | switch offer { |
| 877 | case "approve": |
| 878 | keys = "[a]pprove v" + s.version + ", " + keys |
| 879 | case "view": |
| 880 | default: |
| 881 | offer, keys = "", "review it again to approve it · "+keys |
| 882 | } |
| 883 | fmt.Fprintf(r.stdout, "%s moved to v%s while you were reviewing (%s) · %s ", h, s.version, reason, keys) |
| 884 | choice, _ := r.answer() |
| 885 | if choice == "v" { |
| 886 | changed := dir + "/changed" |
| 887 | if err := os.WriteFile(changed, []byte(text), 0o600); err == nil { |
| 888 | r.page(changed) |
| 889 | } |
| 890 | if offer != "" { |
| 891 | offer = "approve" |
| 892 | fmt.Fprintf(r.stdout, "[a]pprove v%s, [s]kip? ", s.version) |
| 893 | choice, _ = r.answer() |
| 894 | } |
| 895 | } |
| 896 | if choice == "a" && offer == "approve" { |
| 897 | r.approveVersion, r.approveHead = s.version, s.head |
| 898 | return true, nil |
| 899 | } |
| 900 | default: |
| 901 | fmt.Fprintf(r.stdout, "%s moved to v%s while you were reviewing; review it again to approve what would land.\n", h, s.version) |
| 902 | } |
| 903 | return false, nil |
| 904 | } |
| 905 | |
| 906 | // approve is review's [a]pprove: one attempt of approve's own for each |
| 907 | // answer, a stack's layers included (approve.go), told the change and head |
| 908 | // review bound and the name as typed, which it reads again before filing. |
| 909 | // It ends approved, with what it filed; checks-failed, with the head the |
| 910 | // checks failed at, which the prompt stops offering [a]pprove for |
| 911 | // (approveFailedAt); refused or unknown; or interrupted, when the attempt |
| 912 | // has ended the review itself. It reports false unless it filed. |
| 913 | func (r *review) approve(branch string) bool { |
| 914 | c := r.c |
| 915 | if c.id == "" { |
| 916 | c.branch = branch |
| 917 | } |
| 918 | a := reviewApproval(r, c) |
| 919 | r.inForeground(func() { |
| 920 | a.watch(os.Interrupt) |
| 921 | r.attempt(a) |
| 922 | a.unwatch() |
| 923 | }) |
| 924 | result := a.result |
| 925 | switch { |
| 926 | case len(result) == 5 && result[0] == "approved": |
| 927 | r.outcome(stepOutcome{kind: "approved", id: result[1], handle: result[2], version: result[3], head: result[4]}) |
| 928 | return true |
| 929 | case len(result) == 2 && result[0] == "checks-failed": |
| 930 | r.checksFailedAt = result[1] |
| 931 | case len(result) == 1 && (result[0] == "refused" || result[0] == "unknown"): |
| 932 | default: |
| 933 | fmt.Fprintln(r.stderr, "valley: warning: the approval left no outcome; taking it as refused") |
| 934 | } |
| 935 | r.refused = true |
| 936 | return false |
| 937 | } |
| 938 | |
| 939 | // reject deletes the change's branch on origin, once the operator confirms |
| 940 | // it, and withdraws a change with a record (rejected). For a change with a |
| 941 | // record, what is rejected is the head its record names, so the branch is |
| 942 | // deleted only while its tip is that head, and the delete is leased on it. |
| 943 | // When the tip is another commit, reject refuses and names both, and |
| 944 | // deletes nothing. A plain branch is deleted as it stands. It reports |
| 945 | // false when it refused. |
| 946 | func (r *review) reject(branch string) (bool, error) { |
| 947 | var lease []string |
| 948 | if r.c.head != "" { |
| 949 | if !r.recordedTip(branch, "nothing deleted") { |
| 950 | r.refused = true |
| 951 | return false, nil |
| 952 | } |
| 953 | lease = []string{"--force-with-lease=refs/heads/" + branch + ":" + r.c.head} |
| 954 | } |
| 955 | fmt.Fprintf(r.stdout, "really delete origin/%s? [y/N] ", branch) |
| 956 | switch choice, _ := r.answer(); choice { |
| 957 | case "y", "Y", "yes": |
| 958 | // The change may have moved while the confirmation waited: say so, |
| 959 | // and delete nothing. |
| 960 | if moved, err := r.moved(false); err != nil || moved { |
| 961 | return true, err |
| 962 | } |
| 963 | push := r.g.command(append(append([]string{"push", "--quiet"}, lease...), "origin", "--delete", branch)...) |
| 964 | push.Stdout, push.Stderr = r.stdout, r.stderr |
| 965 | var err error |
| 966 | r.inForeground(func() { err = push.Run() }) |
| 967 | if err != nil { |
| 968 | r.refuse("origin did not delete " + branch + ", which may have moved since the fetch; nothing deleted") |
| 969 | r.refused = true |
| 970 | return false, nil |
| 971 | } |
| 972 | r.rejected(branch) |
| 973 | default: |
| 974 | fmt.Fprintf(r.stdout, "kept %s\n", branch) |
| 975 | r.outcome(stepOutcome{kind: "kept", branch: branch}) |
| 976 | } |
| 977 | return true, nil |
| 978 | } |
| 979 | |
| 980 | // rejected says reject deleted the branch. A change with a record is |
| 981 | // withdrawn too, by valley withdraw's own code and under its rules, while |
| 982 | // its active version is still at the head deleted: a rejection says the |
| 983 | // change is not wanted, and withdrawing it voids what is outstanding on it |
| 984 | // and takes it off what needs the operator. When the |
| 985 | // withdrawal fails, the line says so and names the command to run. The |
| 986 | // branch is gone either way, so the outcome is rejected. Signals are held |
| 987 | // until the line is printed, so a deleted branch is always reported. |
| 988 | func (r *review) rejected(branch string) { |
| 989 | r.inForeground(func() { |
| 990 | line, run := "rejected "+branch+": deleted from origin", "" |
| 991 | if r.c.id != "" { |
| 992 | if err := setState(r.g, []string{r.c.id}, io.Discard, r.stderr, "withdraw", change.Withdrawn, r.c.head); err != nil { |
| 993 | fmt.Fprintf(r.stderr, "valley: %s\n", err) |
| 994 | line += " · withdrawing " + r.c.handle + " failed" |
| 995 | run = " run: valley withdraw " + r.c.handle + "\n" |
| 996 | } else { |
| 997 | line += " · " + r.c.handle + " withdrawn" |
| 998 | } |
| 999 | } |
| 1000 | fmt.Fprint(r.stdout, line+"\n"+run) |
| 1001 | r.outcome(stepOutcome{kind: "rejected", branch: branch}) |
| 1002 | }) |
| 1003 | } |
| 1004 | |
| 1005 | // recordedTip reports whether the branch's tip on origin is the head a |
| 1006 | // change's record names, and refuses, naming both, when it is not: |
| 1007 | // [r]eject acts on the branch itself, and deleting it while it is |
| 1008 | // elsewhere would delete a commit no version records. undone is what the |
| 1009 | // refusal leaves undone. |
| 1010 | func (r *review) recordedTip(branch, undone string) bool { |
| 1011 | tip := r.ref("refs/remotes/origin/" + branch) |
| 1012 | switch { |
| 1013 | case tip == "": |
| 1014 | return r.refuse("no such branch on origin: " + branch + "; " + undone) |
| 1015 | case tip != r.c.head: |
| 1016 | return r.refuse(fmt.Sprintf("change %s v%s records the head %s, and origin/%s is at %s; %s. Ready the branch's head as a new version of %s to act on it", |
| 1017 | r.c.handle, r.c.version, r.c.head, branch, tip, undone, r.c.handle)) |
| 1018 | } |
| 1019 | return true |
| 1020 | } |