| 1 | #!/usr/bin/env bash |
| 2 | # valley — the S1 integrator's verbs, as a plain shell script. |
| 3 | # |
| 4 | # EVICTION CLAUSE (owner-ratified; the decision node is |
| 5 | # archive/.the-valley/decisions/dcr-74c3158-valley-cli-lifecycle.md). |
| 6 | # This tool is deliberately a shell script: it runs from any checkout with |
| 7 | # nothing but git and coreutils — no machinery, the portability stance of |
| 8 | # ida-b9f646c. It GRADUATES to its own repo and a real language the moment |
| 9 | # ANY tripwire is crossed: |
| 10 | # - it grows past ~300 lines; |
| 11 | # - it needs persistent state, config, or a daemon |
| 12 | # (CROSSED by review's notes, which outlive the session in |
| 13 | # $XDG_STATE_HOME/valley/notes.); |
| 14 | # - it needs dependencies beyond git + coreutils |
| 15 | # (CROSSED by Phase 1's tail/replay, which need the nats CLI, and again |
| 16 | # by checks, which needs the cue CLI, again by review's [a]pprove, which |
| 17 | # needs the attest helper, and again by status, ready, unready and |
| 18 | # withdraw, which are a Go program (status/) this script runs — |
| 19 | # graduation is now due; recording the decision node is part of taking it.); |
| 20 | # - its behavior gets subtle enough to need real tests |
| 21 | # (CROSSED by checks: glob matching is pinned by the policy-deriver |
| 22 | # flake check, not by reading the code.); |
| 23 | # - it needs nontrivial parsing or concurrency |
| 24 | # (CROSSED by checks: glob-to-regex translation and -z diff parsing.). |
| 25 | # The size tripwire (~300 lines) is crossed too. Which way to take the |
| 26 | # graduation is dcr-2aa1a12, still `proposed`. |
| 27 | # Graduation is a deliberate act, recorded as a decision node. |
| 28 | # Precedent: klaus — born in cosmo, graduated deliberately. |
| 29 | |
| 30 | set -euo pipefail |
| 31 | |
| 32 | # Every verb reads the repository's own objects. A replacement ref makes one |
| 33 | # object stand in for another wherever git looks it up, so a fetched or |
| 34 | # planted refs/replace/* could change the diff a reviewer reads, the policy |
| 35 | # a derivation composes, and the tree a request is filed for. git is told |
| 36 | # to ignore them, whatever configuration says, and the variables that point |
| 37 | # it at other objects or at another replacement namespace are dropped. What |
| 38 | # chooses the repository and the person's own configuration are kept: a |
| 39 | # fetch of the owning valley needs that configuration, and neither changes |
| 40 | # what an object id reads as once replacement refs are off. |
| 41 | export GIT_NO_REPLACE_OBJECTS=1 |
| 42 | unset GIT_REPLACE_REF_BASE GIT_GRAFT_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES |
| 43 | |
| 44 | # The version the package stamped on this script when nix built it |
| 45 | # (nix/packages.nix): the CLI's version and the commit it was built from. |
| 46 | # A script run straight from a checkout has none. It is taken here and |
| 47 | # unset, so no program this script runs inherits a stamp that is not its |
| 48 | # own; self-update is handed it explicitly. |
| 49 | VALLEY_STAMP="${VALLEY_VERSION:-}" |
| 50 | unset VALLEY_VERSION |
| 51 | |
| 52 | die() { |
| 53 | printf 'valley: %s\n' "$*" >&2 |
| 54 | exit 1 |
| 55 | } |
| 56 | |
| 57 | usage() { |
| 58 | cat <<'EOF' |
| 59 | valley — S1 integrator verbs for a valley project |
| 60 | |
| 61 | usage: valley <verb> [args] |
| 62 | |
| 63 | pending [--all] [--repo <name>] [--checks] [--json] [-v] |
| 64 | list what is waiting to land, in three sections: ready, |
| 65 | the ready changes whose active version has not landed; |
| 66 | branches, each branch on origin that main does not |
| 67 | reach and that is no change's active head; and drafts, |
| 68 | the changes whose record says they are drafts. each |
| 69 | entry is a block. its first line is its name, with its |
| 70 | head's age and author: handle and version for a change, |
| 71 | the branch for a branch, and handle and version on its |
| 72 | branch for a draft. under it come its title (the |
| 73 | record's for a change, followed by the first linked |
| 74 | knot's id; the subject of its first own commit for a |
| 75 | branch), then the commits and the diff it |
| 76 | adds to its base: the nearest other entry its head |
| 77 | strictly contains, or main. a stack is its top entry, |
| 78 | then every entry the top contains, listed under it at |
| 79 | one indentation, each before the entries it contains |
| 80 | (ancestry alone: approving the top lands everything |
| 81 | listed under it, and the top says how many). each |
| 82 | listed entry is on the one listed after it, or says |
| 83 | `on <base>`, as at a fork. a listed entry is marked |
| 84 | when it is from another section. a |
| 85 | change also says `includes <handle>` for a change its |
| 86 | head contains that is no entry, such as a withdrawn |
| 87 | one, and `uncertain` when the version may have landed |
| 88 | without its evidence being stored. the stack with the |
| 89 | latest commit comes first. lines fit the terminal, or |
| 90 | 80 columns off one; titles are cut short with …, names |
| 91 | never. names are bold on a terminal unless NO_COLOR is |
| 92 | set. |
| 93 | --all every project the serving host |
| 94 | publishes, each headed by its name (see |
| 95 | changes below) |
| 96 | --repo NAME that one project |
| 97 | --checks add a line to each change with the |
| 98 | checks the policy requires of it, by |
| 99 | cost, as checks derives them |
| 100 | --json print valley-pending/v1: the |
| 101 | valley-changes/v1 shape, with the |
| 102 | pending changes in changes and the |
| 103 | drafts in drafts |
| 104 | -v warn on stderr when a plan graph is |
| 105 | missing or unreadable |
| 106 | needs the valley-go helper. |
| 107 | changes --json [--all] [--repo <name>] [-v] |
| 108 | print every change origin holds a record of, in every |
| 109 | state, as one JSON object, valley-changes/v1, for front |
| 110 | ends to read (docs/architecture.md): each version's |
| 111 | head, title and branch, how far it has landed |
| 112 | (landed, landed-recorded, uncertain or not-landed), |
| 113 | its pending request and its last outcome, the knots in |
| 114 | refs/the-valley/knots that link it |
| 115 | (valley:<project>/<change id>, active first), and |
| 116 | the branches no change is on. -v warns on stderr when a |
| 117 | plan graph is missing or unreadable. |
| 118 | handles are unique across |
| 119 | everything listed: a change id recorded in more than |
| 120 | one repository listed has the handle <repo>:<prefix> |
| 121 | in each. without --all it covers this |
| 122 | repository. --all covers every project the serving |
| 123 | host lists at refs/the-valley/projects in its instance |
| 124 | repository, which policy/valley names at main's tip on |
| 125 | origin; each project's URL is the instance's with its |
| 126 | last path segment replaced, and is read through a clone |
| 127 | kept under $XDG_CACHE_HOME/valley/repos. a url that |
| 128 | opens with a dash, names a <transport>:: helper such |
| 129 | as ext::, or has a scheme other than ssh, git, https or |
| 130 | file is refused. a repository that cannot be read is |
| 131 | listed with its error. --repo limits it to one |
| 132 | project. landed means on main's first-parent history |
| 133 | with no merge above it. landed-recorded needs a landed |
| 134 | commit with one parent and the integrator's transfer |
| 135 | statement for the version's request name, verified; |
| 136 | the integrator's outcome record for that name saying |
| 137 | land at the version's head; and the integrator's merge |
| 138 | of the head onto the landed commit's parent giving the |
| 139 | landed tree. the statement is verified with these: |
| 140 | VALLEY_KNOWN_SIGNERS names the registry's known-signers |
| 141 | file and VALLEY_INTEGRATOR_SIGNER the integrator's |
| 142 | signing name, and attest checks the note. |
| 143 | needs the valley-go helper. |
| 144 | review [-v] <change> |
| 145 | page through the change's branch: its log and diff vs |
| 146 | origin/main, or a stack layer by layer (below). above |
| 147 | each change's diff, review shows every knot in the plan |
| 148 | graph that links the change, as `does oc-6335749 (open): |
| 149 | <title>` (valley-go does). a missing or unreadable graph |
| 150 | shows no knots; -v warns on stderr. then choose: |
| 151 | [a]pprove file an integration request for the |
| 152 | head shown. runs the |
| 153 | checks the composed policy requires of |
| 154 | this change over a throwaway worktree of |
| 155 | that head, signs each result as an |
| 156 | attestation, and publishes the evidence |
| 157 | and the request ref to origin in ONE |
| 158 | atomic push. this files the request; |
| 159 | it does not wait for integration or |
| 160 | update your local checkout. use status |
| 161 | to read the result. the policy comes |
| 162 | from the target's tip and the owning |
| 163 | valley's, as checks describes below, |
| 164 | so your checkout |
| 165 | can be on any branch. a change the policy |
| 166 | requires no check of is filed too, as a |
| 167 | request with no evidence, and says so. |
| 168 | approving a change already requested is |
| 169 | how it is filed again. at a head rebased |
| 170 | onto a moved main it replaces the request |
| 171 | ref under a lease and names the head it |
| 172 | replaced; at the same head it leaves the |
| 173 | ref alone and publishes only the new |
| 174 | evidence, which the create-only |
| 175 | attestation namespace refuses when this |
| 176 | signer has already attested that tree |
| 177 | (bd-7b7d58f). every write of the request |
| 178 | ref is leased, filing included, so an |
| 179 | approval that lost a race is refused |
| 180 | rather than clobbering the one that won. |
| 181 | needs the attest helper and |
| 182 | VALLEY_ATTEST_KEY, the ed25519 key this |
| 183 | host signs with, whenever a check is |
| 184 | required; VALLEY_ATTEST_NAME is the name it |
| 185 | signs under, defaulting to this host's. |
| 186 | [v]iew full diff |
| 187 | page the change's whole diff, with |
| 188 | notes taken as below, and ask again |
| 189 | [r]eject delete the remote branch (confirmed first) |
| 190 | [s]kip do nothing |
| 191 | a change with a record is first set against its |
| 192 | approved version: the greatest version below the one |
| 193 | shown whose exact head an integration request or a |
| 194 | land, stale or reject outcome on origin names, under |
| 195 | c-<change id> or a branch name of the change that |
| 196 | nothing else maps to (valley-go compare). with none, |
| 197 | or an ambiguous one, review pages the whole diff. when |
| 198 | each version's diff from its merge base with main |
| 199 | changes the same files the same way (valley-delta-v1), |
| 200 | review prints `<change> v<n> at <head>: clean restack |
| 201 | of approved v<m> at <old head>.`, the two bases, and |
| 202 | the commits main landed between them, and pages |
| 203 | nothing. otherwise it pages `changed since approved |
| 204 | v<m>`, the files whose change differs, git |
| 205 | range-diff of the two versions, and the shown |
| 206 | version's diff of those files. a changed commit |
| 207 | message or author is shown either way. this is |
| 208 | information only: it files nothing and skips no check. |
| 209 | when [a]pprove's checks fail, the prompt says approving |
| 210 | needs a new head and stops offering [a]pprove until the |
| 211 | change has one. any other refusal keeps the prompt as |
| 212 | it was. |
| 213 | [a]pprove files the head shown, for a branch no record |
| 214 | is on too: it refuses, naming both heads, when the |
| 215 | branch on origin has moved off that head since. |
| 216 | [a]pprove is the only way a change reaches main from |
| 217 | here, and approve below is the same act without the |
| 218 | pager. |
| 219 | Nothing in this script writes the protected ref: the |
| 220 | integrator does, having judged the request. |
| 221 | a stale branch (won't fast-forward from main) is first |
| 222 | offered: |
| 223 | [b]ase rebase it onto origin/main, as rebase |
| 224 | does below, then re-review the diff that |
| 225 | would actually land |
| 226 | [a]pprove is offered there too: a stale branch is nothing |
| 227 | the integrator cannot apply, so it need not be rebased |
| 228 | first. |
| 229 | a change whose record is not ready says so above its |
| 230 | diff, and again in place of the prompt, which then |
| 231 | offers only [v]iew full diff and [s]kip: |
| 232 | `<change> v<n> is a draft: its owner (<owner>) hasn't |
| 233 | marked it ready, so it can't be approved yet.` |
| 234 | `<change> is withdrawn: there is nothing to approve.` |
| 235 | a draft is often a change an agent is still working on. |
| 236 | [a]pprove would be refused, and so would [b]ase, since |
| 237 | adding the rebased head as a version would ready the |
| 238 | change behind its owner's back. [r]eject records |
| 239 | nothing on the change: it would only delete a branch |
| 240 | its owner may still be working on. a change unreadied |
| 241 | or withdrawn while the review is open is still refused |
| 242 | by [a]pprove and [b]ase, which read the record again. |
| 243 | a branch no record is on has no state, and is offered |
| 244 | the usual prompt. |
| 245 | while paging, press v: less opens the diff in $EDITOR. |
| 246 | every line you insert there is a note. it is anchored to |
| 247 | the file and new-side line it sits on — to the file alone |
| 248 | when it sits above the first hunk. range-diff gives no |
| 249 | line numbers, so on a changed version a note keeps its |
| 250 | file and line only in the shown version's diff at the |
| 251 | end of the page. each note is then appended to |
| 252 | $XDG_STATE_HOME/valley/notes/<branch>.md (default |
| 253 | ~/.local/state) and printed after the verdict. notes |
| 254 | survive [b]ase. lines you change or delete instead of |
| 255 | inserting are ignored, with a warning. |
| 256 | a change whose active version names no branch is |
| 257 | refused, since notes and [r]eject act on a branch; |
| 258 | approve files its head without one. |
| 259 | a stack is reviewed one layer at a time, bottom first. |
| 260 | its layers are what pending lists under the change: the |
| 261 | changes not landed and the branches no record is on |
| 262 | that its head contains (valley-go stack). review first |
| 263 | lists them, warns of a lower layer that is a draft or |
| 264 | withdrawn, since the integrator lands nothing above it, |
| 265 | and says how the review goes: [n] through the layers, |
| 266 | and approving the top lands them all. a stack whose |
| 267 | bottom does not fast-forward from main is then asked, |
| 268 | before any layer is paged, as in `one, at the bottom, |
| 269 | does not fast-forward from main:`: |
| 270 | [b]ase all <n> |
| 271 | rebase the whole stack onto origin/main, |
| 272 | as rebase does below, then list it again |
| 273 | and page the rebased layers, so the |
| 274 | heads paged are the heads approved |
| 275 | [s]top end the review; nothing done |
| 276 | a rebase that is refused or left unfinished says what |
| 277 | happened, and then only [s]top is offered. |
| 278 | it then shows each layer, headed `change <k> of <n>: |
| 279 | <name> on <below>`, as a change alone is shown: set |
| 280 | against its own change's approved version, or its log |
| 281 | and diff against the layer below it paged, with notes |
| 282 | kept under that layer's branch. |
| 283 | after each lower layer it asks, naming the layer and |
| 284 | its place, as in `two, 2 of 3:`: |
| 285 | [n]ext layer go on to the layer above; the usual |
| 286 | answer |
| 287 | [a]pprove approve that layer at the head shown, |
| 288 | as review of it would. this lands it |
| 289 | and every layer under it, which the |
| 290 | prompt names (`one alone`, `one and |
| 291 | two`, `the <k> up to <name>`), and |
| 292 | ends the review |
| 293 | [v]iew full diff |
| 294 | page that layer's log and diff against |
| 295 | the layer below it, and ask again |
| 296 | [s]top end the review; nothing done |
| 297 | after the top it asks, as in `three, 3 of 3:`: |
| 298 | [a]pprove all <n> |
| 299 | approve the top at the head shown, |
| 300 | which lands every layer |
| 301 | [v]iew full diff |
| 302 | page the top's log and diff against |
| 303 | the layer below it, and ask again |
| 304 | [r]eject <name> |
| 305 | delete the top's branch (confirmed |
| 306 | first) |
| 307 | [s]top end the review; nothing done |
| 308 | only the top is offered [r]eject. when a layer's checks |
| 309 | fail, its prompt says approving needs a new head and |
| 310 | stops offering [a]pprove, as for any change. a layer |
| 311 | that is not ready says so as a change alone does, under |
| 312 | its heading and in its prompt. neither it nor any layer |
| 313 | above it is offered [a]pprove, since approving one would |
| 314 | land it: each prompt above it says `it would bring |
| 315 | <name>, change <k> of <n>, which is a draft` (or |
| 316 | withdrawn) in its place. a top that is not ready is not |
| 317 | offered [r]eject either. every layer's notes are printed |
| 318 | at the end. |
| 319 | approve [--head <sha>] <change> |
| 320 | do what review's [a]pprove does for the change, without |
| 321 | paging it: the same checks, the same refusals, the same |
| 322 | leased request write and the same output. exits non-zero |
| 323 | when it refuses. a change with a record is approved at |
| 324 | exactly one head, and its request is filed as |
| 325 | refs/the-valley/integration-requests/main/c-<change id>, |
| 326 | at that head: |
| 327 | --head SHA approve exactly this head, which must be |
| 328 | one of the record's version heads, |
| 329 | active or not. a front end passes the |
| 330 | head it showed, so a record that moves |
| 331 | after review cannot change what lands. |
| 332 | without --head, approve shows the active version's head |
| 333 | and title and asks to confirm them, on a terminal; off |
| 334 | one it refuses. a branch no record is on is approved at |
| 335 | its tip on origin, which --head must name if it is |
| 336 | given (review's [a]pprove files the head it showed, and |
| 337 | refuses once the branch on origin has moved off it), under |
| 338 | the branch's request name: the branch with |
| 339 | each / replaced by -. a branch whose request name would |
| 340 | be c-<change id> is refused, since that name is the |
| 341 | change's. |
| 342 | approve refuses, before any check runs, a head that does |
| 343 | not fast-forward from main and would bring a version of |
| 344 | another change that has not landed: a layer above the |
| 345 | bottom of a stale stack. the refusal names the command |
| 346 | that rebases the stack, valley rebase <change>. this is |
| 347 | the project's choice, not a rule the integrator needs: |
| 348 | approved as it is, the stack would land as one commit |
| 349 | recorded against this request alone, so the changes |
| 350 | under it would have no landing record of their own, and |
| 351 | the checks would run over a head that has to be rebased |
| 352 | anyway. a stale head with no change under it, such as |
| 353 | the bottom layer of a stack or a change alone, is |
| 354 | approved as it is, and the integrator merges it onto |
| 355 | main. |
| 356 | approve prints how long each required check took, and |
| 357 | appends one JSON line per check to the timing log, |
| 358 | $XDG_STATE_HOME/valley/timings.jsonl (default |
| 359 | ~/.local/state/valley/timings.jsonl). the log stays on |
| 360 | this machine and is not evidence; a log that cannot be |
| 361 | written is a warning, and approving goes on. |
| 362 | rebase <change> rebase the change's whole stack onto origin/main, as |
| 363 | review's [b]ase does: the layers valley-go stack lists |
| 364 | under the change, and the change itself. in a throwaway |
| 365 | worktree, bottom first, each layer's own commits go onto |
| 366 | the rebased layer below it, and the bottom's onto |
| 367 | origin/main, so each layer keeps its own diff. commits |
| 368 | under the bottom that a lower change landed through the |
| 369 | integrator drop out, since main holds them; that landing |
| 370 | is read as changes reads it, with VALLEY_KNOWN_SIGNERS |
| 371 | and VALLEY_INTEGRATOR_SIGNER. one atomic |
| 372 | push then moves every layer's branch, each leased on the |
| 373 | tip read: every branch moves, or none does. last, bottom |
| 374 | first, each change gains its rebased head as a new |
| 375 | version, as valley.principal names, the way ready |
| 376 | --if-active does: only while the change is still ready |
| 377 | at the head the rebase began with. |
| 378 | it refuses, with nothing changed, a stack holding a |
| 379 | change that is not ready, records no branch, or whose |
| 380 | branch is missing on origin or is not at its recorded |
| 381 | head; a plain branch that is the branch of a change not |
| 382 | in the stack; two layers on one branch; a merge among the |
| 383 | commits to be rebased, or a version head of a change |
| 384 | that is not ready or not in the stack, since a rebase |
| 385 | gives each commit a new id; and a stack holding a change |
| 386 | while git config valley.principal names no one. a |
| 387 | conflict in any layer pushes nothing, records nothing |
| 388 | and names the layer. |
| 389 | a change alone prints its new head. a |
| 390 | stack prints a manifest: one line for each layer, with |
| 391 | its old and new head, whether its branch was pushed, and |
| 392 | whether a version was added. then, for each change left |
| 393 | unfinished, the valley ready command that finishes it, |
| 394 | and the exit status is non-zero. rebasing again refuses |
| 395 | a change whose branch holds a head no version records, |
| 396 | and prints the same command. last, it names each ready |
| 397 | change or branch outside the stack that is left on an |
| 398 | old head of a layer, such as a second stack on a lower |
| 399 | layer, with the rebase that moves it; it does not move |
| 400 | it. rebase does not re-review the result. a stack that |
| 401 | already fast-forwards from main is left as it is. |
| 402 | status [--json] [-v] <change> |
| 403 | read the request and the integrator's last decision from |
| 404 | origin. distinguish waiting, a decision that needs |
| 405 | attention, and a change integrated into origin/main. |
| 406 | a change with a record is read at its active version's |
| 407 | head, under c-<change id>, and the status says the |
| 408 | version, its title, the owner, and how far the version |
| 409 | has landed, as changes reads it. a refusal for a record |
| 410 | that is not ready, or for a merge in the change's |
| 411 | commits, is a state of its own. |
| 412 | after integration, show how to update the local checkout. |
| 413 | fetches objects without moving local branches or files; |
| 414 | no checks run and no request is filed. |
| 415 | --json print the status as one JSON object, |
| 416 | valley-status/v1, for front ends to read |
| 417 | (docs/architecture.md), with the knots |
| 418 | in the plan graph that link the change. |
| 419 | an error prints no object: it goes to |
| 420 | stderr and the exit status is non-zero. |
| 421 | -v with --json, warn on stderr when the plan |
| 422 | graph is missing or unreadable |
| 423 | needs the valley-go helper. |
| 424 | ready [--head <sha>] [--change <id>] [--if-active <sha>] [--stamp] [<branch>] |
| 425 | mark a head of the branch ready, as a new version of its |
| 426 | change, in the change's record on origin: |
| 427 | refs/the-valley/changes/main/<change id>. the branch |
| 428 | defaults to the current one. the head is --head, or else |
| 429 | origin/<branch>, and it must be on origin already. the |
| 430 | first ready of a change makes v1; each later one adds |
| 431 | the next version, at a head other than the active |
| 432 | version's. a draft or a withdrawn change comes back this |
| 433 | way: at the active version's head, it is ready again at |
| 434 | that version, and at another head it gains the next |
| 435 | version. prints the handle and version, e.g. |
| 436 | `cosmo nxvp v2 ready`. |
| 437 | the change id is --change, else the head commit's jj |
| 438 | change-id header, else a Change-Id: trailer in its |
| 439 | message, else a new one, which is printed. a header and |
| 440 | a trailer that disagree are refused. so is a change |
| 441 | whose commits from its merge base with origin/main hold |
| 442 | a merge, or that has more than one merge base. |
| 443 | --if-active SHA |
| 444 | add the version only while the change |
| 445 | is ready at an active version whose head |
| 446 | is SHA. the record ref is leased on the |
| 447 | record read, so nothing comes between |
| 448 | that check and the write. rebase readies |
| 449 | the head it rebased this way. |
| 450 | --stamp amend the head of your local branch to |
| 451 | carry the id as a Change-Id: trailer and |
| 452 | push the branch, leased, before marking it |
| 453 | ready. it only starts a new change: it |
| 454 | is refused for a change that has a |
| 455 | record on origin already, and for a head |
| 456 | already on origin. |
| 457 | the record names the principal your key pushes as, read |
| 458 | from git config valley.principal; the host refuses one |
| 459 | naming anyone else. the record ref is pushed leased on |
| 460 | the value read. a new change is owned by the principal |
| 461 | that readies it, and a later version may come from its |
| 462 | owner, the operator, or a restack holder while the |
| 463 | change is ready. |
| 464 | a record never makes anything land, and approve is |
| 465 | still the only way to main. the integrator reads |
| 466 | records only to refuse: it lands no request that would |
| 467 | bring a version of a change that is not ready. |
| 468 | needs the valley-go helper. |
| 469 | unready <change> |
| 470 | make a ready change a draft in its record on origin, as |
| 471 | its owner or the operator. a draft is a pause: it voids |
| 472 | nothing, and approvals of its active version stand when |
| 473 | it is readied again. ready brings it back. prints e.g. |
| 474 | `cosmo nxvp v2 draft`. needs the valley-go helper. |
| 475 | withdraw <change> |
| 476 | mark a ready change or a draft withdrawn in its record |
| 477 | on origin, as its owner or the operator. withdrawing |
| 478 | voids everything outstanding on the change: its |
| 479 | integration requests, and the approvals and reviews of |
| 480 | every version, so a change brought back needs fresh |
| 481 | approvals. ready brings it back, at the same version |
| 482 | or a new one. prints e.g. `cosmo nxvp v2 withdrawn`. |
| 483 | needs the valley-go helper. |
| 484 | the integrator lands no request that would bring a |
| 485 | version of a change that is not ready, so unready and |
| 486 | withdraw both stop a |
| 487 | pending integration request, under c-<change id> or a |
| 488 | branch's request name. each says so when origin holds |
| 489 | one. a draft's request stands and lands once the change |
| 490 | is ready again. withdrawing voids the request: the |
| 491 | integrator deletes it on its next pass while the change |
| 492 | is still withdrawn. withdraw warns, since a ready before |
| 493 | then leaves the request standing: only a holder of the |
| 494 | approve grant (also accepted as request) may touch a |
| 495 | request ref, so the operator must drop it then. |
| 496 | knot <verb> [args] |
| 497 | read and write the project's plan graph: knots, at |
| 498 | refs/the-valley/knots on origin. a knot is an outcome, |
| 499 | a bug, an idea or a question; `knot schema` prints the |
| 500 | types, their statuses, active and terminal, the fields |
| 501 | and the edges. a write is one commit on the ref, made |
| 502 | without a checkout and pushed with a lease on the tip |
| 503 | it was made on; no review is involved. a write that |
| 504 | loses the lease is made again on the new tip, up to 5 |
| 505 | tries. every verb takes --project NAME, another |
| 506 | project on the same host, and --json. exit status 3 |
| 507 | is a conflict. needs the valley-go helper, and |
| 508 | git config valley.principal for every write. |
| 509 | new <type> <title> [--parent ID] [--blocked-by ID]... |
| 510 | [--owner P] [--target PROJECT] [--body -] |
| 511 | make a knot. its id is the type's prefix |
| 512 | and 7 hex digits of the sha-256 of the |
| 513 | title's slug. |
| 514 | show <id> [--json] |
| 515 | its fields, body and comments, and the |
| 516 | edges both ways |
| 517 | list [--type T] [--status S] [--owner P] [--ready] |
| 518 | every knot. --status takes a status, |
| 519 | active or terminal. --ready lists the |
| 520 | active, unclaimed knots whose blockers |
| 521 | are all terminal. |
| 522 | set <id> <field>=<value>... [--if <field>=<value>]... |
| 523 | [--force] |
| 524 | change status, title, source, owner, |
| 525 | target or graduated_into. a field that |
| 526 | another write changed meanwhile is a |
| 527 | conflict unless --force. --if requires |
| 528 | a field to hold a value first. |
| 529 | link <id> (--blocked-by ID | --parent ID | --change X)... |
| 530 | unlink <id> (--blocked-by ID | --parent ID | --change X)... |
| 531 | add or drop an edge. a change is |
| 532 | valley:<project>/<change id> or a github |
| 533 | pull request url. outcomes and bugs link |
| 534 | the changes that do the work, and a |
| 535 | question links the change that records |
| 536 | its answer in the source. |
| 537 | edit <id> --body - |
| 538 | replace the body with stdin |
| 539 | comment <id> (-m TEXT | --body -) |
| 540 | add a comment. comments are never |
| 541 | changed or deleted. |
| 542 | claim <id> --run RUN |
| 543 | claim an active outcome or bug for a |
| 544 | run; an open outcome becomes |
| 545 | in-progress. a knot claimed already is |
| 546 | a conflict. |
| 547 | unclaim <id> |
| 548 | release the claim. a claim that |
| 549 | changes before the release lands is |
| 550 | a conflict. |
| 551 | schema [--json] |
| 552 | print the model |
| 553 | plan [--project NAME | --all] [--owner P] [--json] |
| 554 | show the project's plan graph as a tree, to see where |
| 555 | things stand. the roots are the outcomes with no |
| 556 | parent, the milestones, each with its progress: its |
| 557 | descendants that are terminal and not abandoned, out |
| 558 | of those not abandoned, as a count and a bar. under |
| 559 | each come its children, indented, in the order they |
| 560 | were created, then by title. each knot has a glyph for |
| 561 | its state (○ ready, ◐ in progress, ⊘ blocked, ◇ |
| 562 | active, ✓ done, ✗ abandoned), its id and title, and, |
| 563 | while active, its claim and the claim's age (marked |
| 564 | stale past a day), its owner, and what blocks it. a |
| 565 | blocker that ended abandoned, discarded, superseded or |
| 566 | dropped clears the knot, and is marked with how it |
| 567 | ended. a reference to another project's knot, |
| 568 | <project>:<id>, is marked dangling when that |
| 569 | project's graph cannot be read or holds no such knot. |
| 570 | a root whose descendants are all terminal is one |
| 571 | line. three sections follow. waiting on you: the |
| 572 | active outcomes and bugs and the open questions the |
| 573 | operator owns, then the changes ready for |
| 574 | approval, as pending lists them. in flight: the |
| 575 | claimed knots, oldest claim first. open residuals: |
| 576 | the open bugs, under their parent. the operator is |
| 577 | git config valley.principal, or --owner. lines fit |
| 578 | the terminal as pending's do; glyphs are coloured on |
| 579 | a terminal unless NO_COLOR is set. |
| 580 | --project NAME another project on the same host |
| 581 | --all every project the serving host |
| 582 | publishes, each under its name, as |
| 583 | pending --all reads them. a project |
| 584 | that cannot be read is reported in |
| 585 | its own section |
| 586 | --owner P read P as the operator |
| 587 | --json print valley-plan/v1: every knot with |
| 588 | what the graph derives of it, and the |
| 589 | three sections (docs/architecture.md) |
| 590 | needs the valley-go helper. |
| 591 | checks [<base> [<head>]] |
| 592 | print the check names the composed verification policy |
| 593 | (dcr-f41f718) requires of the change between two |
| 594 | commits, which class made each one required, and the |
| 595 | change's carry mode: how far an approval of it carries |
| 596 | across a rebase, the strictest of exact, same-class and |
| 597 | same-paths among the classes it matched. It prints: no |
| 598 | check is run, nothing is gated, nothing is written. |
| 599 | base defaults to the merge base with main, head to HEAD. |
| 600 | by default both layers come from the target's tip |
| 601 | (origin/main), never from your checkout: the project's |
| 602 | own layer is policy/project there, and the owning valley |
| 603 | is named one line at policy/valley there, fetched and |
| 604 | read at policy/instance in that repository's integrated |
| 605 | tip. a valley's own config repository carries |
| 606 | policy/instance and uses it. a target carrying no |
| 607 | policy/project is derived under the floor alone; a floor |
| 608 | that is not there is refused. this is what the |
| 609 | integrator reads, so the answer is the same from any |
| 610 | branch, however stale. an option overrides each layer, |
| 611 | which is the only way to derive against a layer that |
| 612 | does not govern — the worked example under |
| 613 | examples/policy/, say: |
| 614 | --schema FILE default $VALLEY_VERIFICATION_SCHEMA |
| 615 | --instance DIR default the owning valley's floor |
| 616 | --project DIR default policy/project at the target |
| 617 | naming a layer stops this reading the source it |
| 618 | replaced. the schema is the shape a policy is written |
| 619 | in rather than a layer, so it ships with this tool: the |
| 620 | package sets VALLEY_VERIFICATION_SCHEMA to its own copy. |
| 621 | with that unset, as when this script runs straight from |
| 622 | a the-valley checkout, it is schema/verification.cue in |
| 623 | the checkout. |
| 624 | a class's globs match bytes, in the C locale whatever |
| 625 | yours is: `?` is one byte, so `?.md` does not match |
| 626 | é.md, whose é is two. |
| 627 | after the report, two lines group the required checks |
| 628 | by the cost the policy declares, `quick` or `slow`. a |
| 629 | slow check takes minutes or needs special hardware. the |
| 630 | cost changes nothing required: approving runs every |
| 631 | required check whatever its cost, and the integrator |
| 632 | requires evidence for every one. |
| 633 | --names print only the required check names, |
| 634 | one per line |
| 635 | --cost COST with --names, only the quick or |
| 636 | the slow ones, e.g. |
| 637 | `valley checks --names --cost slow` |
| 638 | tail [subject] watch bus events scroll by (default subject: valley.>); |
| 639 | the server comes from the nats CLI's own config |
| 640 | (NATS_URL or a nats context; default localhost) |
| 641 | replay [dir] publish one ref-updated event per current ref of the |
| 642 | repo at dir (default: .) — rebuilds the stream's |
| 643 | projection, e.g. after nats stream purge valley |
| 644 | self-update [-v] [--retry | --rollback | --pin | --unpin] |
| 645 | [--repo <url>] [--branch <name>] |
| 646 | install the tip of a branch of the-valley into your |
| 647 | default nix profile, in place of its valley element, |
| 648 | once that exact commit builds and passes a smoke test. |
| 649 | a commit that fails is skipped until the branch moves, |
| 650 | or until --retry. it is off until --repo or |
| 651 | git config valley.selfUpdate.repo names the |
| 652 | repository; the branch is valley.selfUpdate.branch, |
| 653 | or main. --rollback reinstalls the previous build, and |
| 654 | --pin and --unpin freeze and release the install. |
| 655 | `valley help self-update` gives every step. needs the |
| 656 | valley-go helper and nix. |
| 657 | --version print this valley's version: the package's version and |
| 658 | the commit it was built from, e.g. 0.1.0-97db597, with |
| 659 | -dirty after it for a tree with uncommitted changes and |
| 660 | -unknown in its place when nix knew no commit. run |
| 661 | straight from a checkout, it says it is unstamped. |
| 662 | help [self-update] |
| 663 | show this help, or self-update's |
| 664 | |
| 665 | A change is named by its full change id, by its handle — the shortest |
| 666 | prefix of the id, four letters at least, that no other change recorded on |
| 667 | origin shares, written <repo>:<handle> too, with this repository's name — |
| 668 | or by its branch. A name written origin/<branch> is always |
| 669 | a branch. A handle that is also a branch on origin is refused unless the |
| 670 | change is on that branch. review, approve and rebase read a branch name, |
| 671 | written either way, against every record on origin: when exactly one |
| 672 | record's active version is on the branch, the name is that change, as if |
| 673 | its id were given, and when more than one's is, the verb refuses and lists |
| 674 | their ids. A change whose active version has landed — its head is on |
| 675 | main's first-parent history with no merge above it, or the integrator's |
| 676 | outcome record and verified transfer statement record that exact head |
| 677 | landing — is skipped, so a branch can be used again once its change has |
| 678 | landed. unready, withdraw and status read a branch the same way. A |
| 679 | branch no record's active version is on is acted on as a branch. A |
| 680 | change with a record is reviewed, approved and rebased at the |
| 681 | head its active version records, never at its branch's tip: review shows |
| 682 | that head and its [a]pprove approves it, and rebase and review's |
| 683 | [r]eject refuse, naming both, when the branch's tip on origin is another |
| 684 | commit. approve approves the head bound to it, wherever the branch is. |
| 685 | [r]eject deletes the branch under a lease on the recorded head. approve, |
| 686 | rebase, and review's [a]pprove and [b]ase refuse a change whose record is |
| 687 | not ready, naming its state, and review offers none of them for a change |
| 688 | that is not ready when it shows it. Each reads the record again just before it |
| 689 | acts, and again before approve files or rebase pushes, and refuses when |
| 690 | the change is not ready. rebase also refuses when the change's |
| 691 | record has changed at all since it began, and approve when the head it |
| 692 | approves is not a version's. A name that read as a plain branch is |
| 693 | read again before approve files or rebase pushes. If it names a change by |
| 694 | then, rebase refuses, and approve files only while the change is ready at |
| 695 | the head it began with. An approval already past that last check can |
| 696 | still file if the change is unreadied or withdrawn in the moment before |
| 697 | its push; the integrator refuses to land it, since it lands no request |
| 698 | that would bring a version of a change that is not ready, whatever the |
| 699 | request is named. |
| 700 | The integrator reads the records last just before it moves main, so a |
| 701 | change paused in that moment can still land at the head the operator |
| 702 | approved, and never at any other. |
| 703 | |
| 704 | Runs from any checkout of the project; needs only git and coreutils, |
| 705 | except tail and replay, which also need the nats CLI (natscli), checks, |
| 706 | which also needs the cue CLI, review's notes, which need less, approve |
| 707 | and review's [a]pprove, which need both the cue CLI and the attest |
| 708 | helper, and status, ready, unready, withdraw, review, approve and rebase, |
| 709 | which need the valley-go helper, as pending, changes, knot, plan and |
| 710 | self-update do. nix run .#valley bundles all of them. self-update also |
| 711 | needs nix. Deriving a policy fetches the owning valley's repository, so |
| 712 | checks and approving also need whatever reaching it takes. |
| 713 | No config but valley.principal, which ready, rebase, knot and plan read, |
| 714 | and valley.selfUpdate.repo and .branch, which self-update reads; no batch |
| 715 | mode; and no state but the notes file, the handle cache pending and |
| 716 | changes write for completion, and self-update's record of what it |
| 717 | installed — the judgment stays with the human. |
| 718 | EOF |
| 719 | } |
| 720 | |
| 721 | fetch_origin() { |
| 722 | git fetch --prune --quiet origin |
| 723 | } |
| 724 | |
| 725 | # Resolve the pager as git would for a diff: pager.diff first (per-command |
| 726 | # pager configs — e.g. home-manager's delta module — set pager.diff and leave |
| 727 | # core.pager alone), then git var GIT_PAGER (core.pager, GIT_PAGER, PAGER, |
| 728 | # less). pager.<cmd> may legally be a boolean; treat true/false — and an |
| 729 | # empty or missing value — as unset and fall back. The value is |
| 730 | # shell-interpreted, hence sh -c. |
| 731 | diff_pager() { |
| 732 | local pager |
| 733 | pager="$(git config --get pager.diff || true)" |
| 734 | case "$pager" in '' | true | false) pager="$(git var GIT_PAGER)" ;; esac |
| 735 | printf '%s' "$pager" |
| 736 | } |
| 737 | |
| 738 | # Page a file rather than a pipe, so less's v key has something to hand |
| 739 | # $EDITOR: with a real file open, v runs LESSEDIT, whose default |
| 740 | # `%E ?lm+%lm. %g` is the editor, +<line>, and the file name. That file is |
| 741 | # the review buffer, and editing it is how notes get written (see |
| 742 | # collect_notes). A pipe has no name, so less refuses to edit one — which is |
| 743 | # why review no longer pipes. |
| 744 | # |
| 745 | # The configured diff pager stays the renderer, but as less's input |
| 746 | # preprocessor rather than as the pager. It still reads the buffer on stdin |
| 747 | # exactly as it did when it was the pager, less displays what it emits, and |
| 748 | # less still calls the raw buffer "the current file" — so v edits the diff |
| 749 | # and not the rendering. The alternative was to page the buffer with no |
| 750 | # filter at all: exact, and it throws delta's rendering away, which is the |
| 751 | # worse trade for a verb whose whole job is reading a diff. |
| 752 | # |
| 753 | # What this costs is the line number. A filter that restructures the diff — |
| 754 | # delta's default rendering does — makes %lm a line of the rendered view, so |
| 755 | # the editor opens near the cursor rather than on it. A structure-preserving |
| 756 | # filter (pager.diff = `delta --color-only`) makes it exact. No anchor |
| 757 | # depends on it: a note is anchored by where it was inserted, not by where |
| 758 | # the editor opened. |
| 759 | page_file() { |
| 760 | local file="$1" pager |
| 761 | [ -t 1 ] || { cat -- "$file"; return; } # a non-tty just cats |
| 762 | pager="$(diff_pager)" |
| 763 | case "${pager%%[[:space:]]*}" in |
| 764 | less | */less) |
| 765 | # Already less: give it the file and there is no filter to run. |
| 766 | sh -c "$pager \"\$1\"" sh "$file" |
| 767 | return |
| 768 | ;; |
| 769 | esac |
| 770 | # Without less there is no editor key; fall back to filtering into a pipe. |
| 771 | command -v less > /dev/null 2>&1 || { |
| 772 | sh -c "$pager" < "$file" |
| 773 | return |
| 774 | } |
| 775 | # -R because the filter's color is the whole point of running it, and no |
| 776 | # LESSCLOSE because a stray one would run against a buffer we still read. |
| 777 | LESSOPEN="|$pager < %s" env -u LESSCLOSE less -R -- "$file" |
| 778 | } |
| 779 | |
| 780 | # pending is valley-go's (status/pending.go). The fetch first keeps this |
| 781 | # checkout's remote-tracking branches current, as pending always has. The |
| 782 | # helper runs valley checks for --checks, so it is told how this script was |
| 783 | # run. |
| 784 | cmd_pending() { |
| 785 | command -v valley-go > /dev/null 2>&1 \ |
| 786 | || die "pending needs the valley-go helper; nix run .#valley bundles it" |
| 787 | fetch_origin |
| 788 | VALLEY_COMMAND="$0" exec valley-go pending "$@" |
| 789 | } |
| 790 | |
| 791 | # For a change with a record, what is rejected is the head its record |
| 792 | # names. So the branch is deleted only while its tip is that head, and the |
| 793 | # delete is leased on it. When the tip is another commit, reject refuses |
| 794 | # and names both, and deletes nothing. A branch no record names is deleted |
| 795 | # as before. |
| 796 | # |
| 797 | # reject runs inside a condition, where bash suspends `set -e`, so the push |
| 798 | # is checked by hand. |
| 799 | reject() { |
| 800 | local branch="$1" answer |
| 801 | local -a lease=() |
| 802 | if [ -n "$CHANGE_HEAD" ]; then |
| 803 | check_recorded_tip "$branch" "nothing deleted" || return 1 |
| 804 | lease=("--force-with-lease=refs/heads/$branch:$CHANGE_HEAD") |
| 805 | fi |
| 806 | printf 'really delete origin/%s? [y/N] ' "$branch" |
| 807 | read -r answer || answer="" |
| 808 | case "$answer" in |
| 809 | y | Y | yes) |
| 810 | if ! git push --quiet "${lease[@]}" origin --delete "$branch"; then |
| 811 | refuse "origin did not delete $branch, which may have moved since the fetch; nothing deleted" |
| 812 | return 1 |
| 813 | fi |
| 814 | echo "rejected $branch: deleted from origin" |
| 815 | ;; |
| 816 | *) echo "kept $branch" ;; |
| 817 | esac |
| 818 | } |
| 819 | |
| 820 | # ---------------------------------------------------------------------- |
| 821 | # review notes — the lines the reviewer inserts into the paged diff. |
| 822 | # |
| 823 | # The buffer the reviewer pages is a file, so the pager's editor key can open |
| 824 | # it (page_file). Whatever comes back with lines inserted is read as notes: |
| 825 | # no marker, no syntax, no mode — an inserted line is a note because it was |
| 826 | # not there before. What a note is *about* is where it sits, so each one is |
| 827 | # anchored to the file and the new-side line number of the diff line above |
| 828 | # it, which is the pair a reviewer would otherwise have to copy out by hand. |
| 829 | |
| 830 | # The scratch directory holding the buffer and its pristine copy, and the |
| 831 | # notes taken so far in this review — a global because [b]ase pages a second |
| 832 | # time and the notes from the first pass are not thereby withdrawn. |
| 833 | REVIEW_TMP="" |
| 834 | NOTES=() |
| 835 | |
| 836 | cleanup_review_tmp() { |
| 837 | [ -n "$REVIEW_TMP" ] || return 0 |
| 838 | rm -rf "$REVIEW_TMP" |
| 839 | REVIEW_TMP="" |
| 840 | } |
| 841 | |
| 842 | # Where a branch's notes live. XDG_STATE_HOME is the right home for them: |
| 843 | # they are neither config nor cache, and losing them loses judgment. |
| 844 | notes_file() { |
| 845 | printf '%s/valley/notes/%s.md\n' "${XDG_STATE_HOME:-$HOME/.local/state}" "$1" |
| 846 | } |
| 847 | |
| 848 | # The anchor of every line of the buffer, by line: the file that line belongs |
| 849 | # to, and the new-side line number it sits on. Built in one pass so that |
| 850 | # anchoring a note is a lookup. An empty path means the line is above every |
| 851 | # file (the log); line 0 means the line is a file header, above every hunk. |
| 852 | ANCHOR_PATH=() |
| 853 | ANCHOR_LINE=() |
| 854 | |
| 855 | anchor_buffer() { |
| 856 | local file="$1" line path="" at=0 next=0 inhunk=0 rest |
| 857 | ANCHOR_PATH=() |
| 858 | ANCHOR_LINE=() |
| 859 | while IFS= read -r line; do |
| 860 | # Header lines are only headers outside a hunk: inside one, every line |
| 861 | # carries a +/-/space prefix, so a diff of a diff cannot be misread. |
| 862 | if [ "$inhunk" -eq 1 ]; then |
| 863 | case "$line" in |
| 864 | ' '* | '+'* | '') |
| 865 | at="$next" # this line's own new-side number |
| 866 | next=$((next + 1)) |
| 867 | ;; |
| 868 | '-'* | \\*) ;; # a removal or the no-newline marker: no new-side line |
| 869 | *) inhunk=0 ;; |
| 870 | esac |
| 871 | fi |
| 872 | if [ "$inhunk" -eq 0 ]; then |
| 873 | case "$line" in |
| 874 | 'diff --git '*) |
| 875 | path="" |
| 876 | # The b/ side, so a rename anchors to the name it will have. |
| 877 | if [[ "$line" =~ ^diff\ --git\ a/(.*)\ b/(.*)$ ]]; then |
| 878 | path="${BASH_REMATCH[2]}" |
| 879 | fi |
| 880 | at=0 |
| 881 | ;; |
| 882 | '@@ '*) |
| 883 | rest="${line#*+}" # -12,7 +34,9 @@ -> 34,9 @@ |
| 884 | at="${rest%%[!0-9]*}" |
| 885 | [ -n "$at" ] || at=0 |
| 886 | next="$at" |
| 887 | inhunk=1 |
| 888 | ;; |
| 889 | esac |
| 890 | fi |
| 891 | ANCHOR_PATH+=("$path") |
| 892 | ANCHOR_LINE+=("$at") |
| 893 | done < "$file" |
| 894 | } |
| 895 | |
| 896 | # The anchor for a note inserted after buffer line n, as it is written down. |
| 897 | anchor_of() { |
| 898 | local n="$1" path line |
| 899 | [ "$n" -ge 1 ] || { printf '(unanchored)'; return; } |
| 900 | path="${ANCHOR_PATH[n - 1]:-}" |
| 901 | line="${ANCHOR_LINE[n - 1]:-0}" |
| 902 | if [ -z "$path" ]; then |
| 903 | printf '(unanchored)' |
| 904 | elif [ "$line" -eq 0 ]; then |
| 905 | printf '%s' "$path" |
| 906 | else |
| 907 | printf '%s:%s' "$path" "$line" |
| 908 | fi |
| 909 | } |
| 910 | |
| 911 | # The notes in an edited buffer, one `anchor: text` line each. git says where |
| 912 | # the reviewer's lines landed; a hunk that removes nothing is an insertion, |
| 913 | # and the line it was inserted after is the anchor. A hunk that removes |
| 914 | # something is an edit of the diff itself — it anchors to nothing, since the |
| 915 | # line it would name is the line it changed — so it is dropped with a count. |
| 916 | notes_in() { |
| 917 | local orig="$1" edited="$2" line rest at old insert=0 touched=0 text |
| 918 | anchor_buffer "$orig" |
| 919 | while IFS= read -r line; do |
| 920 | case "$line" in |
| 921 | '@@ '*) |
| 922 | rest="${line#@@ -}" # 12,0 +13,2 @@ -> 12,0 +13,2 @@ |
| 923 | at="${rest%%[!0-9]*}" |
| 924 | rest="${rest#"$at"}" |
| 925 | old=1 |
| 926 | case "$rest" in |
| 927 | ,*) |
| 928 | rest="${rest#,}" |
| 929 | old="${rest%%[!0-9]*}" |
| 930 | ;; |
| 931 | esac |
| 932 | if [ "$old" -eq 0 ]; then |
| 933 | insert=1 |
| 934 | else |
| 935 | insert=0 |
| 936 | touched=$((touched + old)) |
| 937 | fi |
| 938 | ;; |
| 939 | '+'*) |
| 940 | # Only inside an insertion hunk: the +++ of git's own header is not |
| 941 | # a note, and neither is the new side of an edit. |
| 942 | [ "$insert" -eq 1 ] || continue |
| 943 | text="${line#+}" |
| 944 | text="${text#"${text%%[![:space:]]*}"}" |
| 945 | [ -n "$text" ] || continue # a blank line says nothing |
| 946 | printf '%s: %s\n' "$(anchor_of "$at")" "$text" |
| 947 | ;; |
| 948 | esac |
| 949 | done < <(git --no-pager diff --no-ext-diff --no-index -U0 -- "$orig" "$edited") |
| 950 | [ "$touched" -eq 0 ] || printf \ |
| 951 | 'valley: warning: %d changed or deleted diff line(s) ignored — a note is a line you insert\n' \ |
| 952 | "$touched" >&2 |
| 953 | } |
| 954 | |
| 955 | # Read the notes out of the paged buffer and keep them: appended to the |
| 956 | # branch's notes file under the commit they were taken against, and held for |
| 957 | # the print that follows the verdict. A notes file that cannot be written is |
| 958 | # a warning, never a failure — the notes are still printed, and a review is |
| 959 | # not undone by a missing directory. |
| 960 | collect_notes() { |
| 961 | local branch="$1" sha="$2" file dir |
| 962 | local -a found=() |
| 963 | mapfile -t found < <(notes_in "$REVIEW_TMP/review.orig" "$REVIEW_TMP/review.diff") |
| 964 | [ "${#found[@]}" -gt 0 ] || return 0 |
| 965 | NOTES+=("${found[@]}") |
| 966 | file="$(notes_file "$branch")" |
| 967 | dir="${file%/*}" |
| 968 | mkdir -p "$dir" 2> /dev/null && { |
| 969 | printf '## %s at %s\n\n' "$branch" "$sha" |
| 970 | printf '%s\n' "${found[@]}" |
| 971 | printf '\n' |
| 972 | } >> "$file" 2> /dev/null && return 0 |
| 973 | printf 'valley: warning: could not write %s — the notes are printed only\n' \ |
| 974 | "$file" >&2 |
| 975 | } |
| 976 | |
| 977 | print_notes() { |
| 978 | local branch="$1" |
| 979 | [ "${#NOTES[@]}" -gt 0 ] || return 0 |
| 980 | printf '\n%d note(s) on %s, appended to %s:\n' \ |
| 981 | "${#NOTES[@]}" "$branch" "$(notes_file "$branch")" |
| 982 | printf ' %s\n' "${NOTES[@]}" |
| 983 | } |
| 984 | |
| 985 | # ---------------------------------------------------------------------- |
| 986 | # review sets the version it shows of a change against the change's |
| 987 | # approved version: the greatest version below it whose head a request or |
| 988 | # an outcome on origin names (valley-go compare, status/compare.go). The |
| 989 | # logic is valley-go's, and this script prints what it says: |
| 990 | # |
| 991 | # - none: there is no approved version to compare with, so review pages |
| 992 | # the whole diff, as for any change. Any note valley-go gives, such as |
| 993 | # an approved version it could not compare, heads the page. |
| 994 | # - clean: the file delta is the approved version's. review prints that |
| 995 | # in a few lines, with the landings between the two bases, and pages |
| 996 | # nothing. |
| 997 | # - changed: review pages the files whose change differs, the |
| 998 | # range-diff between the two versions, and the shown version's diff |
| 999 | # of those files. range-diff's hunks carry no line numbers, so a note |
| 1000 | # taken beside them is unanchored. That last diff is in git's ordinary |
| 1001 | # form, so a note taken there keeps its file and line (anchor_buffer). |
| 1002 | # |
| 1003 | # A changed commit message or author is in valley-go's text either way. |
| 1004 | # [v]iew full diff pages the whole diff at every prompt. The comparison |
| 1005 | # files no request and skips no check: [a]pprove is the same act whatever |
| 1006 | # it says. |
| 1007 | # |
| 1008 | # compare_shown <change ID> <version> <head>: compare the version shown, |
| 1009 | # against main's tip as the fetch left it. Leaves none, clean or changed |
| 1010 | # in COMPARE_KIND, and the text to show in $REVIEW_TMP/compare. A plain |
| 1011 | # branch is none, and so is a comparison valley-go could not make, with a |
| 1012 | # warning. |
| 1013 | COMPARE_KIND=none |
| 1014 | compare_shown() { |
| 1015 | local out="$REVIEW_TMP/compare.out" tip |
| 1016 | COMPARE_KIND=none |
| 1017 | : > "$REVIEW_TMP/compare" |
| 1018 | [ -n "$1" ] || return 0 |
| 1019 | if ! tip="$(git rev-parse --verify --quiet refs/remotes/origin/main)" \ |
| 1020 | || ! valley-go compare --tip "$tip" "$1" "$2" "$3" > "$out"; then |
| 1021 | printf 'valley: warning: could not compare v%s of change %s with its approved version; reviewing it in full\n' \ |
| 1022 | "$2" "$1" >&2 |
| 1023 | return 0 |
| 1024 | fi |
| 1025 | IFS= read -r COMPARE_KIND < "$out" || COMPARE_KIND="" |
| 1026 | case "$COMPARE_KIND" in |
| 1027 | none | clean | changed) tail -n +2 "$out" > "$REVIEW_TMP/compare" ;; |
| 1028 | *) |
| 1029 | printf 'valley: warning: valley-go compare answered %s, which this script does not read; reviewing it in full\n' \ |
| 1030 | "$COMPARE_KIND" >&2 |
| 1031 | COMPARE_KIND=none |
| 1032 | ;; |
| 1033 | esac |
| 1034 | } |
| 1035 | |
| 1036 | # show_compared <notes key> <head> <heading> <knots> <writer> [<arg>…]: |
| 1037 | # show what compare_shown found, under the heading when there is one. A |
| 1038 | # clean restack is printed, and nothing is paged. A changed version pages |
| 1039 | # the comparison. Otherwise the writer writes the whole diff, and it is |
| 1040 | # paged. Notes taken in a page are kept under the notes key, against the |
| 1041 | # head. |
| 1042 | # |
| 1043 | # Knots follow the heading when there is one. Otherwise they follow the |
| 1044 | # first line of the comparison or full diff, which names the change. |
| 1045 | show_compared() { |
| 1046 | local key="$1" sha heading="$3" knots="$4" |
| 1047 | sha="$(git rev-parse --short "$2")" |
| 1048 | shift 4 |
| 1049 | { |
| 1050 | if [ -n "$heading" ]; then |
| 1051 | printf '%s\n' "$heading" |
| 1052 | [ -z "$knots" ] || printf '%s\n' "$knots" |
| 1053 | cat "$REVIEW_TMP/compare" |
| 1054 | [ "$COMPARE_KIND" != none ] || "$@" |
| 1055 | else |
| 1056 | { |
| 1057 | cat "$REVIEW_TMP/compare" |
| 1058 | [ "$COMPARE_KIND" != none ] || "$@" |
| 1059 | } | under_first "$knots" |
| 1060 | fi |
| 1061 | } > "$REVIEW_TMP/review.diff" |
| 1062 | if [ "$COMPARE_KIND" = clean ]; then |
| 1063 | cat "$REVIEW_TMP/review.diff" |
| 1064 | cleanup_review_tmp |
| 1065 | else |
| 1066 | page_buffer "$key" "$sha" |
| 1067 | fi |
| 1068 | } |
| 1069 | |
| 1070 | # under_first <lines>: copy stdin, inserting <lines> after the first line. |
| 1071 | under_first() { |
| 1072 | local first |
| 1073 | IFS= read -r first || [ -n "$first" ] || return 0 |
| 1074 | printf '%s\n' "$first" |
| 1075 | [ -z "$1" ] || printf '%s\n' "$1" |
| 1076 | cat |
| 1077 | } |
| 1078 | |
| 1079 | # DOES caches the output of `valley-go does` for the review (status/does.go): |
| 1080 | # one `<change ID>\tdoes <knot ID> (<status>): <title>` line per knot. |
| 1081 | DOES="" |
| 1082 | |
| 1083 | # read_does [-v]: read the plan graph once for every change in the review. |
| 1084 | # A missing or unreadable graph leaves DOES empty; -v warns on stderr. |
| 1085 | read_does() { |
| 1086 | local ids=() k |
| 1087 | DOES="" |
| 1088 | if [ "${#L_NAME[@]}" -gt 1 ]; then |
| 1089 | for ((k = 0; k < ${#L_ID[@]}; k++)); do |
| 1090 | [ -z "${L_ID[k]}" ] || ids+=("${L_ID[k]}") |
| 1091 | done |
| 1092 | elif [ -n "$CHANGE_ID" ]; then |
| 1093 | ids=("$CHANGE_ID") |
| 1094 | fi |
| 1095 | [ "${#ids[@]}" -gt 0 ] || return 0 |
| 1096 | DOES="$(valley-go does "$@" "${ids[@]}")" || DOES="" |
| 1097 | } |
| 1098 | |
| 1099 | # does_of <change ID>: print the `does ...` lines for one change from DOES. |
| 1100 | does_of() { |
| 1101 | local line |
| 1102 | [ -n "$1" ] || return 0 |
| 1103 | while IFS= read -r line; do |
| 1104 | [ "${line%%$'\t'*}" != "$1" ] || printf '%s\n' "${line#*$'\t'}" |
| 1105 | done <<< "$DOES" |
| 1106 | } |
| 1107 | |
| 1108 | # full_diff <branch> <head>: the whole diff of a change alone: its log |
| 1109 | # and its diff against origin/main. |
| 1110 | full_diff() { |
| 1111 | local sha |
| 1112 | sha="$(git rev-parse --short "$2")" |
| 1113 | if [ -n "$CHANGE_HEAD" ]; then |
| 1114 | printf 'change %s v%s at %s, on branch %s, log against origin/main:\n\n' \ |
| 1115 | "$CHANGE_HANDLE" "$CHANGE_VERSION" "$sha" "$1" |
| 1116 | else |
| 1117 | printf 'branch %s, log against origin/main:\n\n' "$1" |
| 1118 | fi |
| 1119 | git --no-pager log --oneline "origin/main..$2" |
| 1120 | printf '\n' |
| 1121 | git --no-pager diff "origin/main...$2" |
| 1122 | } |
| 1123 | |
| 1124 | # show_review <branch>: show a change alone. A change that is not ready |
| 1125 | # says so first, before anything else shown of it (shown_not_ready). That |
| 1126 | # notice becomes the heading, so the change's knots follow it. |
| 1127 | show_review() { |
| 1128 | local branch="$1" head held |
| 1129 | head="$(change_head "$branch")" |
| 1130 | # [a]pprove approves exactly the head shown here (bind_review). |
| 1131 | [ -n "$CHANGE_ID" ] || APPROVE_HEAD="$head" |
| 1132 | held="$(shown_not_ready)" || held="" |
| 1133 | REVIEW_TMP="$(mktemp -d)" |
| 1134 | compare_shown "$CHANGE_ID" "$CHANGE_VERSION" "$head" |
| 1135 | show_compared "$branch" "$head" "$held" "$(does_of "$CHANGE_ID")" full_diff "$branch" "$head" |
| 1136 | } |
| 1137 | |
| 1138 | # view_full <branch>: [v]iew full diff for a change alone. It pages the |
| 1139 | # change's whole diff, and keeps the notes taken in it. |
| 1140 | view_full() { |
| 1141 | local head |
| 1142 | head="$(change_head "$1")" |
| 1143 | REVIEW_TMP="$(mktemp -d)" |
| 1144 | full_diff "$1" "$head" > "$REVIEW_TMP/review.diff" |
| 1145 | page_buffer "$1" "$(git rev-parse --short "$head")" |
| 1146 | } |
| 1147 | |
| 1148 | # page_buffer <branch> <sha>: page the buffer written to REVIEW_TMP, and |
| 1149 | # keep the notes taken in it under the branch, against the commit shown. |
| 1150 | page_buffer() { |
| 1151 | cp "$REVIEW_TMP/review.diff" "$REVIEW_TMP/review.orig" |
| 1152 | page_file "$REVIEW_TMP/review.diff" || true # quitting the pager early is not an error |
| 1153 | collect_notes "$1" "$2" |
| 1154 | cleanup_review_tmp |
| 1155 | } |
| 1156 | |
| 1157 | # Everything review and approve leave outside the repository: approve's |
| 1158 | # throwaway worktree, the paged buffer, the two policy layers read out of |
| 1159 | # their tips, and the scratch the policy deriver writes into. One handler, |
| 1160 | # installed once by each of those verbs, so that no path through them — |
| 1161 | # and no ctrl-C in the pager — leaves any of them behind. The rebase's |
| 1162 | # worktree is valley-go's, which removes it itself. |
| 1163 | cleanup_scratch() { |
| 1164 | cleanup_approve_wt |
| 1165 | cleanup_policy_tmp |
| 1166 | cleanup_instance_tmp |
| 1167 | cleanup_project_tmp |
| 1168 | cleanup_review_tmp |
| 1169 | } |
| 1170 | |
| 1171 | # [b]ase and the rebase verb are one piece of code, valley-go rebase |
| 1172 | # (status/rebase.go). It rebases the change's whole stack in a throwaway |
| 1173 | # worktree of its own — the user's checkout is never touched — pushes |
| 1174 | # every layer's branch in one atomic push, leased on the tips it read, and |
| 1175 | # adds each change's rebased head to its record as a new version. A |
| 1176 | # change alone prints what rebase always has; a stack prints a manifest |
| 1177 | # of what became of each layer. The judgment on the rebased diff stays |
| 1178 | # with the human, back at the review prompt or in whatever front end ran |
| 1179 | # the rebase verb. |
| 1180 | # |
| 1181 | # rebase_shown <head>: [b]ase for the change under review, while it is |
| 1182 | # still at <head>, the head review showed of it. |
| 1183 | rebase_shown() { |
| 1184 | valley-go rebase --head "$1" "${CHANGE_ID:-$CHANGE_NAME}" |
| 1185 | } |
| 1186 | |
| 1187 | # ---------------------------------------------------------------------- |
| 1188 | # approve — file an integration request for the branch under review. |
| 1189 | # |
| 1190 | # This is the one way a change reaches main from here: the checks the |
| 1191 | # policy requires are run over the branch's tree and signed, the evidence |
| 1192 | # and one request ref are published, and the integrator judges the request |
| 1193 | # and lands it (archive/design/integration.md). main is never touched from here. |
| 1194 | # |
| 1195 | # Nothing in this script writes the protected ref. Whether a given pusher |
| 1196 | # may write it is a fact about origin's pre-receive hook, so origin is |
| 1197 | # where it is answered, and a verb that merged onto main locally would only |
| 1198 | # be asking the same question in a worse place. |
| 1199 | # |
| 1200 | # What this verb removes is a second push. `attest run --push` publishes |
| 1201 | # the branch and the attestation refs in one atomic push and knows nothing |
| 1202 | # about a request ref, so filing a request by hand is two beats: the |
| 1203 | # evidence, then the request after it. In between, evidence stands |
| 1204 | # published for a change nobody has approved. So this verb does not |
| 1205 | # use --push at all. `attest run` without it stores each note at its ref in |
| 1206 | # the repository and stops, and one `git push --atomic` then carries the |
| 1207 | # evidence and the request together — all of it or none of it. Nothing in |
| 1208 | # attest had to change for that; the sequencing was the whole of it. |
| 1209 | # |
| 1210 | # The branch itself is not in that push. What is under review is |
| 1211 | # origin/<branch>, so the commit the request names is already on origin and |
| 1212 | # the objects with it. |
| 1213 | |
| 1214 | # The tree the checks run over: a throwaway worktree of the branch, as |
| 1215 | # [b]ase uses, so that what is checked is the branch's tree and not the |
| 1216 | # operator's working copy. A global, so the traps clean it up whatever |
| 1217 | # happens. |
| 1218 | APPROVE_WT="" |
| 1219 | # The head approve last ran the checks over and saw them fail, for the |
| 1220 | # review prompt: see approve_failed_at. |
| 1221 | CHECKS_FAILED_AT="" |
| 1222 | cleanup_approve_wt() { |
| 1223 | [ -n "$APPROVE_WT" ] || return 0 |
| 1224 | git worktree remove --force "$APPROVE_WT" > /dev/null 2>&1 || true |
| 1225 | rm -rf "$APPROVE_WT" |
| 1226 | APPROVE_WT="" |
| 1227 | } |
| 1228 | |
| 1229 | # Report and fail, without leaving the review. A refusal here is not a |
| 1230 | # reason to end the session: the branch is still under review and the |
| 1231 | # operator can still answer. die() is for the verbs that have nothing left |
| 1232 | # to offer. The approve verb has nothing left to offer either, and turns a |
| 1233 | # refusal into its exit status itself. |
| 1234 | refuse() { |
| 1235 | printf 'valley: %s\n' "$*" >&2 |
| 1236 | return 1 |
| 1237 | } |
| 1238 | |
| 1239 | # The change segment of a request ref. The integrator reads a request as |
| 1240 | # <prefix>/<target>/<change> and refuses a change segment holding a slash |
| 1241 | # (integrator/refs.go), so a branch name with one is written with dashes |
| 1242 | # instead. The ref that results is printed, never left to be assumed. |
| 1243 | # |
| 1244 | # The mapping is lossy: `topic/x` and `topic-x` are one change name and so |
| 1245 | # one request ref, and this verb cannot tell a sibling's request from its |
| 1246 | # own. That is bd-57f7f9f, and what is done about it here is to print the |
| 1247 | # subject of any request being replaced, so a collision is visible at the |
| 1248 | # moment it happens rather than after it. |
| 1249 | change_name() { |
| 1250 | printf '%s' "${1//\//-}" |
| 1251 | } |
| 1252 | |
| 1253 | # One line of a commit, for saying which commit is meant. A request being |
| 1254 | # replaced is usually this operator's own pre-rebase head and so is in |
| 1255 | # this repository; one another operator filed may not be, and saying so is |
| 1256 | # better than printing an id alone. |
| 1257 | subject_of() { |
| 1258 | local s |
| 1259 | if s="$(git log -1 --format=%s "$1" 2>/dev/null)" && [ -n "$s" ]; then |
| 1260 | printf '%s' "$s" |
| 1261 | else |
| 1262 | printf 'subject not in this repository' |
| 1263 | fi |
| 1264 | } |
| 1265 | |
| 1266 | # The value of a ref on origin, as one id or the empty string. Failing to |
| 1267 | # read it is not the same as reading that it is absent, so the two are |
| 1268 | # separate answers: this returns 1 for a read that did not happen. |
| 1269 | remote_ref() { |
| 1270 | local out |
| 1271 | out="$(git ls-remote origin "$1" 2>/dev/null)" || return 1 |
| 1272 | printf '%s' "$out" | cut -f1 |
| 1273 | return 0 |
| 1274 | } |
| 1275 | |
| 1276 | # Where the checks this run signed are stored, for every failure that |
| 1277 | # happens after they have run. One stanza, so no failure path invents its |
| 1278 | # own wording. Called with no refs it says nothing. |
| 1279 | report_local_evidence() { |
| 1280 | [ "$#" -gt 0 ] || return 0 |
| 1281 | printf 'valley: the evidence is stored locally at:\n' >&2 |
| 1282 | printf ' %s\n' "$@" >&2 |
| 1283 | } |
| 1284 | |
| 1285 | # Whether origin already holds one of these attestation refs at a value |
| 1286 | # other than the one being pushed. The namespace admits creations only, so |
| 1287 | # that is the one refusal approving again will never clear: it is what a |
| 1288 | # re-observation of a tree this signer has already attested runs into |
| 1289 | # (bd-7b7d58f). |
| 1290 | attestation_stands_elsewhere() { |
| 1291 | local digest="$1" remote spec have want |
| 1292 | shift |
| 1293 | remote="$(git ls-remote origin "refs/the-valley/attestations/$digest/*" 2>/dev/null)" || return 1 |
| 1294 | for spec in "$@"; do |
| 1295 | have="$(printf '%s\n' "$remote" | awk -v r="$spec" '$2 == r { print $1 }')" |
| 1296 | [ -n "$have" ] || continue |
| 1297 | want="$(git rev-parse "$spec" 2>/dev/null)" || continue |
| 1298 | [ "$have" = "$want" ] || return 0 |
| 1299 | done |
| 1300 | return 1 |
| 1301 | } |
| 1302 | |
| 1303 | # Why a push carrying the request ref failed, said as precisely as the |
| 1304 | # state allows. Every such push is leased, and a lease refuses when the |
| 1305 | # ref it named is not where the read before it saw it — which covers three |
| 1306 | # different events. Reading the ref again is what tells them apart, and a |
| 1307 | # read that does not happen is its own answer rather than a guess. |
| 1308 | # |
| 1309 | # Anything the lease is not responsible for is origin's own refusal, and |
| 1310 | # git has already printed it. |
| 1311 | diagnose_request_push() { |
| 1312 | local ref="$1" pending="$2" now |
| 1313 | if ! now="$(remote_ref "$ref")"; then |
| 1314 | refuse "origin refused the push, and $ref could not be read again to say why. Nothing was published there and main is untouched" |
| 1315 | return 0 |
| 1316 | fi |
| 1317 | if [ "$now" = "$pending" ]; then |
| 1318 | refuse "origin refused the push; nothing was published there and main is untouched" |
| 1319 | elif [ -z "$now" ]; then |
| 1320 | refuse "the pending request was consumed while this one was being prepared, which is what the integrator does when a change lands. The lease on $ref refused and nothing was published; fetch and review the branch again before approving it a second time" |
| 1321 | elif [ -z "$pending" ]; then |
| 1322 | refuse "a request for this change appeared while this one was being prepared, at $(git rev-parse --short "$now") ($(subject_of "$now")); the lease on $ref refused and nothing was published" |
| 1323 | else |
| 1324 | refuse "the pending request moved from $(git rev-parse --short "$pending") to $(git rev-parse --short "$now") while this one was being prepared; the lease on $ref refused, nothing was published, and the request stands where the other approval put it" |
| 1325 | fi |
| 1326 | } |
| 1327 | |
| 1328 | # File the request. Called in a condition, from the review prompt and from |
| 1329 | # the approve verb alike, which suspends `set -e` for the whole call — so |
| 1330 | # every step below is checked by hand, and the failures say what was |
| 1331 | # published before they happened. |
| 1332 | approve() { |
| 1333 | local branch="$1" target=main tip change head base digest root ref check spec what |
| 1334 | local key |
| 1335 | local -a signer=() required=() run=() timing=() evidence=() |
| 1336 | |
| 1337 | root="$(git rev-parse --show-toplevel)" || { |
| 1338 | refuse "not a git repository; nothing published" |
| 1339 | return 1 |
| 1340 | } |
| 1341 | tip=refs/remotes/origin/main |
| 1342 | # A change with a record is filed at exactly the head bound to this |
| 1343 | # approval, under its own request name, c-<change ID>, and only while its |
| 1344 | # record says it is ready and lists that head as a version's. Where its |
| 1345 | # branch is does not matter: the request names the head. |
| 1346 | if [ -n "$CHANGE_ID" ]; then |
| 1347 | head="$APPROVE_HEAD" |
| 1348 | [ -n "$head" ] || { |
| 1349 | refuse "no head of change $CHANGE_HANDLE is bound to this approval; nothing published" |
| 1350 | return 1 |
| 1351 | } |
| 1352 | check_record "nothing published" "" "$head" || return 1 |
| 1353 | change="c-$CHANGE_ID" |
| 1354 | what="$CHANGE_HANDLE v$APPROVE_VERSION" |
| 1355 | elif [ -n "$APPROVE_HEAD" ]; then |
| 1356 | # A plain branch that review showed is filed at the head shown. The |
| 1357 | # last check before the push refuses if the branch on origin has moved |
| 1358 | # off it since. |
| 1359 | head="$APPROVE_HEAD" |
| 1360 | change="$(change_name "$branch")" |
| 1361 | what="$branch" |
| 1362 | else |
| 1363 | head="$(git rev-parse "refs/remotes/origin/$branch")" || { |
| 1364 | refuse "no such branch on origin: $branch; nothing published" |
| 1365 | return 1 |
| 1366 | } |
| 1367 | change="$(change_name "$branch")" |
| 1368 | what="$branch" |
| 1369 | fi |
| 1370 | # c-<change ID> is the request name of the change with that ID, so a |
| 1371 | # branch is never filed under one. |
| 1372 | if [ -z "$CHANGE_ID" ] && [[ "$change" =~ ^c-[k-z]{32}$ ]]; then |
| 1373 | refuse "the branch $branch would be requested as $change, and that name is reserved for the change with the ID ${change#c-}; rename the branch, or name the change by its ID. Nothing published" |
| 1374 | return 1 |
| 1375 | fi |
| 1376 | base="$(git merge-base "$tip" "$head")" || { |
| 1377 | refuse "$what and main share no history; nothing published" |
| 1378 | return 1 |
| 1379 | } |
| 1380 | ref="refs/the-valley/integration-requests/$target/$change" |
| 1381 | local named="$branch" under of |
| 1382 | [ -z "$CHANGE_ID" ] || named="$CHANGE_HANDLE" |
| 1383 | |
| 1384 | # A head that does not fast-forward from main, and would bring a version |
| 1385 | # of another change that has not landed, is a layer above the bottom of |
| 1386 | # a stale stack. The integrator could land it all the same, as one |
| 1387 | # commit recorded against this request alone, and the changes under it |
| 1388 | # would have no landing record of their own. So it is refused before |
| 1389 | # any check runs over a head that has to be rebased anyway, and the |
| 1390 | # stack is rebased first. This is the project's choice, not a rule the |
| 1391 | # integrator needs. A stale head with no change under it, such as the |
| 1392 | # bottom layer of a stack or a change alone, is approved as it is, and |
| 1393 | # the integrator merges it onto main. |
| 1394 | if ! git merge-base --is-ancestor "$tip" "$head"; then |
| 1395 | if ! under="$(valley-go brings ${CHANGE_ID:+--change "$CHANGE_ID"} "$head")"; then |
| 1396 | refuse "could not read which changes $what would bring to main; nothing published" |
| 1397 | return 1 |
| 1398 | fi |
| 1399 | if [ -n "$under" ]; then |
| 1400 | of="a version of $under" |
| 1401 | [ "$(printf '%s\n' "$under" | wc -l)" -eq 1 ] || of="versions of $(printf '%s\n' "$under" | join_lines)" |
| 1402 | refuse "$what does not fast-forward from main, and the commits it would bring hold $of, not landed. Approved as it is, it would land them with no landing record of their own; nothing published. Rebase the stack first: valley rebase $(printf '%q' "$named")" |
| 1403 | return 1 |
| 1404 | fi |
| 1405 | fi |
| 1406 | |
| 1407 | # What the policy requires of the change, read out of exactly the |
| 1408 | # sources the integrator reads: both layers from the target's tip, which |
| 1409 | # review has just fetched, and the floor from the owning valley's own |
| 1410 | # tip. A change never supplies the policy that gates it, and neither does |
| 1411 | # the checkout this runs from — so the answer here is the same answer the |
| 1412 | # integrator will derive, from whatever branch the operator happens to be |
| 1413 | # on. It is printed for exactly that reason. |
| 1414 | policy_defaults "$root" |
| 1415 | resolve_instance_layer "$root" "$tip" || return 1 |
| 1416 | resolve_project_layer "$root" "$tip" || return 1 |
| 1417 | derive_required "$POLICY_SCHEMA" "$POLICY_INSTANCE" "$POLICY_PROJECT" \ |
| 1418 | "$base" "$head" || return 1 |
| 1419 | sort_into required "${!DERIVED_REQUIRED[@]}" || { |
| 1420 | refuse "could not sort the required checks; nothing published" |
| 1421 | return 1 |
| 1422 | } |
| 1423 | |
| 1424 | # A change the policy asks nothing of is still a change, and approving |
| 1425 | # is how one reaches main. The integrator already lands a request that |
| 1426 | # requires no check and records the landing as one that required nothing |
| 1427 | # (archive/design/integration.md), so refusing to file it here would strand |
| 1428 | # exactly the changes the policy requires nothing of. Nothing is required, |
| 1429 | # so nothing is signed and no key is needed: what gets published is one |
| 1430 | # request ref and no evidence at all. |
| 1431 | if [ "${#required[@]}" -gt 0 ]; then |
| 1432 | key="${VALLEY_ATTEST_KEY:-}" |
| 1433 | [ -n "$key" ] || { |
| 1434 | refuse "no signing key: set VALLEY_ATTEST_KEY to the ed25519 key this host attests with. The host signs; nothing published" |
| 1435 | return 1 |
| 1436 | } |
| 1437 | [ -f "$key" ] || { |
| 1438 | refuse "no signing key at $key; nothing published" |
| 1439 | return 1 |
| 1440 | } |
| 1441 | command -v attest > /dev/null 2>&1 || { |
| 1442 | refuse "this choice needs the attest helper; nix run .#valley bundles it. Nothing published" |
| 1443 | return 1 |
| 1444 | } |
| 1445 | signer=(--key "$key") |
| 1446 | [ -z "${VALLEY_ATTEST_NAME:-}" ] || signer+=(--name "$VALLEY_ATTEST_NAME") |
| 1447 | |
| 1448 | # attest logs how long each check took, on this machine and outside |
| 1449 | # the evidence. What it cannot know itself is passed in: the change, |
| 1450 | # and each check's declared cost. |
| 1451 | timing=(--timings) |
| 1452 | [ -z "$CHANGE_ID" ] || timing+=(--change "$CHANGE_ID") |
| 1453 | [ -z "$APPROVE_VERSION" ] || timing+=(--change-version "$APPROVE_VERSION") |
| 1454 | for check in "${required[@]}"; do |
| 1455 | case "${CHECK_RUNNER[$check]:-}" in |
| 1456 | nix) run+=(--check "$check=${CHECK_ATTRIBUTE[$check]}") ;; |
| 1457 | command) run+=(--command "$check=${CHECK_COMMAND[$check]}") ;; |
| 1458 | *) |
| 1459 | refuse "the policy requires $check but does not say how to run it; nothing published" |
| 1460 | return 1 |
| 1461 | ;; |
| 1462 | esac |
| 1463 | [ -z "${CHECK_COST[$check]:-}" ] || timing+=(--cost "$check=${CHECK_COST[$check]}") |
| 1464 | done |
| 1465 | |
| 1466 | cleanup_approve_wt |
| 1467 | APPROVE_WT="$(mktemp -d)" || { |
| 1468 | refuse "no scratch directory to check $what in; nothing published" |
| 1469 | return 1 |
| 1470 | } |
| 1471 | git worktree add --quiet --detach "$APPROVE_WT" "$head" || { |
| 1472 | cleanup_approve_wt |
| 1473 | refuse "could not check $what out to run its checks; nothing published" |
| 1474 | return 1 |
| 1475 | } |
| 1476 | |
| 1477 | printf '\nchecking %s at %s: %s\n' "$what" "$(git rev-parse --short "$head")" \ |
| 1478 | "$(printf '%s\n' "${required[@]}" | join_lines)" |
| 1479 | if ! attest run --repo "$APPROVE_WT" "${signer[@]}" "${timing[@]}" "${run[@]}"; then |
| 1480 | cleanup_approve_wt |
| 1481 | CHECKS_FAILED_AT="$head" |
| 1482 | refuse "the checks did not pass; nothing published, main untouched" |
| 1483 | return 1 |
| 1484 | fi |
| 1485 | digest="$(attest digest --repo "$APPROVE_WT")" || { |
| 1486 | cleanup_approve_wt |
| 1487 | refuse "could not digest the tree that was checked; nothing published" |
| 1488 | return 1 |
| 1489 | } |
| 1490 | cleanup_approve_wt |
| 1491 | digest="${digest#*:}" |
| 1492 | |
| 1493 | # Every attestation stored for this tree, whoever signed it: the |
| 1494 | # evidence for this change is what the subject digest is keyed by, not |
| 1495 | # what this run happened to add to it. |
| 1496 | mapfile -t evidence < <(git for-each-ref --format='%(refname)' \ |
| 1497 | "refs/the-valley/attestations/$digest") |
| 1498 | [ "${#evidence[@]}" -gt 0 ] || { |
| 1499 | refuse "no attestation is stored for $digest; nothing published" |
| 1500 | return 1 |
| 1501 | } |
| 1502 | else |
| 1503 | printf '\n%s requires no check under %s + %s; filing the request with no evidence\n' \ |
| 1504 | "$what" "$POLICY_INSTANCE_LABEL" "$POLICY_PROJECT_LABEL" |
| 1505 | fi |
| 1506 | |
| 1507 | # What origin already holds for this request, read once and remembered. |
| 1508 | # Approving the same change twice is ordinary: evidence expires, and a |
| 1509 | # change whose input closure moved under it is told to re-attest. So the |
| 1510 | # request ref may already exist, and what to do about it depends on |
| 1511 | # where it points. A read that does not happen is not a read that found |
| 1512 | # nothing, so it ends the verb here. |
| 1513 | local pending |
| 1514 | if ! pending="$(remote_ref "$ref")"; then |
| 1515 | refuse "could not read $ref from origin; nothing published" |
| 1516 | report_local_evidence ${evidence[@]+"${evidence[@]}"} |
| 1517 | return 1 |
| 1518 | fi |
| 1519 | |
| 1520 | # The one push: every evidence ref as it stands, and the request ref |
| 1521 | # wherever it has to go. --atomic, so a hook that refuses any of them |
| 1522 | # leaves origin exactly as it was. |
| 1523 | # |
| 1524 | # Every write of the request ref is leased against exactly what the read |
| 1525 | # above saw, including when it saw nothing: git spells that expectation |
| 1526 | # `--force-with-lease=<ref>:` with an empty value, and it refuses if the |
| 1527 | # ref exists at all. So filing a change for the first time is a |
| 1528 | # compare-and-swap too. Without it, two operators filing one change at |
| 1529 | # once would have the second silently fast-forward the first away. |
| 1530 | # |
| 1531 | # A pending request at this same head is a re-attestation of the tree |
| 1532 | # already approved. The evidence is new and the ref does not move, so |
| 1533 | # the ref is left out of the refspec entirely — a push cannot carry a |
| 1534 | # compare-and-swap on a ref it is not changing, because git leaves an |
| 1535 | # up-to-date ref out of what it sends. Nothing this run does establishes |
| 1536 | # where that ref stands afterwards, so it is read again below. |
| 1537 | # |
| 1538 | # A pending request at a different head is a replacement. This is the |
| 1539 | # shape a change takes when main moved through its checks' input |
| 1540 | # closures: no attestation over the old tree can transfer again, so the |
| 1541 | # cure is a new head rebased onto the moved main — and that head is not |
| 1542 | # a descendant of the old one, which makes replacing the ref a |
| 1543 | # non-fast-forward update that git refuses unless told. |
| 1544 | # |
| 1545 | # The lease is the whole of the force, and the refspec carries no `+`. |
| 1546 | # A leading `+` sets git's per-ref force flag, and that flag defeats |
| 1547 | # every rejection the push rules produce — the lease's stale-info among |
| 1548 | # them. The two together are a plain force wearing a lease's name. |
| 1549 | local -a push=() lease=() |
| 1550 | for spec in ${evidence[@]+"${evidence[@]}"}; do push+=("$spec:$spec"); done |
| 1551 | if [ "$pending" != "$head" ]; then |
| 1552 | lease+=("--force-with-lease=$ref:$pending") |
| 1553 | push+=("$head:$ref") |
| 1554 | fi |
| 1555 | |
| 1556 | # The checks can take minutes, and the record may have moved meanwhile. |
| 1557 | # So it is read once more, just before anything is filed, and a name that |
| 1558 | # read as a plain branch is read again in case it names a change now. |
| 1559 | # |
| 1560 | # This is the last check, and a window remains between it and the push |
| 1561 | # below. A change unreadied or withdrawn in that window is filed all the |
| 1562 | # same. git compares only the refs a push changes, and the record ref is |
| 1563 | # not one of them, so no client can make the filing conditional on the |
| 1564 | # record. The integrator narrows the window: it lands no request that |
| 1565 | # would bring a version of a change that is not ready, whatever the |
| 1566 | # request is named, so a request filed in the window stays until the |
| 1567 | # change is ready again. It reads the records last just before it moves |
| 1568 | # the target, so only a record written in that moment is missed. |
| 1569 | if ! check_record "nothing published" "$head" "$APPROVE_HEAD"; then |
| 1570 | report_local_evidence ${evidence[@]+"${evidence[@]}"} |
| 1571 | return 1 |
| 1572 | fi |
| 1573 | # A plain branch is filed at the head review showed only while the |
| 1574 | # branch on origin is still there. The operator reviewed a branch, and a |
| 1575 | # branch that moved holds commits nobody has read. A change with a |
| 1576 | # record is filed at its head wherever its branch is. |
| 1577 | if [ -z "$CHANGE_ID" ] && [ -n "$APPROVE_HEAD" ] && ! branch_at_shown "$branch"; then |
| 1578 | report_local_evidence ${evidence[@]+"${evidence[@]}"} |
| 1579 | return 1 |
| 1580 | fi |
| 1581 | |
| 1582 | # Nothing required and the request already standing at this head leaves |
| 1583 | # nothing to push at all, and a push assembled with an empty refspec |
| 1584 | # would fall back to origin's default one. So none is made. |
| 1585 | local made_push=no |
| 1586 | if [ "${#push[@]}" -gt 0 ]; then |
| 1587 | made_push=yes |
| 1588 | if ! git push --atomic --quiet ${lease[@]+"${lease[@]}"} origin "${push[@]}"; then |
| 1589 | if [ "${#lease[@]}" -eq 0 ] \ |
| 1590 | && attestation_stands_elsewhere "$digest" ${evidence[@]+"${evidence[@]}"}; then |
| 1591 | refuse "origin already holds an attestation for this tree from this signer, and that namespace admits creations only, so a second observation of the same tree has nowhere to go. That is bd-7b7d58f rather than anything approving again will clear. Nothing was published and main is untouched" |
| 1592 | else |
| 1593 | diagnose_request_push "$ref" "$pending" |
| 1594 | fi |
| 1595 | report_local_evidence ${evidence[@]+"${evidence[@]}"} |
| 1596 | return 1 |
| 1597 | fi |
| 1598 | fi |
| 1599 | |
| 1600 | # The lines under the request line, which differ by what was found and |
| 1601 | # what was done about it. |
| 1602 | local -a standing=() |
| 1603 | |
| 1604 | if [ "$pending" = "$head" ]; then |
| 1605 | # The request ref was not in that push, so where it stands now is not |
| 1606 | # something this run established. Read it again rather than repeat |
| 1607 | # what was read before it: in between, the integrator may have landed |
| 1608 | # the change and consumed the ref, or another approval may have |
| 1609 | # replaced it. |
| 1610 | # |
| 1611 | # Nothing here writes it back. Re-creating a consumed request would |
| 1612 | # file a change already in the stream, which the commit rule calls out |
| 1613 | # as the state it consumes the ref to avoid; replacing a request |
| 1614 | # another approval has just filed would clobber the winner; and |
| 1615 | # either would be a second push behind the one this verb promises. So |
| 1616 | # what this does is say what is true and stop. Approving again is the |
| 1617 | # repair, and by then it takes the create or the replace path against |
| 1618 | # what is actually there. |
| 1619 | local after |
| 1620 | if ! after="$(remote_ref "$ref")"; then |
| 1621 | refuse "$ref could not be read again, so whether the request still stands at this head is not known here" |
| 1622 | report_local_evidence ${evidence[@]+"${evidence[@]}"} |
| 1623 | return 1 |
| 1624 | fi |
| 1625 | if [ -z "$after" ]; then |
| 1626 | refuse "the request was consumed while this run was working, which is what the integrator does when a change lands. Nothing here re-filed it: a request for a change already in the stream is judged against an empty delta on every pass. Fetch and review $what again, and approve it once more if it has not landed" |
| 1627 | report_local_evidence ${evidence[@]+"${evidence[@]}"} |
| 1628 | return 1 |
| 1629 | fi |
| 1630 | if [ "$after" != "$head" ]; then |
| 1631 | refuse "the request moved to $(git rev-parse --short "$after") ($(subject_of "$after")) while this run was working, so another approval filed it. Nothing here replaced it; approve again to replace it deliberately" |
| 1632 | report_local_evidence ${evidence[@]+"${evidence[@]}"} |
| 1633 | return 1 |
| 1634 | fi |
| 1635 | if [ "$made_push" = yes ]; then |
| 1636 | standing+=("the request already stood at this head; the evidence is what was published") |
| 1637 | else |
| 1638 | standing+=("the request already stood at this head and nothing was required, so nothing needed publishing") |
| 1639 | fi |
| 1640 | elif [ -n "$pending" ]; then |
| 1641 | standing+=("replaced the pending request, which stood at $(git rev-parse --short "$pending") ($(subject_of "$pending"))") |
| 1642 | fi |
| 1643 | |
| 1644 | printf '\nApproved %s (%s): its integration request for origin/%s is filed.\n' \ |
| 1645 | "$what" "$(git rev-parse --short "$head")" "$target" |
| 1646 | printf 'The integrator will decide whether this change can land.\n' |
| 1647 | printf 'Your local checkout has not changed.\n\n' |
| 1648 | printf 'Check the result: valley status %q\n\n' "$named" |
| 1649 | printf ' request %s -> %s\n' "$ref" "$(git rev-parse --short "$head")" |
| 1650 | [ "${#standing[@]}" -eq 0 ] || printf ' %s\n' "${standing[@]}" |
| 1651 | if [ "${#evidence[@]}" -eq 0 ]; then |
| 1652 | printf ' evidence none — nothing was required, so nothing was signed\n' |
| 1653 | printf ' checks none — the policy requires no check of this change\n' |
| 1654 | else |
| 1655 | printf ' evidence %s\n' "${evidence[@]}" |
| 1656 | printf ' checks %s\n' "$(printf '%s\n' "${required[@]}" | join_lines)" |
| 1657 | fi |
| 1658 | } |
| 1659 | |
| 1660 | # status, ready, unready and withdraw are a Go program, valley-go |
| 1661 | # (status/). status reads a change's request and outcome from origin, |
| 1662 | # because what front ends read from it is a JSON contract, and because the |
| 1663 | # read is subtle enough to want a real parser: an outcome record it does |
| 1664 | # not recognise must read as unknown, never as success. It writes nothing |
| 1665 | # and moves no local branch. ready, unready and withdraw write a change's |
| 1666 | # record, in the one format the push hook reads it in (change/). knot |
| 1667 | # reads and writes the plan graph at refs/the-valley/knots, in the one |
| 1668 | # form the push hook validates (knot/), and plan shows that graph as a |
| 1669 | # view. |
| 1670 | cmd_go() { |
| 1671 | command -v valley-go > /dev/null 2>&1 \ |
| 1672 | || die "$1 needs the valley-go helper; nix run .#valley bundles it" |
| 1673 | exec valley-go "$@" |
| 1674 | } |
| 1675 | |
| 1676 | # A change is named by its full change ID, its handle, or its branch. |
| 1677 | # resolve_change reads the name with the helper and sets CHANGE_BRANCH, the |
| 1678 | # branch to act on. When the name is a change's, it also sets the change's |
| 1679 | # ID, handle, active version, state and owner, and CHANGE_HEAD, the head |
| 1680 | # that version records. A branch name is a change's when exactly one record's |
| 1681 | # active version is on that branch, and is refused when more than one's is. |
| 1682 | # So whether a change's record is read never depends on how it is named, |
| 1683 | # and review, approve and rebase cannot run without the helper. |
| 1684 | # |
| 1685 | # A change with a record is acted on at a head its record names, and never |
| 1686 | # at the branch's tip: review at CHANGE_HEAD, and approve at the head bound |
| 1687 | # to it (bind_head). rebase reads the record itself (status/rebase.go). |
| 1688 | # The branch a record names is only where its head is expected to be, and |
| 1689 | # anyone who can push the branch can move it, in the same push as the |
| 1690 | # record if they like. |
| 1691 | CHANGE_NAME="" CHANGE_ID="" CHANGE_HANDLE="" CHANGE_VERSION="" CHANGE_HEAD="" CHANGE_BRANCH="" CHANGE_STATE="" |
| 1692 | CHANGE_OWNER="" |
| 1693 | |
| 1694 | # read_resolved <line> <name…>: read a line of valley-go resolve into the |
| 1695 | # variables named, one field each. A version's branch may be empty, and |
| 1696 | # read runs consecutive tabs together, since a tab is whitespace to it: an |
| 1697 | # empty branch would shift the state into the branch. So each tab is made |
| 1698 | # a unit separator first, which read keeps apart. |
| 1699 | read_resolved() { |
| 1700 | local resolved="$1" |
| 1701 | shift |
| 1702 | IFS=$'\x1f' read -r "$@" <<< "${resolved//$'\t'/$'\x1f'}" |
| 1703 | } |
| 1704 | |
| 1705 | # ready_hint <change ID> <branch>: the valley ready line that readies the |
| 1706 | # change again, naming the branch unless its version records none. |
| 1707 | ready_hint() { |
| 1708 | printf 'valley ready --change %s' "$1" |
| 1709 | [ -z "$2" ] || printf ' %q' "$2" |
| 1710 | } |
| 1711 | |
| 1712 | resolve_change() { |
| 1713 | local line kind |
| 1714 | CHANGE_NAME="$1" |
| 1715 | command -v valley-go > /dev/null 2>&1 \ |
| 1716 | || die "review, approve and rebase read the change's record with the valley-go helper; nix run .#valley bundles it" |
| 1717 | line="$(valley-go resolve "$1")" || return 1 |
| 1718 | read_resolved "$line" kind CHANGE_ID CHANGE_HANDLE CHANGE_VERSION CHANGE_HEAD CHANGE_BRANCH CHANGE_STATE CHANGE_OWNER |
| 1719 | case "$kind" in |
| 1720 | branch) |
| 1721 | CHANGE_BRANCH="$CHANGE_ID" |
| 1722 | CHANGE_ID="" |
| 1723 | ;; |
| 1724 | change) |
| 1725 | [[ "$CHANGE_HEAD" =~ ^[0-9a-f]{40}$ ]] \ |
| 1726 | || die "valley-go resolve named the head $CHANGE_HEAD, which is no object ID" |
| 1727 | case "$CHANGE_STATE" in |
| 1728 | ready | draft | withdrawn) ;; |
| 1729 | *) die "valley-go resolve named the state $CHANGE_STATE, which this script does not read" ;; |
| 1730 | esac |
| 1731 | ;; |
| 1732 | *) die "valley-go resolve answered $line, which this script does not read" ;; |
| 1733 | esac |
| 1734 | } |
| 1735 | |
| 1736 | # need_branch <verb>: refuse a change whose active version records no |
| 1737 | # branch. review acts on the change's branch: it files its notes under it |
| 1738 | # and its [r]eject deletes it. rebase, which pushes it, refuses such a |
| 1739 | # change in valley-go. approve files the head alone, so it approves such a |
| 1740 | # change. |
| 1741 | need_branch() { |
| 1742 | if [ -n "$CHANGE_ID" ] && [ -z "$CHANGE_BRANCH" ]; then |
| 1743 | die "change $CHANGE_HANDLE v$CHANGE_VERSION records no branch, and $1 acts on a change's branch. Approve its head alone: valley approve --head $CHANGE_HEAD $CHANGE_HANDLE" |
| 1744 | fi |
| 1745 | } |
| 1746 | |
| 1747 | # The commit review and approve act on: the recorded head for a change |
| 1748 | # with a record, and otherwise the branch's head on origin. |
| 1749 | change_head() { |
| 1750 | if [ -n "$CHANGE_HEAD" ]; then |
| 1751 | printf '%s\n' "$CHANGE_HEAD" |
| 1752 | else |
| 1753 | git rev-parse "refs/remotes/origin/$1" |
| 1754 | fi |
| 1755 | } |
| 1756 | |
| 1757 | # For a change with a record, refuse unless the branch's tip on origin is |
| 1758 | # the recorded head, and name both. review's [r]eject acts on the branch |
| 1759 | # itself: deleting it while it is elsewhere would delete a commit no |
| 1760 | # version records. rebase asks the same in valley-go. approve does not ask |
| 1761 | # this, since the request it files names the head it approves and not the |
| 1762 | # branch. The second argument is what the refusal leaves undone. |
| 1763 | check_recorded_tip() { |
| 1764 | local branch="$1" undone="$2" tip |
| 1765 | [ -n "$CHANGE_HEAD" ] || return 0 |
| 1766 | tip="$(git rev-parse "refs/remotes/origin/$branch")" || { |
| 1767 | refuse "no such branch on origin: $branch; $undone" |
| 1768 | return 1 |
| 1769 | } |
| 1770 | [ "$tip" = "$CHANGE_HEAD" ] && return 0 |
| 1771 | refuse "change $CHANGE_HANDLE v$CHANGE_VERSION records the head $CHANGE_HEAD, and origin/$branch is at $tip; $undone. Ready the branch's head as a new version of $CHANGE_HANDLE to act on it" |
| 1772 | } |
| 1773 | |
| 1774 | # For a change with a record, read the record again from origin, and |
| 1775 | # refuse unless the change is still ready at the head this run began with. |
| 1776 | # A draft is paused and a withdrawn change given up, and neither is |
| 1777 | # approved until it is readied again. The record was read when review or |
| 1778 | # approve began, and another principal may unready, withdraw or ready a |
| 1779 | # new version of the change while a review is open or the checks run. So |
| 1780 | # approve reads it again before it runs the checks and before it files. |
| 1781 | # These checks save a run that would come to nothing: the integrator makes |
| 1782 | # the same one on origin, and lands no request for a change that is not |
| 1783 | # ready. rebase makes its own checks in valley-go (status/rebase.go). |
| 1784 | # |
| 1785 | # A name that read as a plain branch can come to name a change meanwhile: |
| 1786 | # someone readies the branch, and perhaps unreadies it, while the review |
| 1787 | # is open. So the last check before approve files reads the original name |
| 1788 | # again too. approve passes the head it is about to file as the second |
| 1789 | # argument. When the name now names a change, the same rules hold: the |
| 1790 | # change must be ready, and its recorded head must be that head. The first |
| 1791 | # argument is what the refusal leaves undone. |
| 1792 | # |
| 1793 | # approve binds the head it approves (APPROVE_HEAD), and passes it as the |
| 1794 | # third argument. For approve the change must be ready, and that head must |
| 1795 | # be one of its version heads, whichever version is active by then: a |
| 1796 | # record that moves after review cannot change what lands, since the |
| 1797 | # request names the head approved. |
| 1798 | check_record() { |
| 1799 | local undone="$1" acting="${2:-}" bound="${3:-}" line kind id handle version head branch state owner |
| 1800 | if [ -z "$CHANGE_ID" ]; then |
| 1801 | [ -n "$acting" ] || return 0 |
| 1802 | if ! line="$(valley-go resolve "$CHANGE_NAME")"; then |
| 1803 | refuse "could not read $CHANGE_NAME again against origin's records; $undone" |
| 1804 | return 1 |
| 1805 | fi |
| 1806 | read_resolved "$line" kind id handle version head branch state owner |
| 1807 | [ "$kind" = change ] || return 0 |
| 1808 | if [ "$state" != ready ]; then |
| 1809 | refuse "$CHANGE_NAME is now change $handle v$version, which is $state, not ready; $undone. Ready it first: $(ready_hint "$id" "$branch")" |
| 1810 | return 1 |
| 1811 | fi |
| 1812 | if [ "$head" != "$acting" ]; then |
| 1813 | refuse "$CHANGE_NAME is now change $handle v$version, which records the head $head, and this run began at $acting; $undone. Review it again by its handle" |
| 1814 | return 1 |
| 1815 | fi |
| 1816 | return 0 |
| 1817 | fi |
| 1818 | if ! line="$(valley-go resolve "$CHANGE_ID")"; then |
| 1819 | refuse "could not read the record of change $CHANGE_HANDLE again; $undone" |
| 1820 | return 1 |
| 1821 | fi |
| 1822 | read_resolved "$line" kind id handle version head branch state owner |
| 1823 | if [ "$kind" != change ] || [ "$id" != "$CHANGE_ID" ]; then |
| 1824 | refuse "origin no longer holds a record of change $CHANGE_HANDLE; $undone" |
| 1825 | return 1 |
| 1826 | fi |
| 1827 | if [ "$state" != ready ]; then |
| 1828 | refuse "change $CHANGE_HANDLE v$version is $state, not ready; $undone. Ready it first: $(ready_hint "$CHANGE_ID" "$branch")" |
| 1829 | return 1 |
| 1830 | fi |
| 1831 | if [ -n "$bound" ]; then |
| 1832 | check_version "$undone" "$bound" |
| 1833 | return |
| 1834 | fi |
| 1835 | if [ "$head" != "$CHANGE_HEAD" ]; then |
| 1836 | refuse "change $CHANGE_HANDLE is at v$version, $head, and this run began at v$CHANGE_VERSION, $CHANGE_HEAD; $undone. Review it again at its new head" |
| 1837 | return 1 |
| 1838 | fi |
| 1839 | } |
| 1840 | |
| 1841 | # For a change with a record, read its versions from origin again, and |
| 1842 | # refuse unless the change is ready and the head given is one of its |
| 1843 | # versions' heads. Sets APPROVE_VERSION and APPROVE_TITLE to that |
| 1844 | # version's number and title. The first argument is what the refusal |
| 1845 | # leaves undone. |
| 1846 | APPROVE_VERSION="" APPROVE_TITLE="" |
| 1847 | check_version() { |
| 1848 | local undone="$1" head="$2" out n vhead vstate title state="" |
| 1849 | local -a known=() |
| 1850 | APPROVE_VERSION="" APPROVE_TITLE="" |
| 1851 | if ! out="$(valley-go versions "$CHANGE_ID")"; then |
| 1852 | refuse "could not read the versions of change $CHANGE_HANDLE; $undone" |
| 1853 | return 1 |
| 1854 | fi |
| 1855 | # read empties its variables at the end of its input, so the state is |
| 1856 | # kept apart from them. |
| 1857 | while IFS=$'\t' read -r n vhead vstate title; do |
| 1858 | state="$vstate" |
| 1859 | known+=("v$n at $vhead") |
| 1860 | if [ "$vhead" = "$head" ]; then |
| 1861 | APPROVE_VERSION="$n" |
| 1862 | APPROVE_TITLE="$title" |
| 1863 | fi |
| 1864 | done <<< "$out" |
| 1865 | if [ "$state" != ready ]; then |
| 1866 | refuse "change $CHANGE_HANDLE is $state, not ready; $undone. Ready it first: $(ready_hint "$CHANGE_ID" "$CHANGE_BRANCH")" |
| 1867 | return 1 |
| 1868 | fi |
| 1869 | if [ -z "$APPROVE_VERSION" ]; then |
| 1870 | refuse "$head is no version of change $CHANGE_HANDLE, whose versions are $(printf '%s\n' "${known[@]}" | join_lines); $undone" |
| 1871 | return 1 |
| 1872 | fi |
| 1873 | } |
| 1874 | |
| 1875 | # The head approve approves for a change with a record, in APPROVE_HEAD. |
| 1876 | # With --head, it is exactly that head, which must be one of the record's |
| 1877 | # version heads. A front end that shows the change its own way passes the |
| 1878 | # head it showed, so what is approved is what was seen, whichever version |
| 1879 | # the record makes active by then. Without --head, the operator is shown |
| 1880 | # the active version's head and title and asked to confirm them, which |
| 1881 | # needs a terminal; off one, approve refuses. The first argument is |
| 1882 | # --head's value, or empty. |
| 1883 | APPROVE_HEAD="" |
| 1884 | bind_head() { |
| 1885 | local want="$1" sha answer |
| 1886 | APPROVE_HEAD="" |
| 1887 | if [ "$CHANGE_STATE" != ready ]; then |
| 1888 | refuse "change $CHANGE_HANDLE v$CHANGE_VERSION is $CHANGE_STATE, not ready; nothing published. Ready it first: $(ready_hint "$CHANGE_ID" "$CHANGE_BRANCH")" |
| 1889 | return 1 |
| 1890 | fi |
| 1891 | if [ -n "$want" ]; then |
| 1892 | if ! [[ "$want" =~ ^[0-9a-f]{4,64}$ ]] \ |
| 1893 | || ! sha="$(git rev-parse --verify --quiet "$want^{commit}")"; then |
| 1894 | refuse "--head takes the object ID of one of change $CHANGE_HANDLE's version heads, and $want is none here; nothing published" |
| 1895 | return 1 |
| 1896 | fi |
| 1897 | check_version "nothing published" "$sha" || return 1 |
| 1898 | APPROVE_HEAD="$sha" |
| 1899 | return 0 |
| 1900 | fi |
| 1901 | check_version "nothing published" "$CHANGE_HEAD" || return 1 |
| 1902 | if [ ! -t 0 ]; then |
| 1903 | refuse "approving change $CHANGE_HANDLE names the head it approves: off a terminal, pass it with --head. Its active version is v$APPROVE_VERSION at $CHANGE_HEAD, $APPROVE_TITLE: valley approve --head $CHANGE_HEAD $CHANGE_HANDLE. Nothing published" |
| 1904 | return 1 |
| 1905 | fi |
| 1906 | printf 'approve change %s v%s at %s, %s? [y/N] ' \ |
| 1907 | "$CHANGE_HANDLE" "$APPROVE_VERSION" "$CHANGE_HEAD" "$APPROVE_TITLE" |
| 1908 | read -r answer || answer="" |
| 1909 | case "$answer" in |
| 1910 | y | Y | yes) APPROVE_HEAD="$CHANGE_HEAD" ;; |
| 1911 | *) |
| 1912 | refuse "not approved; nothing published" |
| 1913 | return 1 |
| 1914 | ;; |
| 1915 | esac |
| 1916 | } |
| 1917 | |
| 1918 | # review shows a change with a record at its active version's head, and |
| 1919 | # its [a]pprove approves exactly that head. A plain branch is bound to the |
| 1920 | # head review pages, when it pages it (show_review, use_layer), and approve |
| 1921 | # files that head. approve with no review binds nothing, and files the |
| 1922 | # branch's tip on origin as the fetch left it. |
| 1923 | bind_review() { |
| 1924 | APPROVE_HEAD="" APPROVE_VERSION="" |
| 1925 | if [ -n "$CHANGE_ID" ]; then |
| 1926 | APPROVE_HEAD="$CHANGE_HEAD" |
| 1927 | APPROVE_VERSION="$CHANGE_VERSION" |
| 1928 | fi |
| 1929 | } |
| 1930 | |
| 1931 | # branch_at_shown <branch>: refuse unless the branch on origin is still at |
| 1932 | # the head bound to this approval, and name both. origin itself is read, |
| 1933 | # not the tracking ref: a push from another checkout moves the branch |
| 1934 | # without moving this clone's ref. ls-remote matches a pattern against the |
| 1935 | # tail of a ref name, so each ref it lists is compared whole. |
| 1936 | branch_at_shown() { |
| 1937 | local out now |
| 1938 | out="$(git ls-remote origin "refs/heads/$1")" || { |
| 1939 | refuse "could not read $1 on origin to check that it is still at $APPROVE_HEAD, the head shown; nothing published" |
| 1940 | return 1 |
| 1941 | } |
| 1942 | now="$(printf '%s\n' "$out" | awk -v r="refs/heads/$1" '$2 == r { print $1 }')" |
| 1943 | if [ -z "$now" ]; then |
| 1944 | refuse "origin/$1 is gone, and is no longer at $APPROVE_HEAD, the head shown; nothing published" |
| 1945 | return 1 |
| 1946 | fi |
| 1947 | [ "$now" = "$APPROVE_HEAD" ] && return 0 |
| 1948 | refuse "origin/$1 is at $now, and is no longer at $APPROVE_HEAD, the head shown; nothing published" |
| 1949 | } |
| 1950 | |
| 1951 | # The head the checks failed at in this review, short, while the change is |
| 1952 | # still at it. Approving that head again would run the same checks over |
| 1953 | # the same tree and fail the same way, so the prompt stops offering |
| 1954 | # [a]pprove until the change has a new head. Fails when the checks have |
| 1955 | # not failed at the head the change is at. |
| 1956 | approve_failed_at() { |
| 1957 | if [ -z "$CHECKS_FAILED_AT" ] || [ "$CHECKS_FAILED_AT" != "$(change_head "$1")" ]; then |
| 1958 | return 1 |
| 1959 | fi |
| 1960 | git rev-parse --short "$CHECKS_FAILED_AT" |
| 1961 | } |
| 1962 | |
| 1963 | # not_ready <handle> <version> <state> <owner>: the line review shows for a |
| 1964 | # change whose record is not ready, above its diff and in its prompt. |
| 1965 | # Prints nothing, and fails, for a ready change, and for a plain branch, |
| 1966 | # which has no record. A draft is often a change an agent is still working |
| 1967 | # on, so the prompt then offers only [v]iew full diff and [s]kip. |
| 1968 | # [a]pprove would be refused. [b]ase is not offered either: rebase refuses |
| 1969 | # such a change, since adding its rebased head as a version would ready it |
| 1970 | # behind its owner's back. Nor is [r]eject, which records nothing on the |
| 1971 | # change: it deletes the branch its owner may still be working on, and the |
| 1972 | # record says what it said before. approve still reads the record again |
| 1973 | # when it acts, since the state can change while the review is open. |
| 1974 | not_ready() { |
| 1975 | case "$3" in |
| 1976 | draft) printf "%s v%s is a draft: its owner (%s) hasn't marked it ready, so it can't be approved yet." "$1" "$2" "$4" ;; |
| 1977 | withdrawn) printf '%s is withdrawn: there is nothing to approve.' "$1" ;; |
| 1978 | *) return 1 ;; |
| 1979 | esac |
| 1980 | } |
| 1981 | |
| 1982 | # shown_not_ready: not_ready for the change review is showing. |
| 1983 | shown_not_ready() { |
| 1984 | not_ready "$CHANGE_HANDLE" "$CHANGE_VERSION" "$CHANGE_STATE" "$CHANGE_OWNER" |
| 1985 | } |
| 1986 | |
| 1987 | # Show the branch and take the verdict. Split out of cmd_review so that |
| 1988 | # every way out of it — approve, reject, skip — is followed by the notes. |
| 1989 | # A change that is not ready is asked as decide asks it: it can be neither |
| 1990 | # approved nor rebased, so whether it fast-forwards from main changes |
| 1991 | # nothing. |
| 1992 | read_and_decide() { |
| 1993 | local branch="$1" choice failed answers |
| 1994 | show_review "$branch" |
| 1995 | # A stale branch's pre-rebase diff is not what would land; offer to rebase it |
| 1996 | # mechanically, then re-review what actually would. [a]pprove is offered |
| 1997 | # here as well, because the integrator applies a delta to the tip rather |
| 1998 | # than fast-forwarding: a stale branch is a request it can judge, and |
| 1999 | # rebasing first is a convenience rather than a precondition. |
| 2000 | while ! shown_not_ready > /dev/null \ |
| 2001 | && ! git merge-base --is-ancestor refs/remotes/origin/main "$(change_head "$branch")"; do |
| 2002 | if failed="$(approve_failed_at "$branch")"; then |
| 2003 | printf '%s does not fast-forward from main. Its checks failed at %s, so approving needs a new head: [v]iew full diff, [b]ase onto main, [r]eject, [s]kip? ' \ |
| 2004 | "$branch" "$failed" |
| 2005 | answers="v, b, r, or s" |
| 2006 | else |
| 2007 | printf '%s does not fast-forward from main: [a]pprove, [v]iew full diff, [b]ase onto main, [r]eject, [s]kip? ' \ |
| 2008 | "$branch" |
| 2009 | answers="a, v, b, r, or s" |
| 2010 | fi |
| 2011 | read -r choice || die "stdin closed before an answer; nothing done" |
| 2012 | case "$choice" in |
| 2013 | a) |
| 2014 | if [ -n "$failed" ]; then |
| 2015 | echo "answer $answers" |
| 2016 | elif approve "$branch"; then |
| 2017 | return |
| 2018 | fi |
| 2019 | ;; |
| 2020 | v) view_full "$branch" ;; |
| 2021 | b) |
| 2022 | # A change with a record gains the rebased head as a new version, |
| 2023 | # so the review goes on at that version, as it does at the rebased |
| 2024 | # tip of a plain branch. A refused rebase has said why, and the |
| 2025 | # prompt comes back. |
| 2026 | if rebase_shown "$(change_head "$branch")"; then |
| 2027 | if [ -n "$CHANGE_ID" ]; then |
| 2028 | resolve_change "$CHANGE_ID" || die "could not read change $CHANGE_HANDLE again after the rebase" |
| 2029 | bind_review |
| 2030 | fi |
| 2031 | fetch_origin |
| 2032 | show_review "$branch" |
| 2033 | fi |
| 2034 | ;; |
| 2035 | r) reject "$branch" && return ;; |
| 2036 | s) |
| 2037 | echo "skipped $branch; nothing done" |
| 2038 | return |
| 2039 | ;; |
| 2040 | *) echo "answer $answers" ;; |
| 2041 | esac |
| 2042 | done |
| 2043 | decide "$branch" |
| 2044 | } |
| 2045 | |
| 2046 | # decide <branch> [<name> <n>]: the verdict on a change that fast-forwards |
| 2047 | # from main, and on the top of a stack, which is never offered [b]ase. A |
| 2048 | # change alone is asked about by its branch. The top of a stack of <n> is |
| 2049 | # asked about by its <name>, as the stack lists it, and its prompt says |
| 2050 | # that approving it lands all <n> layers. [v]iew full diff pages the |
| 2051 | # change's whole diff, or the top layer's. |
| 2052 | # |
| 2053 | # A change that is not ready is asked about by the line not_ready gives, |
| 2054 | # in place of the label, and is offered only [v]iew full diff and |
| 2055 | # [s]kip, or [s]top. The top of a stack with a layer under it that is not |
| 2056 | # ready says why it can't be approved (layer_held), and is offered the |
| 2057 | # rest. |
| 2058 | decide() { |
| 2059 | local branch="$1" name="${2:-}" n="${3:-}" choice failed answers own held="" |
| 2060 | local label="$1" approve="[a]pprove" reject="[r]eject" stop="[s]kip" |
| 2061 | local stopped="skipped $1; nothing done" |
| 2062 | own="$(shown_not_ready)" || own="" |
| 2063 | if [ -n "$n" ]; then |
| 2064 | label="$name, $n of $n" approve="[a]pprove all $n" reject="[r]eject $name" stop="[s]top" |
| 2065 | stopped="stopped reviewing $name at change $n of $n; nothing done" |
| 2066 | held="$(layer_held $((n - 1)))" || held="" |
| 2067 | fi |
| 2068 | while true; do |
| 2069 | failed="" |
| 2070 | if [ -n "$own" ]; then |
| 2071 | if [ -n "$n" ]; then |
| 2072 | printf '%s: %s [v]iew full diff, %s? ' "$label" "$own" "$stop" |
| 2073 | else |
| 2074 | printf '%s [v]iew full diff, %s? ' "$own" "$stop" |
| 2075 | fi |
| 2076 | answers="v or s" |
| 2077 | elif [ -n "$held" ]; then |
| 2078 | printf '%s: %s [v]iew full diff, %s, %s? ' "$label" "$held" "$reject" "$stop" |
| 2079 | answers="v, r, or s" |
| 2080 | elif failed="$(approve_failed_at "$branch")"; then |
| 2081 | printf '%s: its checks failed at %s, so approving needs a new head: [v]iew full diff, %s, %s? ' \ |
| 2082 | "$label" "$failed" "$reject" "$stop" |
| 2083 | answers="v, r, or s" |
| 2084 | else |
| 2085 | printf '%s: %s, [v]iew full diff, %s, %s? ' "$label" "$approve" "$reject" "$stop" |
| 2086 | answers="a, v, r, or s" |
| 2087 | fi |
| 2088 | read -r choice || die "stdin closed before an answer; nothing done" |
| 2089 | case "$choice" in |
| 2090 | # A refusal from approve is not a verdict: it says what it did not do |
| 2091 | # and the prompt comes back, so the branch can still be rejected or |
| 2092 | # skipped. When the checks failed, approving the same head again is |
| 2093 | # no longer offered, nor is approving a change that is not ready. |
| 2094 | a) |
| 2095 | if [ -n "$failed" ] || [ -n "$own" ] || [ -n "$held" ]; then |
| 2096 | echo "answer $answers" |
| 2097 | elif approve "$branch"; then |
| 2098 | return |
| 2099 | fi |
| 2100 | ;; |
| 2101 | v) |
| 2102 | if [ -n "$n" ]; then |
| 2103 | view_layer $((n - 1)) |
| 2104 | else |
| 2105 | view_full "$branch" |
| 2106 | fi |
| 2107 | ;; |
| 2108 | r) |
| 2109 | if [ -n "$own" ]; then |
| 2110 | echo "answer $answers" |
| 2111 | else |
| 2112 | reject "$branch" && return |
| 2113 | fi |
| 2114 | ;; |
| 2115 | s) |
| 2116 | echo "$stopped" |
| 2117 | return |
| 2118 | ;; |
| 2119 | *) echo "answer $answers" ;; |
| 2120 | esac |
| 2121 | done |
| 2122 | } |
| 2123 | |
| 2124 | # ---------------------------------------------------------------------- |
| 2125 | # A stack — a change with layers under it that have not landed — is |
| 2126 | # reviewed one layer at a time, bottom first, each against the layer below |
| 2127 | # it, the way it was built. Approving the top lands every layer under it, |
| 2128 | # so reading the top against main alone would ask the operator to judge |
| 2129 | # the whole stack as one diff. |
| 2130 | # |
| 2131 | # The layers are valley-go stack's (status/stack.go): one line each, bottom |
| 2132 | # first, the change itself last, read here into one array per field. |
| 2133 | # L_BRANCH is the branch a layer's notes are kept under: a branch's own |
| 2134 | # name, or the branch a change's active version names. L_OWNER is a |
| 2135 | # change's owner, as its record names it. L_NOTES holds the notes taken on |
| 2136 | # each layer, for the print after the verdict. |
| 2137 | L_KIND=() L_NAME=() L_BASE=() L_HEAD=() L_STATE=() L_TITLE=() |
| 2138 | L_ID=() L_VERSION=() L_BRANCH=() L_OWNER=() L_NOTES=() |
| 2139 | # The name review was given, which the top is read again by. |
| 2140 | TOP_NAME="" |
| 2141 | |
| 2142 | # read_stack: read the stack under the change review resolved. Each change |
| 2143 | # under the top is read again with resolve, for its ID, version, branch |
| 2144 | # and owner, and must still be at the head the stack names. The top must be |
| 2145 | # what review resolved. Fails, having said why, when either does not hold |
| 2146 | # or the helper cannot read the stack. Called in a condition, so each step |
| 2147 | # is checked by hand. |
| 2148 | read_stack() { |
| 2149 | local out line kind name base head state title k last |
| 2150 | local rkind id handle version rhead branch rstate owner |
| 2151 | L_KIND=() L_NAME=() L_BASE=() L_HEAD=() L_STATE=() L_TITLE=() |
| 2152 | L_ID=() L_VERSION=() L_BRANCH=() L_OWNER=() L_NOTES=() |
| 2153 | out="$(valley-go stack "${CHANGE_ID:-origin/$CHANGE_BRANCH}")" || return 1 |
| 2154 | while IFS= read -r line; do |
| 2155 | read_resolved "$line" kind name base head state title |
| 2156 | if ! [[ "$kind" =~ ^(change|branch)$ && "$base" =~ ^[0-9a-f]{40}$ && "$head" =~ ^[0-9a-f]{40}$ ]]; then |
| 2157 | refuse "valley-go stack answered $line, which this script does not read" |
| 2158 | return 1 |
| 2159 | fi |
| 2160 | L_KIND+=("$kind") L_NAME+=("$name") L_BASE+=("$base") L_HEAD+=("$head") |
| 2161 | L_STATE+=("$state") L_TITLE+=("$title") L_NOTES+=("") |
| 2162 | L_ID+=("") L_VERSION+=("") L_BRANCH+=("$name") L_OWNER+=("") |
| 2163 | done <<< "$out" |
| 2164 | last=$((${#L_NAME[@]} - 1)) |
| 2165 | if [ "${L_HEAD[last]}" != "$(change_head "$CHANGE_BRANCH")" ] \ |
| 2166 | || [ "${L_KIND[last]}" != "$([ -n "$CHANGE_ID" ] && echo change || echo branch)" ]; then |
| 2167 | refuse "$TOP_NAME moved while its stack was read" |
| 2168 | return 1 |
| 2169 | fi |
| 2170 | L_ID[last]="$CHANGE_ID" L_VERSION[last]="$CHANGE_VERSION" L_BRANCH[last]="$CHANGE_BRANCH" |
| 2171 | L_OWNER[last]="$CHANGE_OWNER" |
| 2172 | for ((k = 0; k < last; k++)); do |
| 2173 | [ "${L_KIND[k]}" = change ] || continue |
| 2174 | line="$(valley-go resolve "${L_NAME[k]}")" || return 1 |
| 2175 | read_resolved "$line" rkind id handle version rhead branch rstate owner |
| 2176 | if [ "$rkind" != change ] || [ "$rhead" != "${L_HEAD[k]}" ]; then |
| 2177 | refuse "change ${L_NAME[k]}, under $TOP_NAME, moved while the stack was read" |
| 2178 | return 1 |
| 2179 | fi |
| 2180 | L_ID[k]="$id" L_VERSION[k]="$version" L_BRANCH[k]="$branch" L_STATE[k]="$rstate" |
| 2181 | L_OWNER[k]="$owner" |
| 2182 | done |
| 2183 | } |
| 2184 | |
| 2185 | # use_layer <k>: act on layer k as review acts on a change it was given, |
| 2186 | # binding [a]pprove to the head the stack shows. A branch under the top is |
| 2187 | # read again as origin/<branch>, which no handle can shadow. |
| 2188 | use_layer() { |
| 2189 | local k="$1" |
| 2190 | CHANGE_ID="${L_ID[k]}" CHANGE_VERSION="${L_VERSION[k]}" CHANGE_BRANCH="${L_BRANCH[k]}" |
| 2191 | CHANGE_STATE="${L_STATE[k]}" CHANGE_OWNER="${L_OWNER[k]}" CHANGE_HANDLE="" CHANGE_HEAD="" |
| 2192 | CHANGE_NAME="origin/${L_NAME[k]}" |
| 2193 | [ "$k" -lt $((${#L_NAME[@]} - 1)) ] || CHANGE_NAME="$TOP_NAME" |
| 2194 | if [ -n "$CHANGE_ID" ]; then |
| 2195 | CHANGE_NAME="${L_NAME[k]}" CHANGE_HANDLE="${L_NAME[k]}" CHANGE_HEAD="${L_HEAD[k]}" |
| 2196 | fi |
| 2197 | bind_review |
| 2198 | [ -n "$CHANGE_ID" ] || APPROVE_HEAD="${L_HEAD[k]}" |
| 2199 | } |
| 2200 | |
| 2201 | # notes_key <k>: the branch layer k's notes are kept under. A change whose |
| 2202 | # active version names no branch keeps them under its handle. |
| 2203 | notes_key() { |
| 2204 | printf '%s' "${L_BRANCH[$1]:-${L_NAME[$1]}}" |
| 2205 | } |
| 2206 | |
| 2207 | # layer_label <k>: how a layer is named in the list: its name, then |
| 2208 | # branch, or the change's state. |
| 2209 | layer_label() { |
| 2210 | local k="$1" |
| 2211 | if [ "${L_KIND[k]}" = branch ]; then |
| 2212 | printf '%s (branch)' "${L_NAME[k]}" |
| 2213 | else |
| 2214 | printf '%s (%s)' "${L_NAME[k]}" "${L_STATE[k]}" |
| 2215 | fi |
| 2216 | } |
| 2217 | |
| 2218 | # What is coming: the layers bottom first, the lower layers the integrator |
| 2219 | # would refuse, and what each answer lands. |
| 2220 | summarize_stack() { |
| 2221 | local n="${#L_NAME[@]}" k |
| 2222 | printf '%s is a stack of %d changes; reviewing from the bottom, each against the one below it:\n' \ |
| 2223 | "${L_NAME[n - 1]}" "$n" |
| 2224 | for ((k = 0; k < n; k++)); do |
| 2225 | printf ' %d. %s: %s\n' "$((k + 1))" "$(layer_label "$k")" "${L_TITLE[k]}" |
| 2226 | done |
| 2227 | for ((k = 0; k < n - 1; k++)); do |
| 2228 | case "${L_STATE[k]}" in |
| 2229 | draft | withdrawn) |
| 2230 | printf 'warning: %s, change %d of %d, is %s. The integrator refuses to land it or anything above it (refused-not-ready).\n' \ |
| 2231 | "${L_NAME[k]}" "$((k + 1))" "$n" "$([ "${L_STATE[k]}" = draft ] && echo 'a draft' || echo withdrawn)" |
| 2232 | ;; |
| 2233 | esac |
| 2234 | done |
| 2235 | printf 'read each layer and go on with [n]; approving %s at the top lands all %d. [a] on a lower layer lands it and the layers under it, and ends the review.\n' \ |
| 2236 | "${L_NAME[n - 1]}" "$n" |
| 2237 | } |
| 2238 | |
| 2239 | # base_stack: before any layer is paged, a stack whose bottom does not |
| 2240 | # fast-forward from main is offered [b]ase, which rebases every layer as |
| 2241 | # the rebase verb does (status/rebase.go), or [s]top. After a rebase the |
| 2242 | # stack is read again and listed again, so the heads paged are the heads |
| 2243 | # approved. A rebase refused or left unfinished has said what happened, |
| 2244 | # and only [s]top is offered then. Fails when the review stops here. |
| 2245 | base_stack() { |
| 2246 | local n choice offered="[b]ase" stopped="; nothing done" |
| 2247 | while ! git merge-base --is-ancestor refs/remotes/origin/main "${L_HEAD[0]}"; do |
| 2248 | n="${#L_NAME[@]}" |
| 2249 | if [ -n "$offered" ]; then |
| 2250 | printf '%s, at the bottom, does not fast-forward from main: [b]ase all %d onto main, [s]top? ' \ |
| 2251 | "${L_NAME[0]}" "$n" |
| 2252 | else |
| 2253 | printf 'the stack under %s is not rebased: [s]top? ' "${L_NAME[n - 1]}" |
| 2254 | fi |
| 2255 | read -r choice || die "stdin closed before an answer; nothing done" |
| 2256 | case "$choice" in |
| 2257 | b) |
| 2258 | if [ -z "$offered" ]; then |
| 2259 | echo "answer s" |
| 2260 | elif rebase_shown "${L_HEAD[n - 1]}"; then |
| 2261 | fetch_origin |
| 2262 | if [ -n "$CHANGE_ID" ]; then |
| 2263 | resolve_change "$CHANGE_ID" || die "could not read change $CHANGE_HANDLE again after the rebase" |
| 2264 | fi |
| 2265 | bind_review |
| 2266 | read_stack || die "could not read the stack under $TOP_NAME again after the rebase" |
| 2267 | summarize_stack |
| 2268 | else |
| 2269 | offered="" stopped="" |
| 2270 | fi |
| 2271 | ;; |
| 2272 | s) |
| 2273 | echo "stopped reviewing ${L_NAME[n - 1]} before its first layer$stopped" |
| 2274 | return 1 |
| 2275 | ;; |
| 2276 | *) echo "answer ${offered:+b or }s" ;; |
| 2277 | esac |
| 2278 | done |
| 2279 | } |
| 2280 | |
| 2281 | # layer_heading <k>: the line that heads layer k wherever it is shown. |
| 2282 | layer_heading() { |
| 2283 | local k="$1" below=main |
| 2284 | [ "$k" -eq 0 ] || below="${L_NAME[k - 1]}" |
| 2285 | printf 'change %d of %d: %s on %s' "$((k + 1))" "${#L_NAME[@]}" "${L_NAME[k]}" "$below" |
| 2286 | } |
| 2287 | |
| 2288 | # layer_held <k>: why layer k can't be approved, when it or a layer under |
| 2289 | # it is not ready. A layer that is not ready says so as a change alone |
| 2290 | # does (not_ready). A layer above one says which: approving it would land |
| 2291 | # that layer too, and the integrator lands nothing that would bring a |
| 2292 | # version of a change that is not ready. Prints nothing, and fails, when |
| 2293 | # every layer up to k is ready or a plain branch. |
| 2294 | layer_held() { |
| 2295 | local k="$1" j |
| 2296 | not_ready "${L_NAME[k]}" "${L_VERSION[k]}" "${L_STATE[k]}" "${L_OWNER[k]}" && return |
| 2297 | for ((j = k - 1; j >= 0; j--)); do |
| 2298 | case "${L_STATE[j]}" in |
| 2299 | draft | withdrawn) |
| 2300 | printf "it would bring %s, change %d of %d, which is %s, so it can't be approved yet." \ |
| 2301 | "${L_NAME[j]}" "$((j + 1))" "${#L_NAME[@]}" "$([ "${L_STATE[j]}" = draft ] && echo 'a draft' || echo withdrawn)" |
| 2302 | return |
| 2303 | ;; |
| 2304 | esac |
| 2305 | done |
| 2306 | return 1 |
| 2307 | } |
| 2308 | |
| 2309 | # page_layer <k>: show layer k, as show_review shows a change alone. The |
| 2310 | # layer shown is compared with its own change's approved version, so a |
| 2311 | # lower layer is set against its own approval, not the top's. Its whole |
| 2312 | # diff is against the layer below it. The notes taken in it are kept |
| 2313 | # under its own branch. When it can't be approved, layer_held's line |
| 2314 | # follows its heading, before the layer's knots. |
| 2315 | page_layer() { |
| 2316 | local k="$1" heading held |
| 2317 | NOTES=() |
| 2318 | heading="$(layer_heading "$k")" |
| 2319 | if held="$(layer_held "$k")"; then heading+=$'\n'"$held"; fi |
| 2320 | REVIEW_TMP="$(mktemp -d)" |
| 2321 | compare_shown "${L_ID[k]}" "${L_VERSION[k]}" "${L_HEAD[k]}" |
| 2322 | show_compared "$(notes_key "$k")" "${L_HEAD[k]}" "$heading" "$(does_of "${L_ID[k]}")" layer_diff "$k" |
| 2323 | keep_layer_notes "$k" |
| 2324 | } |
| 2325 | |
| 2326 | # view_layer <k>: [v]iew full diff for layer k: its whole diff against |
| 2327 | # the layer below it. |
| 2328 | view_layer() { |
| 2329 | local k="$1" |
| 2330 | NOTES=() |
| 2331 | REVIEW_TMP="$(mktemp -d)" |
| 2332 | { |
| 2333 | layer_heading "$k" |
| 2334 | printf '\n' |
| 2335 | layer_diff "$k" |
| 2336 | } > "$REVIEW_TMP/review.diff" |
| 2337 | page_buffer "$(notes_key "$k")" "$(git rev-parse --short "${L_HEAD[k]}")" |
| 2338 | keep_layer_notes "$k" |
| 2339 | } |
| 2340 | |
| 2341 | # keep_layer_notes <k>: add the notes just taken to layer k's. |
| 2342 | keep_layer_notes() { |
| 2343 | [ "${#NOTES[@]}" -gt 0 ] || return 0 |
| 2344 | L_NOTES[$1]="${L_NOTES[$1]:+${L_NOTES[$1]}$'\n'}$(printf '%s\n' "${NOTES[@]}")" |
| 2345 | } |
| 2346 | |
| 2347 | # layer_diff <k>: layer k's whole diff: its log and its diff against the |
| 2348 | # layer below it. |
| 2349 | layer_diff() { |
| 2350 | local k="$1" below=main head base sha on |
| 2351 | [ "$k" -eq 0 ] || below="${L_NAME[k - 1]}" |
| 2352 | head="${L_HEAD[k]}" base="${L_BASE[k]}" |
| 2353 | sha="$(git rev-parse --short "$head")" |
| 2354 | on="$below at $(git rev-parse --short "$base")" |
| 2355 | if [ "${L_KIND[k]}" = branch ]; then |
| 2356 | printf 'branch %s, log against %s:\n\n' "${L_NAME[k]}" "$on" |
| 2357 | elif [ -n "${L_BRANCH[k]}" ]; then |
| 2358 | printf 'change %s v%s at %s, on branch %s, log against %s:\n\n' \ |
| 2359 | "${L_NAME[k]}" "${L_VERSION[k]}" "$sha" "${L_BRANCH[k]}" "$on" |
| 2360 | else |
| 2361 | printf 'change %s v%s at %s, log against %s:\n\n' "${L_NAME[k]}" "${L_VERSION[k]}" "$sha" "$on" |
| 2362 | fi |
| 2363 | git --no-pager log --oneline "$base..$head" |
| 2364 | printf '\n' |
| 2365 | git --no-pager diff "$base...$head" |
| 2366 | } |
| 2367 | |
| 2368 | # Show each layer under the top and ask after each, naming the layer and |
| 2369 | # its place: [n]ext layer goes on to the layer above, [a]pprove approves |
| 2370 | # that layer at the head shown, which lands it and every layer under it, |
| 2371 | # as the prompt names them, [v]iew full diff pages the layer's whole diff, |
| 2372 | # and [s]top ends the review. [r]eject is not offered under the top: |
| 2373 | # deleting a lower layer's branch would leave the layers above still |
| 2374 | # holding it. [a]pprove is not offered at or above a layer that is not |
| 2375 | # ready, and the prompt says why in its place (layer_held). After the |
| 2376 | # top, its verdict is taken as for any change, with a prompt that names |
| 2377 | # the top and says approving it lands all n. |
| 2378 | review_stack() { |
| 2379 | local n top k choice answers failed lands held |
| 2380 | summarize_stack |
| 2381 | base_stack || return 0 |
| 2382 | printf '\n' |
| 2383 | n="${#L_NAME[@]}" top=$((${#L_NAME[@]} - 1)) |
| 2384 | for ((k = 0; k < top; k++)); do |
| 2385 | page_layer "$k" |
| 2386 | case "$k" in |
| 2387 | 0) lands="${L_NAME[0]} alone" ;; |
| 2388 | 1) lands="${L_NAME[0]} and ${L_NAME[1]}" ;; |
| 2389 | *) lands="the $((k + 1)) up to ${L_NAME[k]}" ;; |
| 2390 | esac |
| 2391 | held="$(layer_held "$k")" || held="" |
| 2392 | while true; do |
| 2393 | failed="" |
| 2394 | [ "$CHECKS_FAILED_AT" != "${L_HEAD[k]}" ] || failed="$(git rev-parse --short "${L_HEAD[k]}")" |
| 2395 | if [ -n "$held" ]; then |
| 2396 | printf '%s, %d of %d: %s [n]ext layer, [v]iew full diff, [s]top? ' \ |
| 2397 | "${L_NAME[k]}" "$((k + 1))" "$n" "$held" |
| 2398 | answers="n, v, or s" |
| 2399 | elif [ -n "$failed" ]; then |
| 2400 | printf '%s, %d of %d: its checks failed at %s, so approving needs a new head: [n]ext layer, [v]iew full diff, [s]top? ' \ |
| 2401 | "${L_NAME[k]}" "$((k + 1))" "$n" "$failed" |
| 2402 | answers="n, v, or s" |
| 2403 | else |
| 2404 | printf '%s, %d of %d: [n]ext layer, [a]pprove %s, [v]iew full diff, [s]top? ' \ |
| 2405 | "${L_NAME[k]}" "$((k + 1))" "$n" "$lands" |
| 2406 | answers="n, a, v, or s" |
| 2407 | fi |
| 2408 | read -r choice || die "stdin closed before an answer; nothing done" |
| 2409 | case "$choice" in |
| 2410 | n) break ;; |
| 2411 | a) |
| 2412 | if [ -n "$failed" ] || [ -n "$held" ]; then |
| 2413 | echo "answer $answers" |
| 2414 | continue |
| 2415 | fi |
| 2416 | use_layer "$k" |
| 2417 | if approve "$CHANGE_BRANCH"; then |
| 2418 | return |
| 2419 | fi |
| 2420 | use_layer "$top" |
| 2421 | ;; |
| 2422 | v) view_layer "$k" ;; |
| 2423 | s) |
| 2424 | echo "stopped reviewing ${L_NAME[top]} at change $((k + 1)) of $n, ${L_NAME[k]}; nothing done" |
| 2425 | return |
| 2426 | ;; |
| 2427 | *) echo "answer $answers" ;; |
| 2428 | esac |
| 2429 | done |
| 2430 | done |
| 2431 | page_layer "$top" |
| 2432 | use_layer "$top" |
| 2433 | decide "${L_BRANCH[top]}" "${L_NAME[top]}" "$n" |
| 2434 | } |
| 2435 | |
| 2436 | # The notes taken on every layer, each under its own branch. |
| 2437 | print_stack_notes() { |
| 2438 | local k |
| 2439 | for ((k = 0; k < ${#L_NAME[@]}; k++)); do |
| 2440 | [ -n "${L_NOTES[k]}" ] || continue |
| 2441 | mapfile -t NOTES <<< "${L_NOTES[k]}" |
| 2442 | print_notes "$(notes_key "$k")" |
| 2443 | done |
| 2444 | } |
| 2445 | |
| 2446 | cmd_review() { |
| 2447 | local name="" verbose=() arg |
| 2448 | for arg in "$@"; do |
| 2449 | case "$arg" in |
| 2450 | -v) verbose=(-v) ;; |
| 2451 | -*) die "usage: valley review [-v] <change>" ;; |
| 2452 | *) |
| 2453 | [ -z "$name" ] || die "usage: valley review [-v] <change>" |
| 2454 | name="$arg" |
| 2455 | ;; |
| 2456 | esac |
| 2457 | done |
| 2458 | [ -n "$name" ] || die "usage: valley review [-v] <change>" |
| 2459 | set -- "$name" |
| 2460 | resolve_change "$1" || exit 1 |
| 2461 | need_branch review |
| 2462 | local branch="$CHANGE_BRANCH" |
| 2463 | TOP_NAME="$1" |
| 2464 | fetch_origin |
| 2465 | git rev-parse --verify --quiet "refs/remotes/origin/$branch" >/dev/null \ |
| 2466 | || die "no such branch on origin: $branch" |
| 2467 | bind_review |
| 2468 | trap cleanup_scratch EXIT |
| 2469 | trap 'exit 130' INT TERM |
| 2470 | # The verdict disposes of the change; the notes are what the human made |
| 2471 | # of it, so they are printed whatever the verdict was. A stack that |
| 2472 | # cannot be read is reviewed whole against main, as a change with |
| 2473 | # nothing under it is. |
| 2474 | if ! read_stack; then |
| 2475 | printf 'valley: warning: the stack under %s could not be read; reviewing it whole, against main\n' "$1" >&2 |
| 2476 | L_NAME=() |
| 2477 | fi |
| 2478 | read_does "${verbose[@]}" |
| 2479 | if [ "${#L_NAME[@]}" -gt 1 ]; then |
| 2480 | review_stack |
| 2481 | print_stack_notes |
| 2482 | else |
| 2483 | read_and_decide "$branch" |
| 2484 | print_notes "$branch" |
| 2485 | fi |
| 2486 | } |
| 2487 | |
| 2488 | # approve and rebase are review's [a]pprove and [b]ase without the pager, |
| 2489 | # for a front end that has shown the change its own way. Each runs the |
| 2490 | # code its key runs, approve and valley-go rebase, so the verb and the key |
| 2491 | # cannot drift apart. A change is named as resolve_change reads it, and |
| 2492 | # rebase reads its name the same way, in valley-go. |
| 2493 | # |
| 2494 | # approve of a change with a record approves exactly one head (bind_head): |
| 2495 | # --head's, or the active version's once the operator confirms it on a |
| 2496 | # terminal. A plain branch is approved at its tip on origin, as the fetch |
| 2497 | # left it, and --head, when given, must be that tip. |
| 2498 | cmd_approve() { |
| 2499 | local name="" want="" |
| 2500 | local usage="usage: valley approve [--head <sha>] <change>" |
| 2501 | while [ $# -gt 0 ]; do |
| 2502 | case "$1" in |
| 2503 | --head) |
| 2504 | [ $# -ge 2 ] || die "$usage" |
| 2505 | want="$2" |
| 2506 | shift 2 |
| 2507 | ;; |
| 2508 | --head=*) |
| 2509 | want="${1#--head=}" |
| 2510 | shift |
| 2511 | ;; |
| 2512 | -*) die "$usage" ;; |
| 2513 | *) |
| 2514 | [ -z "$name" ] || die "$usage" |
| 2515 | name="$1" |
| 2516 | shift |
| 2517 | ;; |
| 2518 | esac |
| 2519 | done |
| 2520 | [ -n "$name" ] || die "$usage" |
| 2521 | resolve_change "$name" || exit 1 |
| 2522 | local branch="$CHANGE_BRANCH" tip |
| 2523 | fetch_origin |
| 2524 | trap cleanup_scratch EXIT |
| 2525 | trap 'exit 130' INT TERM |
| 2526 | if [ -n "$CHANGE_ID" ]; then |
| 2527 | bind_head "$want" || exit 1 |
| 2528 | else |
| 2529 | tip="$(git rev-parse --verify --quiet "refs/remotes/origin/$branch")" \ |
| 2530 | || die "no such branch on origin: $branch" |
| 2531 | if [ -n "$want" ] && [ "$(git rev-parse --verify --quiet "$want^{commit}" || true)" != "$tip" ]; then |
| 2532 | die "origin/$branch is at $tip, not $want; nothing published" |
| 2533 | fi |
| 2534 | fi |
| 2535 | # In a condition, as the review prompt calls it: approve checks each of |
| 2536 | # its own steps, and has already said why when it refuses. |
| 2537 | approve "$branch" || exit 1 |
| 2538 | } |
| 2539 | |
| 2540 | cmd_rebase() { |
| 2541 | case "${1:-}" in -*) die "usage: valley rebase <change>" ;; esac |
| 2542 | [ $# -eq 1 ] || die "usage: valley rebase <change>" |
| 2543 | command -v valley-go > /dev/null 2>&1 \ |
| 2544 | || die "review, approve and rebase read the change's record with the valley-go helper; nix run .#valley bundles it" |
| 2545 | fetch_origin |
| 2546 | exec valley-go rebase "$1" |
| 2547 | } |
| 2548 | |
| 2549 | # tail and replay are thin wrappers over the nats CLI — anything they do, |
| 2550 | # plain nats and git can do. They are the only verbs needing more than git |
| 2551 | # and coreutils. |
| 2552 | need_nats() { |
| 2553 | command -v nats >/dev/null 2>&1 \ |
| 2554 | || die "this verb needs the nats CLI (natscli); nix run .#valley bundles it" |
| 2555 | } |
| 2556 | |
| 2557 | cmd_tail() { |
| 2558 | [ $# -le 1 ] || die "usage: valley tail [subject]" |
| 2559 | need_nats |
| 2560 | exec nats sub "${1:-valley.>}" |
| 2561 | } |
| 2562 | |
| 2563 | # One ref-updated event per existing ref, from git for-each-ref alone. A |
| 2564 | # ref's prior value is not derivable from the repository, so `old` is the |
| 2565 | # all-zero id — the same payload a push creating the ref would produce. |
| 2566 | # The payload construction mirrors the host module's publisher hook |
| 2567 | # (nix/valley-host.nix); change one only with the other. |
| 2568 | cmd_replay() { |
| 2569 | [ $# -le 1 ] || die "usage: valley replay [repo-dir]" |
| 2570 | need_nats |
| 2571 | local dir="${1:-.}" repo sha ref event count=0 |
| 2572 | git -C "$dir" rev-parse --git-dir >/dev/null 2>&1 \ |
| 2573 | || die "not a git repository: $dir" |
| 2574 | if [ "$(git -C "$dir" rev-parse --is-bare-repository)" = true ]; then |
| 2575 | repo="$(basename "$(git -C "$dir" rev-parse --absolute-git-dir)" .git)" |
| 2576 | else |
| 2577 | repo="$(basename "$(git -C "$dir" rev-parse --show-toplevel)")" |
| 2578 | fi |
| 2579 | while read -r sha ref; do |
| 2580 | # " is the only JSON-significant character a refname can contain (git |
| 2581 | # forbids \ and control characters); names and object ids are safe. |
| 2582 | event="$(printf '{"event":"ref-updated","repo":"%s","ref":"%s","old":"%s","new":"%s"}' \ |
| 2583 | "$repo" "${ref//\"/\\\"}" "${sha//?/0}" "$sha")" |
| 2584 | nats pub "valley.git.$repo.ref-updated" "$event" >/dev/null |
| 2585 | count=$((count + 1)) |
| 2586 | done < <(git -C "$dir" for-each-ref --format='%(objectname) %(refname)') |
| 2587 | echo "replayed $count refs of $repo onto the bus" |
| 2588 | } |
| 2589 | |
| 2590 | # ---------------------------------------------------------------------- |
| 2591 | # checks — the verification-policy deriver (dcr-f41f718). It composes the |
| 2592 | # instance layer with the project layer, matches every side of a diff |
| 2593 | # against the composed classes, and prints the check names the policy |
| 2594 | # requires of the change, with the class or classes that made each one |
| 2595 | # required. It reports: no check is run, nothing is gated, nothing is |
| 2596 | # written. |
| 2597 | # |
| 2598 | # Composition is CUE's, never this script's. The whole of the mandatory- |
| 2599 | # versus-overridable distinction is unification, so reimplementing it here |
| 2600 | # would be reimplementing the decision. What this script does that CUE |
| 2601 | # cannot is glob matching and reading a diff. |
| 2602 | |
| 2603 | need_cue() { |
| 2604 | command -v cue >/dev/null 2>&1 \ |
| 2605 | || die "this verb needs the cue CLI; nix run .#valley bundles it" |
| 2606 | } |
| 2607 | |
| 2608 | # One glob segment as an ERE fragment: `*` and `?` are wildcards that never |
| 2609 | # cross a `/`, and every ERE metacharacter is escaped. |
| 2610 | segment_ere() { |
| 2611 | local seg="$1" out="" i c |
| 2612 | for ((i = 0; i < ${#seg}; i++)); do |
| 2613 | c="${seg:i:1}" |
| 2614 | case "$c" in |
| 2615 | '*') out+='[^/]*' ;; |
| 2616 | '?') out+='[^/]' ;; |
| 2617 | '.' | '^' | '$' | '+' | '(' | ')' | '[' | ']' | '{' | '}' | '|' | \\) out+="\\$c" ;; |
| 2618 | *) out+="$c" ;; |
| 2619 | esac |
| 2620 | done |
| 2621 | printf '%s' "$out" |
| 2622 | } |
| 2623 | |
| 2624 | # A path glob as an anchored ERE, segment by segment. A `**` segment matches |
| 2625 | # any number of segments including none, so it swallows the separator that |
| 2626 | # would otherwise follow it and `a/**/b` matches `a/b`; every other segment |
| 2627 | # matches exactly one. |
| 2628 | glob_ere() { |
| 2629 | local ere='^' i last |
| 2630 | local -a segs=() |
| 2631 | IFS=/ read -r -a segs <<<"$1" |
| 2632 | last=$((${#segs[@]} - 1)) |
| 2633 | for ((i = 0; i <= last; i++)); do |
| 2634 | if [ "${segs[i]}" = '**' ]; then |
| 2635 | if [ "$i" -eq "$last" ]; then ere+='.*'; else ere+='(.*/)?'; fi |
| 2636 | else |
| 2637 | ere+="$(segment_ere "${segs[i]}")" |
| 2638 | [ "$i" -eq "$last" ] || ere+='/' |
| 2639 | fi |
| 2640 | done |
| 2641 | printf '%s$' "$ere" |
| 2642 | } |
| 2643 | |
| 2644 | # The composed policy as one tab-separated line per covered pattern, per |
| 2645 | # required check, per class's carry mode, and per field of the check |
| 2646 | # catalogue, so the shell never parses CUE. Every line is three fields — a |
| 2647 | # kind, a name, and a value — whatever the kind, so one reader takes them |
| 2648 | # all. The field is hidden because the schema's sentinel rejects unknown |
| 2649 | # top-level fields, and `unclassified` is rendered as a class of that name: |
| 2650 | # it is one more source a check can come from. A class's carry mode is the |
| 2651 | # one the schema resolves (`_mode`), so the shell only picks the strictest. |
| 2652 | # |
| 2653 | # Each line is held to its shape (`line`): three fields, two tabs, and no |
| 2654 | # other control character. A newline in a field would end its line and |
| 2655 | # start one of the policy's choosing, and a tab would add a field. The |
| 2656 | # schema already refuses control characters in every string rendered here, |
| 2657 | # so this is a second wall: a line out of shape fails the render, and the |
| 2658 | # deriver refuses a render that fails. The whole line is checked because a |
| 2659 | # bound on a value inside an interpolation does not fail the string. |
| 2660 | # |
| 2661 | # The catalogue is rendered because a caller that runs a check needs what |
| 2662 | # the policy says a check IS — its runner, and the attribute or command |
| 2663 | # line that runner takes — and a tool that groups the required checks |
| 2664 | # needs their cost. `checks` reads only the cost, to group the required |
| 2665 | # checks by it; approve turns the rest into the arguments attest is given. |
| 2666 | render_cue() { |
| 2667 | cat <<'EOF' |
| 2668 | package verification |
| 2669 | |
| 2670 | import ( |
| 2671 | "list" |
| 2672 | "strings" |
| 2673 | ) |
| 2674 | |
| 2675 | let line = =~"^[^\\x00-\\x1f\\x7f]+\t[^\\x00-\\x1f\\x7f]+\t[^\\x00-\\x1f\\x7f]+$" |
| 2676 | |
| 2677 | _render: strings.Join(list.Concat([[ |
| 2678 | for cn, c in policy.classes |
| 2679 | for p, on in c.paths if on {"path\t\(cn)\t\(p)" & line} |
| 2680 | ], [ |
| 2681 | for cn, c in policy.classes |
| 2682 | for k, on in c.requires if on {"requires\t\(cn)\t\(k)" & line} |
| 2683 | ], [ |
| 2684 | for k, on in policy.unclassified if on {"requires\tunclassified\t\(k)" & line} |
| 2685 | ], [ |
| 2686 | for cn, m in _mode {"carry\t\(cn)\t\(m)" & line} |
| 2687 | ], [ |
| 2688 | for n, c in policy.checks {"runner\t\(n)\t\(c.runner)" & line} |
| 2689 | ], [ |
| 2690 | for n, c in policy.checks if c.runner == "nix" {"attribute\t\(n)\t\(c.attribute)" & line} |
| 2691 | ], [ |
| 2692 | for n, c in policy.checks if c.runner == "command" {"command\t\(n)\t\(c.command)" & line} |
| 2693 | ], [ |
| 2694 | for n, c in policy.checks {"cost\t\(n)\t\(c.cost)" & line} |
| 2695 | ]]), "\n") |
| 2696 | EOF |
| 2697 | } |
| 2698 | |
| 2699 | # The composed layer, and a scratch directory for the documents the |
| 2700 | # derivation writes into the composition. Globals so the probe below can |
| 2701 | # see them. |
| 2702 | POLICY_LAYER=() |
| 2703 | POLICY_TMP="" |
| 2704 | |
| 2705 | cleanup_policy_tmp() { |
| 2706 | [ -n "$POLICY_TMP" ] || return 0 |
| 2707 | rm -rf "$POLICY_TMP" |
| 2708 | POLICY_TMP="" |
| 2709 | } |
| 2710 | |
| 2711 | # Where the schema and the two policy layers come from in a project |
| 2712 | # following the convention (dcr-f41f718). |
| 2713 | # |
| 2714 | # The client derives from exactly the sources the integrator uses |
| 2715 | # (integrator/policy.go), and the working tree is not among them. The |
| 2716 | # project's own layer is read from the TARGET's tip, the pointer naming the |
| 2717 | # owning valley is read from the target's tip, and the floor is read from |
| 2718 | # that valley's integrated tip. A change never supplies the policy that |
| 2719 | # gates it, and neither does whoever happens to be reviewing it: the |
| 2720 | # reviewer's checkout may be any branch, however stale, and derivation |
| 2721 | # gives the same answer from all of them. |
| 2722 | # |
| 2723 | # The schema is not a policy layer. It is the shape a policy is written in, |
| 2724 | # and it ships with this tool: the package sets VALLEY_VERIFICATION_SCHEMA |
| 2725 | # to the copy it was built with, as the integrator's package does. Only |
| 2726 | # the-valley's own tree carries schema/, so this script reads the schema |
| 2727 | # there only when it runs straight from such a checkout with the variable |
| 2728 | # unset. |
| 2729 | # |
| 2730 | # Neither layer is ever the worked example under examples/policy/ — a gate |
| 2731 | # derived from an example is a gate nobody wrote. `checks` takes an option |
| 2732 | # for each, so a reader can point it at the example or anywhere else on |
| 2733 | # purpose; approve takes no such option, because a request is filed |
| 2734 | # against the policy that governs the project and nowhere else. |
| 2735 | # |
| 2736 | # Neither layer is a path in this tree, so policy_defaults leaves both |
| 2737 | # empty and the two resolvers answer for them. That split is also what |
| 2738 | # keeps `checks --instance DIR` from fetching a repository whose layer it |
| 2739 | # was told to ignore, and `checks --project DIR` from reading a tip whose |
| 2740 | # layer it was told to ignore. |
| 2741 | POLICY_SCHEMA="" |
| 2742 | POLICY_INSTANCE="" |
| 2743 | POLICY_INSTANCE_LABEL="" |
| 2744 | POLICY_PROJECT="" |
| 2745 | POLICY_PROJECT_LABEL="" |
| 2746 | policy_defaults() { |
| 2747 | POLICY_SCHEMA="${VALLEY_VERIFICATION_SCHEMA:-$1/schema/verification.cue}" |
| 2748 | POLICY_INSTANCE="" |
| 2749 | POLICY_INSTANCE_LABEL="" |
| 2750 | POLICY_PROJECT="" |
| 2751 | POLICY_PROJECT_LABEL="" |
| 2752 | } |
| 2753 | |
| 2754 | # The floor, copied out of a fetch of the owning valley's repository, and |
| 2755 | # the project's own layer, copied out of the target's tip. Globals, so the |
| 2756 | # traps clean them up whatever happens. |
| 2757 | INSTANCE_TMP="" |
| 2758 | cleanup_instance_tmp() { |
| 2759 | [ -n "$INSTANCE_TMP" ] || return 0 |
| 2760 | rm -rf "$INSTANCE_TMP" |
| 2761 | INSTANCE_TMP="" |
| 2762 | } |
| 2763 | |
| 2764 | PROJECT_TMP="" |
| 2765 | cleanup_project_tmp() { |
| 2766 | [ -n "$PROJECT_TMP" ] || return 0 |
| 2767 | rm -rf "$PROJECT_TMP" |
| 2768 | PROJECT_TMP="" |
| 2769 | } |
| 2770 | |
| 2771 | # The directory each layer lives in, the file naming this project's owning |
| 2772 | # valley, and the ref of that valley whose tip carries the floor. The floor |
| 2773 | # resolves at the integrated tip, because a floor a project could pin is |
| 2774 | # not a floor (dcr-f41f718). |
| 2775 | INSTANCE_DIR=policy/instance |
| 2776 | PROJECT_DIR=policy/project |
| 2777 | VALLEY_POINTER=policy/valley |
| 2778 | VALLEY_POINTER_REF=refs/heads/main |
| 2779 | |
| 2780 | # The *.cue documents of one directory at one tree-ish, copied out of a |
| 2781 | # repository into a directory on disk. Reading a tip with ls-tree and |
| 2782 | # cat-file rather than checking it out is what lets the layers come from |
| 2783 | # the target while the checkout stays on whatever branch the reviewer is |
| 2784 | # on. 0 copied something, 1 the directory holds no *.cue, 2 the read |
| 2785 | # failed. |
| 2786 | # |
| 2787 | # -z because a path may hold anything but NUL, and *.cue because the layer |
| 2788 | # is the directory's documents — the same reading the composer takes. |
| 2789 | # |
| 2790 | # The listing is written out and its status checked before it is read. A |
| 2791 | # listing read straight from a process substitution that failed reads as a |
| 2792 | # directory holding nothing, and for the project layer that is the floor |
| 2793 | # alone: fewer checks required, from a read that never happened. The listing's |
| 2794 | # name is not a *.cue name, so the composer never reads it. |
| 2795 | copy_layer() { |
| 2796 | local gitdir="$1" treeish="$2" dir="$3" dest="$4" path name copied=0 |
| 2797 | local listing="$dest/.listing" |
| 2798 | git -C "$gitdir" ls-tree --name-only -z "$treeish" "$dir/" > "$listing" \ |
| 2799 | || return 2 |
| 2800 | while IFS= read -r -d '' path; do |
| 2801 | case "$path" in *.cue) ;; *) continue ;; esac |
| 2802 | name="${path##*/}" |
| 2803 | git -C "$gitdir" cat-file blob "$treeish:$path" > "$dest/$name" || return 2 |
| 2804 | copied=1 |
| 2805 | done < "$listing" |
| 2806 | rm -f "$listing" |
| 2807 | [ "$copied" -eq 1 ] |
| 2808 | } |
| 2809 | |
| 2810 | # The ref whose tip supplies the policy, printed the way a person names it, |
| 2811 | # with the commit it stood at. Staleness in a remote-tracking ref is a real |
| 2812 | # thing to see, so the report says which commit the layers came out of |
| 2813 | # rather than only which ref. A ref that cannot be read fails, so that no |
| 2814 | # report names a tip nobody read. |
| 2815 | target_label() { |
| 2816 | local ref="$1" short |
| 2817 | short="$(git rev-parse --short "$ref")" || return 1 |
| 2818 | ref="${ref#refs/remotes/}" |
| 2819 | ref="${ref#refs/heads/}" |
| 2820 | printf '%s %s' "$ref" "$short" |
| 2821 | } |
| 2822 | |
| 2823 | # Resolve the project's own layer, out of the target's tip, into |
| 2824 | # POLICY_PROJECT, and set POLICY_PROJECT_LABEL to where it came from. |
| 2825 | # |
| 2826 | # A target carrying no layer of its own is derived under the floor alone. |
| 2827 | # The project layer only ever adds (dcr-f41f718), so the identity of "adds" |
| 2828 | # is "adds nothing"; refusing instead would make a repository underivable |
| 2829 | # until its first policy commit, and that commit could not derive either. |
| 2830 | # This is the integrator's own reading (integrator/policy.go), and the two |
| 2831 | # have to agree about an absent layer as much as about a present one. |
| 2832 | # |
| 2833 | # Absence is answered with a document that adds nothing rather than with an |
| 2834 | # argument left off, because the composer is handed a directory of *.cue |
| 2835 | # and has no spelling for "no layer". |
| 2836 | # |
| 2837 | # It reports and returns rather than dying, for the reason derive_required |
| 2838 | # does: a refusal inside the review loop is not the end of the session. |
| 2839 | resolve_project_layer() { |
| 2840 | local root="$1" target="$2" rc at |
| 2841 | |
| 2842 | at="$(target_label "$target")" || { |
| 2843 | refuse "could not read $target; nothing derived" |
| 2844 | return 1 |
| 2845 | } |
| 2846 | cleanup_project_tmp |
| 2847 | PROJECT_TMP="$(mktemp -d)" || { |
| 2848 | refuse "no scratch directory to read the project layer into; nothing derived" |
| 2849 | return 1 |
| 2850 | } |
| 2851 | copy_layer "$root" "$target" "$PROJECT_DIR" "$PROJECT_TMP" && rc=0 || rc=$? |
| 2852 | case "$rc" in |
| 2853 | 0) |
| 2854 | POLICY_PROJECT="$PROJECT_TMP" |
| 2855 | POLICY_PROJECT_LABEL="$PROJECT_DIR@$at" |
| 2856 | ;; |
| 2857 | 1) |
| 2858 | printf 'package verification\n' > "$PROJECT_TMP/adds-nothing.cue" || { |
| 2859 | cleanup_project_tmp |
| 2860 | refuse "could not compose an empty project layer; nothing derived" |
| 2861 | return 1 |
| 2862 | } |
| 2863 | POLICY_PROJECT="$PROJECT_TMP" |
| 2864 | POLICY_PROJECT_LABEL="no $PROJECT_DIR@$at — the floor alone" |
| 2865 | ;; |
| 2866 | *) |
| 2867 | cleanup_project_tmp |
| 2868 | refuse "could not read $PROJECT_DIR/ at $at; nothing derived" |
| 2869 | return 1 |
| 2870 | ;; |
| 2871 | esac |
| 2872 | } |
| 2873 | |
| 2874 | # The first line of the pointer that is neither blank nor a comment. Read |
| 2875 | # in the shell so that naming an owning valley needs nothing beyond what |
| 2876 | # the rest of this script needs. |
| 2877 | pointer_url() { |
| 2878 | local line url="" |
| 2879 | while IFS= read -r line || [ -n "$line" ]; do |
| 2880 | line="${line%%#*}" |
| 2881 | line="${line#"${line%%[![:space:]]*}"}" |
| 2882 | line="${line%"${line##*[![:space:]]}"}" |
| 2883 | [ -n "$line" ] || continue |
| 2884 | url="$line" |
| 2885 | break |
| 2886 | done < "$1" |
| 2887 | printf '%s' "$url" |
| 2888 | } |
| 2889 | |
| 2890 | # Resolve the floor to a directory on disk, into POLICY_INSTANCE, and set |
| 2891 | # POLICY_INSTANCE_LABEL to where it came from — a temporary directory is |
| 2892 | # not an answer a reader can check. |
| 2893 | # |
| 2894 | # Every project has exactly one owning valley, and the floor is that |
| 2895 | # valley's (dcr-9b5da04). It is read from the valley's own repository and |
| 2896 | # never from the project's tree, which is the tree under judgment. Two |
| 2897 | # shapes, and the first is a special case of the second: |
| 2898 | # |
| 2899 | # - a valley's config repository carries policy/instance itself, and is |
| 2900 | # governed by that floor like every other project. It uses its own, |
| 2901 | # and fetches nothing. |
| 2902 | # - every other project names its owning valley at policy/valley. That |
| 2903 | # repository is fetched into scratch and its policy/instance is read |
| 2904 | # at the integrated tip. Fetching is all this needs, and anyone the |
| 2905 | # valley governs necessarily holds fetch on it. |
| 2906 | # |
| 2907 | # Both the pointer and a valley's own floor are read from the TARGET's tip |
| 2908 | # and never from the working tree. The pointer is a file a branch can edit, |
| 2909 | # so reading it from the branch would let a change name the valley that |
| 2910 | # gates it; reading it from the checkout would let whoever is reviewing |
| 2911 | # decide the same thing by accident, which is the failure this fixes. An |
| 2912 | # absent floor is refused — that is the direction absence defaults in for |
| 2913 | # this layer, because the floor is the minimum (dcr-f41f718). |
| 2914 | # |
| 2915 | # It reports and returns rather than dying, for the reason derive_required |
| 2916 | # does: a refusal inside the review loop is not the end of the session. |
| 2917 | resolve_instance_layer() { |
| 2918 | local root="$1" target="$2" url bare dest rc at |
| 2919 | at="$(target_label "$target")" || { |
| 2920 | refuse "could not read $target; nothing derived" |
| 2921 | return 1 |
| 2922 | } |
| 2923 | cleanup_instance_tmp |
| 2924 | INSTANCE_TMP="$(mktemp -d)" || { |
| 2925 | refuse "no scratch directory to read the floor into; nothing derived" |
| 2926 | return 1 |
| 2927 | } |
| 2928 | dest="$INSTANCE_TMP/instance" |
| 2929 | mkdir -p "$dest" |
| 2930 | |
| 2931 | copy_layer "$root" "$target" "$INSTANCE_DIR" "$dest" && rc=0 || rc=$? |
| 2932 | case "$rc" in |
| 2933 | 0) |
| 2934 | POLICY_INSTANCE="$dest" |
| 2935 | POLICY_INSTANCE_LABEL="$INSTANCE_DIR@$at (this repository is its own valley)" |
| 2936 | return 0 |
| 2937 | ;; |
| 2938 | 1) ;; # no floor of its own: the pointer names the valley that has one |
| 2939 | *) |
| 2940 | cleanup_instance_tmp |
| 2941 | refuse "could not read $INSTANCE_DIR/ at $at; nothing derived" |
| 2942 | return 1 |
| 2943 | ;; |
| 2944 | esac |
| 2945 | |
| 2946 | git -C "$root" cat-file blob "$target:$VALLEY_POINTER" > "$INSTANCE_TMP/pointer" 2> /dev/null || { |
| 2947 | cleanup_instance_tmp |
| 2948 | refuse "no owning valley: $VALLEY_POINTER names the repository whose $INSTANCE_DIR is this project's floor, and $at carries no such file. A valley's own config repository carries $INSTANCE_DIR instead. Nothing derived" |
| 2949 | return 1 |
| 2950 | } |
| 2951 | url="$(pointer_url "$INSTANCE_TMP/pointer")" |
| 2952 | [ -n "$url" ] || { |
| 2953 | cleanup_instance_tmp |
| 2954 | refuse "$VALLEY_POINTER at $at names no repository — it wants one line, the owning valley as git fetch takes it; nothing derived" |
| 2955 | return 1 |
| 2956 | } |
| 2957 | |
| 2958 | bare="$INSTANCE_TMP/valley.git" |
| 2959 | git init --quiet --bare "$bare" || { |
| 2960 | cleanup_instance_tmp |
| 2961 | refuse "no scratch repository to fetch the floor into; nothing derived" |
| 2962 | return 1 |
| 2963 | } |
| 2964 | git -C "$bare" fetch --quiet --depth 1 "$url" "$VALLEY_POINTER_REF" || { |
| 2965 | cleanup_instance_tmp |
| 2966 | refuse "could not fetch the owning valley $url at $VALLEY_POINTER_REF (named by $VALLEY_POINTER at $at); nothing derived" |
| 2967 | return 1 |
| 2968 | } |
| 2969 | |
| 2970 | copy_layer "$bare" FETCH_HEAD "$INSTANCE_DIR" "$dest" && rc=0 || rc=$? |
| 2971 | case "$rc" in |
| 2972 | 0) ;; |
| 2973 | 1) |
| 2974 | cleanup_instance_tmp |
| 2975 | refuse "the owning valley $url holds no *.cue in $INSTANCE_DIR/ at $VALLEY_POINTER_REF; nothing derived" |
| 2976 | return 1 |
| 2977 | ;; |
| 2978 | *) |
| 2979 | cleanup_instance_tmp |
| 2980 | refuse "could not read $INSTANCE_DIR/ from $url; nothing derived" |
| 2981 | return 1 |
| 2982 | ;; |
| 2983 | esac |
| 2984 | |
| 2985 | POLICY_INSTANCE="$dest" |
| 2986 | POLICY_INSTANCE_LABEL="$url $INSTANCE_DIR@$VALLEY_POINTER_REF" |
| 2987 | } |
| 2988 | |
| 2989 | # Is this check mandatory in this class, or a default the project left |
| 2990 | # alone? The bit is the form of the composed value, so the test is CUE's |
| 2991 | # own: a project supplying `false` either conflicts or unifies cleanly. |
| 2992 | # Answering by unifying rather than by inspecting syntax means the script |
| 2993 | # cannot disagree with `cue vet`. |
| 2994 | # |
| 2995 | # This is the one producer whose failure does not end the derivation. A |
| 2996 | # probe that fails for any other reason, or that cannot be written, reads as |
| 2997 | # mandatory, which is the strict answer, and the bit never changes which |
| 2998 | # checks are required. |
| 2999 | check_is_mandatory() { |
| 3000 | local class="$1" check="$2" field |
| 3001 | if [ "$class" = unclassified ]; then |
| 3002 | field="policy: unclassified: \"$check\": false" |
| 3003 | else |
| 3004 | field="policy: classes: \"$class\": requires: \"$check\": false" |
| 3005 | fi |
| 3006 | # A probe that could not be written may leave the last one in its place, |
| 3007 | # and that one answers for another check. Mandatory is the strict answer. |
| 3008 | printf 'package verification\n%s\n' "$field" > "$POLICY_TMP/probe.cue" \ |
| 3009 | || return 0 |
| 3010 | ! cue vet "${POLICY_LAYER[@]}" "$POLICY_TMP/probe.cue" > /dev/null 2>&1 |
| 3011 | } |
| 3012 | |
| 3013 | # Every side of the diff: an added, modified or deleted path, and both the |
| 3014 | # old and the new path of a rename or copy. It reads `git diff -z |
| 3015 | # --name-status` on stdin, because a path may contain anything but NUL, and |
| 3016 | # fails on a record cut short — including one cut inside its status, which |
| 3017 | # a plain `while read` would end on quietly, dropping the record. It also |
| 3018 | # fails when a write fails: the caller runs without `errexit`, so a lost |
| 3019 | # path would otherwise end in a normal EOF and a status of 0. |
| 3020 | diff_paths() { |
| 3021 | local status path |
| 3022 | while IFS= read -r -d '' status || { [ -z "$status" ] || return 1; false; }; do |
| 3023 | IFS= read -r -d '' path || return 1 |
| 3024 | printf '%s\n' "$path" || return 1 |
| 3025 | case "$status" in |
| 3026 | R* | C*) |
| 3027 | IFS= read -r -d '' path || return 1 |
| 3028 | printf '%s\n' "$path" || return 1 |
| 3029 | ;; |
| 3030 | esac |
| 3031 | done |
| 3032 | } |
| 3033 | |
| 3034 | # Lines in, one comma-separated line out. |
| 3035 | join_lines() { |
| 3036 | local out="" line |
| 3037 | while IFS= read -r line; do out="${out:+$out, }$line"; done |
| 3038 | printf '%s' "$out" |
| 3039 | } |
| 3040 | |
| 3041 | # Arguments in, one comma-separated line out, or `none` when there are none. |
| 3042 | join_or_none() { |
| 3043 | if [ $# -eq 0 ]; then echo none; else printf '%s\n' "$@" | join_lines; fi |
| 3044 | } |
| 3045 | |
| 3046 | # The remaining arguments, sorted bytewise, into the array the first one |
| 3047 | # names. The order is the C locale's whatever the caller's is, as it is |
| 3048 | # throughout the derivation (derive_required). The sort is captured and its |
| 3049 | # status returned: a sort read through a process substitution that fails |
| 3050 | # reads as a sort of nothing, and for a list of required checks that is a |
| 3051 | # change that requires none. |
| 3052 | # |
| 3053 | # The destination is a nameref, so what is assigned is the caller's array, |
| 3054 | # and the linter cannot see that array being read. |
| 3055 | # shellcheck disable=SC2034 |
| 3056 | sort_into() { |
| 3057 | local -n sort_into_dest="$1" |
| 3058 | local sorted |
| 3059 | shift |
| 3060 | sort_into_dest=() |
| 3061 | [ $# -gt 0 ] || return 0 |
| 3062 | sorted="$(printf '%s\n' "$@" | LC_ALL=C sort)" || return 1 |
| 3063 | mapfile -t sort_into_dest <<<"$sorted" |
| 3064 | } |
| 3065 | |
| 3066 | # The target: the ref a change is derived against, and the tip both policy |
| 3067 | # layers are read out of. origin's answer is preferred, because the target |
| 3068 | # is the branch as the integrator holds it and not as this checkout last |
| 3069 | # built it. |
| 3070 | # |
| 3071 | # Only a ref that is not there falls through to the next. show-ref says |
| 3072 | # "not there" with 1 and fails any other way with another status, so a |
| 3073 | # read of origin's ref that failed ends here: falling through would read |
| 3074 | # the policy out of the local main, which may be older. 1 no main at all, |
| 3075 | # 2 a read failed. |
| 3076 | target_ref() { |
| 3077 | local ref rc |
| 3078 | for ref in refs/remotes/origin/main refs/heads/main; do |
| 3079 | git show-ref --verify --quiet "$ref" && rc=0 || rc=$? |
| 3080 | case "$rc" in |
| 3081 | 0) echo "$ref"; return 0 ;; |
| 3082 | 1) ;; |
| 3083 | *) return 2 ;; |
| 3084 | esac |
| 3085 | done |
| 3086 | return 1 |
| 3087 | } |
| 3088 | |
| 3089 | # ---------------------------------------------------------------------- |
| 3090 | # The derivation itself, and the globals it answers with. Everything a |
| 3091 | # caller could want from the policy comes out of one pass, because every |
| 3092 | # part of it is a function of the same composition: `checks` prints the |
| 3093 | # answer, approve runs what it names. |
| 3094 | # |
| 3095 | # It reports and returns rather than dying, so that a caller inside the |
| 3096 | # review loop is refused rather than ended. Its callers are all in a |
| 3097 | # condition, which suspends `set -e` for the whole call, so every step is |
| 3098 | # checked by hand. |
| 3099 | # |
| 3100 | # The derivation runs in the C locale, whatever locale its caller runs in, |
| 3101 | # so a glob matches bytes and every list sorts bytewise. Under a UTF-8 |
| 3102 | # locale bash's regular expressions read characters instead: `?` matches é, |
| 3103 | # and a path holding a byte that is not UTF-8 matches no pattern at all, |
| 3104 | # not even `**`, so it would fall to `unclassified`. sort -u under |
| 3105 | # en_US.UTF-8 also merges distinct paths that collate equally. Which checks |
| 3106 | # a change requires would then depend on the locale of whoever derives it: |
| 3107 | # an operator's shell for approve, the integrator's unit for the integrator. |
| 3108 | # The Go deriver (derive/) matches bytes too, and its tests hold the two to |
| 3109 | # the same answer under UTF-8 locales. |
| 3110 | # |
| 3111 | # Every producer — git, cue, sort — writes a file or a variable, and its |
| 3112 | # status is checked before anything reads what it wrote. A producer read |
| 3113 | # through a process substitution loses its status, and a failed one then |
| 3114 | # reads as an empty list: no changed paths, no catalogue, no classes. Each |
| 3115 | # of those derives a change that requires nothing, which is the one answer a |
| 3116 | # failure must never give. |
| 3117 | DERIVED_PATHS=() # every side of the diff, sorted |
| 3118 | DERIVED_MATCHED=() # the class names at least one path matched, sorted |
| 3119 | DERIVED_UNCOVERED=() # the paths no class covered |
| 3120 | DERIVED_UNCLASSIFIED=0 # whether `unclassified` requires something |
| 3121 | DERIVED_CARRY="" # the strictest carry mode among the matched classes |
| 3122 | declare -A DERIVED_REQUIRED=() # check -> the classes that required it |
| 3123 | declare -A DERIVED_PINNED=() # check -> mandatory | default |
| 3124 | declare -A CHECK_RUNNER=() # check -> nix | command |
| 3125 | declare -A CHECK_ATTRIBUTE=() # check -> the flake check attribute |
| 3126 | declare -A CHECK_COMMAND=() # check -> the command line |
| 3127 | declare -A CHECK_COST=() # check -> quick | slow |
| 3128 | |
| 3129 | derive_required() { |
| 3130 | local schema="$1" instance="$2" project="$3" base="$4" head="$5" |
| 3131 | # Exported, so sort and every other program the derivation runs reads |
| 3132 | # bytes too; bash restores the caller's locale when this returns. |
| 3133 | local -x LC_ALL=C |
| 3134 | |
| 3135 | need_cue |
| 3136 | [ -f "$schema" ] || { |
| 3137 | refuse "no policy schema at $schema; nothing derived" |
| 3138 | return 1 |
| 3139 | } |
| 3140 | # An unmatched glob stays literal, so the first entry is a real file |
| 3141 | # exactly when the directory holds a document. |
| 3142 | local -a instance_docs=("$instance"/*.cue) project_docs=("$project"/*.cue) |
| 3143 | [ -f "${instance_docs[0]}" ] || { |
| 3144 | refuse "no instance policy layer at $instance; nothing derived" |
| 3145 | return 1 |
| 3146 | } |
| 3147 | [ -f "${project_docs[0]}" ] || { |
| 3148 | refuse "no project policy layer at $project: a project's own layer is the *.cue documents under $PROJECT_DIR/ (dcr-f41f718). Nothing derived" |
| 3149 | return 1 |
| 3150 | } |
| 3151 | |
| 3152 | cleanup_policy_tmp |
| 3153 | POLICY_LAYER=("$schema" "${instance_docs[@]}" "${project_docs[@]}") |
| 3154 | POLICY_TMP="$(mktemp -d)" || { |
| 3155 | refuse "no scratch directory to compose the policy in; nothing derived" |
| 3156 | return 1 |
| 3157 | } |
| 3158 | |
| 3159 | git diff --no-relative --name-status -M -z "$base" "$head" > "$POLICY_TMP/diff" || { |
| 3160 | refuse "could not diff $base..$head; nothing derived" |
| 3161 | return 1 |
| 3162 | } |
| 3163 | diff_paths < "$POLICY_TMP/diff" > "$POLICY_TMP/paths" || { |
| 3164 | refuse "truncated diff output; nothing derived" |
| 3165 | return 1 |
| 3166 | } |
| 3167 | sort -u "$POLICY_TMP/paths" > "$POLICY_TMP/paths.sorted" || { |
| 3168 | refuse "could not sort the changed paths; nothing derived" |
| 3169 | return 1 |
| 3170 | } |
| 3171 | DERIVED_PATHS=() |
| 3172 | mapfile -t DERIVED_PATHS < "$POLICY_TMP/paths.sorted" |
| 3173 | |
| 3174 | render_cue > "$POLICY_TMP/render.cue" || { |
| 3175 | refuse "could not write the policy render; nothing derived" |
| 3176 | return 1 |
| 3177 | } |
| 3178 | cue vet -c "${POLICY_LAYER[@]}" || { |
| 3179 | refuse "the policy layers do not compose; nothing derived" |
| 3180 | return 1 |
| 3181 | } |
| 3182 | |
| 3183 | # The render is checked before it is read. A render that fails prints |
| 3184 | # nothing, and an empty render reads as a policy that requires nothing. A |
| 3185 | # schema older than this script fails it, by lacking a field it reads. So |
| 3186 | # does a policy string holding a control character, which a schema that |
| 3187 | # admits one lets through. |
| 3188 | cue eval --out text -e _render "${POLICY_LAYER[@]}" "$POLICY_TMP/render.cue" \ |
| 3189 | > "$POLICY_TMP/rendered" || { |
| 3190 | refuse "the composed policy does not render (a schema older than this script, and a policy string holding a control character, are two causes); nothing derived" |
| 3191 | return 1 |
| 3192 | } |
| 3193 | # Sorted once here, so every list below comes out in a stable order. |
| 3194 | sort "$POLICY_TMP/rendered" > "$POLICY_TMP/rendered.sorted" || { |
| 3195 | refuse "could not sort the composed policy; nothing derived" |
| 3196 | return 1 |
| 3197 | } |
| 3198 | local -a rendered=() |
| 3199 | mapfile -t rendered < "$POLICY_TMP/rendered.sorted" |
| 3200 | |
| 3201 | unset CHECK_RUNNER CHECK_ATTRIBUTE CHECK_COMMAND CHECK_COST DERIVED_REQUIRED \ |
| 3202 | DERIVED_PINNED |
| 3203 | declare -gA CHECK_RUNNER=() CHECK_ATTRIBUTE=() CHECK_COMMAND=() \ |
| 3204 | CHECK_COST=() DERIVED_REQUIRED=() DERIVED_PINNED=() |
| 3205 | |
| 3206 | # The middle field is a class name for a coverage or requirement line and |
| 3207 | # a check name for a catalogue one; `key` is what both are. |
| 3208 | # |
| 3209 | # The render states each thing once: a class has one carry mode, a check |
| 3210 | # one runner, and a class requires a check once. A second line for the |
| 3211 | # same thing is forged, and a reader that took the last one would let the |
| 3212 | # forgery win, so it is refused. A coverage or requirement line is one |
| 3213 | # member of a set, so it is identified by its value too. |
| 3214 | local -a pat_class=() pat_ere=() req_class=() req_check=() |
| 3215 | local -A class_carry=() seen=() |
| 3216 | local line kind key value id |
| 3217 | for line in "${rendered[@]}"; do |
| 3218 | kind="${line%%$'\t'*}" |
| 3219 | key="${line#*$'\t'}" |
| 3220 | value="${key#*$'\t'}" |
| 3221 | key="${key%%$'\t'*}" |
| 3222 | case "$kind" in |
| 3223 | path | requires) id="$kind $key $value" ;; |
| 3224 | *) id="$kind $key" ;; |
| 3225 | esac |
| 3226 | [ -z "${seen[$id]:-}" ] || { |
| 3227 | refuse "the composed policy rendered '$id' twice, and states each thing once; nothing derived" |
| 3228 | return 1 |
| 3229 | } |
| 3230 | seen["$id"]=1 |
| 3231 | case "$kind" in |
| 3232 | path) |
| 3233 | pat_class+=("$key") |
| 3234 | pat_ere+=("$(glob_ere "$value")") |
| 3235 | ;; |
| 3236 | requires) |
| 3237 | req_class+=("$key") |
| 3238 | req_check+=("$value") |
| 3239 | ;; |
| 3240 | carry) class_carry["$key"]="$value" ;; |
| 3241 | runner) CHECK_RUNNER["$key"]="$value" ;; |
| 3242 | attribute) CHECK_ATTRIBUTE["$key"]="$value" ;; |
| 3243 | command) CHECK_COMMAND["$key"]="$value" ;; |
| 3244 | cost) CHECK_COST["$key"]="$value" ;; |
| 3245 | esac |
| 3246 | done |
| 3247 | |
| 3248 | # A pattern bash cannot compile answers 2, not 1. Read as "no match", it |
| 3249 | # would drop the pattern's class from every change it covers. |
| 3250 | local -A matched=() |
| 3251 | local path i hit rc |
| 3252 | DERIVED_UNCOVERED=() |
| 3253 | for path in "${DERIVED_PATHS[@]}"; do |
| 3254 | hit=0 |
| 3255 | for ((i = 0; i < ${#pat_ere[@]}; i++)); do |
| 3256 | [[ "$path" =~ ${pat_ere[i]} ]] && rc=0 || rc=$? |
| 3257 | case "$rc" in |
| 3258 | 0) |
| 3259 | matched["${pat_class[i]}"]=1 |
| 3260 | hit=1 |
| 3261 | ;; |
| 3262 | 1) ;; |
| 3263 | *) |
| 3264 | refuse "class ${pat_class[i]} has a path pattern that does not compile (${pat_ere[i]}); nothing derived" |
| 3265 | return 1 |
| 3266 | ;; |
| 3267 | esac |
| 3268 | done |
| 3269 | [ "$hit" -eq 1 ] || DERIVED_UNCOVERED+=("$path") |
| 3270 | done |
| 3271 | |
| 3272 | # The change's carry mode: the strictest among the classes its paths |
| 3273 | # matched, each as the schema resolved it. It is taken before |
| 3274 | # `unclassified` joins the matched classes, because an unclassified path |
| 3275 | # is same-paths and can make no change stricter. A matched class with no |
| 3276 | # mode this script knows is refused rather than read as the loosest. |
| 3277 | local class |
| 3278 | DERIVED_CARRY=same-paths |
| 3279 | for class in "${!matched[@]}"; do |
| 3280 | case "${class_carry[$class]:-}" in |
| 3281 | exact) DERIVED_CARRY=exact ;; |
| 3282 | same-class) [ "$DERIVED_CARRY" = exact ] || DERIVED_CARRY=same-class ;; |
| 3283 | same-paths) ;; |
| 3284 | *) |
| 3285 | refuse "class $class has no carry mode this script knows ('${class_carry[$class]:-}'); nothing derived" |
| 3286 | return 1 |
| 3287 | ;; |
| 3288 | esac |
| 3289 | done |
| 3290 | |
| 3291 | # A path no class covers is answered by the policy's `unclassified` set, |
| 3292 | # which is why it is treated as one more matched class rather than as an |
| 3293 | # absence. |
| 3294 | [ "${#DERIVED_UNCOVERED[@]}" -eq 0 ] || matched[unclassified]=1 |
| 3295 | |
| 3296 | local check |
| 3297 | DERIVED_UNCLASSIFIED=0 |
| 3298 | for ((i = 0; i < ${#req_check[@]}; i++)); do |
| 3299 | key="${req_class[i]}" |
| 3300 | check="${req_check[i]}" |
| 3301 | [ -n "${matched[$key]:-}" ] || continue |
| 3302 | [ "$key" != unclassified ] || DERIVED_UNCLASSIFIED=1 |
| 3303 | DERIVED_REQUIRED["$check"]="${DERIVED_REQUIRED[$check]:+${DERIVED_REQUIRED[$check]}, }$key" |
| 3304 | if check_is_mandatory "$key" "$check"; then |
| 3305 | DERIVED_PINNED["$check"]=mandatory |
| 3306 | else |
| 3307 | DERIVED_PINNED["$check"]="${DERIVED_PINNED[$check]:-default}" |
| 3308 | fi |
| 3309 | done |
| 3310 | |
| 3311 | sort_into DERIVED_MATCHED "${!matched[@]}" || { |
| 3312 | refuse "could not sort the matched classes; nothing derived" |
| 3313 | return 1 |
| 3314 | } |
| 3315 | } |
| 3316 | |
| 3317 | cmd_checks() { |
| 3318 | local root schema instance="" label project="" plabel base head target="" |
| 3319 | local names=0 cost="" costed=0 short_base short_head |
| 3320 | root="$(git rev-parse --show-toplevel 2> /dev/null)" \ |
| 3321 | || die "not a git repository" |
| 3322 | policy_defaults "$root" |
| 3323 | schema="$POLICY_SCHEMA" |
| 3324 | while [ $# -gt 0 ]; do |
| 3325 | case "$1" in |
| 3326 | --schema) [ $# -ge 2 ] || die "--schema needs a file"; schema="$2"; shift 2 ;; |
| 3327 | --instance) [ $# -ge 2 ] || die "--instance needs a directory"; instance="$2"; shift 2 ;; |
| 3328 | --project) [ $# -ge 2 ] || die "--project needs a directory"; project="$2"; shift 2 ;; |
| 3329 | --names) names=1; shift ;; |
| 3330 | --cost) [ $# -ge 2 ] || die "--cost needs quick or slow"; cost="$2"; costed=1; shift 2 ;; |
| 3331 | --) shift; break ;; |
| 3332 | -*) die "unknown option: $1" ;; |
| 3333 | *) break ;; |
| 3334 | esac |
| 3335 | done |
| 3336 | [ $# -le 2 ] || die "usage: valley checks [options] [<base> [<head>]]" |
| 3337 | if [ "$costed" -eq 1 ]; then |
| 3338 | case "$cost" in |
| 3339 | quick | slow) ;; |
| 3340 | *) die "--cost takes quick or slow, not '$cost'" ;; |
| 3341 | esac |
| 3342 | [ "$names" -eq 1 ] || die "--cost filters the --names list; pass --names too" |
| 3343 | fi |
| 3344 | |
| 3345 | head="$(git rev-parse --verify --quiet "${2:-HEAD}^{commit}")" \ |
| 3346 | || die "no such commit: ${2:-HEAD}" |
| 3347 | # The target is what the layers are read out of, so it is needed whenever |
| 3348 | # either layer is left to its default — and it is what an unnamed base |
| 3349 | # is the merge base with. |
| 3350 | if [ -z "$instance" ] || [ -z "$project" ] || [ $# -lt 1 ]; then |
| 3351 | target="$(target_ref)" || { |
| 3352 | [ $? -eq 1 ] || die "could not read the main branch; nothing derived" |
| 3353 | die "no main branch: the target's tip is where the policy layers are read from, and there is none here" |
| 3354 | } |
| 3355 | fi |
| 3356 | if [ $# -ge 1 ]; then |
| 3357 | base="$(git rev-parse --verify --quiet "$1^{commit}")" \ |
| 3358 | || die "no such commit: $1" |
| 3359 | else |
| 3360 | base="$(git merge-base "$target" "$head")" \ |
| 3361 | || die "$head and ${target#refs/} do not share history — name a base" |
| 3362 | fi |
| 3363 | |
| 3364 | trap 'cleanup_policy_tmp; cleanup_instance_tmp; cleanup_project_tmp' EXIT |
| 3365 | # Each layer is resolved only when nobody named one. --instance and |
| 3366 | # --project are a reader pointing this at a layer on purpose, and |
| 3367 | # honouring that means not fetching the repository, and not reading the |
| 3368 | # tip, that the named layer replaced. |
| 3369 | if [ -n "$instance" ]; then |
| 3370 | label="${instance#"$root"/}" |
| 3371 | else |
| 3372 | resolve_instance_layer "$root" "$target" || exit 1 |
| 3373 | instance="$POLICY_INSTANCE" |
| 3374 | label="$POLICY_INSTANCE_LABEL" |
| 3375 | fi |
| 3376 | if [ -n "$project" ]; then |
| 3377 | plabel="${project#"$root"/}" |
| 3378 | else |
| 3379 | resolve_project_layer "$root" "$target" || exit 1 |
| 3380 | project="$POLICY_PROJECT" |
| 3381 | plabel="$POLICY_PROJECT_LABEL" |
| 3382 | fi |
| 3383 | derive_required "$schema" "$instance" "$project" "$base" "$head" || exit 1 |
| 3384 | |
| 3385 | # The required checks, sorted, and grouped by the cost the policy |
| 3386 | # declares. The grouping changes nothing required: approve runs every |
| 3387 | # required check, and the integrator requires evidence for every one, |
| 3388 | # whatever it costs. |
| 3389 | local check width=0 |
| 3390 | local -a required=() quick=() slow=() listed=() |
| 3391 | sort_into required "${!DERIVED_REQUIRED[@]}" \ |
| 3392 | || die "could not sort the required checks; nothing derived" |
| 3393 | for check in "${required[@]}"; do |
| 3394 | [ "${#check}" -le "$width" ] || width="${#check}" |
| 3395 | if [ "${CHECK_COST[$check]}" = slow ]; then |
| 3396 | slow+=("$check") |
| 3397 | else |
| 3398 | quick+=("$check") |
| 3399 | fi |
| 3400 | done |
| 3401 | |
| 3402 | # The form for agents and scripts: the names alone, one per line. |
| 3403 | if [ "$names" -eq 1 ]; then |
| 3404 | case "$cost" in |
| 3405 | quick) listed=("${quick[@]}") ;; |
| 3406 | slow) listed=("${slow[@]}") ;; |
| 3407 | *) listed=("${required[@]}") ;; |
| 3408 | esac |
| 3409 | [ "${#listed[@]}" -eq 0 ] || printf '%s\n' "${listed[@]}" |
| 3410 | return |
| 3411 | fi |
| 3412 | |
| 3413 | # Read before the first line is printed, so a read that fails leaves no |
| 3414 | # partial report behind. |
| 3415 | short_base="$(git rev-parse --short "$base")" \ |
| 3416 | || die "could not read $base; nothing derived" |
| 3417 | short_head="$(git rev-parse --short "$head")" \ |
| 3418 | || die "could not read $head; nothing derived" |
| 3419 | |
| 3420 | printf '%s..%s: %d changed path(s)\n' "$short_base" "$short_head" \ |
| 3421 | "${#DERIVED_PATHS[@]}" |
| 3422 | printf 'policy: %s + %s\n' "$label" "$plabel" |
| 3423 | if [ "${#DERIVED_MATCHED[@]}" -eq 0 ]; then |
| 3424 | printf 'classes matched: none\n' |
| 3425 | else |
| 3426 | printf 'classes matched: %s\n' \ |
| 3427 | "$(printf '%s\n' "${DERIVED_MATCHED[@]}" | join_lines)" |
| 3428 | fi |
| 3429 | # How far an approval of this change carries across a rebase. Nothing |
| 3430 | # here acts on it. |
| 3431 | printf 'carry mode: %s\n\n' "$DERIVED_CARRY" |
| 3432 | |
| 3433 | if [ "${#DERIVED_REQUIRED[@]}" -eq 0 ]; then |
| 3434 | echo "required checks: none" |
| 3435 | else |
| 3436 | echo "required checks:" |
| 3437 | for check in "${required[@]}"; do |
| 3438 | printf ' %-*s %-9s %s\n' \ |
| 3439 | "$width" "$check" "${DERIVED_PINNED[$check]}" "${DERIVED_REQUIRED[$check]}" |
| 3440 | done |
| 3441 | fi |
| 3442 | |
| 3443 | if [ "${#DERIVED_UNCOVERED[@]}" -gt 0 ]; then |
| 3444 | printf '\nunclassified: %d path(s) matched no class\n' "${#DERIVED_UNCOVERED[@]}" |
| 3445 | printf ' %s\n' "${DERIVED_UNCOVERED[@]}" |
| 3446 | # The failure the `unclassified` field exists to prevent: paths outside |
| 3447 | # every class that the policy then asks nothing of. |
| 3448 | [ "$DERIVED_UNCLASSIFIED" -eq 1 ] \ |
| 3449 | || echo " the policy requires nothing of them — it is behind the tree" |
| 3450 | fi |
| 3451 | |
| 3452 | # Below every line the integrator reads (integrator/policy.go), and |
| 3453 | # unindented, so neither line can read as a required check. |
| 3454 | if [ "${#required[@]}" -gt 0 ]; then |
| 3455 | printf '\nquick: %s\n' "$(join_or_none "${quick[@]}")" |
| 3456 | printf 'slow: %s\n' "$(join_or_none "${slow[@]}")" |
| 3457 | fi |
| 3458 | } |
| 3459 | |
| 3460 | # self-update installs the tip of a branch of the-valley into the user's |
| 3461 | # nix profile (status/selfupdate.go). It is told this valley's stamp, which |
| 3462 | # says what is installed until it has installed something itself. |
| 3463 | cmd_self_update() { |
| 3464 | export VALLEY_VERSION="$VALLEY_STAMP" |
| 3465 | cmd_go self-update "$@" |
| 3466 | } |
| 3467 | |
| 3468 | cmd_version() { |
| 3469 | printf 'valley %s\n' "${VALLEY_STAMP:-unstamped (run from a checkout, not built by nix)}" |
| 3470 | } |
| 3471 | |
| 3472 | cmd_help() { |
| 3473 | case "${1:-}" in |
| 3474 | self-update) cmd_go self-update --help ;; |
| 3475 | *) usage ;; |
| 3476 | esac |
| 3477 | } |
| 3478 | |
| 3479 | main() { |
| 3480 | local verb="${1:-help}" |
| 3481 | if [ $# -gt 0 ]; then shift; fi |
| 3482 | # The verb set is mirrored in completions/ (valley.bash, _valley) — |
| 3483 | # update both when it changes. |
| 3484 | case "$verb" in |
| 3485 | pending) cmd_pending "$@" ;; |
| 3486 | review) cmd_review "$@" ;; |
| 3487 | approve) cmd_approve "$@" ;; |
| 3488 | rebase) cmd_rebase "$@" ;; |
| 3489 | status | ready | unready | withdraw | changes | knot | plan) cmd_go "$verb" "$@" ;; |
| 3490 | checks) cmd_checks "$@" ;; |
| 3491 | tail) cmd_tail "$@" ;; |
| 3492 | replay) cmd_replay "$@" ;; |
| 3493 | self-update) cmd_self_update "$@" ;; |
| 3494 | --version) cmd_version ;; |
| 3495 | help | -h | --help) cmd_help "$@" ;; |
| 3496 | *) |
| 3497 | printf 'valley: unknown verb: %s\n\n' "$verb" >&2 |
| 3498 | usage >&2 |
| 3499 | exit 1 |
| 3500 | ;; |
| 3501 | esac |
| 3502 | } |
| 3503 | |
| 3504 | main "$@" |