| 1 | # shellcheck shell=bash |
| 2 | # valley ready and valley withdraw, end to end and unmocked: every record |
| 3 | # they write is pushed for real to a bare repository wired with the real |
| 4 | # rendered pre-receive hook, as each principal, and what that repository |
| 5 | # then holds is read back. What sshd would set from a key's |
| 6 | # authorized_keys entry, these pushes set in the environment directly, as |
| 7 | # protect-e2e's do. |
| 8 | # Paths and fixtures come from the derivation environment. |
| 9 | # shellcheck disable=SC2154 |
| 10 | set -euo pipefail |
| 11 | |
| 12 | w="$TMPDIR/valley-ready" |
| 13 | mkdir -p "$w" |
| 14 | export HOME="$w" |
| 15 | export GIT_CONFIG_NOSYSTEM=1 |
| 16 | git config --global user.name valley-ready-check |
| 17 | git config --global user.email valley-ready-check@localhost |
| 18 | git config --global init.defaultBranch main |
| 19 | git config --global advice.detachedHead false |
| 20 | |
| 21 | fail() { |
| 22 | printf 'valley-ready: %s\n' "$*" >&2 |
| 23 | for f in "$w"/out "$w"/err; do |
| 24 | if [ -s "$f" ]; then cat "$f" >&2; fi |
| 25 | done |
| 26 | exit 1 |
| 27 | } |
| 28 | |
| 29 | # The project "open" of the protected host: it protects nothing, and the |
| 30 | # host grants request to the principal named requester. Its hook is the |
| 31 | # one valley-init installs, followed from the rendered init script. |
| 32 | origin="$w/project.git" |
| 33 | hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$initScriptPath" | head -n1)" |
| 34 | test -x "$hook" |
| 35 | git init --quiet --bare "$origin" |
| 36 | cp "$hook" "$origin/hooks/pre-receive" |
| 37 | chmod +x "$origin/hooks/pre-receive" |
| 38 | |
| 39 | repo="$w/project" |
| 40 | git init --quiet "$repo" |
| 41 | cd "$repo" |
| 42 | git remote add origin "$origin" |
| 43 | echo base > readme |
| 44 | git add readme |
| 45 | git commit --quiet -m base |
| 46 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 47 | |
| 48 | # as <principal> <command…>: run a command as a principal. The record names |
| 49 | # the principal git config valley.principal does, and the hook reads the |
| 50 | # one the push carries; an honest client has the two agree. anonymous is a |
| 51 | # key that names no principal. |
| 52 | as() { |
| 53 | local who="$1" |
| 54 | shift |
| 55 | if [ "$who" = anonymous ]; then |
| 56 | git config --unset valley.principal || true |
| 57 | env -u VALLEY_PRINCIPAL "$@" |
| 58 | else |
| 59 | git config valley.principal "$who" |
| 60 | VALLEY_PRINCIPAL="$who" "$@" |
| 61 | fi |
| 62 | } |
| 63 | |
| 64 | # ok <command…>: the command succeeds, and its output is kept. |
| 65 | ok() { |
| 66 | "$@" > "$w/out" 2> "$w/err" || fail "$* failed" |
| 67 | } |
| 68 | |
| 69 | # refused <phrase> <command…>: the command fails, saying the phrase. |
| 70 | refused() { |
| 71 | local says="$1" |
| 72 | shift |
| 73 | if "$@" > "$w/out" 2> "$w/err"; then |
| 74 | fail "$* succeeded where it had to be refused" |
| 75 | fi |
| 76 | grep -qF -- "$says" "$w/err" || fail "$* was refused without saying: $says" |
| 77 | } |
| 78 | |
| 79 | # record <id> <jq condition…>: origin's record of the change holds each. |
| 80 | record() { |
| 81 | local id="$1" cond |
| 82 | shift |
| 83 | git -C "$origin" cat-file blob "refs/the-valley/changes/main/$id:record.json" > "$w/record.json" \ |
| 84 | || fail "origin holds no record of $id" |
| 85 | for cond in "$@"; do |
| 86 | jq -e "$cond" "$w/record.json" > /dev/null || fail "the record of $id: $cond does not hold" |
| 87 | done |
| 88 | } |
| 89 | |
| 90 | # unchanged <id> <command…>: the command leaves origin's record as it was. |
| 91 | unchanged() { |
| 92 | local id="$1" before |
| 93 | shift |
| 94 | before="$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id")" |
| 95 | "$@" |
| 96 | [ "$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id")" = "$before" ] \ |
| 97 | || fail "the record of $id moved" |
| 98 | } |
| 99 | |
| 100 | # one_parent <id>: origin's record commit of the change has one parent, |
| 101 | # the record before it, so it added no version. |
| 102 | one_parent() { |
| 103 | [ "$(git -C "$origin" rev-list --parents -n 1 "refs/the-valley/changes/main/$1" | wc -w)" = 2 ] \ |
| 104 | || fail "the record of $1 added a version" |
| 105 | } |
| 106 | |
| 107 | no_records() { |
| 108 | [ -z "$(git -C "$origin" for-each-ref refs/the-valley/changes)" ] || fail "$1" |
| 109 | } |
| 110 | |
| 111 | # commit <subject> [<trailer>]: a new commit on the current branch, adding |
| 112 | # a file of its own, so that any two branches merge cleanly. |
| 113 | commit() { |
| 114 | echo "$1" > "$1.txt" |
| 115 | git add "$1.txt" |
| 116 | if [ $# -gt 1 ]; then |
| 117 | git commit --quiet -m "$1" -m "$2" |
| 118 | else |
| 119 | git commit --quiet -m "$1" |
| 120 | fi |
| 121 | } |
| 122 | |
| 123 | # with_header <id>: HEAD rewritten to carry a change-id header, the way jj |
| 124 | # writes one, after the committer. |
| 125 | with_header() { |
| 126 | local amended |
| 127 | amended="$(git cat-file commit HEAD | sed "/^committer /a change-id $1" | |
| 128 | git hash-object -t commit -w --stdin)" |
| 129 | git update-ref refs/heads/"$(git symbolic-ref --short HEAD)" "$amended" |
| 130 | } |
| 131 | |
| 132 | # ---------------------------------------------------------------------- |
| 133 | # A head marked ready for the first time is v1 of a new change. Its head |
| 134 | # carries no change ID, so ready makes one and prints it. |
| 135 | git checkout --quiet -b topic |
| 136 | commit one |
| 137 | first="$(git rev-parse HEAD)" |
| 138 | refused 'push it there first' as contributor valley ready |
| 139 | no_records 'a head not on origin was recorded' |
| 140 | as contributor git push --quiet origin topic |
| 141 | ok as contributor valley ready |
| 142 | grep -qE '^project [k-z]{4} v1 ready$' "$w/out" || fail 'ready did not print the handle and v1' |
| 143 | id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 144 | [ "${#id}" = 32 ] || fail 'ready did not print the change ID it made' |
| 145 | handle="${id:0:4}" |
| 146 | grep -qx "project $handle v1 ready" "$w/out" || fail 'the handle is not the ID'"'"'s first four letters' |
| 147 | record "$id" '.schema == "valley-change-record/v1"' ".change_id == \"$id\"" '.target == "main"' \ |
| 148 | '.owner == "contributor"' '.state == "ready"' '.seq == 1' '.active == 1' \ |
| 149 | ".versions == [{n: 1, head: \"git-sha1:$first\", title: \"one\", branch: \"topic\", uploader: \"contributor\"}]" |
| 150 | # The record commit holds on to the head it records. |
| 151 | [ "$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id^1")" = "$first" ] \ |
| 152 | || fail 'the record commit does not have the head as its parent' |
| 153 | |
| 154 | # A second ready at a new head is v2, named by the ID it printed. |
| 155 | commit two |
| 156 | second="$(git rev-parse HEAD)" |
| 157 | as contributor git push --quiet origin topic |
| 158 | ok as contributor valley ready --change "$id" |
| 159 | grep -qx "project $handle v2 ready" "$w/out" || fail 'the second ready was not v2' |
| 160 | record "$id" '.seq == 2' '.active == 2' '.state == "ready"' '.versions | length == 2' \ |
| 161 | ".versions[0].head == \"git-sha1:$first\"" ".versions[1] == {n: 2, head: \"git-sha1:$second\", title: \"two\", branch: \"topic\", uploader: \"contributor\"}" |
| 162 | |
| 163 | # The same head again is no new version. |
| 164 | unchanged "$id" refused 'a new version needs a new head' as contributor valley ready --change "$id" |
| 165 | |
| 166 | # Withdrawn, the change comes back by ready. At the head it was withdrawn |
| 167 | # at, it is ready again at the version it had: the record commit's one |
| 168 | # parent is the record before it. It is never made a draft. |
| 169 | ok as contributor valley withdraw "$handle" |
| 170 | grep -qx "project $handle v2 withdrawn" "$w/out" || fail 'withdraw did not say what it withdrew' |
| 171 | record "$id" '.state == "withdrawn"' '.seq == 3' '.active == 2' '.versions | length == 2' |
| 172 | unchanged "$id" refused 'withdrawn already' as contributor valley withdraw "$handle" |
| 173 | unchanged "$id" refused 'never made a draft' as contributor valley unready "$handle" |
| 174 | ok as contributor valley ready --change "$id" |
| 175 | grep -qx "project $handle v2 ready" "$w/out" || fail 'the change readied at the head it was withdrawn at was not v2' |
| 176 | record "$id" '.state == "ready"' '.seq == 4' '.active == 2' '.versions | length == 2' |
| 177 | one_parent "$id" |
| 178 | # Withdrawn again, it comes back by a new version at a new head too. |
| 179 | ok as contributor valley withdraw "$handle" |
| 180 | commit three |
| 181 | third="$(git rev-parse HEAD)" |
| 182 | as contributor git push --quiet origin topic |
| 183 | ok as contributor valley ready --change "$id" |
| 184 | grep -qx "project $handle v3 ready" "$w/out" || fail 'the revived change was not v3' |
| 185 | record "$id" '.state == "ready"' '.seq == 6' '.active == 3' ".versions[2].head == \"git-sha1:$third\"" |
| 186 | |
| 187 | # ---------------------------------------------------------------------- |
| 188 | # Who may write the record. A principal that neither owns the change nor |
| 189 | # holds the request grant is refused by the hook, whatever the command |
| 190 | # writes, and so is a key that names no principal. |
| 191 | commit four |
| 192 | fourth="$(git rev-parse HEAD)" |
| 193 | as contributor git push --quiet origin topic |
| 194 | unchanged "$id" refused "only the change's owner, contributor" as integrator valley ready --change "$id" |
| 195 | unchanged "$id" refused "only the change's owner, contributor" as integrator valley withdraw "$handle" |
| 196 | unchanged "$id" refused "only the change's owner, contributor" as integrator valley unready "$handle" |
| 197 | unchanged "$id" refused 'a key that names no principal' env -u VALLEY_PRINCIPAL valley withdraw "$handle" |
| 198 | refused 'git config valley.principal names none' as anonymous valley ready --change "$id" |
| 199 | # The operator, who holds the request grant, may add a version and |
| 200 | # withdraw, and the owner stays the owner. |
| 201 | ok as requester valley ready --head "$fourth" --change "$id" |
| 202 | grep -qx "project $handle v4 ready" "$w/out" || fail "the operator's version was not v4" |
| 203 | record "$id" '.owner == "contributor"' '.versions[3].uploader == "requester"' '.seq == 7' |
| 204 | ok as requester valley withdraw "$id" |
| 205 | record "$id" '.state == "withdrawn"' '.seq == 8' '.owner == "contributor"' |
| 206 | |
| 207 | # --head marks exactly the head given, which must be on origin. An older |
| 208 | # head of the branch is on origin, and a commit nobody pushed is not. |
| 209 | commit five |
| 210 | unpushed="$(git rev-parse HEAD)" |
| 211 | unchanged "$id" refused 'push it there first' as contributor valley ready --head "$unpushed" --change "$id" |
| 212 | git reset --quiet --hard "$fourth" |
| 213 | |
| 214 | # ---------------------------------------------------------------------- |
| 215 | # Change identity. A jj change-id header in the head commit names the |
| 216 | # change; so does a Change-Id trailer in its message. The two disagreeing |
| 217 | # is refused, and so is a value that is no change ID. |
| 218 | header_id=zyxwvutsrqponmlkzyxwvutsrqponmlk |
| 219 | trailer_id=kkkkllllmmmmnnnnooooppppqqqqrrrr |
| 220 | git checkout --quiet -b from-header main |
| 221 | commit header |
| 222 | with_header "$header_id" |
| 223 | as contributor git push --quiet origin from-header |
| 224 | ok as contributor valley ready |
| 225 | grep -qx "project zyxw v1 ready" "$w/out" || fail 'the header did not name the change' |
| 226 | grep -q 'is new' "$w/out" && fail 'a change ID read from the header was reported as made' |
| 227 | record "$header_id" '.versions[0].branch == "from-header"' |
| 228 | |
| 229 | git checkout --quiet -b from-trailer main |
| 230 | commit trailer "Change-Id: $trailer_id" |
| 231 | as contributor git push --quiet origin from-trailer |
| 232 | ok as contributor valley ready |
| 233 | grep -qx "project kkkk v1 ready" "$w/out" || fail 'the trailer did not name the change' |
| 234 | record "$trailer_id" '.versions[0].branch == "from-trailer"' |
| 235 | |
| 236 | git checkout --quiet -b disagreeing main |
| 237 | commit disagreeing "Change-Id: $trailer_id" |
| 238 | with_header "$header_id" |
| 239 | as contributor git push --quiet origin disagreeing |
| 240 | unchanged "$trailer_id" unchanged "$header_id" \ |
| 241 | refused 'they must agree' as contributor valley ready |
| 242 | refused 'they must agree' as contributor valley ready --change "$id" |
| 243 | |
| 244 | git checkout --quiet -b malformed main |
| 245 | commit malformed "Change-Id: I0123456789abcdef0123456789abcdef01234567" |
| 246 | as contributor git push --quiet origin malformed |
| 247 | refused 'which is not a change ID' as contributor valley ready |
| 248 | |
| 249 | # A change is a line of commits from its merge base with main. A merge |
| 250 | # in that range is refused. |
| 251 | git checkout --quiet -b merged main |
| 252 | commit merged |
| 253 | git merge --quiet --no-ff -m merge from-trailer |
| 254 | as contributor git push --quiet origin merged |
| 255 | refused 'include the merge' as contributor valley ready |
| 256 | |
| 257 | # ---------------------------------------------------------------------- |
| 258 | # A change is named by its handle wherever a change is taken: status |
| 259 | # reads the branch its active version names, and withdraw takes a branch |
| 260 | # name too. |
| 261 | ok valley status kkkk |
| 262 | grep -q '^from-trailer: not approved' "$w/out" || fail 'status did not read the change by its handle' |
| 263 | ok as contributor valley withdraw from-trailer |
| 264 | grep -qx 'project kkkk v1 withdrawn' "$w/out" || fail 'withdraw did not take a branch name' |
| 265 | |
| 266 | # --stamp carries a new change's ID in an unpushed head as a trailer, |
| 267 | # pushes the branch, and marks it ready. It will not rewrite a head that |
| 268 | # is on origin in a change that is not the user's. |
| 269 | git checkout --quiet -b stamped main |
| 270 | commit stamped |
| 271 | ok as contributor valley ready --stamp |
| 272 | stamped_id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 273 | [ "${#stamped_id}" = 32 ] || fail '--stamp made no change ID' |
| 274 | [ "$(git log -1 --format='%(trailers:key=Change-Id,valueonly)' | head -n1)" = "$stamped_id" ] \ |
| 275 | || fail '--stamp did not carry the ID in the head' |
| 276 | [ "$(git rev-parse HEAD)" = "$(git -C "$origin" rev-parse refs/heads/stamped)" ] \ |
| 277 | || fail '--stamp did not push the stamped head' |
| 278 | record "$stamped_id" ".versions[0].head == \"git-sha1:$(git rev-parse HEAD)\"" |
| 279 | git checkout --quiet -b pushed main |
| 280 | commit pushed |
| 281 | as contributor git push --quiet origin pushed |
| 282 | refused 'will not rewrite it' as integrator valley ready --stamp |
| 283 | # A change that has a record already is never stamped, whoever |
| 284 | # valley.principal says is asking: neither the local branch nor origin's |
| 285 | # moves. |
| 286 | git checkout --quiet stamped |
| 287 | commit restamped |
| 288 | restamped="$(git rev-parse HEAD)" |
| 289 | unchanged "$stamped_id" refused 'has a record on origin already' \ |
| 290 | as contributor valley ready --stamp --change "$stamped_id" |
| 291 | [ "$(git rev-parse HEAD)" = "$restamped" ] || fail '--stamp rewrote the head of a change with a record' |
| 292 | [ "$(git -C "$origin" rev-parse refs/heads/stamped)" != "$restamped" ] \ |
| 293 | || fail '--stamp pushed for a change with a record' |
| 294 | |
| 295 | # ---------------------------------------------------------------------- |
| 296 | # A change named by its handle is approved, reviewed and rebased at the |
| 297 | # head its record names, never at its branch's tip. The record's branch is |
| 298 | # only where that head is expected to be, and whoever can push the branch |
| 299 | # can move it, in the same push as the record if they like. |
| 300 | # |
| 301 | # approve derives what the change owes from the owning valley's floor, |
| 302 | # named at policy/valley on main. This floor owes nothing, so approving |
| 303 | # files a request with no evidence, and no signing key is needed. |
| 304 | valley="$w/valley.git" |
| 305 | git init --quiet --bare "$valley" |
| 306 | git init --quiet "$w/valley-work" |
| 307 | mkdir -p "$w/valley-work/policy/instance" |
| 308 | cat > "$w/valley-work/policy/instance/floor.cue" << 'CUE' |
| 309 | package verification |
| 310 | |
| 311 | floor: { |
| 312 | checks: {} |
| 313 | classes: {} |
| 314 | unclassified: {} |
| 315 | } |
| 316 | CUE |
| 317 | git -C "$w/valley-work" add -A |
| 318 | git -C "$w/valley-work" commit --quiet -m floor |
| 319 | git -C "$w/valley-work" push --quiet "$valley" HEAD:main |
| 320 | git checkout --quiet main |
| 321 | mkdir -p policy |
| 322 | echo "$valley" > policy/valley |
| 323 | git add policy/valley |
| 324 | git commit --quiet -m 'name the owning valley' |
| 325 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 326 | |
| 327 | # write_record <json> <parent…>: a record commit holding the JSON, written |
| 328 | # by hand, the way anyone holding a key could write one. |
| 329 | write_record() { |
| 330 | local json="$1" blob tree parent |
| 331 | local -a parents=() |
| 332 | shift |
| 333 | for parent in "$@"; do parents+=(-p "$parent"); done |
| 334 | blob="$(printf '%s\n' "$json" | git hash-object -w --stdin)" |
| 335 | tree="$(printf '100644 blob %s\trecord.json\n' "$blob" | git mktree)" |
| 336 | git commit-tree "$tree" "${parents[@]}" -m 'a change record' |
| 337 | } |
| 338 | request=refs/the-valley/integration-requests/main/victim |
| 339 | no_request() { |
| 340 | if git -C "$origin" rev-parse --verify --quiet "$request" > /dev/null; then |
| 341 | fail "$1" |
| 342 | fi |
| 343 | } |
| 344 | |
| 345 | # The record names H on victim, and the same push puts B on victim. |
| 346 | target_id=ppppqqqqrrrrssssttttuuuuvvvvwwww |
| 347 | git checkout --quiet -b victim main |
| 348 | commit honest |
| 349 | honest="$(git rev-parse HEAD)" |
| 350 | commit substituted |
| 351 | substituted="$(git rev-parse HEAD)" |
| 352 | v1="$(jq -cn --arg id "$target_id" --arg head "git-sha1:$honest" '{schema: "valley-change-record/v1", |
| 353 | change_id: $id, target: "main", owner: "contributor", state: "ready", seq: 1, |
| 354 | versions: [{n: 1, head: $head, title: "honest", branch: "victim", uploader: "contributor"}], active: 1}')" |
| 355 | r1="$(write_record "$v1" "$honest")" |
| 356 | as contributor git push --quiet origin "$r1:refs/the-valley/changes/main/$target_id" \ |
| 357 | "$substituted:refs/heads/victim" |
| 358 | [ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] || fail 'the branch did not move with the record' |
| 359 | |
| 360 | refused "records the head $honest, and origin/victim is at $substituted" as requester valley approve pppp |
| 361 | no_request 'approve filed a request for a branch tip no version records' |
| 362 | refused "origin/victim is at $substituted; nothing rebased" as requester valley rebase pppp |
| 363 | [ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] || fail 'rebase moved the branch' |
| 364 | # review shows the recorded head, and none of what the branch added. |
| 365 | printf 's\n' | valley review pppp > "$w/out" 2> "$w/err" || fail 'review of a handle failed' |
| 366 | grep -q '^change pppp v1 at' "$w/out" || fail 'review did not say which version it shows' |
| 367 | grep -q ' honest$' "$w/out" || fail 'review did not show the recorded head' |
| 368 | if grep -q 'substituted' "$w/out"; then fail 'review showed the branch tip, not the recorded head'; fi |
| 369 | # Rejecting it there deletes nothing while the branch is elsewhere: the |
| 370 | # refusal names both commits, and the next answer goes to the prompt. |
| 371 | printf 'r\nyes\ns\n' | as requester valley review pppp > "$w/out" 2> "$w/err" \ |
| 372 | || fail 'review of a handle failed' |
| 373 | grep -qF "records the head $honest, and origin/victim is at $substituted; nothing deleted" "$w/err" \ |
| 374 | || fail 'reject did not refuse a branch whose tip is not the recorded head' |
| 375 | [ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] \ |
| 376 | || fail 'reject deleted a branch whose tip is not the recorded head' |
| 377 | |
| 378 | # With the branch at the recorded head, approve files exactly that head. |
| 379 | as contributor git push --quiet --force origin "$honest:refs/heads/victim" |
| 380 | ok as requester valley approve pppp |
| 381 | [ "$(git -C "$origin" rev-parse "$request")" = "$honest" ] || fail 'approve did not file the recorded head' |
| 382 | |
| 383 | # A second agent acting as the same principal adds v2 at a head that is |
| 384 | # not on victim, while its record says victim. approve refuses, and the |
| 385 | # request stays where it was. |
| 386 | git checkout --quiet -b elsewhere main |
| 387 | commit elsewhere |
| 388 | elsewhere="$(git rev-parse HEAD)" |
| 389 | as contributor git push --quiet origin elsewhere |
| 390 | v2="$(jq -c --arg head "git-sha1:$elsewhere" '.versions += [{n: 2, head: $head, title: "elsewhere", |
| 391 | branch: "victim", uploader: "contributor"}] | .active = 2 | .seq = 2' <<< "$v1")" |
| 392 | r2="$(write_record "$v2" "$r1" "$elsewhere")" |
| 393 | as contributor git push --quiet origin "$r2:refs/the-valley/changes/main/$target_id" |
| 394 | refused "records the head $elsewhere, and origin/victim is at $honest" as requester valley approve pppp |
| 395 | [ "$(git -C "$origin" rev-parse "$request")" = "$honest" ] || fail 'a redirected version moved the request' |
| 396 | # With the branch at the recorded head, rejecting it in review deletes it. |
| 397 | as contributor git push --quiet --force origin "$elsewhere:refs/heads/victim" |
| 398 | printf 'r\nyes\n' | as requester valley review pppp > "$w/out" 2> "$w/err" \ |
| 399 | || fail 'rejecting a handle at its recorded head failed' |
| 400 | grep -qF 'rejected victim: deleted from origin' "$w/out" || fail 'reject did not say it deleted the branch' |
| 401 | if git -C "$origin" rev-parse --verify --quiet refs/heads/victim > /dev/null; then |
| 402 | fail 'reject kept a branch at the recorded head' |
| 403 | fi |
| 404 | |
| 405 | # ---------------------------------------------------------------------- |
| 406 | # A draft is a change paused. unready makes a ready change a draft, and |
| 407 | # ready brings it back: at the head of its active version it is ready |
| 408 | # again at that version, and at a new head it gains the next one. pending |
| 409 | # lists a draft apart from the branches, and approve and rebase refuse it, |
| 410 | # naming its state. Until the integrator reads records, a request filed |
| 411 | # before a change was paused or withdrawn can still land it, so unready and |
| 412 | # withdraw warn when one is pending, and leave it for the operator. |
| 413 | git checkout --quiet -b drafts/paused main |
| 414 | commit paused |
| 415 | paused="$(git rev-parse HEAD)" |
| 416 | as contributor git push --quiet origin drafts/paused |
| 417 | ok as contributor valley ready |
| 418 | pid="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 419 | ph="$(sed -n 's/^project \([k-z]*\) v1 ready$/\1/p' "$w/out")" |
| 420 | if [ "${#pid}" != 32 ] || [ -z "$ph" ]; then fail 'ready did not name the new change'; fi |
| 421 | |
| 422 | ok as contributor valley unready "$ph" |
| 423 | grep -qx "project $ph v1 draft" "$w/out" || fail 'unready did not say it made a draft' |
| 424 | record "$pid" '.state == "draft"' '.seq == 2' '.active == 1' '.versions | length == 1' |
| 425 | one_parent "$pid" |
| 426 | if grep -q 'warning' "$w/err"; then fail 'unready warned with no request pending'; fi |
| 427 | unchanged "$pid" refused 'is a draft already' as contributor valley unready "$ph" |
| 428 | |
| 429 | # pending lists the draft under drafts, and not among the branches, since |
| 430 | # its branch is at the draft's head. |
| 431 | ok valley pending |
| 432 | grep -qx 'drafts' "$w/out" || fail 'pending listed no drafts' |
| 433 | grep -q "^$ph v1 on drafts/paused | paused | " "$w/out" || fail 'pending did not list the draft' |
| 434 | if grep -q '^drafts/paused | ' "$w/out"; then fail 'pending listed a draft among the branches'; fi |
| 435 | grep -q '^topic | ' "$w/out" || fail 'pending no longer lists the branches' |
| 436 | |
| 437 | # A draft is not approved or rebased. Refused in review, approving is no |
| 438 | # check failure, so the prompt still offers [a]pprove. |
| 439 | request_ref=refs/the-valley/integration-requests/main/drafts-paused |
| 440 | unchanged "$pid" refused "change $ph v1 is draft, not ready; nothing published" as requester valley approve "$ph" |
| 441 | if git -C "$origin" rev-parse --verify --quiet "$request_ref" > /dev/null; then |
| 442 | fail 'approve filed a request for a draft' |
| 443 | fi |
| 444 | unchanged "$pid" refused "change $ph v1 is draft, not ready; nothing rebased" as requester valley rebase "$ph" |
| 445 | printf 'a\ns\n' | as requester valley review "$ph" > "$w/out" 2> "$w/err" || fail 'review of a draft failed' |
| 446 | grep -qF "change $ph v1 is draft, not ready; nothing published" "$w/err" \ |
| 447 | || fail "review's [a]pprove did not refuse a draft" |
| 448 | [ "$(grep -oF 'drafts/paused: [a]pprove, [r]eject, [s]kip? ' "$w/out" | wc -l)" = 2 ] \ |
| 449 | || fail "review's prompt changed after a refusal that was no check failure" |
| 450 | # Named by its branch, written either way, the draft is the same change, |
| 451 | # since its active version is the one record on that branch. It is |
| 452 | # refused the same way. |
| 453 | for name in drafts/paused origin/drafts/paused; do |
| 454 | unchanged "$pid" refused "change $ph v1 is draft, not ready; nothing published" as requester valley approve "$name" |
| 455 | unchanged "$pid" refused "change $ph v1 is draft, not ready; nothing rebased" as requester valley rebase "$name" |
| 456 | printf 'a\ns\n' | as requester valley review "$name" > "$w/out" 2> "$w/err" || fail "review of $name failed" |
| 457 | grep -qF "change $ph v1 is draft, not ready; nothing published" "$w/err" \ |
| 458 | || fail "review's [a]pprove did not refuse a draft named $name" |
| 459 | done |
| 460 | if git -C "$origin" rev-parse --verify --quiet "$request_ref" > /dev/null; then |
| 461 | fail 'approve filed a request for a draft named by its branch' |
| 462 | fi |
| 463 | |
| 464 | # ready at the same head is v1 again, and a draft once more after it. |
| 465 | ok as contributor valley ready --change "$pid" |
| 466 | grep -qx "project $ph v1 ready" "$w/out" || fail 'a draft readied at its head was not v1 again' |
| 467 | record "$pid" '.state == "ready"' '.seq == 3' '.active == 1' '.versions | length == 1' |
| 468 | one_parent "$pid" |
| 469 | |
| 470 | # Approved now, the change has a request under its branch-derived name. |
| 471 | # Ready and at its recorded head, it is approved by its branch, written |
| 472 | # either way, and in review. unready voids nothing, so the request stays. |
| 473 | # It warns, because the integrator does not read records yet. |
| 474 | ok as requester valley approve drafts/paused |
| 475 | [ "$(git -C "$origin" rev-parse "$request_ref")" = "$paused" ] || fail 'approve did not file the ready change' |
| 476 | ok as requester valley approve origin/drafts/paused |
| 477 | grep -q '^Approved drafts/paused' "$w/out" || fail 'approve of origin/drafts/paused did not go ahead' |
| 478 | printf 'a\n' | as requester valley review drafts/paused > "$w/out" 2> "$w/err" || fail 'review of drafts/paused failed' |
| 479 | grep -q "^change $ph v1 at " "$w/out" || fail 'review of a branch did not read its change' |
| 480 | grep -q '^Approved drafts/paused' "$w/out" || fail "review's [a]pprove of drafts/paused did not go ahead" |
| 481 | ok as contributor valley unready "$ph" |
| 482 | grep -qF "valley: warning: the integration request $request_ref for $ph is pending on origin" "$w/err" \ |
| 483 | || fail 'unready did not warn of the pending request' |
| 484 | grep -qF 'so the operator must drop it' "$w/err" || fail 'the warning did not say who must drop the request' |
| 485 | [ "$(git -C "$origin" rev-parse "$request_ref")" = "$paused" ] || fail 'unready touched the request' |
| 486 | |
| 487 | # unready, then ready at a new head, is v2. |
| 488 | commit paused-again |
| 489 | paused_again="$(git rev-parse HEAD)" |
| 490 | as contributor git push --quiet origin drafts/paused |
| 491 | ok as contributor valley ready --change "$pid" |
| 492 | grep -qx "project $ph v2 ready" "$w/out" || fail 'a draft readied at a new head was not v2' |
| 493 | record "$pid" '.state == "ready"' '.seq == 5' '.active == 2' '.versions | length == 2' \ |
| 494 | ".versions[1].head == \"git-sha1:$paused_again\"" |
| 495 | |
| 496 | # withdraw warns of the same request, and the change, withdrawn, is never |
| 497 | # made a draft. ready at the same head brings it back at v2. |
| 498 | ok as contributor valley withdraw "$ph" |
| 499 | grep -qx "project $ph v2 withdrawn" "$w/out" || fail 'withdraw did not say what it withdrew' |
| 500 | grep -qF "valley: warning: the integration request $request_ref for $ph is pending on origin" "$w/err" \ |
| 501 | || fail 'withdraw did not warn of the pending request' |
| 502 | record "$pid" '.state == "withdrawn"' '.seq == 6' '.active == 2' |
| 503 | unchanged "$pid" refused 'never made a draft' as contributor valley unready "$ph" |
| 504 | ok as contributor valley ready --change "$pid" |
| 505 | grep -qx "project $ph v2 ready" "$w/out" || fail 'a withdrawn change readied at its head was not v2 again' |
| 506 | record "$pid" '.state == "ready"' '.seq == 7' '.active == 2' '.versions | length == 2' |
| 507 | one_parent "$pid" |
| 508 | |
| 509 | # A request named c-<change ID> is warned of too. withdraw takes a draft. |
| 510 | as requester git push --quiet origin --delete "$request_ref" |
| 511 | as requester git push --quiet origin "$paused_again:refs/the-valley/integration-requests/main/c-$pid" |
| 512 | ok as contributor valley unready "$ph" |
| 513 | grep -qF "the integration request refs/the-valley/integration-requests/main/c-$pid for $ph is pending" "$w/err" \ |
| 514 | || fail 'unready did not warn of a request named by the change ID' |
| 515 | if grep -qF "$request_ref " "$w/err"; then fail 'unready warned of a request that was dropped'; fi |
| 516 | ok as contributor valley withdraw "$ph" |
| 517 | record "$pid" '.state == "withdrawn"' '.seq == 9' '.active == 2' |
| 518 | grep -qF "main/c-$pid for $ph is pending" "$w/err" || fail 'withdraw did not warn of a request named by the change ID' |
| 519 | |
| 520 | # ---------------------------------------------------------------------- |
| 521 | # review, approve and rebase read the record again just before they act. |
| 522 | # A review opened while the change is ready refuses to approve or rebase |
| 523 | # it once another principal has unreadied it, and refuses to approve it |
| 524 | # once it has a new version. Its branch starts behind main, so the review |
| 525 | # offers [b]ase as well. |
| 526 | # |
| 527 | # review_while <change> <answers> <command…>: review the change as the |
| 528 | # operator, run the command once the review waits at its prompt, then |
| 529 | # give the answers. |
| 530 | review_while() { |
| 531 | local name="$1" answers="$2" reviewer i |
| 532 | shift 2 |
| 533 | # A review.out left from an earlier review would show a prompt this one |
| 534 | # has not reached. |
| 535 | rm -f "$w/fifo" "$w/review.out" |
| 536 | mkfifo "$w/fifo" |
| 537 | VALLEY_PRINCIPAL=requester valley review "$name" < "$w/fifo" > "$w/review.out" 2> "$w/review.err" & |
| 538 | reviewer=$! |
| 539 | exec 3> "$w/fifo" |
| 540 | for i in $(seq 600); do |
| 541 | if grep -qF '[s]kip? ' "$w/review.out" 2> /dev/null; then break; fi |
| 542 | sleep 0.1 |
| 543 | done |
| 544 | grep -qF '[s]kip? ' "$w/review.out" || fail "review of $name never reached its prompt (after $i tries)" |
| 545 | "$@" > "$w/out" 2> "$w/err" || fail "$* failed while the review was open" |
| 546 | printf '%b' "$answers" >&3 |
| 547 | exec 3>&- |
| 548 | wait "$reviewer" || fail "review of $name failed" |
| 549 | } |
| 550 | git checkout --quiet -b drafts/raced main~1 |
| 551 | commit raced |
| 552 | raced="$(git rev-parse HEAD)" |
| 553 | as contributor git push --quiet origin drafts/raced |
| 554 | ok as contributor valley ready |
| 555 | rid="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 556 | rh="$(sed -n 's/^project \([k-z]*\) v1 ready$/\1/p' "$w/out")" |
| 557 | if [ "${#rid}" != 32 ] || [ -z "$rh" ]; then fail 'ready did not name the new change'; fi |
| 558 | raced_request=refs/the-valley/integration-requests/main/drafts-raced |
| 559 | |
| 560 | review_while "$rh" 'a\nb\ns\n' as contributor valley unready "$rh" |
| 561 | grep -qF "change $rh v1 is draft, not ready; nothing published" "$w/review.err" \ |
| 562 | || fail "review's [a]pprove went ahead for a change unreadied while the review was open" |
| 563 | grep -qF "change $rh v1 is draft, not ready; nothing rebased" "$w/review.err" \ |
| 564 | || fail "review's [b]ase went ahead for a change unreadied while the review was open" |
| 565 | if git -C "$origin" rev-parse --verify --quiet "$raced_request" > /dev/null; then |
| 566 | fail 'review filed a request for a change unreadied while it was open' |
| 567 | fi |
| 568 | [ "$(git -C "$origin" rev-parse refs/heads/drafts/raced)" = "$raced" ] \ |
| 569 | || fail 'review rebased a change unreadied while it was open' |
| 570 | |
| 571 | ok as contributor valley ready --change "$rid" |
| 572 | commit raced-again |
| 573 | raced_again="$(git rev-parse HEAD)" |
| 574 | new_version() { |
| 575 | as contributor git push --quiet origin drafts/raced && as contributor valley ready --change "$rid" |
| 576 | } |
| 577 | review_while "$rh" 'a\ns\n' new_version |
| 578 | grep -qF "change $rh is at v2, $raced_again, and this run began at v1, $raced; nothing published" "$w/review.err" \ |
| 579 | || fail "review's [a]pprove went ahead for a change that gained a version while the review was open" |
| 580 | if git -C "$origin" rev-parse --verify --quiet "$raced_request" > /dev/null; then |
| 581 | fail 'review filed a request for a change that gained a version while it was open' |
| 582 | fi |
| 583 | |
| 584 | # ---------------------------------------------------------------------- |
| 585 | # A request filed for an earlier version is named by that version's |
| 586 | # branch. withdraw warns of it, though the active version is on another. |
| 587 | git checkout --quiet -b moved/old main |
| 588 | commit old |
| 589 | as contributor git push --quiet origin moved/old |
| 590 | ok as contributor valley ready |
| 591 | old_id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 592 | old_handle="$(sed -n 's/^project \([k-z]*\) v1 ready$/\1/p' "$w/out")" |
| 593 | ok as requester valley approve "$old_handle" |
| 594 | old_request=refs/the-valley/integration-requests/main/moved-old |
| 595 | git -C "$origin" rev-parse --verify --quiet "$old_request" > /dev/null || fail 'approve filed no request for moved/old' |
| 596 | git checkout --quiet -b moved/new |
| 597 | commit new |
| 598 | as contributor git push --quiet origin moved/new |
| 599 | ok as contributor valley ready --change "$old_id" |
| 600 | grep -qx "project $old_handle v2 ready" "$w/out" || fail 'the version on moved/new was not v2' |
| 601 | ok as contributor valley withdraw "$old_handle" |
| 602 | grep -qF "valley: warning: the integration request $old_request for $old_handle is pending on origin" "$w/err" \ |
| 603 | || fail "withdraw did not warn of the request filed for v1's branch" |
| 604 | |
| 605 | # ---------------------------------------------------------------------- |
| 606 | # A branch no record's active version is on is a branch, as before |
| 607 | # records: review shows its tip, and approve files it under its own name. |
| 608 | printf 's\n' | valley review pushed > "$w/out" 2> "$w/err" || fail 'review of a branch with no record failed' |
| 609 | grep -q '^branch pushed, log against origin/main' "$w/out" || fail 'review read a change for a branch with no record' |
| 610 | ok as requester valley approve pushed |
| 611 | [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/pushed)" \ |
| 612 | = "$(git -C "$origin" rev-parse refs/heads/pushed)" ] || fail 'approve did not file a branch with no record at its tip' |
| 613 | |
| 614 | # A branch two records' active versions are on names neither: the verbs |
| 615 | # that act on a change refuse it, and list both IDs. |
| 616 | twin=llllmmmmnnnnoooollllmmmmnnnnoooo |
| 617 | ok as contributor valley ready --change "$twin" topic |
| 618 | for verb in approve rebase; do |
| 619 | refused 'name one by its change ID' as requester valley "$verb" topic |
| 620 | grep -qF "$id" "$w/err" || fail "$verb of topic did not list $id" |
| 621 | grep -qF "$twin" "$w/err" || fail "$verb of topic did not list $twin" |
| 622 | done |
| 623 | refused 'name one by its change ID' as requester valley approve origin/topic |
| 624 | |
| 625 | echo 'valley-ready: change records through real pushes passed' |
| 626 | touch "$out" |