| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export XDG_STATE_HOME="$TMPDIR/state" |
| 6 | export GIT_CONFIG_NOSYSTEM=1 |
| 7 | # What this check writes down, kept out of the repository under test: an |
| 8 | # answer file or a captured stream sitting in the working tree would be |
| 9 | # committed by the next `git add -A` and change the tree being attested. |
| 10 | w="$TMPDIR/scratch" |
| 11 | mkdir -p "$w" |
| 12 | git config --global user.name valley-check |
| 13 | git config --global user.email valley-check@localhost |
| 14 | git config --global init.defaultBranch main |
| 15 | git config --global advice.detachedHead false |
| 16 | |
| 17 | # The operator's signing key. Provisioning one is deployment, so the verb |
| 18 | # takes it as a parameter and this check supplies a throwaway. |
| 19 | ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/host" |
| 20 | export VALLEY_ATTEST_KEY="$TMPDIR/host" |
| 21 | export VALLEY_ATTEST_NAME=laddie.valley.invalid/attestations |
| 22 | keyhash="$(attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" | cut -d+ -f2)" |
| 23 | |
| 24 | origin="$TMPDIR/project.git" |
| 25 | git init --quiet --bare "$origin" |
| 26 | |
| 27 | # Every push the bare repo receives, as its hook sees it. The claim approve |
| 28 | # makes is that ONE push carries the evidence and the request together, and |
| 29 | # that claim is only observable from the receiving side. |
| 30 | pushlog="$TMPDIR/pushlog" |
| 31 | cat > "$origin/hooks/pre-receive" <<EOF |
| 32 | #!/bin/sh |
| 33 | echo '--- push' >> $pushlog |
| 34 | cat >> $pushlog |
| 35 | EOF |
| 36 | chmod +x "$origin/hooks/pre-receive" |
| 37 | |
| 38 | # Interpose on origin's receive-pack so that the request namespace moves |
| 39 | # between approve's read of it and the push it then makes. That window is |
| 40 | # where every race below lives, and a hook cannot reach it: by the time |
| 41 | # pre-receive runs the client has already been told what the refs were. |
| 42 | # One shot, so only the push under test is raced. |
| 43 | arm_race() { |
| 44 | cat > "$TMPDIR/racing-receive-pack" <<EOF |
| 45 | #!/bin/sh |
| 46 | if [ -e "$TMPDIR/race-armed" ]; then |
| 47 | rm -f "$TMPDIR/race-armed" |
| 48 | $1 |
| 49 | fi |
| 50 | exec git-receive-pack "\$@" |
| 51 | EOF |
| 52 | chmod +x "$TMPDIR/racing-receive-pack" |
| 53 | git config remote.origin.receivepack "$TMPDIR/racing-receive-pack" |
| 54 | touch "$TMPDIR/race-armed" |
| 55 | } |
| 56 | |
| 57 | # Put it away, and fail if it never fired: a race nothing raced is a |
| 58 | # scenario that proved nothing. |
| 59 | disarm_race() { |
| 60 | git config --unset remote.origin.receivepack |
| 61 | if [ -e "$TMPDIR/race-armed" ]; then |
| 62 | echo "valley-request: the racing receive-pack never ran, so nothing was raced" >&2 |
| 63 | exit 1 |
| 64 | fi |
| 65 | } |
| 66 | |
| 67 | # ---------------------------------------------------------------------- |
| 68 | # The owning valley's repository, carrying the floor at policy/instance. |
| 69 | # The floor is read from here and never from the project, which is the |
| 70 | # tree under judgment: what governs a change has to come from outside it. |
| 71 | # Fetching is all the deriver does to this repository, which is what |
| 72 | # anyone the valley governs necessarily holds. |
| 73 | valley="$TMPDIR/valley.git" |
| 74 | git init --quiet --bare "$valley" |
| 75 | valleywork="$TMPDIR/valley-work" |
| 76 | git init --quiet "$valleywork" |
| 77 | mkdir -p "$valleywork/policy/instance" |
| 78 | cat > "$valleywork/policy/instance/floor.cue" <<'EOF' |
| 79 | package verification |
| 80 | |
| 81 | floor: { |
| 82 | checks: "tree-ok": { |
| 83 | runner: "command" |
| 84 | command: "test -f docs/readme.md" |
| 85 | } |
| 86 | classes: prose: { |
| 87 | paths: "docs/**": true |
| 88 | requires: "tree-ok": true |
| 89 | } |
| 90 | // A class that covers paths and asks nothing of them. Requiring no |
| 91 | // check is an ordinary outcome, not a hole: the policy has read these paths |
| 92 | // and has nothing to require of them. |
| 93 | classes: quiet: { |
| 94 | paths: "quiet/**": true |
| 95 | requires: {} |
| 96 | } |
| 97 | unclassified: "tree-ok": true |
| 98 | } |
| 99 | EOF |
| 100 | git -C "$valleywork" add -A |
| 101 | git -C "$valleywork" commit --quiet -m floor |
| 102 | git -C "$valleywork" push --quiet "$valley" main |
| 103 | |
| 104 | # ---------------------------------------------------------------------- |
| 105 | # The project, laid out the way the deriver reads one without being told: |
| 106 | # its own layer at policy/project, and its owning valley named at |
| 107 | # policy/valley. Both checks use the command runner: the nix runner cannot |
| 108 | # be exercised from inside a nix build, since the sandbox has no daemon to |
| 109 | # call, and what is under test here is the sequencing rather than the |
| 110 | # runner. |
| 111 | # |
| 112 | # examples/policy/ holds a second, contradictory pair of layers, and every |
| 113 | # check they require is named `phantom`. Nothing may derive it: a gate that |
| 114 | # came from a worked example is a gate nobody wrote, and the name is here |
| 115 | # so that the failure would be visible rather than plausible. |
| 116 | # |
| 117 | # schema/ is here because the-valley's own tree carries it. The packaged |
| 118 | # CLI never reads it; only bin/valley run straight from the checkout does |
| 119 | # (16). |
| 120 | repo="$TMPDIR/project" |
| 121 | git init --quiet "$repo" |
| 122 | cd "$repo" || exit 1 |
| 123 | git remote add origin "$origin" |
| 124 | |
| 125 | mkdir -p docs src schema policy/project examples/policy/instance examples/policy/project |
| 126 | cp "$schemaFile" schema/verification.cue |
| 127 | echo "the readme" > docs/readme.md |
| 128 | echo "$valley" > policy/valley |
| 129 | cat > policy/project/policy.cue <<'EOF' |
| 130 | package verification |
| 131 | |
| 132 | project: { |
| 133 | checks: "no-secrets": { |
| 134 | runner: "command" |
| 135 | command: "! grep -rqF SECRET src" |
| 136 | } |
| 137 | classes: source: { |
| 138 | paths: "src/**": true |
| 139 | requires: "no-secrets": true |
| 140 | } |
| 141 | } |
| 142 | EOF |
| 143 | cat > examples/policy/instance/floor.cue <<'EOF' |
| 144 | package verification |
| 145 | |
| 146 | floor: { |
| 147 | checks: phantom: { |
| 148 | runner: "command" |
| 149 | command: "false" |
| 150 | } |
| 151 | classes: everything: { |
| 152 | paths: "**": true |
| 153 | requires: phantom: true |
| 154 | } |
| 155 | unclassified: phantom: true |
| 156 | } |
| 157 | EOF |
| 158 | cat > examples/policy/project/policy.cue <<'EOF' |
| 159 | package verification |
| 160 | |
| 161 | project: {} |
| 162 | EOF |
| 163 | git add -A |
| 164 | git commit --quiet -m base |
| 165 | git push --quiet -u origin main |
| 166 | first="$(git rev-parse main)" |
| 167 | |
| 168 | # ---------------------------------------------------------------------- |
| 169 | # 1. A branch whose required check passes: the evidence and the request |
| 170 | # reach origin in one push, and main does not move. |
| 171 | git checkout --quiet -b topic/one |
| 172 | echo "a better readme" > docs/readme.md |
| 173 | git commit --quiet -am "improve the readme" |
| 174 | git push --quiet origin topic/one |
| 175 | digest="$(attest digest --rev topic/one | cut -d: -f2)" |
| 176 | |
| 177 | : > "$pushlog" |
| 178 | printf 'a\n' > "$w/answers" |
| 179 | valley review topic/one < "$w/answers" > "$w/approve.out" 2> "$w/approve.err" |
| 180 | |
| 181 | grep -q '^checking topic/one at .*: tree-ok$' "$w/approve.out" |
| 182 | grep -qE '^check tree-ok +command passed$' "$w/approve.out" |
| 183 | grep -qx "Approved topic/one ($(git rev-parse --short topic/one)): its integration request for origin/main is filed." "$w/approve.out" |
| 184 | grep -qx " request refs/the-valley/integration-requests/main/topic-one -> $(git rev-parse --short topic/one)" "$w/approve.out" |
| 185 | grep -qx " evidence refs/the-valley/attestations/$digest/$keyhash" "$w/approve.out" |
| 186 | grep -qx ' checks tree-ok' "$w/approve.out" |
| 187 | grep -qx 'Your local checkout has not changed.' "$w/approve.out" |
| 188 | grep -qx 'Check the result: valley status topic/one' "$w/approve.out" |
| 189 | |
| 190 | # What origin holds now. The attestation is keyed by the digest of the |
| 191 | # BRANCH's tree, which is what says the checks ran over the right tree and |
| 192 | # not over the operator's working copy. |
| 193 | git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/published.txt" |
| 194 | grep -qx "refs/the-valley/integration-requests/main/topic-one" "$w/published.txt" |
| 195 | grep -qx "refs/the-valley/attestations/$digest/$keyhash" "$w/published.txt" |
| 196 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-one)" \ |
| 197 | != "$(git rev-parse topic/one)" ]; then |
| 198 | echo "valley-request: the request ref does not name the branch's head" >&2 |
| 199 | exit 1 |
| 200 | fi |
| 201 | if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$first" ]; then |
| 202 | echo "valley-request: [a]pprove moved main" >&2 |
| 203 | exit 1 |
| 204 | fi |
| 205 | |
| 206 | # One push carried both. This is the friction the verb exists to remove: |
| 207 | # `attest run --push` publishes the branch and the evidence and knows |
| 208 | # nothing about a request ref, so doing this by hand is two beats. |
| 209 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 210 | if [ "$pushes" != 1 ]; then |
| 211 | echo "valley-request: the evidence and the request took $pushes pushes, not 1" >&2 |
| 212 | cat "$pushlog" >&2 |
| 213 | exit 1 |
| 214 | fi |
| 215 | grep -q "refs/the-valley/attestations/$digest/$keyhash" "$pushlog" |
| 216 | grep -q 'refs/the-valley/integration-requests/main/topic-one' "$pushlog" |
| 217 | |
| 218 | # The note origin holds is the one the operator signed, over that tree. |
| 219 | git cat-file -p "refs/the-valley/attestations/$digest/$keyhash:tree-ok/statement.note" \ |
| 220 | > "$w/statement.note" |
| 221 | attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" > "$w/known_keys" |
| 222 | attest verify --note "$w/statement.note" --repo "$repo" --rev topic/one \ |
| 223 | --known-keys "$w/known_keys" --signer "$VALLEY_ATTEST_NAME" > "$w/verify.out" |
| 224 | grep -q 'matches the recorded subject digest' "$w/verify.out" |
| 225 | |
| 226 | # ---------------------------------------------------------------------- |
| 227 | # 2. The layers the derivation used are the ones that govern, and it says |
| 228 | # which they were: the floor fetched from the owning valley, and the |
| 229 | # project's own layer at policy/project in the TARGET's tip. |
| 230 | # examples/policy/ is in this tree and requires `phantom` of every path, |
| 231 | # and no default reaches it — it is derivable only by naming it, which |
| 232 | # is what the options are for. |
| 233 | valley checks "$first" topic/one > "$w/checks.out" 2> "$w/checks.err" |
| 234 | grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/checks.out" |
| 235 | grep -qx 'classes matched: prose' "$w/checks.out" |
| 236 | grep -qE '^ tree-ok +mandatory +prose$' "$w/checks.out" |
| 237 | if grep -q phantom "$w/checks.out" "$w/checks.err"; then |
| 238 | echo "valley-request: a default derived a check from examples/policy/" >&2 |
| 239 | cat "$w/checks.out" "$w/checks.err" >&2 |
| 240 | exit 1 |
| 241 | fi |
| 242 | |
| 243 | # Named, the example layers derive exactly what they declare. This is the |
| 244 | # other half of the claim above: examples/policy/ is readable, and reading |
| 245 | # it is something a person asks for. |
| 246 | valley checks --instance examples/policy/instance --project examples/policy/project \ |
| 247 | "$first" topic/one > "$w/example.out" |
| 248 | grep -qE '^ phantom +mandatory' "$w/example.out" |
| 249 | grep -qx 'policy: examples/policy/instance + examples/policy/project' "$w/example.out" |
| 250 | |
| 251 | # ---------------------------------------------------------------------- |
| 252 | # 3. main moves on, so the next branch is stale. [a]pprove is offered there |
| 253 | # too — the integrator applies a delta to the tip rather than |
| 254 | # fast-forwarding — and a branch whose check fails publishes nothing |
| 255 | # and leaves the review loop usable. Approving the same head again |
| 256 | # would only fail again, so after the failure the prompt says approving |
| 257 | # needs a new head and offers [b]ase, [r]eject and [s]kip alone, and an |
| 258 | # answer of a is not taken. |
| 259 | git checkout --quiet main |
| 260 | echo "a note" > docs/notes.md |
| 261 | git add -A |
| 262 | git commit --quiet -m "a landing that moves main" |
| 263 | git push --quiet origin main |
| 264 | |
| 265 | git checkout --quiet -b topic/two "$first" |
| 266 | printf 'SECRET\n' > src/leak.txt |
| 267 | git add -A |
| 268 | git commit --quiet -m "a leak" |
| 269 | git push --quiet origin topic/two |
| 270 | |
| 271 | : > "$pushlog" |
| 272 | printf 'a\na\ns\n' > "$w/answers" |
| 273 | valley review topic/two < "$w/answers" > "$w/fail.out" 2> "$w/fail.err" |
| 274 | |
| 275 | # The prompt is the whole of what the operator is offered, so it is pinned |
| 276 | # character for character: a verb that stops being offered, or one that |
| 277 | # starts, is a change to the review loop and not an incidental one. |
| 278 | grep -qF 'topic/two does not fast-forward from main: [a]pprove, [v]iew full diff, [b]ase onto main, [r]eject, [s]kip? ' "$w/fail.out" |
| 279 | grep -q '^checking topic/two at .*: no-secrets$' "$w/fail.out" |
| 280 | twohead="$(git rev-parse --short topic/two)" |
| 281 | grep -qF "topic/two does not fast-forward from main. Its checks failed at $twohead, so approving needs a new head: [v]iew full diff, [b]ase onto main, [r]eject, [s]kip? " "$w/fail.out" |
| 282 | grep -qF 'answer v, b, r, or s' "$w/fail.out" |
| 283 | offers="$(grep -oF '[a]pprove' "$w/fail.out" | wc -l)" |
| 284 | checked="$(grep -c '^checking topic/two at ' "$w/fail.out" || true)" |
| 285 | if [ "$offers" != 1 ] || [ "$checked" != 1 ]; then |
| 286 | echo "valley-request: after its checks failed, the prompt offered [a]pprove again ($offers offers, $checked runs)" >&2 |
| 287 | cat "$w/fail.out" >&2 |
| 288 | exit 1 |
| 289 | fi |
| 290 | grep -qE '^check no-secrets +command failed$' "$w/fail.out" |
| 291 | grep -q 'the checks did not pass; nothing published, main untouched' "$w/fail.err" |
| 292 | # The prompt is printed without a newline, so what follows a refusal shares |
| 293 | # its line — the answer to it is not anchored. |
| 294 | grep -q 'skipped topic/two; nothing done' "$w/fail.out" |
| 295 | if [ -s "$pushlog" ]; then |
| 296 | echo "valley-request: a failing check pushed something" >&2 |
| 297 | cat "$pushlog" >&2 |
| 298 | exit 1 |
| 299 | fi |
| 300 | git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/after.txt" |
| 301 | if grep -q 'topic-two' "$w/after.txt"; then |
| 302 | echo "valley-request: a failing check left a ref on origin" >&2 |
| 303 | cat "$w/after.txt" >&2 |
| 304 | exit 1 |
| 305 | fi |
| 306 | |
| 307 | # The same for a branch that fast-forwards: after its checks fail, the |
| 308 | # prompt offers [r]eject and [s]kip alone. |
| 309 | git checkout --quiet -b topic/leak main |
| 310 | mkdir -p src |
| 311 | printf 'SECRET\n' > src/leak.txt |
| 312 | git add -A |
| 313 | git commit --quiet -m "another leak" |
| 314 | git push --quiet origin topic/leak |
| 315 | leakhead="$(git rev-parse --short topic/leak)" |
| 316 | : > "$pushlog" |
| 317 | printf 'a\na\ns\n' > "$w/answers" |
| 318 | valley review topic/leak < "$w/answers" > "$w/leak.out" 2> "$w/leak.err" |
| 319 | grep -qF 'topic/leak: [a]pprove, [v]iew full diff, [r]eject, [s]kip? ' "$w/leak.out" |
| 320 | grep -qF "topic/leak: its checks failed at $leakhead, so approving needs a new head: [v]iew full diff, [r]eject, [s]kip? " "$w/leak.out" |
| 321 | grep -qF 'answer v, r, or s' "$w/leak.out" |
| 322 | grep -q 'skipped topic/leak; nothing done' "$w/leak.out" |
| 323 | offers="$(grep -oF '[a]pprove' "$w/leak.out" | wc -l)" |
| 324 | checked="$(grep -c '^checking topic/leak at ' "$w/leak.out" || true)" |
| 325 | if [ "$offers" != 1 ] || [ "$checked" != 1 ] || [ -s "$pushlog" ]; then |
| 326 | echo "valley-request: after its checks failed, a branch that fast-forwards was offered [a]pprove again ($offers offers, $checked runs)" >&2 |
| 327 | cat "$w/leak.out" "$pushlog" >&2 |
| 328 | exit 1 |
| 329 | fi |
| 330 | |
| 331 | # ---------------------------------------------------------------------- |
| 332 | # 4. No signing key, with a check required: refused before any check is run, |
| 333 | # and the prompt still comes back. The host signs, and there is no |
| 334 | # unsigned mode. |
| 335 | : > "$pushlog" |
| 336 | printf 'a\ns\n' > "$w/answers" |
| 337 | env -u VALLEY_ATTEST_KEY valley review topic/two < "$w/answers" > "$w/nokey.out" 2> "$w/nokey.err" |
| 338 | grep -q 'no signing key: set VALLEY_ATTEST_KEY' "$w/nokey.err" |
| 339 | grep -q 'skipped topic/two; nothing done' "$w/nokey.out" |
| 340 | # A refusal that is no check failure leaves the prompt as it was, [a]pprove |
| 341 | # and all: a new session starts with no check failed. |
| 342 | offers="$(grep -oF 'topic/two does not fast-forward from main: [a]pprove, [v]iew full diff, [b]ase onto main, [r]eject, [s]kip? ' "$w/nokey.out" | wc -l)" |
| 343 | if [ "$offers" != 2 ]; then |
| 344 | echo "valley-request: a refusal that was no check failure changed the prompt" >&2 |
| 345 | cat "$w/nokey.out" >&2 |
| 346 | exit 1 |
| 347 | fi |
| 348 | if [ -s "$pushlog" ]; then |
| 349 | echo "valley-request: a run with no signing key pushed something" >&2 |
| 350 | exit 1 |
| 351 | fi |
| 352 | |
| 353 | # ---------------------------------------------------------------------- |
| 354 | # 5. A branch the policy requires no check of is filed as a request with |
| 355 | # no evidence. The integrator lands one and records it as a landing |
| 356 | # that required nothing, so refusing to file it here would strand |
| 357 | # exactly the changes a policy leaves unrequired. Nothing is required, |
| 358 | # so nothing is signed: this runs with no signing key at all. |
| 359 | git checkout --quiet main |
| 360 | git checkout --quiet -b topic/quiet |
| 361 | mkdir -p quiet |
| 362 | echo "a quiet note" > quiet/note.md |
| 363 | git add -A |
| 364 | git commit --quiet -m "a change under a class that asks nothing" |
| 365 | git push --quiet origin topic/quiet |
| 366 | quiethead="$(git rev-parse --short topic/quiet)" |
| 367 | |
| 368 | : > "$pushlog" |
| 369 | printf 'a\n' > "$w/answers" |
| 370 | env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \ |
| 371 | > "$w/quiet.out" 2> "$w/quiet.err" |
| 372 | |
| 373 | grep -q 'requires no check under .* + policy/project@origin/main .*; filing the request with no evidence' "$w/quiet.out" |
| 374 | grep -qF "$valley policy/instance@refs/heads/main" "$w/quiet.out" |
| 375 | grep -q '^Approved topic/quiet .*: its integration request for origin/main is filed\.$' "$w/quiet.out" |
| 376 | grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet.out" |
| 377 | grep -qx ' evidence none — nothing was required, so nothing was signed' "$w/quiet.out" |
| 378 | grep -qx ' checks none — the policy requires no check of this change' "$w/quiet.out" |
| 379 | |
| 380 | # One push, carrying the request and nothing else. |
| 381 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 382 | if [ "$pushes" != 1 ]; then |
| 383 | echo "valley-request: the bare request took $pushes pushes, not 1" >&2 |
| 384 | cat "$pushlog" >&2 |
| 385 | exit 1 |
| 386 | fi |
| 387 | if grep -q 'refs/the-valley/attestations/' "$pushlog"; then |
| 388 | echo "valley-request: a request that requires nothing published evidence" >&2 |
| 389 | cat "$pushlog" >&2 |
| 390 | exit 1 |
| 391 | fi |
| 392 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-quiet)" \ |
| 393 | != "$(git rev-parse topic/quiet)" ]; then |
| 394 | echo "valley-request: the bare request does not name the branch's head" >&2 |
| 395 | exit 1 |
| 396 | fi |
| 397 | |
| 398 | # ---------------------------------------------------------------------- |
| 399 | # 6. [b]ase, and the prompt it comes back to. A stale branch is rebased |
| 400 | # onto main in a throwaway worktree and re-reviewed, and the prompt the |
| 401 | # loop then prints is the fast-forward one — the path a reader of |
| 402 | # bin/valley has to trace two loops to reach, so it is exercised here |
| 403 | # rather than argued about. |
| 404 | git checkout --quiet main |
| 405 | git checkout --quiet -b topic/three "$first" |
| 406 | echo "another readme" > docs/readme.md |
| 407 | git commit --quiet -am "another readme" |
| 408 | git push --quiet origin topic/three |
| 409 | before_base="$(git -C "$origin" rev-parse refs/heads/main)" |
| 410 | |
| 411 | printf 'b\ns\n' > "$w/answers" |
| 412 | valley review topic/three < "$w/answers" > "$w/base.out" 2>&1 |
| 413 | grep -qF 'topic/three does not fast-forward from main: [a]pprove, [v]iew full diff, [b]ase onto main, [r]eject, [s]kip? ' "$w/base.out" |
| 414 | grep -qF 'rebased topic/three onto origin/main, pushed with --force-with-lease' "$w/base.out" |
| 415 | # The post-[b]ase prompt: the branch fast-forwards now, so the loop falls |
| 416 | # through to the prompt that has no [b]ase in it. |
| 417 | grep -qF 'topic/three: [a]pprove, [v]iew full diff, [r]eject, [s]kip? ' "$w/base.out" |
| 418 | grep -q 'skipped topic/three; nothing done' "$w/base.out" |
| 419 | git merge-base --is-ancestor refs/remotes/origin/main refs/remotes/origin/topic/three \ |
| 420 | || { echo "valley-request: [b]ase left the branch stale" >&2; exit 1; } |
| 421 | if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_base" ]; then |
| 422 | echo "valley-request: the review loop moved main" >&2 |
| 423 | exit 1 |
| 424 | fi |
| 425 | git checkout --quiet main |
| 426 | |
| 427 | # The request filed in 1 is still pending: nothing here consumes it, which |
| 428 | # is the integrator's own step. |
| 429 | git -C "$origin" rev-parse --verify --quiet \ |
| 430 | refs/the-valley/integration-requests/main/topic-one > /dev/null |
| 431 | |
| 432 | # ---------------------------------------------------------------------- |
| 433 | # 7. A change does not supply the policy that gates it. This branch |
| 434 | # deletes its own policy/project and repoints policy/valley at a |
| 435 | # repository that does not exist. Both layers come from the target's |
| 436 | # tip, so neither edit reaches the derivation: it composes the same two |
| 437 | # layers it would for any other branch, and the fetch that a branch-read |
| 438 | # pointer would have attempted never happens. |
| 439 | git checkout --quiet -b topic/nolayer |
| 440 | git rm --quiet -r policy/project |
| 441 | echo "$TMPDIR/there-is-no-such-valley.git" > policy/valley |
| 442 | git add -A |
| 443 | git commit --quiet -m "delete the project layer and repoint the valley" |
| 444 | git push --quiet origin topic/nolayer |
| 445 | |
| 446 | valley checks "$first" topic/nolayer > "$w/nolayer.out" 2> "$w/nolayer.err" |
| 447 | grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/nolayer.out" |
| 448 | if grep -q 'there-is-no-such-valley' "$w/nolayer.out" "$w/nolayer.err"; then |
| 449 | echo "valley-request: the deriver followed the branch's own valley pointer" >&2 |
| 450 | cat "$w/nolayer.out" "$w/nolayer.err" >&2 |
| 451 | exit 1 |
| 452 | fi |
| 453 | |
| 454 | : > "$pushlog" |
| 455 | printf 'a\n' > "$w/answers" |
| 456 | valley review topic/nolayer < "$w/answers" \ |
| 457 | > "$w/nolayer.review.out" 2> "$w/nolayer.review.err" |
| 458 | grep -q '^Approved topic/nolayer .*: its integration request for origin/main is filed\.$' "$w/nolayer.review.out" |
| 459 | |
| 460 | # ---------------------------------------------------------------------- |
| 461 | # 8. The reviewer's checkout contributes nothing. This is the case that |
| 462 | # failed in live use: the operator's checkout was an older branch |
| 463 | # without the landed policy layer, and the deriver read the working |
| 464 | # tree and refused. topic/nolayer is exactly such a checkout — no |
| 465 | # policy/project in it, and a policy/valley naming nowhere — so every |
| 466 | # derivation below runs from it. |
| 467 | # |
| 468 | # Byte-identical output is the claim, not merely a successful one: |
| 469 | # review works the same from any branch, however stale. |
| 470 | git checkout --quiet main |
| 471 | valley checks "$first" topic/one > "$w/from-main.out" |
| 472 | git checkout --quiet topic/nolayer |
| 473 | valley checks "$first" topic/one > "$w/from-stale.out" |
| 474 | if ! cmp -s "$w/from-main.out" "$w/from-stale.out"; then |
| 475 | echo "valley-request: the checkout changed what the derivation said" >&2 |
| 476 | diff -u "$w/from-main.out" "$w/from-stale.out" >&2 || true |
| 477 | exit 1 |
| 478 | fi |
| 479 | |
| 480 | # And [a]pprove, which is where it failed: a request filed from that same |
| 481 | # checkout, over a branch the stale tree knows nothing about. |
| 482 | git checkout --quiet -b topic/four origin/main |
| 483 | echo "one more readme" > docs/readme.md |
| 484 | git commit --quiet -am "one more readme" |
| 485 | git push --quiet origin topic/four |
| 486 | git checkout --quiet topic/nolayer |
| 487 | |
| 488 | : > "$pushlog" |
| 489 | printf 'a\n' > "$w/answers" |
| 490 | valley review topic/four < "$w/answers" > "$w/stale.out" 2> "$w/stale.err" |
| 491 | grep -q '^checking topic/four at .*: tree-ok$' "$w/stale.out" |
| 492 | grep -q '^Approved topic/four .*: its integration request for origin/main is filed\.$' "$w/stale.out" |
| 493 | grep -qx ' checks tree-ok' "$w/stale.out" |
| 494 | |
| 495 | # ---------------------------------------------------------------------- |
| 496 | # 9. A target carrying no project layer at all is derived under the floor |
| 497 | # alone, and says so. The project layer only ever adds, so the identity |
| 498 | # of "adds" is "adds nothing"; refusing instead would make a repository |
| 499 | # underivable until its first policy commit, and that commit could not |
| 500 | # derive either. This is the integrator's own reading of an absent |
| 501 | # layer (integrator/policy.go), and the two have to agree. |
| 502 | bare2="$TMPDIR/floorless.git" |
| 503 | git init --quiet --bare "$bare2" |
| 504 | repo2="$TMPDIR/floorless" |
| 505 | git init --quiet "$repo2" |
| 506 | cd "$repo2" || exit 1 |
| 507 | git remote add origin "$bare2" |
| 508 | mkdir -p docs policy |
| 509 | echo "the readme" > docs/readme.md |
| 510 | echo "$valley" > policy/valley |
| 511 | git add -A |
| 512 | git commit --quiet -m base |
| 513 | git push --quiet -u origin main |
| 514 | base2="$(git rev-parse main)" |
| 515 | echo "a second readme" > docs/readme.md |
| 516 | git commit --quiet -am "a second readme" |
| 517 | |
| 518 | valley checks "$base2" HEAD > "$w/floorless.out" 2> "$w/floorless.err" |
| 519 | grep -qx "policy: $valley policy/instance@refs/heads/main + no policy/project@origin/main $(git rev-parse --short origin/main) — the floor alone" "$w/floorless.out" |
| 520 | grep -qE '^ tree-ok +mandatory +prose$' "$w/floorless.out" |
| 521 | if grep -q no-secrets "$w/floorless.out"; then |
| 522 | echo "valley-request: the floor-alone derivation saw another project's layer" >&2 |
| 523 | exit 1 |
| 524 | fi |
| 525 | cd "$repo" || exit 1 |
| 526 | git checkout --quiet main |
| 527 | |
| 528 | # ---------------------------------------------------------------------- |
| 529 | # 10. Approving again after main moved. This is the case that wedged in live |
| 530 | # use. A request goes stale with reason=evidence when main lands |
| 531 | # inside a required check's input closure: no attestation over the |
| 532 | # requested tree can transfer again, so the cure is a new head rebased |
| 533 | # onto the moved main. That head is not a descendant of the one the |
| 534 | # request ref holds, which makes writing it a non-fast-forward |
| 535 | # replacement — and a push without a lease is refused by git's own |
| 536 | # rule before origin's hook is ever consulted. The verdict that sends |
| 537 | # a change down this path is the integrator's and is pinned by |
| 538 | # integrator-e2e; what is under test here is that [a]pprove can act on it. |
| 539 | git checkout --quiet -b topic/five origin/main |
| 540 | echo "a readme worth approving" > docs/readme.md |
| 541 | git commit --quiet -am "a readme worth approving" |
| 542 | git push --quiet origin topic/five |
| 543 | stalehead="$(git rev-parse topic/five)" |
| 544 | staledigest="$(attest digest --rev topic/five | cut -d: -f2)" |
| 545 | |
| 546 | printf 'a\n' > "$w/answers" |
| 547 | valley review topic/five < "$w/answers" > "$w/five-first.out" 2> "$w/five-first.err" |
| 548 | grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$stalehead")" "$w/five-first.out" |
| 549 | |
| 550 | # main moves, and the change is rebased onto it — the operator's half of |
| 551 | # approving again, which [a]pprove does not do and does not need to. |
| 552 | git checkout --quiet main |
| 553 | echo "a second note" > docs/second.md |
| 554 | git add -A |
| 555 | git commit --quiet -m "a landing under the same class" |
| 556 | git push --quiet origin main |
| 557 | git checkout --quiet topic/five |
| 558 | git rebase --quiet main |
| 559 | git push --quiet --force-with-lease origin topic/five |
| 560 | newhead="$(git rev-parse topic/five)" |
| 561 | newdigest="$(attest digest --rev topic/five | cut -d: -f2)" |
| 562 | |
| 563 | # The premise of the scenario, asserted rather than assumed: the rebase |
| 564 | # produced a head off the old one's line and a tree the old evidence is |
| 565 | # not about. Without both, the push below would be an ordinary one and |
| 566 | # would prove nothing. |
| 567 | if git merge-base --is-ancestor "$stalehead" "$newhead"; then |
| 568 | echo "valley-request: the rebased head still descends from the old one" >&2 |
| 569 | exit 1 |
| 570 | fi |
| 571 | if [ "$staledigest" = "$newdigest" ]; then |
| 572 | echo "valley-request: the rebase left the tree the old evidence is about" >&2 |
| 573 | exit 1 |
| 574 | fi |
| 575 | |
| 576 | : > "$pushlog" |
| 577 | printf 'a\n' > "$w/answers" |
| 578 | valley review topic/five < "$w/answers" > "$w/five-again.out" 2> "$w/five-again.err" |
| 579 | |
| 580 | grep -q '^Approved topic/five .*: its integration request for origin/main is filed\.$' "$w/five-again.out" |
| 581 | grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$newhead")" "$w/five-again.out" |
| 582 | # Replacing a pending request is said out loud, and the sentence names the |
| 583 | # head that was replaced. |
| 584 | grep -qx " replaced the pending request, which stood at $(git rev-parse --short "$stalehead") (a readme worth approving)" "$w/five-again.out" |
| 585 | |
| 586 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-five)" \ |
| 587 | != "$newhead" ]; then |
| 588 | echo "valley-request: the request filed again does not name the rebased head" >&2 |
| 589 | exit 1 |
| 590 | fi |
| 591 | |
| 592 | # Both attestations stand. The old one is about a tree that will never |
| 593 | # land, but the namespace is create-only and a record that can be dropped |
| 594 | # is not one; the second approval publishes beside it, never over it. |
| 595 | for stands in "refs/the-valley/attestations/$staledigest/$keyhash" \ |
| 596 | "refs/the-valley/attestations/$newdigest/$keyhash"; do |
| 597 | git -C "$origin" rev-parse --verify --quiet "$stands" > /dev/null || { |
| 598 | echo "valley-request: $stands is not on origin after the second approval" >&2 |
| 599 | exit 1 |
| 600 | } |
| 601 | done |
| 602 | |
| 603 | # One push again, and the request ref moved off the old head inside it. |
| 604 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 605 | if [ "$pushes" != 1 ]; then |
| 606 | echo "valley-request: the second approval took $pushes pushes, not 1" >&2 |
| 607 | cat "$pushlog" >&2 |
| 608 | exit 1 |
| 609 | fi |
| 610 | grep -qx "$stalehead $newhead refs/the-valley/integration-requests/main/topic-five" "$pushlog" |
| 611 | # And every attestation ref in that push is a creation: an all-zero old id |
| 612 | # is what the create-only namespace admits, and an approval that |
| 613 | # updated one would be refused by the real hook. |
| 614 | if grep '^[0-9a-f]* [0-9a-f]* refs/the-valley/attestations/' "$pushlog" \ |
| 615 | | grep -qv '^0* '; then |
| 616 | echo "valley-request: the second approval updated an attestation ref" >&2 |
| 617 | cat "$pushlog" >&2 |
| 618 | exit 1 |
| 619 | fi |
| 620 | if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$(git rev-parse main)" ]; then |
| 621 | echo "valley-request: the second approval moved main" >&2 |
| 622 | exit 1 |
| 623 | fi |
| 624 | |
| 625 | # ---------------------------------------------------------------------- |
| 626 | # 11. Two approvals of one change race for one ref, and the loser |
| 627 | # loses visibly. The request ref's value is read before the push and |
| 628 | # the push is leased against exactly that value, so the second one to |
| 629 | # arrive is refused rather than allowed to overwrite the first. The |
| 630 | # race is made real: a receive-pack wrapper moves the ref on origin |
| 631 | # after approve has read it and before the push it then makes, which is |
| 632 | # what another operator getting there first looks like from here. |
| 633 | git checkout --quiet main |
| 634 | git checkout --quiet -b topic/six origin/main |
| 635 | echo "a readme two approvals both rebased" > docs/readme.md |
| 636 | git commit --quiet -am "a readme two approvals both rebased" |
| 637 | git push --quiet origin topic/six |
| 638 | sixfiled="$(git rev-parse topic/six)" |
| 639 | |
| 640 | printf 'a\n' > "$w/answers" |
| 641 | valley review topic/six < "$w/answers" > "$w/six-first.out" 2> "$w/six-first.err" |
| 642 | grep -q '^Approved topic/six .*: its integration request for origin/main is filed\.$' "$w/six-first.out" |
| 643 | |
| 644 | git checkout --quiet main |
| 645 | echo "a third note" > docs/third.md |
| 646 | git add -A |
| 647 | git commit --quiet -m "another landing under the same class" |
| 648 | git push --quiet origin main |
| 649 | git checkout --quiet topic/six |
| 650 | git rebase --quiet main |
| 651 | git push --quiet --force-with-lease origin topic/six |
| 652 | sixhead="$(git rev-parse topic/six)" |
| 653 | sixdigest="$(attest digest --rev topic/six | cut -d: -f2)" |
| 654 | |
| 655 | # The other approval's head, built where origin can already reach |
| 656 | # every object in it: the same tree, parented on the same tip, so it is |
| 657 | # the commit a second operator rebasing this change would have produced. |
| 658 | racer="$(git -C "$origin" commit-tree "$(git rev-parse 'topic/six^{tree}')" \ |
| 659 | -p "$(git rev-parse origin/main)" -m "the other approval")" |
| 660 | |
| 661 | arm_race "git -C '$origin' update-ref refs/the-valley/integration-requests/main/topic-six '$racer'" |
| 662 | |
| 663 | : > "$pushlog" |
| 664 | printf 'a\ns\n' > "$w/answers" |
| 665 | valley review topic/six < "$w/answers" > "$w/race.out" 2> "$w/race.err" |
| 666 | disarm_race |
| 667 | |
| 668 | # A refusal, named as one, and the review loop still usable after it. |
| 669 | grep -q "the pending request moved from $(git rev-parse --short "$sixfiled") to $(git rev-parse --short "$racer") while this one was being prepared" "$w/race.err" |
| 670 | grep -q 'the lease on refs/the-valley/integration-requests/main/topic-six refused' "$w/race.err" |
| 671 | grep -q 'skipped topic/six; nothing done' "$w/race.out" |
| 672 | |
| 673 | # Nothing published. The winner's request stands untouched, the loser's |
| 674 | # evidence never reached origin, and origin saw no push at all — the lease |
| 675 | # refuses before any command is sent, which is what --atomic is worth. |
| 676 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-six)" \ |
| 677 | != "$racer" ]; then |
| 678 | echo "valley-request: the lease did not hold; the other approval was clobbered" >&2 |
| 679 | exit 1 |
| 680 | fi |
| 681 | if git -C "$origin" rev-parse --verify --quiet \ |
| 682 | "refs/the-valley/attestations/$sixdigest/$keyhash" > /dev/null; then |
| 683 | echo "valley-request: an approval that was refused published its evidence" >&2 |
| 684 | exit 1 |
| 685 | fi |
| 686 | if [ -s "$pushlog" ]; then |
| 687 | echo "valley-request: an approval that was refused reached origin's hook" >&2 |
| 688 | cat "$pushlog" >&2 |
| 689 | exit 1 |
| 690 | fi |
| 691 | |
| 692 | # The checks that were run are not lost with the refusal: their notes are |
| 693 | # in the operator's own repository, and the refusal says where. |
| 694 | git rev-parse --verify --quiet "refs/the-valley/attestations/$sixdigest/$keyhash" \ |
| 695 | > /dev/null || { |
| 696 | echo "valley-request: the refused approval kept no evidence locally" >&2 |
| 697 | exit 1 |
| 698 | } |
| 699 | grep -qx 'valley: the evidence is stored locally at:' "$w/race.err" |
| 700 | grep -qx " refs/the-valley/attestations/$sixdigest/$keyhash" "$w/race.err" |
| 701 | git checkout --quiet main |
| 702 | |
| 703 | # ---------------------------------------------------------------------- |
| 704 | # 12. Approving again at the same head. Evidence with a validity window |
| 705 | # expires while the tree stays the right tree, and the answer to that |
| 706 | # is a fresh observation over the same head. The request ref already |
| 707 | # names that head, so it is left out of the refspec entirely rather |
| 708 | # than pushed at the value origin already holds. |
| 709 | # |
| 710 | # The request is filed by hand here. Origin holding a request at this |
| 711 | # head is the whole of the state under test, and re-running [a]pprove to |
| 712 | # produce it would put a second-granular observation instant in the |
| 713 | # note: the same tree re-attested inside one second is the identical |
| 714 | # ref, and across a second boundary it is not. |
| 715 | git checkout --quiet -b topic/seven origin/main |
| 716 | echo "a readme approved twice" > docs/readme.md |
| 717 | git commit --quiet -am "a readme approved twice" |
| 718 | git push --quiet origin topic/seven |
| 719 | sevenhead="$(git rev-parse topic/seven)" |
| 720 | sevendigest="$(attest digest --rev topic/seven | cut -d: -f2)" |
| 721 | git push --quiet origin \ |
| 722 | "$sevenhead:refs/the-valley/integration-requests/main/topic-seven" |
| 723 | |
| 724 | : > "$pushlog" |
| 725 | printf 'a\n' > "$w/answers" |
| 726 | valley review topic/seven < "$w/answers" > "$w/seven.out" 2> "$w/seven.err" |
| 727 | |
| 728 | grep -q '^Approved topic/seven .*: its integration request for origin/main is filed\.$' "$w/seven.out" |
| 729 | grep -qx ' the request already stood at this head; the evidence is what was published' "$w/seven.out" |
| 730 | # The evidence went; the request ref was not in the refspec at all. |
| 731 | grep -q "refs/the-valley/attestations/$sevendigest/$keyhash" "$pushlog" |
| 732 | if grep -q 'refs/the-valley/integration-requests/main/topic-seven' "$pushlog"; then |
| 733 | echo "valley-request: a same-head re-approval pushed the request ref again" >&2 |
| 734 | cat "$pushlog" >&2 |
| 735 | exit 1 |
| 736 | fi |
| 737 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-seven)" \ |
| 738 | != "$sevenhead" ]; then |
| 739 | echo "valley-request: a same-head re-approval disturbed the request ref" >&2 |
| 740 | exit 1 |
| 741 | fi |
| 742 | |
| 743 | # The same head with nothing required: no evidence to publish and no ref to |
| 744 | # write, so no push is made at all. topic/quiet's request has stood on |
| 745 | # origin at this head since 5. A push assembled with an empty refspec |
| 746 | # would fall back to origin's default one, and pushing whatever that |
| 747 | # happens to name is not what approving a change means. |
| 748 | git checkout --quiet main |
| 749 | : > "$pushlog" |
| 750 | printf 'a\n' > "$w/answers" |
| 751 | env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \ |
| 752 | > "$w/quiet-again.out" 2> "$w/quiet-again.err" |
| 753 | grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet-again.out" |
| 754 | grep -qx ' the request already stood at this head and nothing was required, so nothing needed publishing' "$w/quiet-again.out" |
| 755 | if [ -s "$pushlog" ]; then |
| 756 | echo "valley-request: a re-approval with nothing to publish still pushed" >&2 |
| 757 | cat "$pushlog" >&2 |
| 758 | exit 1 |
| 759 | fi |
| 760 | |
| 761 | # ---------------------------------------------------------------------- |
| 762 | # 13. Two operators filing one change at once. Reading that the request |
| 763 | # ref is absent is a value like any other, and the lease covers it: |
| 764 | # git spells that expectation with an empty value and refuses if the |
| 765 | # ref exists at all. Unleased, this push would have fast-forwarded the |
| 766 | # other filing away without saying a word. |
| 767 | git checkout --quiet -b topic/eight origin/main |
| 768 | echo "a readme two operators both filed" > docs/readme.md |
| 769 | git commit --quiet -am "a readme two operators both filed" |
| 770 | git push --quiet origin topic/eight |
| 771 | eighthead="$(git rev-parse topic/eight)" |
| 772 | eightdigest="$(attest digest --rev topic/eight | cut -d: -f2)" |
| 773 | # The other operator's filing: this same tree on this same tip, which is |
| 774 | # what a second checkout of one branch produces. It is a descendant of |
| 775 | # nothing here, so an unleased push over it would be a plain fast-forward. |
| 776 | firstfiler="$(git -C "$origin" commit-tree "$(git rev-parse 'topic/eight^{tree}')" \ |
| 777 | -p "$(git rev-parse origin/main)" -m "the other operator's filing")" |
| 778 | |
| 779 | arm_race "git -C '$origin' update-ref refs/the-valley/integration-requests/main/topic-eight '$firstfiler'" |
| 780 | : > "$pushlog" |
| 781 | printf 'a\ns\n' > "$w/answers" |
| 782 | valley review topic/eight < "$w/answers" > "$w/create-race.out" 2> "$w/create-race.err" |
| 783 | disarm_race |
| 784 | |
| 785 | # The other operator's commit is on origin and not here, so the subject |
| 786 | # says so rather than inventing one. Scenario 10 pins the case where the |
| 787 | # replaced head IS local and its subject is printed. |
| 788 | grep -q "a request for this change appeared while this one was being prepared, at $(git rev-parse --short "$firstfiler") (subject not in this repository)" "$w/create-race.err" |
| 789 | grep -q 'skipped topic/eight; nothing done' "$w/create-race.out" |
| 790 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-eight)" \ |
| 791 | != "$firstfiler" ]; then |
| 792 | echo "valley-request: an unleased filing clobbered the request that got there first" >&2 |
| 793 | exit 1 |
| 794 | fi |
| 795 | if git -C "$origin" rev-parse --verify --quiet \ |
| 796 | "refs/the-valley/attestations/$eightdigest/$keyhash" > /dev/null; then |
| 797 | echo "valley-request: a refused filing published its evidence" >&2 |
| 798 | exit 1 |
| 799 | fi |
| 800 | if [ -s "$pushlog" ]; then |
| 801 | echo "valley-request: a refused filing reached origin's hook" >&2 |
| 802 | cat "$pushlog" >&2 |
| 803 | exit 1 |
| 804 | fi |
| 805 | |
| 806 | # ---------------------------------------------------------------------- |
| 807 | # 14. A lease that refuses because the ref is gone, not because it moved. |
| 808 | # The integrator consumes the request ref when the change lands, so an |
| 809 | # approval prepared just before that lands into a lease against a |
| 810 | # ref that no longer exists. Git reports it as stale info either way, |
| 811 | # and only reading the ref again tells the two apart — so the two are |
| 812 | # told apart, and the operator is pointed at the landing rather than |
| 813 | # at a race that did not happen. |
| 814 | git checkout --quiet main |
| 815 | git checkout --quiet -b topic/nine origin/main |
| 816 | echo "a readme that lands mid-approval" > docs/readme.md |
| 817 | git commit --quiet -am "a readme that lands mid-approval" |
| 818 | git push --quiet origin topic/nine |
| 819 | |
| 820 | printf 'a\n' > "$w/answers" |
| 821 | valley review topic/nine < "$w/answers" > "$w/nine-first.out" 2> "$w/nine-first.err" |
| 822 | grep -q '^Approved topic/nine .*: its integration request for origin/main is filed\.$' "$w/nine-first.out" |
| 823 | |
| 824 | git checkout --quiet main |
| 825 | echo "a fourth note" > docs/fourth.md |
| 826 | git add -A |
| 827 | git commit --quiet -m "a landing that moves main again" |
| 828 | git push --quiet origin main |
| 829 | git checkout --quiet topic/nine |
| 830 | git rebase --quiet main |
| 831 | git push --quiet --force-with-lease origin topic/nine |
| 832 | ninedigest="$(attest digest --rev topic/nine | cut -d: -f2)" |
| 833 | |
| 834 | arm_race "git -C '$origin' update-ref -d refs/the-valley/integration-requests/main/topic-nine" |
| 835 | : > "$pushlog" |
| 836 | printf 'a\ns\n' > "$w/answers" |
| 837 | valley review topic/nine < "$w/answers" > "$w/consumed.out" 2> "$w/consumed.err" |
| 838 | disarm_race |
| 839 | |
| 840 | grep -q 'the pending request was consumed while this one was being prepared, which is what the integrator does when a change lands' "$w/consumed.err" |
| 841 | grep -q 'nothing was published; fetch and review the branch again before approving it a second time' "$w/consumed.err" |
| 842 | # Not reported as a race, which is the distinction the re-read buys. |
| 843 | if grep -q 'the pending request moved from' "$w/consumed.err"; then |
| 844 | echo "valley-request: a consumed request was reported as a lost race" >&2 |
| 845 | cat "$w/consumed.err" >&2 |
| 846 | exit 1 |
| 847 | fi |
| 848 | grep -q 'skipped topic/nine; nothing done' "$w/consumed.out" |
| 849 | if git -C "$origin" rev-parse --verify --quiet \ |
| 850 | refs/the-valley/integration-requests/main/topic-nine > /dev/null; then |
| 851 | echo "valley-request: an approval that was refused re-created the consumed request" >&2 |
| 852 | exit 1 |
| 853 | fi |
| 854 | if git -C "$origin" rev-parse --verify --quiet \ |
| 855 | "refs/the-valley/attestations/$ninedigest/$keyhash" > /dev/null; then |
| 856 | echo "valley-request: an approval that was refused published its evidence" >&2 |
| 857 | exit 1 |
| 858 | fi |
| 859 | if [ -s "$pushlog" ]; then |
| 860 | echo "valley-request: an approval that was refused reached origin's hook" >&2 |
| 861 | cat "$pushlog" >&2 |
| 862 | exit 1 |
| 863 | fi |
| 864 | |
| 865 | # ---------------------------------------------------------------------- |
| 866 | # 15. The same-head push carries no request ref, so it carries no |
| 867 | # compare-and-swap on one either — git leaves an up-to-date ref out of |
| 868 | # what it sends. Where the ref stands after that push is therefore not |
| 869 | # something the run established, and it is read again rather than |
| 870 | # asserted from the read before it. |
| 871 | # |
| 872 | # Nothing re-files it. A request for a change already in the stream is |
| 873 | # judged against an empty delta on every pass, which is why the |
| 874 | # integrator consumes the ref in the first place; re-creating it here |
| 875 | # would be a guess about which of consumption and deletion happened, |
| 876 | # paid for with a second push behind the one this verb promises. So |
| 877 | # the run says what is true and stops, and approving again is the repair. |
| 878 | git checkout --quiet main |
| 879 | git checkout --quiet -b topic/ten origin/main |
| 880 | echo "a readme consumed under the evidence" > docs/readme.md |
| 881 | git commit --quiet -am "a readme consumed under the evidence" |
| 882 | git push --quiet origin topic/ten |
| 883 | tenhead="$(git rev-parse topic/ten)" |
| 884 | tendigest="$(attest digest --rev topic/ten | cut -d: -f2)" |
| 885 | git push --quiet origin \ |
| 886 | "$tenhead:refs/the-valley/integration-requests/main/topic-ten" |
| 887 | |
| 888 | arm_race "git -C '$origin' update-ref -d refs/the-valley/integration-requests/main/topic-ten" |
| 889 | : > "$pushlog" |
| 890 | printf 'a\ns\n' > "$w/answers" |
| 891 | valley review topic/ten < "$w/answers" > "$w/posthoc.out" 2> "$w/posthoc.err" |
| 892 | disarm_race |
| 893 | |
| 894 | grep -q 'the request was consumed while this run was working, which is what the integrator does when a change lands' "$w/posthoc.err" |
| 895 | grep -q 'Nothing here re-filed it' "$w/posthoc.err" |
| 896 | # The claim the old code would have made, from a read that had gone out of |
| 897 | # date under it. |
| 898 | if grep -q 'the request already stood at this head' "$w/posthoc.out"; then |
| 899 | echo "valley-request: a consumed request was reported as still standing" >&2 |
| 900 | cat "$w/posthoc.out" >&2 |
| 901 | exit 1 |
| 902 | fi |
| 903 | grep -q 'skipped topic/ten; nothing done' "$w/posthoc.out" |
| 904 | |
| 905 | # The evidence went, because that push succeeded; the request did not come |
| 906 | # back, because nothing here writes it. |
| 907 | git -C "$origin" rev-parse --verify --quiet \ |
| 908 | "refs/the-valley/attestations/$tendigest/$keyhash" > /dev/null || { |
| 909 | echo "valley-request: the evidence push was reported as done and is not on origin" >&2 |
| 910 | exit 1 |
| 911 | } |
| 912 | if git -C "$origin" rev-parse --verify --quiet \ |
| 913 | refs/the-valley/integration-requests/main/topic-ten > /dev/null; then |
| 914 | echo "valley-request: the consumed request was re-filed behind the operator" >&2 |
| 915 | exit 1 |
| 916 | fi |
| 917 | # And that evidence went in one push, which is still the whole claim. |
| 918 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 919 | if [ "$pushes" != 1 ]; then |
| 920 | echo "valley-request: the same-head re-approval took $pushes pushes, not 1" >&2 |
| 921 | cat "$pushlog" >&2 |
| 922 | exit 1 |
| 923 | fi |
| 924 | git checkout --quiet main |
| 925 | |
| 926 | # ---------------------------------------------------------------------- |
| 927 | # 16. A project other than the-valley carries no schema/. The schema ships |
| 928 | # with the tool, so the packaged CLI derives what a change requires |
| 929 | # and files the request from a checkout that has none. This is the case |
| 930 | # that failed in live use: [a]pprove in a valley's config repository, which |
| 931 | # carries its own floor at policy/instance, refused with "no policy |
| 932 | # schema" and published nothing. |
| 933 | cfgorigin="$TMPDIR/config.git" |
| 934 | git init --quiet --bare "$cfgorigin" |
| 935 | cfg="$TMPDIR/config" |
| 936 | git init --quiet "$cfg" |
| 937 | cd "$cfg" || exit 1 |
| 938 | git remote add origin "$cfgorigin" |
| 939 | mkdir -p docs policy/instance |
| 940 | cp "$valleywork/policy/instance/floor.cue" policy/instance/floor.cue |
| 941 | echo "the readme" > docs/readme.md |
| 942 | git add -A |
| 943 | git commit --quiet -m base |
| 944 | git push --quiet -u origin main |
| 945 | git checkout --quiet -b topic/config |
| 946 | echo "a config readme" > docs/readme.md |
| 947 | git commit --quiet -am "a config readme" |
| 948 | git push --quiet origin topic/config |
| 949 | cfgdigest="$(attest digest --rev topic/config | cut -d: -f2)" |
| 950 | |
| 951 | printf 'a\n' > "$w/answers" |
| 952 | valley review topic/config < "$w/answers" > "$w/config.out" 2> "$w/config.err" |
| 953 | if grep -q 'no policy schema' "$w/config.err"; then |
| 954 | echo "valley-request: [a]pprove looked for the schema in a project's checkout" >&2 |
| 955 | cat "$w/config.err" >&2 |
| 956 | exit 1 |
| 957 | fi |
| 958 | grep -q '^checking topic/config at .*: tree-ok$' "$w/config.out" |
| 959 | grep -qx "Approved topic/config ($(git rev-parse --short topic/config)): its integration request for origin/main is filed." "$w/config.out" |
| 960 | git -C "$cfgorigin" rev-parse --verify --quiet \ |
| 961 | "refs/the-valley/attestations/$cfgdigest/$keyhash" > /dev/null || { |
| 962 | echo "valley-request: [a]pprove in a project without schema/ published no evidence" >&2 |
| 963 | exit 1 |
| 964 | } |
| 965 | if [ "$(git -C "$cfgorigin" rev-parse refs/the-valley/integration-requests/main/topic-config)" \ |
| 966 | != "$(git rev-parse topic/config)" ]; then |
| 967 | echo "valley-request: [a]pprove in a project without schema/ filed no request for its head" >&2 |
| 968 | exit 1 |
| 969 | fi |
| 970 | |
| 971 | # checks without --schema reads the packaged schema too, and derives what |
| 972 | # [a]pprove ran from this repository's own floor. |
| 973 | valley checks > "$w/config-checks.out" 2> "$w/config-checks.err" |
| 974 | grep -qF '(this repository is its own valley)' "$w/config-checks.out" |
| 975 | grep -qE '^ tree-ok +mandatory +prose$' "$w/config-checks.out" |
| 976 | |
| 977 | # The packaged schema is a default and not a pin. A schema named in the |
| 978 | # environment is the one read, and --schema overrides both. |
| 979 | if VALLEY_VERIFICATION_SCHEMA="$TMPDIR/no-such-schema.cue" valley checks \ |
| 980 | > "$w/envschema.out" 2> "$w/envschema.err"; then |
| 981 | echo "valley-request: VALLEY_VERIFICATION_SCHEMA did not replace the packaged schema" >&2 |
| 982 | exit 1 |
| 983 | fi |
| 984 | grep -qF "no policy schema at $TMPDIR/no-such-schema.cue" "$w/envschema.err" |
| 985 | VALLEY_VERIFICATION_SCHEMA="$TMPDIR/no-such-schema.cue" valley checks --schema "$schemaFile" \ |
| 986 | > "$w/flagschema.out" 2> "$w/flagschema.err" |
| 987 | grep -qE '^ tree-ok +mandatory +prose$' "$w/flagschema.out" |
| 988 | |
| 989 | # bin/valley run straight from a checkout, with nothing setting the |
| 990 | # variable, reads schema/verification.cue in that checkout. the-valley's own |
| 991 | # tree carries one, as the first scratch project does, and every other |
| 992 | # project's tree does not. |
| 993 | if env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks \ |
| 994 | > "$w/bare-config.out" 2> "$w/bare-config.err"; then |
| 995 | echo "valley-request: bin/valley run bare found a schema in a checkout without one" >&2 |
| 996 | exit 1 |
| 997 | fi |
| 998 | grep -qF "no policy schema at $(git rev-parse --show-toplevel)/schema/verification.cue" \ |
| 999 | "$w/bare-config.err" |
| 1000 | cd "$repo" || exit 1 |
| 1001 | env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks "$first" topic/one \ |
| 1002 | > "$w/bare.out" 2> "$w/bare.err" |
| 1003 | grep -qE '^ tree-ok +mandatory +prose$' "$w/bare.out" |
| 1004 | |
| 1005 | # Every git call the default derivation makes fails closed: finding the |
| 1006 | # target, reading both layers out of its tip, reading the pointer, |
| 1007 | # fetching the owning valley, and the diff. fail-nth fails them one at a |
| 1008 | # time, and each run must end non-zero, say so, and print nothing on |
| 1009 | # stdout. The layer reads matter most here: a listing of policy/project |
| 1010 | # that failed, read as an empty directory, would derive under the floor |
| 1011 | # alone, and a read of origin/main that failed, read as no such ref, |
| 1012 | # would derive from the local main. |
| 1013 | shims="$TMPDIR/shims" |
| 1014 | mkdir -p "$shims" |
| 1015 | ln -s "$failNth" "$shims/git" |
| 1016 | shimmed_checks() { |
| 1017 | echo 0 > "$TMPDIR/shim-count" |
| 1018 | env -u VALLEY_VERIFICATION_SCHEMA SHIM_REAL_PATH="$PATH" SHIM_TOOL=git \ |
| 1019 | SHIM_FAIL_AT="$1" SHIM_STATUS=128 SHIM_COUNT="$TMPDIR/shim-count" \ |
| 1020 | PATH="$shims:$PATH" bash "$cliScript" checks "$first" topic/one |
| 1021 | } |
| 1022 | shimmed_checks 0 > "$w/shim-want.out" |
| 1023 | grep -qE '^ tree-ok +mandatory +prose$' "$w/shim-want.out" |
| 1024 | calls="$(cat "$TMPDIR/shim-count")" |
| 1025 | [ "$calls" -gt 0 ] || { |
| 1026 | echo "valley-request: the derivation never called git, so failing it proves nothing" >&2 |
| 1027 | exit 1 |
| 1028 | } |
| 1029 | for ((at = 1; at <= calls; at++)); do |
| 1030 | if shimmed_checks "$at" > "$w/shim.out" 2> "$w/shim.err" || [ -s "$w/shim.out" ] \ |
| 1031 | || ! grep -q '^valley: ' "$w/shim.err"; then |
| 1032 | echo "valley-request: git call $at of $calls failed, and the deriver did not refuse cleanly" >&2 |
| 1033 | cat "$w/shim.out" "$w/shim.err" >&2 |
| 1034 | exit 1 |
| 1035 | fi |
| 1036 | done |
| 1037 | |
| 1038 | # The same, for the derivation that resolves its own base: `merge-base` is |
| 1039 | # a call the explicit-base runs above never make, and --names is a second |
| 1040 | # way of printing what the derivation found. A run is shimmed for each |
| 1041 | # call, with the arguments the caller gives. |
| 1042 | defaulted_checks() { |
| 1043 | echo 0 > "$TMPDIR/shim-count" |
| 1044 | env -u VALLEY_VERIFICATION_SCHEMA SHIM_REAL_PATH="$PATH" SHIM_TOOL=git \ |
| 1045 | SHIM_FAIL_AT="$1" SHIM_STATUS=128 SHIM_COUNT="$TMPDIR/shim-count" \ |
| 1046 | PATH="$shims:$PATH" bash "$cliScript" checks "${@:2}" |
| 1047 | } |
| 1048 | git checkout --quiet topic/one |
| 1049 | for mode in report names; do |
| 1050 | margs=() |
| 1051 | [ "$mode" != names ] || margs=(--names) |
| 1052 | defaulted_checks 0 "${margs[@]}" > "$w/dshim-want.out" |
| 1053 | [ -s "$w/dshim-want.out" ] || { |
| 1054 | echo "valley-request: default resolution ($mode) printed nothing" >&2 |
| 1055 | exit 1 |
| 1056 | } |
| 1057 | calls="$(cat "$TMPDIR/shim-count")" |
| 1058 | sawmb=0 |
| 1059 | for ((at = 1; at <= calls; at++)); do |
| 1060 | if defaulted_checks "$at" "${margs[@]}" > "$w/dshim.out" 2> "$w/dshim.err" \ |
| 1061 | || [ -s "$w/dshim.out" ] || ! grep -q '^valley: ' "$w/dshim.err"; then |
| 1062 | echo "valley-request: default resolution ($mode): git call $at of $calls failed, and the deriver did not refuse cleanly" >&2 |
| 1063 | cat "$w/dshim.out" "$w/dshim.err" >&2 |
| 1064 | exit 1 |
| 1065 | fi |
| 1066 | done |
| 1067 | done |
| 1068 | git checkout --quiet - |
| 1069 | |
| 1070 | # A write that fails is a failure of the derivation, not a shorter list. |
| 1071 | # diff_paths runs with errexit off, so its printf is the case: the function |
| 1072 | # is cut out of the script and given a full disk to write to. |
| 1073 | fn="$(sed -n '/^diff_paths() {/,/^}/p' "$cliScript")" |
| 1074 | [ -n "$fn" ] || { echo "valley-request: diff_paths not found in the script" >&2; exit 1; } |
| 1075 | if ( |
| 1076 | eval "$fn" |
| 1077 | printf 'M\0docs/a.md\0R100\0old\0new\0' | diff_paths > /dev/full |
| 1078 | ) 2> "$w/full.err"; then |
| 1079 | echo "valley-request: diff_paths returned 0 after its write failed" >&2 |
| 1080 | exit 1 |
| 1081 | fi |
| 1082 | # And end to end: with no file allowed to grow, every intermediate file the |
| 1083 | # derivation writes fails, and nothing may print. The pipe keeps the |
| 1084 | # refusal readable, since a file stream would be cut off too. |
| 1085 | for mode in report names; do |
| 1086 | margs=() |
| 1087 | [ "$mode" != names ] || margs=(--names) |
| 1088 | if wrote="$( |
| 1089 | ulimit -f 0 |
| 1090 | trap '' XFSZ |
| 1091 | env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks "${margs[@]}" "$first" topic/one 2> /dev/null |
| 1092 | )" || [ -n "$wrote" ]; then |
| 1093 | echo "valley-request: a derivation with unwritable scratch files ($mode) did not fail cleanly" >&2 |
| 1094 | printf '%s\n' "$wrote" >&2 |
| 1095 | exit 1 |
| 1096 | fi |
| 1097 | done |
| 1098 | |
| 1099 | # ---------------------------------------------------------------------- |
| 1100 | # 17. The approve verb: [a]pprove without the pager, for a front end that |
| 1101 | # has shown the change its own way. It runs the function the key |
| 1102 | # runs, so it publishes what the key's session in 1 published, the |
| 1103 | # evidence and the request in one push, and prints what that session |
| 1104 | # printed. stdin is closed, because no answer is read, and the change |
| 1105 | # is named the way status accepts it, with origin/ in front. |
| 1106 | git checkout --quiet -b topic/verb origin/main |
| 1107 | echo "a readme approved without the pager" > docs/readme.md |
| 1108 | git commit --quiet -am "a readme approved without the pager" |
| 1109 | git push --quiet origin topic/verb |
| 1110 | verbhead="$(git rev-parse topic/verb)" |
| 1111 | verbdigest="$(attest digest --rev topic/verb | cut -d: -f2)" |
| 1112 | git checkout --quiet main |
| 1113 | |
| 1114 | : > "$pushlog" |
| 1115 | valley approve origin/topic/verb < /dev/null > "$w/verb.out" 2> "$w/verb.err" |
| 1116 | grep -q '^checking topic/verb at .*: tree-ok$' "$w/verb.out" |
| 1117 | grep -qx "Approved topic/verb ($(git rev-parse --short "$verbhead")): its integration request for origin/main is filed." "$w/verb.out" |
| 1118 | grep -qx " request refs/the-valley/integration-requests/main/topic-verb -> $(git rev-parse --short "$verbhead")" "$w/verb.out" |
| 1119 | grep -qx " evidence refs/the-valley/attestations/$verbdigest/$keyhash" "$w/verb.out" |
| 1120 | grep -qx ' checks tree-ok' "$w/verb.out" |
| 1121 | grep -qx 'Check the result: valley status topic/verb' "$w/verb.out" |
| 1122 | if grep -q '\[[a-z]\]' "$w/verb.out"; then |
| 1123 | echo "valley-request: the approve verb offered a prompt" >&2 |
| 1124 | cat "$w/verb.out" >&2 |
| 1125 | exit 1 |
| 1126 | fi |
| 1127 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-verb)" \ |
| 1128 | != "$verbhead" ]; then |
| 1129 | echo "valley-request: the approve verb's request does not name the branch's head" >&2 |
| 1130 | exit 1 |
| 1131 | fi |
| 1132 | git -C "$origin" rev-parse --verify --quiet \ |
| 1133 | "refs/the-valley/attestations/$verbdigest/$keyhash" > /dev/null || { |
| 1134 | echo "valley-request: the approve verb published no evidence" >&2 |
| 1135 | exit 1 |
| 1136 | } |
| 1137 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 1138 | if [ "$pushes" != 1 ]; then |
| 1139 | echo "valley-request: the approve verb took $pushes pushes, not 1" >&2 |
| 1140 | cat "$pushlog" >&2 |
| 1141 | exit 1 |
| 1142 | fi |
| 1143 | |
| 1144 | # A refusal is the verb's exit status, after the same words the key's |
| 1145 | # refusal prints, and it publishes nothing. |
| 1146 | : > "$pushlog" |
| 1147 | if valley approve topic/two < /dev/null > "$w/verb-refused.out" 2> "$w/verb-refused.err"; then |
| 1148 | echo "valley-request: the approve verb exited 0 for a change whose check failed" >&2 |
| 1149 | exit 1 |
| 1150 | fi |
| 1151 | grep -q 'the checks did not pass; nothing published, main untouched' "$w/verb-refused.err" |
| 1152 | if [ -s "$pushlog" ]; then |
| 1153 | echo "valley-request: a refused approve verb pushed something" >&2 |
| 1154 | cat "$pushlog" >&2 |
| 1155 | exit 1 |
| 1156 | fi |
| 1157 | if valley approve topic/no-such-change < /dev/null > "$w/verb-missing.out" 2> "$w/verb-missing.err"; then |
| 1158 | echo "valley-request: the approve verb exited 0 for a change origin does not have" >&2 |
| 1159 | exit 1 |
| 1160 | fi |
| 1161 | grep -q 'no such branch on origin: topic/no-such-change' "$w/verb-missing.err" |
| 1162 | |
| 1163 | # ---------------------------------------------------------------------- |
| 1164 | # 18. The rebase verb: [b]ase without the pager. A stale branch is |
| 1165 | # rebased onto main in a throwaway worktree and pushed under a lease, |
| 1166 | # and the new head is printed. Nothing is approved and nothing is |
| 1167 | # re-reviewed: rebasing is mechanics, and the judgment on what it |
| 1168 | # produced stays with whoever reads it next. |
| 1169 | git checkout --quiet -b topic/behind "$first" |
| 1170 | echo "a readme left behind" > docs/behind.md |
| 1171 | git add -A |
| 1172 | git commit --quiet -m "a readme left behind" |
| 1173 | git push --quiet origin topic/behind |
| 1174 | behindhead="$(git rev-parse topic/behind)" |
| 1175 | git checkout --quiet main |
| 1176 | if git -C "$origin" merge-base --is-ancestor refs/heads/main refs/heads/topic/behind; then |
| 1177 | echo "valley-request: the branch to rebase fast-forwarded from main already" >&2 |
| 1178 | exit 1 |
| 1179 | fi |
| 1180 | before_rebase="$(git -C "$origin" rev-parse refs/heads/main)" |
| 1181 | |
| 1182 | : > "$pushlog" |
| 1183 | valley rebase topic/behind < /dev/null > "$w/rebase.out" 2> "$w/rebase.err" |
| 1184 | rebased="$(git -C "$origin" rev-parse refs/heads/topic/behind)" |
| 1185 | grep -qx 'rebased topic/behind onto origin/main, pushed with --force-with-lease' "$w/rebase.out" |
| 1186 | grep -qx " head $rebased" "$w/rebase.out" |
| 1187 | if [ "$rebased" = "$behindhead" ]; then |
| 1188 | echo "valley-request: the rebase verb left the branch where it was" >&2 |
| 1189 | exit 1 |
| 1190 | fi |
| 1191 | git -C "$origin" merge-base --is-ancestor refs/heads/main "$rebased" || { |
| 1192 | echo "valley-request: the rebased head does not fast-forward from main" >&2 |
| 1193 | exit 1 |
| 1194 | } |
| 1195 | if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_rebase" ]; then |
| 1196 | echo "valley-request: the rebase verb moved main" >&2 |
| 1197 | exit 1 |
| 1198 | fi |
| 1199 | if git -C "$origin" rev-parse --verify --quiet \ |
| 1200 | refs/the-valley/integration-requests/main/topic-behind > /dev/null; then |
| 1201 | echo "valley-request: the rebase verb filed a request" >&2 |
| 1202 | exit 1 |
| 1203 | fi |
| 1204 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 1205 | if [ "$pushes" != 1 ] || grep -v '^--- push' "$pushlog" | grep -qv ' refs/heads/topic/behind$'; then |
| 1206 | echo "valley-request: the rebase verb pushed something besides the branch" >&2 |
| 1207 | cat "$pushlog" >&2 |
| 1208 | exit 1 |
| 1209 | fi |
| 1210 | |
| 1211 | # A branch that fast-forwards from main already is left as it is. |
| 1212 | : > "$pushlog" |
| 1213 | valley rebase topic/behind < /dev/null > "$w/rebase-again.out" 2> "$w/rebase-again.err" |
| 1214 | grep -qx 'topic/behind already fast-forwards from origin/main; nothing rebased' "$w/rebase-again.out" |
| 1215 | grep -qx " head $rebased" "$w/rebase-again.out" |
| 1216 | if [ -s "$pushlog" ]; then |
| 1217 | echo "valley-request: rebasing a branch that fast-forwards pushed something" >&2 |
| 1218 | cat "$pushlog" >&2 |
| 1219 | exit 1 |
| 1220 | fi |
| 1221 | |
| 1222 | # A rebase that conflicts changes nothing, says so, and exits non-zero. |
| 1223 | git checkout --quiet -b topic/clash "$first" |
| 1224 | echo "a note that clashes" > docs/notes.md |
| 1225 | git add -A |
| 1226 | git commit --quiet -m "a note that clashes" |
| 1227 | git push --quiet origin topic/clash |
| 1228 | clashhead="$(git rev-parse topic/clash)" |
| 1229 | git checkout --quiet main |
| 1230 | if valley rebase topic/clash < /dev/null > "$w/clash.out" 2> "$w/clash.err"; then |
| 1231 | echo "valley-request: a rebase that conflicts exited 0" >&2 |
| 1232 | exit 1 |
| 1233 | fi |
| 1234 | grep -q 'topic/clash conflicts with main — resolve by hand; nothing changed' "$w/clash.err" |
| 1235 | if [ "$(git -C "$origin" rev-parse refs/heads/topic/clash)" != "$clashhead" ]; then |
| 1236 | echo "valley-request: a rebase that conflicted moved the branch" >&2 |
| 1237 | exit 1 |
| 1238 | fi |
| 1239 | |
| 1240 | # ---------------------------------------------------------------------- |
| 1241 | # 19. A change whose version names no branch is approved at its head like |
| 1242 | # any other. The record allows a version with no branch, though valley |
| 1243 | # ready always names one, so the record here is written by hand. The |
| 1244 | # check it requires runs over the recorded head, and the evidence and the |
| 1245 | # c-<change ID> request reach origin in one push. |
| 1246 | git checkout --quiet -b branchless main |
| 1247 | echo "a readme no branch carries" > docs/readme.md |
| 1248 | git commit --quiet -am "a readme no branch carries" |
| 1249 | barehead="$(git rev-parse HEAD)" |
| 1250 | baredigest="$(attest digest --rev "$barehead" | cut -d: -f2)" |
| 1251 | git checkout --quiet main |
| 1252 | bareid=rrrrssssttttuuuuvvvvwwwwxxxxyyyy |
| 1253 | blob="$(printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "contributor", "state": "ready", "seq": 1, "versions": [{"n": 1, "head": "git-sha1:%s", "title": "no branch", "branch": "", "uploader": "contributor"}], "active": 1}\n' \ |
| 1254 | "$bareid" "$barehead" | git hash-object -w --stdin)" |
| 1255 | record="$(git commit-tree "$(printf '100644 blob %s\trecord.json\n' "$blob" | git mktree)" \ |
| 1256 | -p "$barehead" -m 'a change record')" |
| 1257 | git push --quiet origin "$record:refs/the-valley/changes/main/$bareid" |
| 1258 | |
| 1259 | : > "$pushlog" |
| 1260 | valley approve --head "$barehead" rrrr < /dev/null > "$w/branchless.out" 2> "$w/branchless.err" |
| 1261 | grep -q '^checking rrrr v1 at .*: tree-ok$' "$w/branchless.out" |
| 1262 | grep -qx " request refs/the-valley/integration-requests/main/c-$bareid -> $(git rev-parse --short "$barehead")" "$w/branchless.out" |
| 1263 | grep -qx " evidence refs/the-valley/attestations/$baredigest/$keyhash" "$w/branchless.out" |
| 1264 | if [ "$(git -C "$origin" rev-parse "refs/the-valley/integration-requests/main/c-$bareid")" != "$barehead" ]; then |
| 1265 | echo "valley-request: approve of a change with no branch did not file its head" >&2 |
| 1266 | exit 1 |
| 1267 | fi |
| 1268 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 1269 | if [ "$pushes" != 1 ] \ |
| 1270 | || ! grep -q "refs/the-valley/attestations/$baredigest/$keyhash" "$pushlog" \ |
| 1271 | || ! grep -q "refs/the-valley/integration-requests/main/c-$bareid" "$pushlog"; then |
| 1272 | echo "valley-request: the evidence and request for a change with no branch did not go in one push" >&2 |
| 1273 | cat "$pushlog" >&2 |
| 1274 | exit 1 |
| 1275 | fi |
| 1276 | |
| 1277 | # ---------------------------------------------------------------------- |
| 1278 | # The verbs the loop offers are the whole of what an operator can do to a |
| 1279 | # branch, so no path may offer one outside the set: [a]pprove, [b]ase, |
| 1280 | # [r]eject, [s]kip, and [v]iew full diff, which only shows. Every stream this check captured is scanned at the end, |
| 1281 | # which is what makes the claim about paths rather than about the two |
| 1282 | # prompts pinned character for character above — the fast-forward prompt, |
| 1283 | # the stale one, the one [b]ase comes back to, and the ones a refused [a]pprove |
| 1284 | # returns to are all in here. A verb that writes the protected ref from the |
| 1285 | # operator's own checkout — the retired [i]ntegrate, or any successor to |
| 1286 | # it — fails this wherever it is printed. |
| 1287 | offered="$(cat "$w"/*.out | grep -o '\[[a-z]\][a-z]*' | sort -u || true)" |
| 1288 | outside="$(printf '%s\n' "$offered" | grep -vxE '\[a\]pprove|\[b\]ase|\[r\]eject|\[s\]kip|\[v\]iew' || true)" |
| 1289 | if [ -n "$outside" ]; then |
| 1290 | echo "valley-request: a review path offered a verb outside the set:" >&2 |
| 1291 | printf '%s\n' "$outside" >&2 |
| 1292 | exit 1 |
| 1293 | fi |
| 1294 | # The scan is only worth something if it saw the offers at all. |
| 1295 | for verb in '[a]pprove' '[b]ase' '[r]eject' '[s]kip' '[v]iew'; do |
| 1296 | printf '%s\n' "$offered" | grep -qxF "$verb" \ |
| 1297 | || { echo "valley-request: no captured session offered $verb" >&2; exit 1; } |
| 1298 | done |
| 1299 | |
| 1300 | # No throwaway worktree outlived any of it: only the checkout is left. |
| 1301 | worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)" |
| 1302 | if [ "$worktrees" != 1 ]; then |
| 1303 | echo "valley-request: a throwaway worktree was left behind" >&2 |
| 1304 | git worktree list >&2 |
| 1305 | exit 1 |
| 1306 | fi |
| 1307 | |
| 1308 | echo "every scenario held" |
| 1309 | { |
| 1310 | cat "$w/approve.out" |
| 1311 | printf '\n--- the scratch origin, after all of it ---\n' |
| 1312 | git -C "$origin" for-each-ref --format='%(objectname) %(refname)' |
| 1313 | } > "$out" |