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