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