| 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 | # review waits for the checks line, which it otherwise leaves out when it is slow. |
| 16 | export VALLEY_REVIEW_CHECKS_WAIT=120 |
| 17 | export GIT_CONFIG_NOSYSTEM=1 |
| 18 | git config --global user.name valley-ready-check |
| 19 | git config --global user.email valley-ready-check@localhost |
| 20 | git config --global init.defaultBranch main |
| 21 | git config --global advice.detachedHead false |
| 22 | |
| 23 | fail() { |
| 24 | printf 'valley-ready: %s\n' "$*" >&2 |
| 25 | for f in "$w"/out "$w"/err; do |
| 26 | if [ -s "$f" ]; then cat "$f" >&2; fi |
| 27 | done |
| 28 | exit 1 |
| 29 | } |
| 30 | |
| 31 | # The project "open" of the protected host: it protects nothing, and the |
| 32 | # host grants request to the principal named requester. Its hook is the |
| 33 | # one valley-init installs, followed from the rendered init script. |
| 34 | origin="$w/project.git" |
| 35 | hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$initScriptPath" | head -n1)" |
| 36 | test -x "$hook" |
| 37 | git init --quiet --bare "$origin" |
| 38 | cp "$hook" "$origin/hooks/pre-receive" |
| 39 | chmod +x "$origin/hooks/pre-receive" |
| 40 | |
| 41 | repo="$w/project" |
| 42 | git init --quiet "$repo" |
| 43 | cd "$repo" |
| 44 | git remote add origin "$origin" |
| 45 | echo base > readme |
| 46 | git add readme |
| 47 | git commit --quiet -m base |
| 48 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 49 | |
| 50 | # as <principal> <command…>: run a command as a principal. The record names |
| 51 | # the principal git config valley.principal does, and the hook reads the |
| 52 | # one the push carries; an honest client has the two agree. anonymous is a |
| 53 | # key that names no principal. |
| 54 | as() { |
| 55 | local who="$1" |
| 56 | shift |
| 57 | if [ "$who" = anonymous ]; then |
| 58 | git config --unset valley.principal || true |
| 59 | env -u VALLEY_PRINCIPAL "$@" |
| 60 | else |
| 61 | git config valley.principal "$who" |
| 62 | VALLEY_PRINCIPAL="$who" "$@" |
| 63 | fi |
| 64 | } |
| 65 | |
| 66 | # ok <command…>: the command succeeds, and its output is kept. |
| 67 | ok() { |
| 68 | "$@" > "$w/out" 2> "$w/err" || fail "$* failed" |
| 69 | } |
| 70 | |
| 71 | # refused <phrase> <command…>: the command fails, saying the phrase. |
| 72 | refused() { |
| 73 | local says="$1" |
| 74 | shift |
| 75 | if "$@" > "$w/out" 2> "$w/err"; then |
| 76 | fail "$* succeeded where it had to be refused" |
| 77 | fi |
| 78 | grep -qF -- "$says" "$w/err" || fail "$* was refused without saying: $says" |
| 79 | } |
| 80 | |
| 81 | # record <id> <jq condition…>: origin's record of the change holds each. |
| 82 | record() { |
| 83 | local id="$1" cond |
| 84 | shift |
| 85 | git -C "$origin" cat-file blob "refs/the-valley/changes/main/$id:record.json" > "$w/record.json" \ |
| 86 | || fail "origin holds no record of $id" |
| 87 | for cond in "$@"; do |
| 88 | jq -e "$cond" "$w/record.json" > /dev/null || fail "the record of $id: $cond does not hold" |
| 89 | done |
| 90 | } |
| 91 | |
| 92 | # unchanged <id> <command…>: the command leaves origin's record as it was. |
| 93 | unchanged() { |
| 94 | local id="$1" before |
| 95 | shift |
| 96 | before="$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id")" |
| 97 | "$@" |
| 98 | [ "$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id")" = "$before" ] \ |
| 99 | || fail "the record of $id moved" |
| 100 | } |
| 101 | |
| 102 | # one_parent <id>: origin's record commit of the change has one parent, |
| 103 | # the record before it, so it added no version. |
| 104 | one_parent() { |
| 105 | [ "$(git -C "$origin" rev-list --parents -n 1 "refs/the-valley/changes/main/$1" | wc -w)" = 2 ] \ |
| 106 | || fail "the record of $1 added a version" |
| 107 | } |
| 108 | |
| 109 | no_records() { |
| 110 | [ -z "$(git -C "$origin" for-each-ref refs/the-valley/changes)" ] || fail "$1" |
| 111 | } |
| 112 | |
| 113 | # commit <subject> [<trailer>]: a new commit on the current branch, adding |
| 114 | # a file of its own, so that any two branches merge cleanly. |
| 115 | commit() { |
| 116 | echo "$1" > "$1.txt" |
| 117 | git add "$1.txt" |
| 118 | if [ $# -gt 1 ]; then |
| 119 | git commit --quiet -m "$1" -m "$2" |
| 120 | else |
| 121 | git commit --quiet -m "$1" |
| 122 | fi |
| 123 | } |
| 124 | |
| 125 | # with_header <id>: HEAD rewritten to carry a change-id header, the way jj |
| 126 | # writes one, after the committer. |
| 127 | with_header() { |
| 128 | local amended |
| 129 | amended="$(git cat-file commit HEAD | sed "/^committer /a change-id $1" | |
| 130 | git hash-object -t commit -w --stdin)" |
| 131 | git update-ref refs/heads/"$(git symbolic-ref --short HEAD)" "$amended" |
| 132 | } |
| 133 | |
| 134 | # ---------------------------------------------------------------------- |
| 135 | # A head marked ready for the first time is v1 of a new change. Its head |
| 136 | # carries no change ID, so ready makes one and prints it. |
| 137 | git checkout --quiet -b topic |
| 138 | commit one |
| 139 | first="$(git rev-parse HEAD)" |
| 140 | refused 'push it there first' as contributor valley ready |
| 141 | no_records 'a head not on origin was recorded' |
| 142 | as contributor git push --quiet origin topic |
| 143 | ok as contributor valley ready |
| 144 | grep -qE '^project [k-z]{4} v1 ready$' "$w/out" || fail 'ready did not print the handle and v1' |
| 145 | id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 146 | [ "${#id}" = 32 ] || fail 'ready did not print the change ID it made' |
| 147 | handle="${id:0:4}" |
| 148 | grep -qx "project $handle v1 ready" "$w/out" || fail 'the handle is not the ID'"'"'s first four letters' |
| 149 | record "$id" '.schema == "valley-change-record/v1"' ".change_id == \"$id\"" '.target == "main"' \ |
| 150 | '.owner == "contributor"' '.state == "ready"' '.seq == 1' '.active == 1' \ |
| 151 | ".versions == [{n: 1, head: \"git-sha1:$first\", title: \"one\", branch: \"topic\", uploader: \"contributor\"}]" |
| 152 | # The record commit holds on to the head it records. |
| 153 | [ "$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id^1")" = "$first" ] \ |
| 154 | || fail 'the record commit does not have the head as its parent' |
| 155 | |
| 156 | # A second ready at a new head is v2, named by the ID it printed. |
| 157 | commit two |
| 158 | second="$(git rev-parse HEAD)" |
| 159 | as contributor git push --quiet origin topic |
| 160 | ok as contributor valley ready --change "$id" |
| 161 | grep -qx "project $handle v2 ready" "$w/out" || fail 'the second ready was not v2' |
| 162 | record "$id" '.seq == 2' '.active == 2' '.state == "ready"' '.versions | length == 2' \ |
| 163 | ".versions[0].head == \"git-sha1:$first\"" ".versions[1] == {n: 2, head: \"git-sha1:$second\", title: \"two\", branch: \"topic\", uploader: \"contributor\"}" |
| 164 | |
| 165 | # The same head again is no new version. |
| 166 | unchanged "$id" refused 'a new version needs a new head' as contributor valley ready --change "$id" |
| 167 | |
| 168 | # Withdrawn, the change comes back by ready. At the head it was withdrawn |
| 169 | # at, it is ready again at the version it had: the record commit's one |
| 170 | # parent is the record before it. It is never made a draft. |
| 171 | ok as contributor valley withdraw "$handle" |
| 172 | grep -qx "project $handle v2 withdrawn" "$w/out" || fail 'withdraw did not say what it withdrew' |
| 173 | record "$id" '.state == "withdrawn"' '.seq == 3' '.active == 2' '.versions | length == 2' |
| 174 | unchanged "$id" refused 'withdrawn already' as contributor valley withdraw "$handle" |
| 175 | unchanged "$id" refused 'never made a draft' as contributor valley unready "$handle" |
| 176 | ok as contributor valley ready --change "$id" |
| 177 | grep -qx "project $handle v2 ready" "$w/out" || fail 'the change readied at the head it was withdrawn at was not v2' |
| 178 | record "$id" '.state == "ready"' '.seq == 4' '.active == 2' '.versions | length == 2' |
| 179 | one_parent "$id" |
| 180 | # Withdrawn again, it comes back by a new version at a new head too. |
| 181 | ok as contributor valley withdraw "$handle" |
| 182 | commit three |
| 183 | third="$(git rev-parse HEAD)" |
| 184 | as contributor git push --quiet origin topic |
| 185 | ok as contributor valley ready --change "$id" |
| 186 | grep -qx "project $handle v3 ready" "$w/out" || fail 'the revived change was not v3' |
| 187 | record "$id" '.state == "ready"' '.seq == 6' '.active == 3' ".versions[2].head == \"git-sha1:$third\"" |
| 188 | |
| 189 | # ---------------------------------------------------------------------- |
| 190 | # Who may write the record. A principal that neither owns the change nor |
| 191 | # holds the request grant is refused by the hook, whatever the command |
| 192 | # writes, and so is a key that names no principal. |
| 193 | commit four |
| 194 | fourth="$(git rev-parse HEAD)" |
| 195 | as contributor git push --quiet origin topic |
| 196 | unchanged "$id" refused "only the change's owner, contributor" as integrator valley ready --change "$id" |
| 197 | unchanged "$id" refused "only the change's owner, contributor" as integrator valley withdraw "$handle" |
| 198 | unchanged "$id" refused "only the change's owner, contributor" as integrator valley unready "$handle" |
| 199 | unchanged "$id" refused 'a key that names no principal' env -u VALLEY_PRINCIPAL valley withdraw "$handle" |
| 200 | refused 'git config valley.principal names none' as anonymous valley ready --change "$id" |
| 201 | # The operator, who holds the request grant, may add a version and |
| 202 | # withdraw, and the owner stays the owner. |
| 203 | ok as requester valley ready --head "$fourth" --change "$id" |
| 204 | grep -qx "project $handle v4 ready" "$w/out" || fail "the operator's version was not v4" |
| 205 | record "$id" '.owner == "contributor"' '.versions[3].uploader == "requester"' '.seq == 7' |
| 206 | ok as requester valley withdraw "$id" |
| 207 | record "$id" '.state == "withdrawn"' '.seq == 8' '.owner == "contributor"' |
| 208 | |
| 209 | # --head marks exactly the head given, which must be on origin. An older |
| 210 | # head of the branch is on origin, and a commit nobody pushed is not. |
| 211 | commit five |
| 212 | unpushed="$(git rev-parse HEAD)" |
| 213 | unchanged "$id" refused 'push it there first' as contributor valley ready --head "$unpushed" --change "$id" |
| 214 | git reset --quiet --hard "$fourth" |
| 215 | |
| 216 | # ---------------------------------------------------------------------- |
| 217 | # Change identity. A jj change-id header in the head commit names the |
| 218 | # change; so does a Change-Id trailer in its message. The two disagreeing |
| 219 | # is refused, and so is a value that is no change ID. |
| 220 | header_id=zyxwvutsrqponmlkzyxwvutsrqponmlk |
| 221 | trailer_id=kkkkllllmmmmnnnnooooppppqqqqrrrr |
| 222 | git checkout --quiet -b from-header main |
| 223 | commit header |
| 224 | with_header "$header_id" |
| 225 | as contributor git push --quiet origin from-header |
| 226 | ok as contributor valley ready |
| 227 | grep -qx "project zyxw v1 ready" "$w/out" || fail 'the header did not name the change' |
| 228 | grep -q 'is new' "$w/out" && fail 'a change ID read from the header was reported as made' |
| 229 | record "$header_id" '.versions[0].branch == "from-header"' |
| 230 | |
| 231 | git checkout --quiet -b from-trailer main |
| 232 | commit trailer "Change-Id: $trailer_id" |
| 233 | as contributor git push --quiet origin from-trailer |
| 234 | ok as contributor valley ready |
| 235 | grep -qx "project kkkk v1 ready" "$w/out" || fail 'the trailer did not name the change' |
| 236 | record "$trailer_id" '.versions[0].branch == "from-trailer"' |
| 237 | |
| 238 | git checkout --quiet -b disagreeing main |
| 239 | commit disagreeing "Change-Id: $trailer_id" |
| 240 | with_header "$header_id" |
| 241 | as contributor git push --quiet origin disagreeing |
| 242 | unchanged "$trailer_id" unchanged "$header_id" \ |
| 243 | refused 'they must agree' as contributor valley ready |
| 244 | refused 'they must agree' as contributor valley ready --change "$id" |
| 245 | |
| 246 | git checkout --quiet -b malformed main |
| 247 | commit malformed "Change-Id: I0123456789abcdef0123456789abcdef01234567" |
| 248 | as contributor git push --quiet origin malformed |
| 249 | refused 'which is not a change ID' as contributor valley ready |
| 250 | |
| 251 | # A change is a line of commits from its merge base with main. A merge |
| 252 | # in that range is refused. |
| 253 | git checkout --quiet -b merged main |
| 254 | commit merged |
| 255 | git merge --quiet --no-ff -m merge from-trailer |
| 256 | as contributor git push --quiet origin merged |
| 257 | refused 'include the merge' as contributor valley ready |
| 258 | |
| 259 | # ---------------------------------------------------------------------- |
| 260 | # A change is named by its handle wherever a change is taken: status |
| 261 | # reads the branch its active version names, and withdraw takes a branch |
| 262 | # name too. |
| 263 | ok valley status kkkk |
| 264 | grep -q '^kkkk v1: not approved' "$w/out" || fail 'status did not read the change by its handle' |
| 265 | grep -q '^Change kkkk v1 on from-trailer, owned by contributor: trailer$' "$w/out" \ |
| 266 | || fail 'status did not say what the record says' |
| 267 | ok as contributor valley withdraw from-trailer |
| 268 | grep -qx 'project kkkk v1 withdrawn' "$w/out" || fail 'withdraw did not take a branch name' |
| 269 | |
| 270 | # --stamp carries a new change's ID in an unpushed head as a trailer, |
| 271 | # pushes the branch, and marks it ready. It will not rewrite a head that |
| 272 | # is on origin in a change that is not the user's. |
| 273 | git checkout --quiet -b stamped main |
| 274 | commit stamped |
| 275 | ok as contributor valley ready --stamp |
| 276 | grep -q 'is new' "$w/out" && fail '--stamp hinted at carrying an ID the head already has' |
| 277 | stamped_id="$(git log -1 --format='%(trailers:key=Change-Id,valueonly)' | head -n1)" |
| 278 | [ "${#stamped_id}" = 32 ] || fail '--stamp did not carry an ID in the head' |
| 279 | [ "$(git rev-parse HEAD)" = "$(git -C "$origin" rev-parse refs/heads/stamped)" ] \ |
| 280 | || fail '--stamp did not push the stamped head' |
| 281 | record "$stamped_id" ".versions[0].head == \"git-sha1:$(git rev-parse HEAD)\"" |
| 282 | git checkout --quiet -b pushed main |
| 283 | commit pushed |
| 284 | as contributor git push --quiet origin pushed |
| 285 | refused 'will not rewrite it' as integrator valley ready --stamp |
| 286 | # A change that has a record already is never stamped, whoever |
| 287 | # valley.principal says is asking: neither the local branch nor origin's |
| 288 | # moves. |
| 289 | git checkout --quiet stamped |
| 290 | commit restamped |
| 291 | restamped="$(git rev-parse HEAD)" |
| 292 | unchanged "$stamped_id" refused 'has a record on origin already' \ |
| 293 | as contributor valley ready --stamp --change "$stamped_id" |
| 294 | [ "$(git rev-parse HEAD)" = "$restamped" ] || fail '--stamp rewrote the head of a change with a record' |
| 295 | [ "$(git -C "$origin" rev-parse refs/heads/stamped)" != "$restamped" ] \ |
| 296 | || fail '--stamp pushed for a change with a record' |
| 297 | # --stamp reads the local branch by its exact name. With no local ghost, |
| 298 | # git rev-parse would take the branch named refs/heads/ghost, which is |
| 299 | # refs/heads/refs/heads/ghost, for it, and stamp and push that. |
| 300 | git branch refs/heads/ghost "$(git commit-tree -p main -m ghost 'main^{tree}')" |
| 301 | refused 'there is no such branch' as contributor valley ready --stamp ghost |
| 302 | git -C "$origin" show-ref --exists refs/heads/ghost 2> /dev/null \ |
| 303 | && fail '--stamp pushed the branch named like the missing one' |
| 304 | git branch -D --quiet refs/heads/ghost |
| 305 | |
| 306 | # ---------------------------------------------------------------------- |
| 307 | # A change named by its handle is approved, reviewed and rebased at the |
| 308 | # head its record names, never at its branch's tip. The record's branch is |
| 309 | # only where that head is expected to be, and whoever can push the branch |
| 310 | # can move it, in the same push as the record if they like. |
| 311 | # |
| 312 | # approve derives what the change requires from the owning valley's floor, |
| 313 | # named at policy/valley on main. This floor requires nothing, so approving |
| 314 | # files a request with no evidence, and no signing key is needed. |
| 315 | valley="$w/valley.git" |
| 316 | git init --quiet --bare "$valley" |
| 317 | git init --quiet "$w/valley-work" |
| 318 | mkdir -p "$w/valley-work/policy/instance" |
| 319 | cat > "$w/valley-work/policy/instance/floor.cue" << 'CUE' |
| 320 | package verification |
| 321 | |
| 322 | floor: { |
| 323 | checks: {} |
| 324 | classes: {} |
| 325 | unclassified: {} |
| 326 | } |
| 327 | CUE |
| 328 | git -C "$w/valley-work" add -A |
| 329 | git -C "$w/valley-work" commit --quiet -m floor |
| 330 | git -C "$w/valley-work" push --quiet "$valley" HEAD:main |
| 331 | git checkout --quiet main |
| 332 | mkdir -p policy |
| 333 | echo "$valley" > policy/valley |
| 334 | git add policy/valley |
| 335 | git commit --quiet -m 'name the owning valley' |
| 336 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 337 | |
| 338 | # write_record <json> <parent…>: a record commit holding the JSON, written |
| 339 | # by hand, the way anyone holding a key could write one. |
| 340 | write_record() { |
| 341 | local json="$1" blob tree parent |
| 342 | local -a parents=() |
| 343 | shift |
| 344 | for parent in "$@"; do parents+=(-p "$parent"); done |
| 345 | blob="$(printf '%s\n' "$json" | git hash-object -w --stdin)" |
| 346 | tree="$(printf '100644 blob %s\trecord.json\n' "$blob" | git mktree)" |
| 347 | git commit-tree "$tree" "${parents[@]}" -m 'a change record' |
| 348 | } |
| 349 | no_request() { |
| 350 | if git -C "$origin" rev-parse --verify --quiet "$request" > /dev/null; then |
| 351 | fail "$1" |
| 352 | fi |
| 353 | } |
| 354 | |
| 355 | # The record names H on victim, and the same push puts B on victim. |
| 356 | target_id=ppppqqqqrrrrssssttttuuuuvvvvwwww |
| 357 | request="refs/the-valley/integration-requests/main/c-$target_id" |
| 358 | git checkout --quiet -b victim main |
| 359 | commit honest |
| 360 | honest="$(git rev-parse HEAD)" |
| 361 | commit substituted |
| 362 | substituted="$(git rev-parse HEAD)" |
| 363 | v1="$(jq -cn --arg id "$target_id" --arg head "git-sha1:$honest" '{schema: "valley-change-record/v1", |
| 364 | change_id: $id, target: "main", owner: "contributor", state: "ready", seq: 1, |
| 365 | versions: [{n: 1, head: $head, title: "honest", branch: "victim", uploader: "contributor"}], active: 1}')" |
| 366 | r1="$(write_record "$v1" "$honest")" |
| 367 | as contributor git push --quiet origin "$r1:refs/the-valley/changes/main/$target_id" \ |
| 368 | "$substituted:refs/heads/victim" |
| 369 | [ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] || fail 'the branch did not move with the record' |
| 370 | |
| 371 | # approve binds the head it approves. Off a terminal it needs --head, and |
| 372 | # a head no version records is refused. |
| 373 | refused 'not approved · no terminal to confirm the head' as requester valley approve pppp |
| 374 | no_request 'approve filed a request with no head bound to it' |
| 375 | refused "is none of its versions" as requester valley approve --head "$substituted" pppp |
| 376 | no_request 'approve filed a request for a branch tip no version records' |
| 377 | refused "origin/victim is at $substituted; nothing rebased" as requester valley rebase pppp |
| 378 | [ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] || fail 'rebase moved the branch' |
| 379 | # review shows the recorded head, and none of what the branch added. |
| 380 | printf 's\n' | valley review pppp > "$w/out" 2> "$w/err" || fail 'review of a handle failed' |
| 381 | grep -q '^change pppp v1 at' "$w/out" || fail 'review did not say which version it shows' |
| 382 | grep -q ' honest$' "$w/out" || fail 'review did not show the recorded head' |
| 383 | if grep -q 'substituted' "$w/out"; then fail 'review showed the branch tip, not the recorded head'; fi |
| 384 | # Rejecting it there deletes nothing while the branch is elsewhere: the |
| 385 | # refusal names both commits, and the next answer goes to the prompt. |
| 386 | printf 'r\nyes\ns\n' | as requester valley review pppp > "$w/out" 2> "$w/err" \ |
| 387 | || fail 'review of a handle failed' |
| 388 | grep -qF "records the head $honest, and origin/victim is at $substituted; nothing deleted" "$w/err" \ |
| 389 | || fail 'reject did not refuse a branch whose tip is not the recorded head' |
| 390 | [ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] \ |
| 391 | || fail 'reject deleted a branch whose tip is not the recorded head' |
| 392 | |
| 393 | # The dashboard's approve names the head it showed, and approve files |
| 394 | # exactly that head under the change's request name, wherever the branch |
| 395 | # is: here the branch is still at the substituted commit. |
| 396 | ok as requester valley approve -v pppp --head "$honest" |
| 397 | [ "$(git -C "$origin" rev-parse "$request")" = "$honest" ] || fail 'approve did not file the recorded head' |
| 398 | grep -q "^[^ ]* pppp v1 approved · " "$w/out" || fail 'approve did not say which version it approved' |
| 399 | grep -qx " request $request -> $(git rev-parse --short "$honest")" "$w/out" \ |
| 400 | || fail 'approve did not file the request as c-<change ID>' |
| 401 | if git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/victim > /dev/null; then |
| 402 | fail 'approve filed a change with a record under its branch name' |
| 403 | fi |
| 404 | as contributor git push --quiet --force origin "$honest:refs/heads/victim" |
| 405 | |
| 406 | # A second agent acting as the same principal adds v2 at a head that is |
| 407 | # not on victim, while its record says victim. approve refuses, and the |
| 408 | # request stays where it was. |
| 409 | git checkout --quiet -b elsewhere main |
| 410 | commit elsewhere |
| 411 | elsewhere="$(git rev-parse HEAD)" |
| 412 | as contributor git push --quiet origin elsewhere |
| 413 | v2="$(jq -c --arg head "git-sha1:$elsewhere" '.versions += [{n: 2, head: $head, title: "elsewhere", |
| 414 | branch: "victim", uploader: "contributor"}] | .active = 2 | .seq = 2' <<< "$v1")" |
| 415 | r2="$(write_record "$v2" "$r1" "$elsewhere")" |
| 416 | as contributor git push --quiet origin "$r2:refs/the-valley/changes/main/$target_id" |
| 417 | # approve takes the head of any version, active or not, since the request |
| 418 | # names the head: a version is a head its owner marked ready. On a |
| 419 | # terminal, approve with no --head shows the active version and asks. |
| 420 | printf 'n\n' | script -qec "VALLEY_PRINCIPAL=requester valley approve pppp" /dev/null > "$w/out" 2>&1 \ |
| 421 | && fail 'approve went ahead when its confirmation was refused' |
| 422 | grep -qF "approve change pppp v2 at $elsewhere, elsewhere? [y/N]" "$w/out" \ |
| 423 | || fail 'approve did not show the active head and title to confirm' |
| 424 | [ "$(git -C "$origin" rev-parse "$request")" = "$honest" ] || fail 'a refused confirmation moved the request' |
| 425 | printf 'y\n' | script -qec "VALLEY_PRINCIPAL=requester valley approve pppp" /dev/null > "$w/out" 2>&1 \ |
| 426 | || fail 'approve on a terminal failed once confirmed' |
| 427 | [ "$(git -C "$origin" rev-parse "$request")" = "$elsewhere" ] || fail 'a confirmed approval did not file the active head' |
| 428 | ok as requester valley approve --head "$honest" pppp |
| 429 | [ "$(git -C "$origin" rev-parse "$request")" = "$honest" ] || fail 'approve did not file an older version head' |
| 430 | grep -q ' pppp v1 approved · ' "$w/out" || fail "approve did not name the version whose head it filed: $(cat "$w/out")" |
| 431 | ok as requester valley approve --head "$elsewhere" pppp |
| 432 | [ "$(git -C "$origin" rev-parse "$request")" = "$elsewhere" ] || fail 'approve did not file the active head' |
| 433 | |
| 434 | # status reads the change at its active head, under c-<change ID>, and |
| 435 | # says what its record says. |
| 436 | ok valley status --json pppp |
| 437 | jq -e --arg id "$target_id" --arg head "$elsewhere" --arg ref "$request" ' |
| 438 | .change == "victim" and .request_name == ("c-" + $id) and .head == $head |
| 439 | and .request == {ref: $ref, commit: $head} and .state == "awaiting-decision" |
| 440 | and .change_id == $id and .handle == "pppp" and .version == 2 and .title == "elsewhere" |
| 441 | and .owner == "contributor" and .landing == "not-landed"' "$w/out" > /dev/null \ |
| 442 | || fail "status --json pppp: $(cat "$w/out")" |
| 443 | ok valley status --json pushed |
| 444 | jq -e '.change_id == null and .handle == null and .version == null and .title == null |
| 445 | and .owner == null and .landing == null' "$w/out" > /dev/null \ |
| 446 | || fail "status --json of a branch with no record: $(cat "$w/out")" |
| 447 | # With the branch at the recorded head, a reject declined at its |
| 448 | # confirmation keeps the branch and the record as they are. |
| 449 | as contributor git push --quiet --force origin "$elsewhere:refs/heads/victim" |
| 450 | rec="$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$target_id")" |
| 451 | printf 'r\nn\n' | as requester valley review pppp > "$w/out" 2> "$w/err" || fail 'a declined reject failed' |
| 452 | grep -qF 'kept victim' "$w/out" || fail 'a declined reject did not say it kept the branch' |
| 453 | [ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$elsewhere" ] || fail 'a declined reject moved the branch' |
| 454 | [ "$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$target_id")" = "$rec" ] \ |
| 455 | || fail 'a declined reject wrote the record' |
| 456 | # Anyone may delete a topic branch, but only the owner or a holder of the |
| 457 | # approve grant may withdraw the change: bystander's reject deletes the |
| 458 | # branch, says the withdrawal failed, and names the command that does it. |
| 459 | printf 'r\nyes\n' | as bystander valley review pppp > "$w/out" 2> "$w/err" \ |
| 460 | || fail "bystander's reject failed" |
| 461 | grep -qF 'rejected victim: deleted from origin · withdrawing pppp failed' "$w/out" \ |
| 462 | || fail 'reject did not say the withdrawal failed' |
| 463 | grep -qx ' run: valley withdraw pppp' "$w/out" || fail 'a failed withdrawal did not name valley withdraw' |
| 464 | if git -C "$origin" rev-parse --verify --quiet refs/heads/victim > /dev/null; then |
| 465 | fail 'a reject whose withdrawal failed kept the branch' |
| 466 | fi |
| 467 | record "$target_id" '.state == "ready"' |
| 468 | # requester holds the grant: rejecting the change deletes its branch and |
| 469 | # withdraws it. |
| 470 | as contributor git push --quiet origin "$elsewhere:refs/heads/victim" |
| 471 | printf 'r\nyes\n' | as requester valley review pppp > "$w/out" 2> "$w/err" \ |
| 472 | || fail 'rejecting a handle at its recorded head failed' |
| 473 | grep -qF 'rejected victim: deleted from origin · pppp withdrawn' "$w/out" \ |
| 474 | || fail 'reject did not say it deleted the branch and withdrew the change' |
| 475 | if git -C "$origin" rev-parse --verify --quiet refs/heads/victim > /dev/null; then |
| 476 | fail 'reject kept a branch at the recorded head' |
| 477 | fi |
| 478 | record "$target_id" '.state == "withdrawn"' |
| 479 | |
| 480 | # A version may record no branch: the record allows one, though valley |
| 481 | # ready always names a branch. The change's record line then has an empty |
| 482 | # field in its middle, and approve, review and rebase must still read the |
| 483 | # state after it. approve files the head alone. review and rebase act on a |
| 484 | # branch, so they refuse plainly, and a draft's refusal names no branch in |
| 485 | # the ready line it suggests. |
| 486 | bare_id=qqqqkkkkllllmmmmnnnnooooppppqqqq |
| 487 | bare_request="refs/the-valley/integration-requests/main/c-$bare_id" |
| 488 | git checkout --quiet -b branchless main |
| 489 | commit branchless |
| 490 | branchless="$(git rev-parse HEAD)" |
| 491 | b1="$(jq -cn --arg id "$bare_id" --arg head "git-sha1:$branchless" '{schema: "valley-change-record/v1", |
| 492 | change_id: $id, target: "main", owner: "contributor", state: "ready", seq: 1, |
| 493 | versions: [{n: 1, head: $head, title: "branchless", branch: "", uploader: "contributor"}], active: 1}')" |
| 494 | as contributor git push --quiet origin "$(write_record "$b1" "$branchless"):refs/the-valley/changes/main/$bare_id" |
| 495 | ok as requester valley approve --head "$branchless" qqqq |
| 496 | [ "$(git -C "$origin" rev-parse "$bare_request")" = "$branchless" ] || fail 'approve did not file a change with no branch' |
| 497 | grep -q '^[^ ]* qqqq v1 approved · ' "$w/out" || fail 'approve did not say which version of a change with no branch it approved' |
| 498 | for verb in review rebase; do |
| 499 | refused "change qqqq v1 records no branch, and $verb acts on a change's branch" valley "$verb" qqqq |
| 500 | done |
| 501 | ok as contributor valley unready qqqq |
| 502 | # Only the change's owner is told how to ready it again. |
| 503 | refused "qqqq v1 not approved · change is a draft" \ |
| 504 | as requester valley approve --head "$branchless" qqqq |
| 505 | if grep -qF 'valley ready' "$w/err"; then fail "approve told someone other than the change's owner to ready it"; fi |
| 506 | refused " run: valley ready --change $bare_id" \ |
| 507 | as contributor valley approve --head "$branchless" qqqq |
| 508 | if grep -qF "''" "$w/err"; then fail "approve suggested readying a change with no branch on the branch ''"; fi |
| 509 | |
| 510 | # A name no record is on is acted on as a branch only when it is a branch |
| 511 | # name. resolve prints the name in a line of fields, so a name holding a |
| 512 | # separator could otherwise read as main. |
| 513 | before="$(git -C "$origin" for-each-ref refs/the-valley)" |
| 514 | for verb in approve review; do |
| 515 | refused 'is not a branch name in UTF-8' as requester valley "$verb" $'main\x1fjunk' |
| 516 | done |
| 517 | [ "$(git -C "$origin" for-each-ref refs/the-valley)" = "$before" ] \ |
| 518 | || fail 'a name holding a separator published something' |
| 519 | |
| 520 | # ---------------------------------------------------------------------- |
| 521 | # A draft is a change paused. unready makes a ready change a draft, and |
| 522 | # ready brings it back: at the head of its active version it is ready |
| 523 | # again at that version, and at a new head it gains the next one. pending |
| 524 | # lists a draft apart from the branches, and approve and rebase refuse it, |
| 525 | # naming its state. Until the integrator reads records, a request filed |
| 526 | # before a change was paused or withdrawn can still land it, so unready and |
| 527 | # withdraw warn when one is pending, and leave it for the operator. |
| 528 | git checkout --quiet -b drafts/paused main |
| 529 | commit paused |
| 530 | paused="$(git rev-parse HEAD)" |
| 531 | as contributor git push --quiet origin drafts/paused |
| 532 | ok as contributor valley ready |
| 533 | pid="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 534 | ph="$(sed -n 's/^project \([k-z]*\) v1 ready$/\1/p' "$w/out")" |
| 535 | if [ "${#pid}" != 32 ] || [ -z "$ph" ]; then fail 'ready did not name the new change'; fi |
| 536 | |
| 537 | ok as contributor valley unready "$ph" |
| 538 | grep -qx "project $ph v1 draft" "$w/out" || fail 'unready did not say it made a draft' |
| 539 | record "$pid" '.state == "draft"' '.seq == 2' '.active == 1' '.versions | length == 1' |
| 540 | one_parent "$pid" |
| 541 | if grep -q 'warning' "$w/err"; then fail 'unready warned with no request pending'; fi |
| 542 | unchanged "$pid" refused 'is a draft already' as contributor valley unready "$ph" |
| 543 | |
| 544 | # pending counts the draft among the drafts, and lists it under drafts and |
| 545 | # not among the branches, since its branch is at the draft's head. |
| 546 | ok valley pending |
| 547 | grep -qE '^[0-9]+ drafts? ' "$w/out" || fail 'pending counted no drafts' |
| 548 | grep -qE '^ ([0-9]+ |◐|✗|◇) ?[k-z]{4,32} v[0-9]+' "$w/out" || fail 'pending no longer lists the ready changes' |
| 549 | ok valley pending --drafts --branches |
| 550 | grep -qx 'drafts' "$w/out" || fail 'pending listed no drafts' |
| 551 | grep -A1 "^$ph v1 on drafts/paused .* ago · " "$w/out" | grep -qx ' paused' || fail 'pending did not list the draft' |
| 552 | if grep -qE '(^|└ |├ )drafts/paused ' "$w/out"; then fail 'pending listed a draft among the branches'; fi |
| 553 | |
| 554 | # A draft is not approved or rebased. review says so on the first line it |
| 555 | # shows, above the diff, and again in place of its prompt, which offers |
| 556 | # only [v]iew full diff and [s]kip: no [a]pprove, no [b]ase, and no |
| 557 | # [r]eject, which would delete the branch its owner may be working on. An |
| 558 | # answer it does not offer is answered with what it does, and acts on |
| 559 | # nothing. |
| 560 | request_ref="refs/the-valley/integration-requests/main/c-$pid" |
| 561 | unchanged "$pid" refused "$ph v1 not approved · change is a draft" as requester valley approve "$ph" |
| 562 | if git -C "$origin" rev-parse --verify --quiet "$request_ref" > /dev/null; then |
| 563 | fail 'approve filed a request for a draft' |
| 564 | fi |
| 565 | unchanged "$pid" refused "change $ph v1 is draft, not ready; nothing rebased" as requester valley rebase "$ph" |
| 566 | drafted="$ph v1 is a draft: its owner (contributor) hasn't marked it ready, so it can't be approved yet." |
| 567 | draft_header="$ph v1 · draft (its owner, contributor, has not readied it)" |
| 568 | # review_draft <name>: review the draft by that name, answering a, b and |
| 569 | # r before s. A prompt ends with no newline, so what follows it is on its |
| 570 | # line. |
| 571 | review_draft() { |
| 572 | printf 'a\nb\nr\ns\n' | as requester valley review "$1" > "$w/out" 2> "$w/err" || fail "review of $1 failed" |
| 573 | [ "$(head -n 1 "$w/out")" = "$drafted" ] || fail "review of $1 did not open with the draft's line" |
| 574 | grep -q "^change $ph v1 at .*, on branch drafts/paused, log against origin/main:$" "$w/out" \ |
| 575 | || fail "review of $1 did not show the draft" |
| 576 | [ "$(grep -cxF "$draft_header" "$w/out")" = 4 ] \ |
| 577 | || fail "review of $1 did not say the draft's line in the header of each prompt" |
| 578 | [ "$(grep -oF '[v]iew diff [s]kip: answer v or s' "$w/out" | wc -l)" = 3 ] || fail "review of $1 took an answer it does not offer" |
| 579 | grep -qF '[s]kip: skipped drafts/paused; nothing done' "$w/out" || fail "review of $1 did not skip the draft" |
| 580 | if grep -qF -e '[a]pprove' -e '[b]ase' -e '[r]eject' -e 'really delete' "$w/out"; then |
| 581 | fail "review of $1 offered what a draft can't take" |
| 582 | fi |
| 583 | if grep -q 'not ready' "$w/err"; then fail "review of $1 ran approve or rebase on the draft"; fi |
| 584 | [ "$(git -C "$origin" rev-parse refs/heads/drafts/paused)" = "$paused" ] || fail "review of $1 moved the draft's branch" |
| 585 | } |
| 586 | unchanged "$pid" review_draft "$ph" |
| 587 | # Named by its branch, written either way, the draft is the same change, |
| 588 | # since its active version is the one record on that branch. It is |
| 589 | # refused, and reviewed, the same way. |
| 590 | for name in drafts/paused origin/drafts/paused; do |
| 591 | unchanged "$pid" refused "$ph v1 not approved · change is a draft" as requester valley approve "$name" |
| 592 | unchanged "$pid" refused "change $ph v1 is draft, not ready; nothing rebased" as requester valley rebase "$name" |
| 593 | unchanged "$pid" review_draft "$name" |
| 594 | done |
| 595 | if git -C "$origin" rev-parse --verify --quiet "$request_ref" > /dev/null; then |
| 596 | fail 'approve filed a request for a draft named by its branch' |
| 597 | fi |
| 598 | |
| 599 | # ready at the same head is v1 again, and a draft once more after it. |
| 600 | ok as contributor valley ready --change "$pid" |
| 601 | grep -qx "project $ph v1 ready" "$w/out" || fail 'a draft readied at its head was not v1 again' |
| 602 | record "$pid" '.state == "ready"' '.seq == 3' '.active == 1' '.versions | length == 1' |
| 603 | one_parent "$pid" |
| 604 | |
| 605 | # Approved now, the change has a request under its own name, c-<change |
| 606 | # ID>. Ready and at its recorded head, it is approved by its branch, |
| 607 | # written either way, and in review. unready voids nothing, so the request |
| 608 | # stays, and the integrator holds it back until the change is ready again. |
| 609 | ok as requester valley approve --head "$paused" drafts/paused |
| 610 | [ "$(git -C "$origin" rev-parse "$request_ref")" = "$paused" ] || fail 'approve did not file the ready change' |
| 611 | ok as requester valley approve --head "$paused" origin/drafts/paused |
| 612 | grep -q "^[^ ]* $ph v1 approved · " "$w/out" || fail 'approve of origin/drafts/paused did not go ahead' |
| 613 | printf 'a\n' | as requester valley review drafts/paused > "$w/out" 2> "$w/err" || fail 'review of drafts/paused failed' |
| 614 | grep -q "^change $ph v1 at " "$w/out" || fail 'review of a branch did not read its change' |
| 615 | grep -q "^[^ ]* $ph v1 approved · " "$w/out" || fail "review's [a]pprove of drafts/paused did not go ahead" |
| 616 | ok as contributor valley unready "$ph" |
| 617 | grep -qF "valley: note: the integration request $request_ref for $ph is pending on origin" "$w/err" \ |
| 618 | || fail 'unready did not say a request is pending' |
| 619 | grep -qF 'lands once the change is ready again' "$w/err" || fail 'the note did not say when the request lands' |
| 620 | [ "$(git -C "$origin" rev-parse "$request_ref")" = "$paused" ] || fail 'unready touched the request' |
| 621 | |
| 622 | # unready, then ready at a new head, is v2. |
| 623 | commit paused-again |
| 624 | paused_again="$(git rev-parse HEAD)" |
| 625 | as contributor git push --quiet origin drafts/paused |
| 626 | ok as contributor valley ready --change "$pid" |
| 627 | grep -qx "project $ph v2 ready" "$w/out" || fail 'a draft readied at a new head was not v2' |
| 628 | record "$pid" '.state == "ready"' '.seq == 5' '.active == 2' '.versions | length == 2' \ |
| 629 | ".versions[1].head == \"git-sha1:$paused_again\"" |
| 630 | |
| 631 | # withdraw warns of the same request, and the change, withdrawn, is never |
| 632 | # made a draft. ready at the same head brings it back at v2. |
| 633 | ok as contributor valley withdraw "$ph" |
| 634 | grep -qx "project $ph v2 withdrawn" "$w/out" || fail 'withdraw did not say what it withdrew' |
| 635 | grep -qF "valley: warning: the integration request $request_ref for $ph is pending on origin" "$w/err" \ |
| 636 | || fail 'withdraw did not warn of the pending request' |
| 637 | grep -qF 'so the operator must drop it' "$w/err" || fail 'the warning did not say who must drop the request' |
| 638 | record "$pid" '.state == "withdrawn"' '.seq == 6' '.active == 2' |
| 639 | unchanged "$pid" refused 'never made a draft' as contributor valley unready "$ph" |
| 640 | ok as contributor valley ready --change "$pid" |
| 641 | grep -qx "project $ph v2 ready" "$w/out" || fail 'a withdrawn change readied at its head was not v2 again' |
| 642 | record "$pid" '.state == "ready"' '.seq == 7' '.active == 2' '.versions | length == 2' |
| 643 | one_parent "$pid" |
| 644 | |
| 645 | # A request named for the change's branch, as approve named one before it |
| 646 | # named a change by its ID, is warned of too. withdraw takes a draft. |
| 647 | as requester git push --quiet origin --delete "$request_ref" |
| 648 | as requester git push --quiet origin "$paused_again:refs/the-valley/integration-requests/main/drafts-paused" |
| 649 | ok as contributor valley unready "$ph" |
| 650 | grep -qF "the integration request refs/the-valley/integration-requests/main/drafts-paused for $ph is pending" "$w/err" \ |
| 651 | || fail 'unready did not say a request named for the branch is pending' |
| 652 | if grep -qF "$request_ref " "$w/err"; then fail 'unready warned of a request that was dropped'; fi |
| 653 | ok as contributor valley withdraw "$ph" |
| 654 | record "$pid" '.state == "withdrawn"' '.seq == 9' '.active == 2' |
| 655 | grep -qF "main/drafts-paused for $ph is pending" "$w/err" || fail 'withdraw did not warn of a request named for the branch' |
| 656 | |
| 657 | # review shows a withdrawn change as it shows a draft, with a line of its |
| 658 | # own. |
| 659 | withdrawn="$ph is withdrawn: there is nothing to approve." |
| 660 | printf 'a\ns\n' | as requester valley review "$ph" > "$w/out" 2> "$w/err" || fail 'review of a withdrawn change failed' |
| 661 | [ "$(head -n 1 "$w/out")" = "$withdrawn" ] || fail 'review did not open with the withdrawn line' |
| 662 | grep -qx "$ph v[0-9]* · withdrawn" "$w/out" && grep -qF '[v]iew diff [s]kip: answer v or s' "$w/out" \ |
| 663 | || fail 'review did not say the withdrawn line in place of its prompt' |
| 664 | if grep -qF -e '[a]pprove' -e '[b]ase' -e '[r]eject' "$w/out"; then fail "review offered what a withdrawn change can't take"; fi |
| 665 | |
| 666 | # ---------------------------------------------------------------------- |
| 667 | # review, approve and rebase read the record again just before they act. |
| 668 | # The change's branch starts behind main. Review asks it the ordinary |
| 669 | # prompt under one line that says so, and offers no [b]ase. When another |
| 670 | # principal returns the change to draft while the review is open, the |
| 671 | # review says so in one line, does nothing, and does not ask again. It |
| 672 | # never tells the operator to ready the change. |
| 673 | # |
| 674 | # review_while <change> <answers> <command…>: review the change as the |
| 675 | # operator, run the command once the review waits at its prompt, then |
| 676 | # give the answers. Every prompt that follows a change shown offers |
| 677 | # [v]iew full diff, so that is the marker waited on, whatever else the |
| 678 | # prompt offers. |
| 679 | review_while() { |
| 680 | local name="$1" answers="$2" reviewer i |
| 681 | shift 2 |
| 682 | # A review.out left from an earlier review would show a prompt this one |
| 683 | # has not reached. |
| 684 | rm -f "$w/fifo" "$w/review.out" |
| 685 | mkfifo "$w/fifo" |
| 686 | VALLEY_PRINCIPAL=requester valley review "$name" < "$w/fifo" > "$w/review.out" 2> "$w/review.err" & |
| 687 | reviewer=$! |
| 688 | exec 3> "$w/fifo" |
| 689 | for i in $(seq 600); do |
| 690 | if grep -qF '[v]iew diff' "$w/review.out" 2> /dev/null; then break; fi |
| 691 | sleep 0.1 |
| 692 | done |
| 693 | grep -qF '[v]iew diff' "$w/review.out" || fail "review of $name never reached its prompt (after $i tries)" |
| 694 | "$@" > "$w/out" 2> "$w/err" || fail "$* failed while the review was open" |
| 695 | printf '%b' "$answers" >&3 |
| 696 | exec 3>&- |
| 697 | wait "$reviewer" || fail "review of $name failed" |
| 698 | } |
| 699 | git checkout --quiet -b drafts/raced main~1 |
| 700 | commit raced |
| 701 | raced="$(git rev-parse HEAD)" |
| 702 | as contributor git push --quiet origin drafts/raced |
| 703 | ok as contributor valley ready |
| 704 | rid="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 705 | rh="$(sed -n 's/^project \([k-z]*\) v1 ready$/\1/p' "$w/out")" |
| 706 | if [ "${#rid}" != 32 ] || [ -z "$rh" ]; then fail 'ready did not name the new change'; fi |
| 707 | raced_request="refs/the-valley/integration-requests/main/c-$rid" |
| 708 | |
| 709 | prompt="[a]pprove [v]iew diff [r]eject [s]kip: " |
| 710 | behind=' · behind main (integrator merges)' |
| 711 | review_while "$rh" 'a\ns\n' as contributor valley unready "$rh" |
| 712 | grep -qF "$behind" "$w/review.out" || fail 'review did not say the change is behind main' |
| 713 | # The answer's prompt is followed by the one line, and nothing comes after. |
| 714 | last="$(tail -n 1 "$w/review.out")" |
| 715 | [ "${last#"$prompt"}" != "$last" ] \ |
| 716 | || fail "review of a change unreadied while it was open did not end on its prompt and one line: $(cat "$w/review.out")" |
| 717 | printf '%s\n' "${last#"$prompt"}" | grep -qE "^$rh changed while you were reviewing: it was returned to draft \(since ([0-9]{4}-[0-9]{2}-[0-9]{2} )?[0-9]{2}:[0-9]{2}Z\), so there is nothing to approve now\. It comes back when it is ready again\.\$" \ |
| 718 | || fail "review did not say plainly that the change went back to draft: $(cat "$w/review.out")" |
| 719 | [ "$(grep -cF '[v]iew diff' "$w/review.out")" = 1 ] || fail 'review asked again about a change that moved' |
| 720 | if grep -qF -e '[b]ase' -e 'skipped drafts/raced' "$w/review.out"; then fail 'review offered [b]ase, or went on, for a change that moved'; fi |
| 721 | if grep -qF 'valley ready --change' "$w/review.out" "$w/review.err"; then fail 'review told the operator to ready the change'; fi |
| 722 | if git -C "$origin" rev-parse --verify --quiet "$raced_request" > /dev/null; then |
| 723 | fail 'review filed a request for a change unreadied while it was open' |
| 724 | fi |
| 725 | [ "$(git -C "$origin" rev-parse refs/heads/drafts/raced)" = "$raced" ] \ |
| 726 | || fail 'review rebased a change unreadied while it was open' |
| 727 | # Shown as a draft from the start, the change is offered no [a]pprove, |
| 728 | # and is not said to be behind main. |
| 729 | printf 'b\ns\n' | as requester valley review "$rh" > "$w/out" 2> "$w/err" || fail "review of the draft $rh failed" |
| 730 | grep -qxF "$rh v1 · draft (its owner, contributor, has not readied it)" "$w/out" \ |
| 731 | && grep -qF '[v]iew diff [s]kip: answer v or s' "$w/out" \ |
| 732 | || fail 'review of a stale draft did not say it is a draft in place of its prompt' |
| 733 | if grep -qF -e '[a]pprove' -e '[b]ase' "$w/out"; then fail 'review offered a stale draft [a]pprove or [b]ase'; fi |
| 734 | if grep -qF 'behind main' "$w/out"; then fail 'review said a draft is behind main'; fi |
| 735 | [ "$(git -C "$origin" rev-parse refs/heads/drafts/raced)" = "$raced" ] || fail 'review rebased a draft' |
| 736 | |
| 737 | ok as contributor valley ready --change "$rid" |
| 738 | commit raced-again |
| 739 | raced_again="$(git rev-parse HEAD)" |
| 740 | new_version() { |
| 741 | as contributor git push --quiet origin drafts/raced && as contributor valley ready --change "$rid" |
| 742 | } |
| 743 | # A version with new commits, added while the review is open, is said |
| 744 | # plainly, and nothing is approved at once. Only what changed since the |
| 745 | # version shown is offered, and approving the new version only after it |
| 746 | # is paged; [s] then skips it. |
| 747 | review_while "$rh" 'a\nv\ns\n' new_version |
| 748 | moved_line="$prompt$rh moved to v2 while you were reviewing (its commits changed (2 commits, where v1 had 1)) · [v]iew what changed since v1, [s]kip? " |
| 749 | grep -qF "$moved_line$rh v2 at $(git rev-parse --short "$raced_again"): edited since v1 at " "$w/review.out" \ |
| 750 | || fail "review did not say the change moved to v2, and show what changed since v1: $(cat "$w/review.out")" |
| 751 | grep -qF '[a]pprove v2, [s]kip? ' "$w/review.out" \ |
| 752 | || fail "review did not offer to approve v2 once it showed what changed: $(cat "$w/review.out")" |
| 753 | if git -C "$origin" rev-parse --verify --quiet "$raced_request" > /dev/null; then |
| 754 | fail 'review filed a request for a change that moved while it was open' |
| 755 | fi |
| 756 | |
| 757 | # rebase rebases the active version's head, pushes the branch, and adds |
| 758 | # the rebased head as a new version, as the operator. ready --if-active, |
| 759 | # which it does that with, adds nothing unless the change is ready at that |
| 760 | # active head. A rebase while the review is open brings the same changes |
| 761 | # onto main, so the answer covers the rebased version with no second |
| 762 | # review: [a]pprove files the head shown, whose delta the integrator |
| 763 | # applies onto main, and the verdict names the rebased version, which |
| 764 | # lands, with its checks. |
| 765 | unchanged "$rid" refused "$rh v2 is at" as contributor valley ready --if-active "$raced" --change "$rid" |
| 766 | review_while "$rh" 'a\n' as requester valley rebase "$rh" |
| 767 | rebased="$(git -C "$origin" rev-parse refs/heads/drafts/raced)" |
| 768 | [ "$rebased" != "$raced_again" ] || fail 'rebase did not move the branch' |
| 769 | grep -qx "project $rh v3 ready" "$w/out" || fail 'rebase did not add the rebased head as v3' |
| 770 | grep -qF "$behind" "$w/review.out" || fail 'review did not say the change is behind main' |
| 771 | grep -qxF "$prompt$rh moved to v3 while you were reviewing. v3 is v2 rebased onto the new main with no other changes, so your approval covers it." "$w/review.out" \ |
| 772 | || fail "review did not carry the answer across the restack to v3: $(cat "$w/review.out")" |
| 773 | grep -qxF "✓ $rh v3 approved · no checks required · landing on main" "$w/review.out" \ |
| 774 | || fail "review's verdict did not name v3, which lands, with its checks: $(cat "$w/review.out" "$w/review.err")" |
| 775 | [ "$(git -C "$origin" rev-parse "$raced_request")" = "$raced_again" ] \ |
| 776 | || fail 'review filed another head than the one it showed' |
| 777 | [ "$(grep -cF '[v]iew diff' "$w/review.out")" = 1 ] || fail 'review asked again about a restacked change' |
| 778 | if grep -qF 'valley ready --change' "$w/review.out" "$w/review.err"; then fail 'review told the operator to ready the change'; fi |
| 779 | record "$rid" '.state == "ready"' '.active == 3' '.versions | length == 3' \ |
| 780 | ".versions[2].head == \"git-sha1:$rebased\"" '.versions[2].uploader == "requester"' \ |
| 781 | '.versions[2].branch == "drafts/raced"' '.owner == "contributor"' |
| 782 | |
| 783 | # A rebase onto a main that also edited one of the change's files changes |
| 784 | # the file's delta and not the commit. Review says so, offers to approve |
| 785 | # the rebased version at once, and [a] files its head. |
| 786 | git checkout --quiet main |
| 787 | printf '1\n2\n3\n4\n5\n6\n7\n8\n' > shared.txt |
| 788 | git add shared.txt |
| 789 | git commit --quiet -m shared |
| 790 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 791 | git checkout --quiet -b drafts/shared |
| 792 | sed -i 's/^1$/one/' shared.txt |
| 793 | git commit --quiet -am 'shared: one' |
| 794 | as contributor git push --quiet origin drafts/shared |
| 795 | ok as contributor valley ready |
| 796 | sid="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 797 | sh="$(sed -n 's/^project \([k-z]*\) v1 ready$/\1/p' "$w/out")" |
| 798 | if [ "${#sid}" != 32 ] || [ -z "$sh" ]; then fail 'ready did not name the shared change'; fi |
| 799 | # main moves in a clone of its own, leaving the review's checkout alone. |
| 800 | git clone --quiet "$origin" "$w/mainline" |
| 801 | main_edits_shared() { |
| 802 | sed -i 's/^8$/eight/' "$w/mainline/shared.txt" |
| 803 | git -C "$w/mainline" commit --quiet -am 'shared: eight' |
| 804 | env -u VALLEY_PRINCIPAL git -C "$w/mainline" push --quiet origin main |
| 805 | as requester valley rebase "$sh" |
| 806 | } |
| 807 | review_while "$sh" 'a\na\n' main_edits_shared |
| 808 | shared_head="$(git -C "$origin" rev-parse refs/heads/drafts/shared)" |
| 809 | grep -qF "$prompt$sh moved to v2 while you were reviewing (rebased onto main; its commit is unchanged, but main also edited 1 of its files (shared.txt)) · [a]pprove v2, [v]iew what changed since v1, [s]kip? " "$w/review.out" \ |
| 810 | || fail "review did not say the change was rebased onto a file main edited, and offer v2: $(cat "$w/review.out")" |
| 811 | grep -q "^[^ ]* $sh v2 approved · " "$w/review.out" || fail "review did not approve v2 in place: $(cat "$w/review.err")" |
| 812 | [ "$(git -C "$origin" rev-parse "refs/the-valley/integration-requests/main/c-$sid")" = "$shared_head" ] \ |
| 813 | || fail 'review did not file the rebased head of v2' |
| 814 | # A commit edited while the review is open is offered for approval only |
| 815 | # once what changed is paged: [a] at the moved prompt approves nothing. |
| 816 | git fetch --quiet origin drafts/shared |
| 817 | git checkout --quiet drafts/shared |
| 818 | git reset --quiet --hard FETCH_HEAD |
| 819 | sed -i 's/^2$/two/' shared.txt |
| 820 | git commit --quiet -am 'shared: two' |
| 821 | new_shared_version() { |
| 822 | as contributor git push --quiet origin drafts/shared && as contributor valley ready --change "$sid" |
| 823 | } |
| 824 | review_while "$sh" 'a\na\n' new_shared_version |
| 825 | grep -qF "$prompt$sh moved to v3 while you were reviewing (its commits changed (2 commits, where v2 had 1)) · [v]iew what changed since v2, [s]kip? " "$w/review.out" \ |
| 826 | || fail "review did not say the change's commits changed, offering only what changed: $(cat "$w/review.out")" |
| 827 | [ "$(git -C "$origin" rev-parse "refs/the-valley/integration-requests/main/c-$sid")" = "$shared_head" ] \ |
| 828 | || fail 'review approved an edited version before showing what changed' |
| 829 | # pending and the walk count a request at an earlier version's head where |
| 830 | # review's answer would carry. raced's, at v2's head, carries to v3: it is |
| 831 | # moving, approved, and the walk does not offer it. shared's, at v2's head, |
| 832 | # does not carry to v3, whose commits changed, so the operator is asked. |
| 833 | ok valley pending |
| 834 | grep -qE "^ ◐ $rh v3 +approved · no checks required\$" "$w/out" \ |
| 835 | || fail 'pending did not list a change whose approval carries as approved and moving' |
| 836 | if grep -qE "^ +[0-9]+ $rh " "$w/out"; then fail 'pending asked for a change whose approval carries'; fi |
| 837 | grep -qE "^ +[0-9]+ $sh v3( |\$)" "$w/out" || fail 'pending did not ask for a change whose approval does not carry' |
| 838 | seq 50 | sed 's/.*/s/' > "$w/skips" |
| 839 | as requester valley review --repo project < "$w/skips" > "$w/out" 2> "$w/err" || fail 'the walk failed' |
| 840 | if grep -qF "· $rh v3" "$w/out"; then fail 'the walk offered a change whose approval carries'; fi |
| 841 | grep -qF "· $sh v3" "$w/out" || fail 'the walk did not offer a change whose approval does not carry' |
| 842 | git checkout --quiet main |
| 843 | git pull --quiet --ff-only origin main |
| 844 | |
| 845 | # A name that read as a plain branch is read again before approve files. |
| 846 | # A review of a branch no record is on is left open while a second |
| 847 | # principal readies the branch and then unreadies it. [a]pprove then |
| 848 | # refuses, naming the change the branch has become and its state. |
| 849 | git checkout --quiet -b drafts/plain main |
| 850 | commit plain |
| 851 | as contributor git push --quiet origin drafts/plain |
| 852 | ready_then_unready() { |
| 853 | as contributor valley ready drafts/plain > "$w/ready.out" || return 1 |
| 854 | plain_id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/ready.out")" |
| 855 | plain_handle="$(sed -n 's/^project \([k-z]*\) v1 ready$/\1/p' "$w/ready.out")" |
| 856 | [ "${#plain_id}" = 32 ] && [ -n "$plain_handle" ] || return 1 |
| 857 | as contributor valley unready "$plain_id" |
| 858 | } |
| 859 | review_while drafts/plain 'a\ns\n' ready_then_unready |
| 860 | grep -q '^branch drafts/plain, log against origin/main' "$w/review.out" \ |
| 861 | || fail 'the review did not begin on a branch with no record' |
| 862 | record "$plain_id" '.state == "draft"' |
| 863 | grep -qxF "⊘ drafts/plain not approved · now change $plain_handle v1, which is draft" "$w/review.err" \ |
| 864 | || fail "review's [a]pprove went ahead for a branch readied and unreadied while the review was open" |
| 865 | if git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/drafts-plain > /dev/null; then |
| 866 | fail 'review filed a request for a branch that became a draft while it was open' |
| 867 | fi |
| 868 | # A branch readied while the review is open, and left ready at the head |
| 869 | # the review showed, is filed at that head. |
| 870 | git checkout --quiet -b drafts/readied main |
| 871 | commit readied |
| 872 | as contributor git push --quiet origin drafts/readied |
| 873 | review_while drafts/readied 'a\n' as contributor valley ready drafts/readied |
| 874 | grep -q '^[^ ]* drafts/readied approved · ' "$w/review.out" \ |
| 875 | || fail "review's [a]pprove refused a branch readied at the head it showed: $(cat "$w/review.err")" |
| 876 | [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/drafts-readied)" = "$(git rev-parse drafts/readied)" ] \ |
| 877 | || fail 'review did not file the head it showed of a branch readied while it was open' |
| 878 | |
| 879 | # ---------------------------------------------------------------------- |
| 880 | # A request filed for an earlier version is named by that version's |
| 881 | # branch. withdraw warns of it, though the active version is on another. |
| 882 | git checkout --quiet -b moved/old main |
| 883 | commit old |
| 884 | as contributor git push --quiet origin moved/old |
| 885 | ok as contributor valley ready |
| 886 | old_id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 887 | old_handle="$(sed -n 's/^project \([k-z]*\) v1 ready$/\1/p' "$w/out")" |
| 888 | old_request=refs/the-valley/integration-requests/main/moved-old |
| 889 | as requester git push --quiet origin "$(git rev-parse HEAD):$old_request" |
| 890 | git checkout --quiet -b moved/new |
| 891 | commit new |
| 892 | as contributor git push --quiet origin moved/new |
| 893 | ok as contributor valley ready --change "$old_id" |
| 894 | grep -qx "project $old_handle v2 ready" "$w/out" || fail 'the version on moved/new was not v2' |
| 895 | ok as contributor valley withdraw "$old_handle" |
| 896 | grep -qF "valley: warning: the integration request $old_request for $old_handle is pending on origin" "$w/err" \ |
| 897 | || fail "withdraw did not warn of the request filed for v1's branch" |
| 898 | |
| 899 | # ---------------------------------------------------------------------- |
| 900 | # A branch no record's active version is on is a branch, as before |
| 901 | # records: review shows its tip, and approve files it under its own name. |
| 902 | printf 's\n' | valley review pushed > "$w/out" 2> "$w/err" || fail 'review of a branch with no record failed' |
| 903 | grep -q '^branch pushed, log against origin/main' "$w/out" || fail 'review read a change for a branch with no record' |
| 904 | ok as requester valley approve pushed |
| 905 | [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/pushed)" \ |
| 906 | = "$(git -C "$origin" rev-parse refs/heads/pushed)" ] || fail 'approve did not file a branch with no record at its tip' |
| 907 | |
| 908 | # A branch two records' active versions are on names neither: the verbs |
| 909 | # that act on a change refuse it, and list both IDs. |
| 910 | twin=llllmmmmnnnnoooollllmmmmnnnnoooo |
| 911 | ok as contributor valley ready --change "$twin" topic |
| 912 | for verb in approve rebase; do |
| 913 | refused 'name one by its change ID' as requester valley "$verb" topic |
| 914 | grep -qF "$id" "$w/err" || fail "$verb of topic did not list $id" |
| 915 | grep -qF "$twin" "$w/err" || fail "$verb of topic did not list $twin" |
| 916 | done |
| 917 | refused 'name one by its change ID' as requester valley approve origin/topic |
| 918 | |
| 919 | # c-<change ID> is the request name of the change with that ID, so a |
| 920 | # branch whose request name would be one is neither approved nor read by |
| 921 | # that name. |
| 922 | reserved=c-kkkkllllmmmmnnnnooooppppqqqqrrrr |
| 923 | git checkout --quiet -b "$reserved" main |
| 924 | commit reserved |
| 925 | as contributor git push --quiet origin "$reserved" |
| 926 | refused "not approved · the branch's request name" as requester valley approve "$reserved" |
| 927 | if git -C "$origin" rev-parse --verify --quiet "refs/the-valley/integration-requests/main/$reserved" > /dev/null; then |
| 928 | fail "approve filed a branch under a change's request name" |
| 929 | fi |
| 930 | refused 'that name is reserved for the change with the ID' valley status "$reserved" |
| 931 | |
| 932 | # ---------------------------------------------------------------------- |
| 933 | # A stack is rebased whole: every layer onto the rebased layer below it, |
| 934 | # and the bottom onto main, with one atomic push of every branch, and then |
| 935 | # each change's rebased head added to its record as a new version. |
| 936 | # |
| 937 | # new_change <branch>: origin's head of the branch readied by the |
| 938 | # contributor as a new change, whose ID and handle are left in nid and nh. |
| 939 | new_change() { |
| 940 | ok as contributor valley ready "$1" |
| 941 | nid="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 942 | nh="$(sed -n 's/^project \([k-z]*\) v1 ready$/\1/p' "$w/out")" |
| 943 | if [ "${#nid}" != 32 ] || [ -z "$nh" ]; then fail "ready $1 did not name a new change"; fi |
| 944 | } |
| 945 | # build_stack <name>: three layers on main, each adding files of its own: |
| 946 | # <name>/one, a plain branch, then <name>/two and <name>/three, changes the |
| 947 | # contributor readies. The top has two commits. The heads are left in |
| 948 | # one, two and three, and the changes' IDs and handles in id2, h2, id3 |
| 949 | # and h3. |
| 950 | build_stack() { |
| 951 | local from=main layer |
| 952 | for layer in one two three; do |
| 953 | git checkout --quiet -b "$1/$layer" "$from" |
| 954 | commit "$1-$layer" |
| 955 | as contributor git push --quiet origin "$1/$layer" |
| 956 | from="$1/$layer" |
| 957 | done |
| 958 | commit "$1-three-more" |
| 959 | as contributor git push --quiet origin "$1/three" |
| 960 | git checkout --quiet main |
| 961 | one="$(git rev-parse "$1/one")" two="$(git rev-parse "$1/two")" three="$(git rev-parse "$1/three")" |
| 962 | new_change "$1/two" |
| 963 | id2="$nid" h2="$nh" |
| 964 | new_change "$1/three" |
| 965 | id3="$nid" h3="$nh" |
| 966 | } |
| 967 | # move_main <subject>: main moves on, adding a file of its own. |
| 968 | move_main() { |
| 969 | git checkout --quiet main |
| 970 | commit "$1" |
| 971 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 972 | } |
| 973 | at() { git -C "$origin" rev-parse "$1"; } |
| 974 | # moved_nothing <name>: no branch of the stack and no record has moved |
| 975 | # since $before was read. |
| 976 | stack_refs() { |
| 977 | git -C "$origin" for-each-ref --format='%(objectname) %(refname)' "refs/heads/$1/" refs/the-valley/changes/ |
| 978 | } |
| 979 | moved_nothing() { |
| 980 | [ "$(stack_refs "$1")" = "$before" ] || fail "the rebase of $1 moved something" |
| 981 | } |
| 982 | # same_delta <old base> <old head> <new base> <new head>: a layer's diff |
| 983 | # from its base is what it was. |
| 984 | same_delta() { |
| 985 | [ "$(git -C "$origin" diff "$1" "$2")" = "$(git -C "$origin" diff "$3" "$4")" ] \ |
| 986 | || fail "the diff of $2 from $1 changed in the rebase" |
| 987 | } |
| 988 | |
| 989 | # A three-layer stack with main moved under it, without conflict. Every |
| 990 | # branch moves, each change gains a ready version at its new head, the |
| 991 | # operator's, and every layer keeps its diff, on the rebased layer below. |
| 992 | build_stack s1 |
| 993 | oldmain="$(at refs/heads/main)" |
| 994 | move_main s1-main |
| 995 | main="$(at refs/heads/main)" |
| 996 | ok as requester valley rebase "$h3" |
| 997 | new1="$(at refs/heads/s1/one)" new2="$(at refs/heads/s1/two)" new3="$(at refs/heads/s1/three)" |
| 998 | for b in "$one:$new1" "$two:$new2" "$three:$new3"; do |
| 999 | [ "${b%%:*}" != "${b#*:}" ] || fail "the rebase left a branch of s1 where it was" |
| 1000 | done |
| 1001 | record "$id2" '.state == "ready"' '.active == 2' ".versions[1].head == \"git-sha1:$new2\"" \ |
| 1002 | '.versions[1].uploader == "requester"' '.versions[1].branch == "s1/two"' |
| 1003 | record "$id3" '.state == "ready"' '.active == 2' ".versions[1].head == \"git-sha1:$new3\"" \ |
| 1004 | '.versions[1].uploader == "requester"' '.versions[1].branch == "s1/three"' |
| 1005 | git -C "$origin" merge-base --is-ancestor "$main" "$new1" || fail 'the bottom is not on main' |
| 1006 | git -C "$origin" merge-base --is-ancestor "$new1" "$new2" || fail 'the second layer is not on the first' |
| 1007 | git -C "$origin" merge-base --is-ancestor "$new2" "$new3" || fail 'the top is not on the second layer' |
| 1008 | same_delta "$oldmain" "$one" "$main" "$new1" |
| 1009 | same_delta "$one" "$two" "$new1" "$new2" |
| 1010 | same_delta "$two" "$three" "$new2" "$new3" |
| 1011 | grep -qx "the stack under $h3, bottom first:" "$w/out" || fail 'the rebase printed no manifest' |
| 1012 | grep -qE "^ s1/one +${one:0:7} -> ${new1:0:7} origin/s1/one pushed$" "$w/out" || fail 'the manifest misread s1/one' |
| 1013 | grep -qE "^ $h2 +${two:0:7} -> ${new2:0:7} origin/s1/two pushed, v2 added$" "$w/out" || fail "the manifest misread $h2" |
| 1014 | grep -qE "^ $h3 +${three:0:7} -> ${new3:0:7} origin/s1/three pushed, v2 added$" "$w/out" || fail "the manifest misread $h3" |
| 1015 | if grep -q '^to finish' "$w/out"; then fail 'a finished rebase printed a command to finish it'; fi |
| 1016 | # Rebased again, a stack that fast-forwards from main is left as it is. |
| 1017 | before="$(stack_refs s1)" |
| 1018 | ok as requester valley rebase "$h3" |
| 1019 | grep -qx 's1/three already fast-forwards from origin/main; nothing rebased' "$w/out" \ |
| 1020 | || fail 'a stack that fast-forwards was not left alone' |
| 1021 | moved_nothing s1 |
| 1022 | |
| 1023 | # An approved change asks nothing of the reader. With a request at the |
| 1024 | # head of the middle layer, pending marks it approved, and review walks the |
| 1025 | # stack without paging it. The top, which review was named, is still paged. |
| 1026 | req2="refs/the-valley/integration-requests/main/c-$id2" |
| 1027 | req3="refs/the-valley/integration-requests/main/c-$id3" |
| 1028 | as requester git push --quiet origin "$new2:$req2" |
| 1029 | ok valley pending --json |
| 1030 | jq -e --arg a "$h2" --arg b "$h3" '[.repos[0].changes[] | select(.handle == $a or .handle == $b) | [.handle, .approved]] == [[$b, false], [$a, true]] |
| 1031 | or [.repos[0].changes[] | select(.handle == $a or .handle == $b) | [.handle, .approved]] == [[$a, true], [$b, false]]' "$w/out" > /dev/null \ |
| 1032 | || fail 'pending --json did not mark only the middle layer approved' |
| 1033 | ok valley pending |
| 1034 | grep -qE "^ ◐ $h2 v2 +approved" "$w/out" || fail 'pending did not list the approved layer as moving' |
| 1035 | grep -qE "^ [0-9]+ $h3 v2 " "$w/out" || fail 'pending did not list the top as needing an approval' |
| 1036 | printf 'n\ns\n' | as requester valley review "$h3" > "$w/out" 2> "$w/err" || fail 'review of the stack failed' |
| 1037 | grep -qF "$h2 (approved, not paged): " "$w/out" || fail 'review did not say it leaves the approved layer unpaged' |
| 1038 | if grep -qF "change 2 of 3: " "$w/out"; then fail 'review paged the approved layer'; fi |
| 1039 | grep -qF "change 1 of 3: " "$w/out" || fail 'review did not page the first layer' |
| 1040 | grep -qF "change 3 of 3: $h3 on " "$w/out" || fail 'review did not page the top' |
| 1041 | # Named itself, the approved layer is reviewed. |
| 1042 | printf 'n\ns\n' | as requester valley review "$h2" > "$w/out" 2> "$w/err" || fail 'review of the approved change failed' |
| 1043 | grep -qF "change 2 of 2: $h2 on " "$w/out" || fail 'review of a named approved change did not page it' |
| 1044 | # With the top approved too, nothing waits on the reader. pending lists |
| 1045 | # both as moving, one line each, and the top is still paged. |
| 1046 | as requester git push --quiet origin "$new3:$req3" |
| 1047 | ok valley pending |
| 1048 | grep -qE "^ ◐ $h3 v2 +approved" "$w/out" || fail 'pending did not list the approved top as moving' |
| 1049 | grep -qE "^ ◐ $h2 v2 +approved" "$w/out" || fail 'pending did not list the approved layer as moving' |
| 1050 | if grep -qE "^ [0-9]+ ($h2|$h3) v2 " "$w/out"; then fail 'pending listed an approved change as needing an approval'; fi |
| 1051 | ok valley pending --json |
| 1052 | jq -e --arg a "$h2" --arg b "$h3" '[.repos[0].changes[] | select(.handle == $a or .handle == $b) | .approved] == [true, true]' "$w/out" > /dev/null || fail 'pending --json did not mark both approved' |
| 1053 | printf 'n\ns\n' | as requester valley review "$h3" > "$w/out" 2> "$w/err" || fail 'review of the approved stack failed' |
| 1054 | grep -qF "change 3 of 3: $h3 on " "$w/out" || fail 'review did not page an approved top it was named' |
| 1055 | as requester git push --quiet origin --delete "$req2" "$req3" |
| 1056 | |
| 1057 | # A conflict in the middle layer pushes nothing, records nothing, and |
| 1058 | # names the layer. |
| 1059 | build_stack s3 |
| 1060 | git checkout --quiet main |
| 1061 | echo 'main has its own' > s3-two.txt |
| 1062 | git add s3-two.txt |
| 1063 | git commit --quiet -m 'main has its own s3-two' |
| 1064 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 1065 | before="$(stack_refs s3)" |
| 1066 | refused "$h2, layer 2 of 3 under $h3, conflicts with s3/one as rebased — resolve by hand; nothing changed" \ |
| 1067 | as requester valley rebase "$h3" |
| 1068 | moved_nothing s3 |
| 1069 | |
| 1070 | # Each refusal before the rebase changes nothing. |
| 1071 | build_stack s4 |
| 1072 | move_main s4-main |
| 1073 | before="$(stack_refs s4)" |
| 1074 | # A lower change that is a draft. |
| 1075 | ok as contributor valley unready "$h2" |
| 1076 | before="$(stack_refs s4)" |
| 1077 | refused "change $h2 v1, layer 2 of 3 under $h3, is draft, not ready; nothing rebased. Ready it first: valley ready --change $id2 s4/two" \ |
| 1078 | as requester valley rebase "$h3" |
| 1079 | moved_nothing s4 |
| 1080 | ok as contributor valley ready --change "$id2" s4/two |
| 1081 | before="$(stack_refs s4)" |
| 1082 | # A layer's branch missing on origin. |
| 1083 | git -C "$origin" update-ref -d refs/heads/s4/two |
| 1084 | refused "change $h2 v1, layer 2 of 3 under $h3, is on the branch s4/two, and origin has no such branch; nothing rebased" \ |
| 1085 | as requester valley rebase "$h3" |
| 1086 | git -C "$origin" update-ref refs/heads/s4/two "$two" |
| 1087 | moved_nothing s4 |
| 1088 | # A layer's branch at another commit than its recorded head. |
| 1089 | git checkout --quiet -b s4-elsewhere "$two" |
| 1090 | commit s4-elsewhere |
| 1091 | elsewhere="$(git rev-parse HEAD)" |
| 1092 | git checkout --quiet main |
| 1093 | as contributor git push --quiet --force origin "$elsewhere:refs/heads/s4/two" |
| 1094 | refused "change $h2 v1, layer 2 of 3 under $h3, records the head $two, and origin/s4/two is at $elsewhere; nothing rebased. Ready the branch's head as a new version of $h2 to act on it: valley ready --head $elsewhere --change $id2 s4/two" \ |
| 1095 | as requester valley rebase "$h3" |
| 1096 | git -C "$origin" update-ref refs/heads/s4/two "$two" |
| 1097 | moved_nothing s4 |
| 1098 | # Two layers on one branch: s4/two at the top's first commit is a plain |
| 1099 | # branch in the stack, and the change under it is on s4/two as well. |
| 1100 | git -C "$origin" update-ref refs/heads/s4/two "$three~1" |
| 1101 | refused "$h2 and s4/two, layers 2 and 3 of 4 under $h3, are both on the branch s4/two; nothing rebased" \ |
| 1102 | as requester valley rebase "$h3" |
| 1103 | git -C "$origin" update-ref refs/heads/s4/two "$two" |
| 1104 | moved_nothing s4 |
| 1105 | # No valley.principal: the refusal gives the command that sets it. |
| 1106 | refused "git config valley.principal names none, so nothing is rebased; set it with git config valley.principal <name>" \ |
| 1107 | as anonymous valley rebase "$h3" |
| 1108 | moved_nothing s4 |
| 1109 | # A draft's earlier version head among the commits to be rebased. The |
| 1110 | # draft readied s4/one's head as its v1, then readied a head elsewhere as |
| 1111 | # v2, and was unreadied. s4/one is a plain branch again, and its head is |
| 1112 | # a version head of a change that is not ready. |
| 1113 | ok as contributor valley ready --head "$one" s4/one |
| 1114 | did="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 1115 | dh="$(sed -n 's/^project \([k-z]*\) v1 ready$/\1/p' "$w/out")" |
| 1116 | git checkout --quiet -b s4/draft main |
| 1117 | commit s4-draft |
| 1118 | as contributor git push --quiet origin s4/draft |
| 1119 | git checkout --quiet main |
| 1120 | ok as contributor valley ready --change "$did" s4/draft |
| 1121 | ok as contributor valley unready "$dh" |
| 1122 | before="$(stack_refs s4)" |
| 1123 | refused "the commit ${one:0:7} under $h3 is v1 of change $dh, which is draft, not ready; nothing rebased" \ |
| 1124 | as requester valley rebase "$h3" |
| 1125 | moved_nothing s4 |
| 1126 | |
| 1127 | # race_hook <command>: the command runs once, from the next rebase's |
| 1128 | # post-rewrite hook: after the stack is read and a layer is rebased, and |
| 1129 | # before anything is pushed. |
| 1130 | race_hook() { |
| 1131 | cat > "$repo/.git/hooks/post-rewrite" << EOF |
| 1132 | #!/bin/sh |
| 1133 | [ -e "$w/race-armed" ] || exit 0 |
| 1134 | rm -f "$w/race-armed" |
| 1135 | unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_PREFIX |
| 1136 | cd "$repo" && $1 |
| 1137 | exit 0 |
| 1138 | EOF |
| 1139 | chmod +x "$repo/.git/hooks/post-rewrite" |
| 1140 | touch "$w/race-armed" |
| 1141 | } |
| 1142 | disarm_hook() { |
| 1143 | rm -f "$repo/.git/hooks/post-rewrite" |
| 1144 | [ ! -e "$w/race-armed" ] || fail 'the race hook never ran' |
| 1145 | } |
| 1146 | |
| 1147 | # A layer's branch moves after the rebase read it: the atomic push is |
| 1148 | # refused, and no branch moves. |
| 1149 | build_stack s5 |
| 1150 | move_main s5-main |
| 1151 | git checkout --quiet -b s5-moved "$two" |
| 1152 | commit s5-moved |
| 1153 | moved="$(git rev-parse HEAD)" |
| 1154 | as contributor git push --quiet origin "$moved:refs/heads/s5-moved" |
| 1155 | git checkout --quiet main |
| 1156 | race_hook "git -C '$origin' update-ref refs/heads/s5/two $moved" |
| 1157 | refused 'origin refused the push of the stack'"'"'s branches, so none moved' as requester valley rebase "$h3" |
| 1158 | disarm_hook |
| 1159 | [ "$(at refs/heads/s5/one)" = "$one" ] || fail 'a refused push moved s5/one' |
| 1160 | [ "$(at refs/heads/s5/two)" = "$moved" ] || fail 'a refused push moved s5/two' |
| 1161 | [ "$(at refs/heads/s5/three)" = "$three" ] || fail 'a refused push moved s5/three' |
| 1162 | record "$id2" '.active == 1' |
| 1163 | record "$id3" '.active == 1' |
| 1164 | grep -qE "^ $h3 +.* origin/s5/three not pushed, no version added$" "$w/out" || fail 'the manifest did not say nothing was pushed' |
| 1165 | |
| 1166 | # A plain branch in the stack becomes a change after the rebase read it: |
| 1167 | # the last read before the push refuses it, and nothing moves. |
| 1168 | build_stack s6 |
| 1169 | move_main s6-main |
| 1170 | before="$(stack_refs s6)" |
| 1171 | race_hook "valley ready s6/one > '$w/raced.out'" |
| 1172 | refused "origin/s6/one, layer 1 of 3 under $h3, is now change" as requester valley rebase "$h3" |
| 1173 | disarm_hook |
| 1174 | grep -qF 'nothing pushed. Rebase it again by its handle' "$w/err" || fail 'the refusal did not say what to do' |
| 1175 | [ "$(at refs/heads/s6/one)" = "$one" ] || fail 's6/one moved' |
| 1176 | [ "$(at refs/heads/s6/two)" = "$two" ] || fail 's6/two moved' |
| 1177 | [ "$(at refs/heads/s6/three)" = "$three" ] || fail 's6/three moved' |
| 1178 | record "$id2" '.active == 1' |
| 1179 | record "$id3" '.active == 1' |
| 1180 | |
| 1181 | # The middle change is unreadied after the branches are pushed and before |
| 1182 | # its record is written. The other layers are finished, the manifest gives |
| 1183 | # the exact command that finishes the middle one, the exit is non-zero, |
| 1184 | # and that command finishes it. A stand-in for origin's receive-pack |
| 1185 | # unreadies it in front of the second push: the first moves the branches, |
| 1186 | # and the second writes the middle change's record. |
| 1187 | build_stack s7 |
| 1188 | move_main s7-main |
| 1189 | cat > "$w/racing-receive-pack" << EOF |
| 1190 | #!/bin/sh |
| 1191 | n=\$((\$(cat "$w/pushes" 2> /dev/null || echo 0) + 1)) |
| 1192 | echo "\$n" > "$w/pushes" |
| 1193 | if [ "\$n" = 2 ]; then |
| 1194 | (unset GIT_DIR; cd "$repo" && valley unready $id2 > /dev/null 2>&1) |
| 1195 | fi |
| 1196 | exec git-receive-pack "\$@" |
| 1197 | EOF |
| 1198 | chmod +x "$w/racing-receive-pack" |
| 1199 | rm -f "$w/pushes" |
| 1200 | git config remote.origin.receivepack "$w/racing-receive-pack" |
| 1201 | refused '1 change of the stack under' as requester valley rebase "$h3" |
| 1202 | git config --unset remote.origin.receivepack |
| 1203 | [ "$(cat "$w/pushes")" -ge 3 ] || fail 'the record writes did not follow the branch push' |
| 1204 | new2="$(at refs/heads/s7/two)" new3="$(at refs/heads/s7/three)" |
| 1205 | [ "$new2" != "$two" ] || fail 's7/two did not move' |
| 1206 | record "$id2" '.state == "draft"' '.active == 1' |
| 1207 | record "$id3" '.state == "ready"' '.active == 2' ".versions[1].head == \"git-sha1:$new3\"" |
| 1208 | grep -qE "^ $h2 +.* origin/s7/two pushed, no version added$" "$w/out" || fail "the manifest did not say $h2 is unfinished" |
| 1209 | grep -qE "^ $h3 +.* origin/s7/three pushed, v2 added$" "$w/out" || fail "the manifest did not say $h3 is finished" |
| 1210 | finish="$(sed -n "s/^to finish $h2: //p" "$w/out")" |
| 1211 | [ "$finish" = "valley ready --head $new2 --change $id2 s7/two" ] || fail "the manifest gave the command: $finish" |
| 1212 | # Run again, the rebase refuses the stack, since a branch in it holds a |
| 1213 | # head no version of its change records, and gives the same command. |
| 1214 | refused "Ready the branch's head as a new version of $h2 to act on it: $finish" as requester valley rebase "$h3" |
| 1215 | eval "ok as contributor $finish" |
| 1216 | record "$id2" '.state == "ready"' '.active == 2' ".versions[1].head == \"git-sha1:$new2\"" |
| 1217 | |
| 1218 | # A second stack shares the bottom layer. The rebase names it as on an |
| 1219 | # old head of that layer, and does not move it. |
| 1220 | build_stack s9 |
| 1221 | git checkout --quiet -b s9/side "$one" |
| 1222 | commit s9-side |
| 1223 | side="$(git rev-parse HEAD)" |
| 1224 | as contributor git push --quiet origin s9/side |
| 1225 | git checkout --quiet main |
| 1226 | move_main s9-main |
| 1227 | ok as requester valley rebase "$h3" |
| 1228 | grep -qx 's9/side is now on an old head of s9/one; rebase it with valley rebase s9/side' "$w/out" \ |
| 1229 | || fail 'the rebase did not name the stack left on an old head' |
| 1230 | [ "$(at refs/heads/s9/side)" = "$side" ] || fail 'the rebase moved another stack' |
| 1231 | |
| 1232 | # A stack whose middle layer is a draft. review says so on that layer's |
| 1233 | # page and in its prompt, and offers [a]pprove neither there nor above it, |
| 1234 | # since approving either would land the draft. The bottom, under it, is |
| 1235 | # offered [a]pprove as before. A withdrawn middle layer is shown the same |
| 1236 | # way. |
| 1237 | build_stack s10 |
| 1238 | ok as contributor valley unready "$h2" |
| 1239 | s10_request="refs/the-valley/integration-requests/main/c-$id3" |
| 1240 | printf 'n\na\nn\na\ns\n' | as requester valley review "$h3" > "$w/out" 2> "$w/err" || fail "review of the stack under $h3 failed" |
| 1241 | drafted="$h2 v1 is a draft: its owner (contributor) hasn't marked it ready, so it can't be approved yet." |
| 1242 | grep -qF "s10/one, 1 of 3: [n]ext layer, [a]pprove s10/one alone, [v]iew full diff, [s]top? " "$w/out" \ |
| 1243 | || fail 'the layer under the draft was not offered [a]pprove' |
| 1244 | grep -A1 "change 2 of 3: $h2 on s10/one\$" "$w/out" | grep -qxF "$drafted" \ |
| 1245 | || fail "the draft layer's page did not say it is a draft under its heading" |
| 1246 | [ "$(grep -oF "$h2, 2 of 3: $drafted [n]ext layer, [v]iew full diff, [s]top? " "$w/out" | wc -l)" = 2 ] \ |
| 1247 | || fail "the draft layer's prompt did not say it is a draft in place of [a]pprove" |
| 1248 | grep -qF '[s]top? answer n, v, or s' "$w/out" || fail 'the draft layer took [a]pprove' |
| 1249 | held="it would bring $h2, change 2 of 3, which is a draft, so it can't be approved yet." |
| 1250 | [ "$(grep -cF " · stack of 3 · $held" "$w/out")" = 2 ] && grep -qF "[v]iew diff [r]eject $h3 [s]top: " "$w/out" \ |
| 1251 | || fail 'the top above the draft did not say why it cannot be approved' |
| 1252 | grep -qF '[s]top: answer v, r, or s' "$w/out" || fail 'the top above the draft took [a]pprove' |
| 1253 | grep -qF "[s]top: stopped reviewing $h3 at change 3 of 3; nothing done" "$w/out" || fail 'the review of the stack did not stop' |
| 1254 | if grep -qF -e "[a]pprove all 3" -e "[a]pprove s10/one and $h2" "$w/out"; then |
| 1255 | fail 'review offered to approve at or above the draft' |
| 1256 | fi |
| 1257 | if git -C "$origin" rev-parse --verify --quiet "$s10_request" > /dev/null; then |
| 1258 | fail 'review filed a request above the draft' |
| 1259 | fi |
| 1260 | ok as contributor valley withdraw "$h2" |
| 1261 | printf 'n\nn\ns\n' | as requester valley review "$h3" > "$w/out" 2> "$w/err" || fail "review of the stack under $h3 failed" |
| 1262 | grep -A1 "change 2 of 3: $h2 on s10/one\$" "$w/out" | grep -qxF "$h2 is withdrawn: there is nothing to approve." \ |
| 1263 | || fail "the withdrawn layer's page did not say it is withdrawn under its heading" |
| 1264 | grep -qF " · stack of 3 · it would bring $h2, change 2 of 3, which is withdrawn, so it can't be approved yet." "$w/out" \ |
| 1265 | || fail 'the top above the withdrawn layer did not say why it cannot be approved' |
| 1266 | |
| 1267 | # ---------------------------------------------------------------------- |
| 1268 | # review sets the version it shows against the change's approved version: |
| 1269 | # the greatest version below it whose head a request or an outcome names. |
| 1270 | # main moves here by direct pushes, as a landing moves it. |
| 1271 | # |
| 1272 | # after <log> <line>: what <log> holds from its first line that is exactly |
| 1273 | # <line> on. |
| 1274 | after() { |
| 1275 | awk -v l="$2" '$0 == l { on = 1 } on' "$1" |
| 1276 | } |
| 1277 | |
| 1278 | # v1 is approved, another change lands, and valley rebase makes v2. The |
| 1279 | # rebased version has v1's file delta, so review prints a clean restack |
| 1280 | # of v1, its two bases, and the landing between them, and pages no diff. |
| 1281 | git checkout --quiet -b cmp/topic main |
| 1282 | commit cmp-one |
| 1283 | as contributor git push --quiet origin cmp/topic |
| 1284 | new_change cmp/topic |
| 1285 | cid="$nid" ch="$nh" |
| 1286 | cv1="$(git rev-parse HEAD)" |
| 1287 | git checkout --quiet main |
| 1288 | ok as requester valley approve --head "$cv1" "$ch" |
| 1289 | grep -q "^[^ ]* $ch v1 approved · " "$w/out" || fail "approve did not approve $ch v1" |
| 1290 | base1="$(at refs/heads/main)" |
| 1291 | move_main cmp-landing |
| 1292 | landing="$(at refs/heads/main)" |
| 1293 | ok as requester valley rebase "$ch" |
| 1294 | printf 's\n' | as requester valley review "$ch" > "$w/out" 2> "$w/err" || fail "review of $ch failed" |
| 1295 | cv2="$(at refs/heads/cmp/topic)" |
| 1296 | clean="$ch v2 at ${cv2:0:7}: rebased since you approved v1 at ${cv1:0:7}, with the same file changes." |
| 1297 | grep -qx "$clean" "$w/out" || fail "review after a rebase did not say v2 is v1 rebased" |
| 1298 | after "$w/out" "$clean" > "$w/clean" |
| 1299 | [ "$(sed -n 2,3p "$w/clean")" = "$(printf '%s\n' "Main moved ${base1:0:7} → ${landing:0:7}, landing 1 commit:" \ |
| 1300 | " ${landing:0:7} cmp-landing")" ] || fail "the clean restack did not list the landing: $(cat "$w/clean")" |
| 1301 | if grep -q "$(printf '\033')" "$w/out"; then fail 'review styled a clean restack printed to no terminal'; fi |
| 1302 | # The change's checks line is a clause of the prompt's header. |
| 1303 | sed -n 4p "$w/clean" | grep -qF ' · checks: none required' || fail "the clean restack did not show the checks line before the prompt: $(cat "$w/clean")" |
| 1304 | grep -qx "\[a\]pprove \[v\]iew diff \[r\]eject \[s\]kip: skipped cmp/topic; nothing done" "$w/clean" \ |
| 1305 | || fail 'the prompt after a clean restack did not offer [v]iew full diff' |
| 1306 | if grep -q '^diff --git' "$w/clean"; then fail 'review paged the diff of a clean restack'; fi |
| 1307 | # [v]iew full diff pages it, and the prompt comes back. |
| 1308 | printf 'v\ns\n' | as requester valley review "$ch" > "$w/out" 2> "$w/err" || fail "review of $ch failed" |
| 1309 | after "$w/out" "$clean" > "$w/clean" |
| 1310 | grep -qF "[s]kip: change $ch v2 at ${cv2:0:7}, on branch cmp/topic, log against origin/main:" "$w/clean" \ |
| 1311 | || fail '[v]iew full diff did not page the change' |
| 1312 | grep -qx 'diff --git a/cmp-one.txt b/cmp-one.txt' "$w/clean" || fail '[v]iew full diff did not show the diff' |
| 1313 | [ "$(grep -c '^\[a\]pprove \[v\]iew diff' "$w/clean")" = 2 ] || fail 'the prompt did not come back after [v]' |
| 1314 | |
| 1315 | # v3 edits a file. The greatest approved version below it is still v1, |
| 1316 | # so review pages that v3 is edited since v1, the file whose change |
| 1317 | # differs, and the range-diff. |
| 1318 | git checkout --quiet -B cmp/topic "$cv2" |
| 1319 | echo edited > cmp-one.txt |
| 1320 | git commit --quiet -am 'cmp-one edited' |
| 1321 | as contributor git push --quiet origin cmp/topic |
| 1322 | ok as contributor valley ready --change "$cid" |
| 1323 | cv3="$(git rev-parse HEAD)" |
| 1324 | git checkout --quiet main |
| 1325 | printf 's\n' | as requester valley review "$ch" > "$w/out" 2> "$w/err" || fail "review of $ch v3 failed" |
| 1326 | changed="$ch v3 at ${cv3:0:7}: edited since you approved v1 at ${cv1:0:7}." |
| 1327 | grep -qx "$changed" "$w/out" || fail "review did not say v3 is edited since v1: $(cat "$w/out")" |
| 1328 | after "$w/out" "$changed" > "$w/changed" |
| 1329 | grep -qx "Main moved ${base1:0:7} → ${landing:0:7}." "$w/changed" \ |
| 1330 | && grep -A1 -x '1 of 2 commits changed (v1 had 1), and 1 file differs from v1:' "$w/changed" \ |
| 1331 | | grep -qx ' cmp-one.txt not edited on main' || fail "the file whose change differs was not listed: $(cat "$w/changed")" |
| 1332 | if grep -q 'absent -> 100644' "$w/changed"; then fail 'review listed hashes without -v'; fi |
| 1333 | grep -qx "── git range-diff ${base1:0:7}..${cv1:0:7} ${landing:0:7}..${cv3:0:7} ──" "$w/changed" || fail 'no range-diff heading' |
| 1334 | grep -qx "1: ${cv1:0:7} = 1: ${cv2:0:7} cmp-one" "$w/changed" || fail 'the range-diff did not pair the rebased commit' |
| 1335 | grep -qx -- "-: ------- > 2: ${cv3:0:7} cmp-one edited" "$w/changed" || fail 'the range-diff did not show the new commit' |
| 1336 | # The page ends with v3's own diff of that file, where a note keeps its |
| 1337 | # file and line. |
| 1338 | after "$w/changed" "── v3's diff of this file against main ${landing:0:7} ──" > "$w/own" |
| 1339 | [ "$(sed -n 2p "$w/own")" = 'diff --git a/cmp-one.txt b/cmp-one.txt' ] || fail "the changed version did not end with its own diff: $(cat "$w/changed")" |
| 1340 | grep -qx '+edited' "$w/own" || fail "v3's own diff did not show the edited line" |
| 1341 | |
| 1342 | # v3 approved, v4 is v3 with another message, and v5 is v4 with another |
| 1343 | # author, once v4 is approved. Each has its approved version's file delta |
| 1344 | # on the same base, and each is flagged. |
| 1345 | ok as requester valley approve --head "$cv3" "$ch" |
| 1346 | git checkout --quiet cmp/topic |
| 1347 | git commit --quiet --amend -m 'cmp-one edited, and reworded' |
| 1348 | as contributor git push --quiet --force origin cmp/topic |
| 1349 | ok as contributor valley ready --change "$cid" |
| 1350 | cv4="$(git rev-parse HEAD)" |
| 1351 | printf 's\n' | as requester valley review "$ch" > "$w/out" 2> "$w/err" || fail "review of $ch v4 failed" |
| 1352 | after "$w/out" "$ch v4 at ${cv4:0:7}: edited since you approved v3 at ${cv3:0:7}, with the same file changes." > "$w/meta" |
| 1353 | [ -s "$w/meta" ] || fail "a version with another message alone was no clean restack: $(cat "$w/out")" |
| 1354 | grep -qx 'Commit messages edited since you approved v3. Read them before approving:' "$w/meta" \ |
| 1355 | || fail 'a changed message was not flagged' |
| 1356 | grep -A1 -x " v4, commit 2 of 2, ${cv4:0:7}:" "$w/meta" | grep -qx ' cmp-one edited, and reworded' \ |
| 1357 | || fail 'the changed message was not shown' |
| 1358 | if grep -q 'Commit authors edited' "$w/meta"; then fail 'an unchanged author was flagged'; fi |
| 1359 | ok as requester valley approve --head "$cv4" "$ch" |
| 1360 | git commit --quiet --amend --no-edit --author 'Someone Else <else@localhost>' |
| 1361 | as contributor git push --quiet --force origin cmp/topic |
| 1362 | ok as contributor valley ready --change "$cid" |
| 1363 | cv5="$(git rev-parse HEAD)" |
| 1364 | git checkout --quiet main |
| 1365 | printf 's\n' | as requester valley review "$ch" > "$w/out" 2> "$w/err" || fail "review of $ch v5 failed" |
| 1366 | after "$w/out" "$ch v5 at ${cv5:0:7}: edited since you approved v4 at ${cv4:0:7}, with the same file changes." > "$w/meta" |
| 1367 | grep -qx 'Commit authors edited since you approved v4:' "$w/meta" || fail "a changed author was not flagged: $(cat "$w/out")" |
| 1368 | grep -q "^ v5, commit 2 of 2, ${cv5:0:7}: Someone Else <else@localhost> " "$w/meta" || fail 'the new author was not shown' |
| 1369 | if grep -q 'Commit messages edited' "$w/meta"; then fail 'an unchanged message was flagged'; fi |
| 1370 | |
| 1371 | # A change with no approved version is reviewed as any change is: its |
| 1372 | # whole diff, with no comparison. |
| 1373 | git checkout --quiet -b cmp/fresh main |
| 1374 | commit cmp-fresh |
| 1375 | as contributor git push --quiet origin cmp/fresh |
| 1376 | new_change cmp/fresh |
| 1377 | commit cmp-fresh-more |
| 1378 | as contributor git push --quiet origin cmp/fresh |
| 1379 | ok as contributor valley ready --change "$nid" |
| 1380 | git checkout --quiet main |
| 1381 | printf 's\n' | as requester valley review "$nh" > "$w/out" 2> "$w/err" || fail "review of $nh failed" |
| 1382 | grep -q "^change $nh v2 at .*, on branch cmp/fresh, log against origin/main:$" "$w/out" || fail 'a change with no approval was not paged' |
| 1383 | grep -qx 'diff --git a/cmp-fresh.txt b/cmp-fresh.txt' "$w/out" || fail 'a change with no approval did not show its diff' |
| 1384 | if grep -q 'since you approved' "$w/out"; then fail 'a change with no approval was compared'; fi |
| 1385 | |
| 1386 | # A stack of two changes, of which only the lower has an approved |
| 1387 | # version. Once rebased, review sets each layer against its own change's |
| 1388 | # approval: the lower is a clean restack of its v1, and the top, which |
| 1389 | # has none, is paged in full. |
| 1390 | git checkout --quiet -b cmp/low main |
| 1391 | commit cmp-low |
| 1392 | as contributor git push --quiet origin cmp/low |
| 1393 | new_change cmp/low |
| 1394 | lid="$nid" lh="$nh" lv1="$(git rev-parse HEAD)" |
| 1395 | git checkout --quiet -b cmp/high |
| 1396 | commit cmp-high |
| 1397 | as contributor git push --quiet origin cmp/high |
| 1398 | new_change cmp/high |
| 1399 | hh="$nh" |
| 1400 | git checkout --quiet main |
| 1401 | ok as requester valley approve --head "$lv1" "$lh" |
| 1402 | move_main cmp-stack-landing |
| 1403 | ok as requester valley rebase "$hh" |
| 1404 | record "$lid" '.active == 2' |
| 1405 | lv2="$(at refs/heads/cmp/low)" hv2="$(at refs/heads/cmp/high)" |
| 1406 | printf 'n\ns\n' | as requester valley review "$hh" > "$w/out" 2> "$w/err" || fail "review of the stack under $hh failed" |
| 1407 | # A prompt ends with no newline, so the next page's heading follows it on |
| 1408 | # its line: each page runs from its heading to the line holding the next. |
| 1409 | awk '/^change 1 of 2: /{ on = 1 } on { print } /change 2 of 2: /{ on = 0 }' "$w/out" > "$w/page1" |
| 1410 | awk '/change 2 of 2: /{ on = 1 } on' "$w/out" > "$w/page2" |
| 1411 | grep -qx "$lh v2 at ${lv2:0:7}: rebased since you approved v1 at ${lv1:0:7}, with the same file changes." "$w/page1" \ |
| 1412 | || fail "the lower layer was not set against its own approved version: $(cat "$w/out")" |
| 1413 | grep -qF "$lh, 1 of 2: [n]ext layer, [a]pprove $lh alone, [v]iew full diff, [s]top? " "$w/page1" \ |
| 1414 | || fail 'the lower layer did not offer [v]iew full diff' |
| 1415 | if grep -q '^diff --git' "$w/page1"; then fail 'the clean lower layer was paged'; fi |
| 1416 | grep -q "^change $hh v2 at ${hv2:0:7}, on branch cmp/high, log against $lh at " "$w/page2" || fail 'the top was not paged' |
| 1417 | grep -qx 'diff --git a/cmp-high.txt b/cmp-high.txt' "$w/page2" || fail "the top's page did not hold its own diff" |
| 1418 | if grep -q 'since you approved' "$w/page2"; then fail 'the top, which has no approval, was compared'; fi |
| 1419 | |
| 1420 | # ---------------------------------------------------------------------- |
| 1421 | # review names the knots each change it shows does: every knot in the |
| 1422 | # project's plan graph whose changes lists valley:project/<change ID>, one |
| 1423 | # line each, after the line that names the change and before its diff, |
| 1424 | # active knots first. The graph is written through the hook. cmp/fresh is |
| 1425 | # linked from one knot, cmp/low from two, cmp/high from one, and cmp/topic |
| 1426 | # from none. |
| 1427 | change_id() { valley status --json "$1" | jq -r .change_id; } |
| 1428 | fid="$(change_id cmp/fresh)" hid="$(change_id "$hh")" |
| 1429 | ok as contributor valley knot new outcome "Ready changes stay landable as main moves" --json |
| 1430 | goal="$(jq -r .knot.id "$w/out")" |
| 1431 | ok as contributor valley knot new bug "The stack reads the wrong base" --json |
| 1432 | bug="$(jq -r .knot.id "$w/out")" |
| 1433 | ok as contributor valley knot link "$goal" --change "valley:project/$fid" --change "valley:project/$lid" \ |
| 1434 | --change "valley:project/$hid" |
| 1435 | ok as contributor valley knot link "$bug" --change "valley:project/$lid" |
| 1436 | goal_line="does $goal (open): Ready changes stay landable as main moves" |
| 1437 | bug_line="does $bug (open): The stack reads the wrong base" |
| 1438 | printf 's\n' | as requester valley review cmp/fresh > "$w/out" 2> "$w/err" || fail 'review of cmp/fresh failed' |
| 1439 | grep -A1 "^change [a-z]* v2 at .*, on branch cmp/fresh, log against origin/main:$" "$w/out" | sed -n 2p \ |
| 1440 | | grep -qxF "$goal_line" || fail "review of a change one knot links did not name it under the change: $(cat "$w/out")" |
| 1441 | [ "$(grep -c '^does ' "$w/out")" = 1 ] || fail 'review of cmp/fresh named another knot than its own' |
| 1442 | printf 'n\ns\n' | as requester valley review "$hh" > "$w/out" 2> "$w/err" || fail "review of the stack under $hh failed" |
| 1443 | awk '/^change 1 of 2: /{ on = 1 } on { print } /change 2 of 2: /{ on = 0 }' "$w/out" > "$w/page1" |
| 1444 | awk '/change 2 of 2: /{ on = 1 } on' "$w/out" > "$w/page2" |
| 1445 | [ "$(sed -n 2,3p "$w/page1")" = "$(printf '%s\n' "$bug_line" "$goal_line")" ] \ |
| 1446 | || fail "the lower layer's page did not name both its knots under its heading: $(cat "$w/page1")" |
| 1447 | [ "$(grep -c '^does ' "$w/page1")" = 2 ] || fail "the lower layer's page named another layer's knots" |
| 1448 | [ "$(sed -n 2p "$w/page2")" = "$goal_line" ] || fail "the top's page did not name its knot under its heading: $(cat "$w/page2")" |
| 1449 | [ "$(grep -c '^does ' "$w/page2")" = 1 ] || fail "the top's page named another layer's knots" |
| 1450 | printf 's\n' | as requester valley review "$ch" > "$w/out" 2> "$w/err" || fail "review of $ch failed" |
| 1451 | if grep -q '^does ' "$w/out"; then fail 'review named a knot for a change no knot links'; fi |
| 1452 | # A graph that does not read shows no knot, and review goes on as before. |
| 1453 | # It says why only with -v. |
| 1454 | graph_tip="$(at refs/the-valley/knots)" |
| 1455 | junk="$(printf 'junk\n' | git -C "$origin" hash-object -w --stdin)" |
| 1456 | junk_tree="$(printf '100644 blob %s\tjunk.txt\n' "$junk" | git -C "$origin" mktree)" |
| 1457 | git -C "$origin" update-ref refs/the-valley/knots "$(git -C "$origin" commit-tree "$junk_tree" -m junk)" |
| 1458 | printf 's\n' | as requester valley review cmp/fresh > "$w/out" 2> "$w/err" || fail 'review of cmp/fresh failed over a broken graph' |
| 1459 | grep -q 'on branch cmp/fresh, log against origin/main:$' "$w/out" || fail 'review over a broken graph did not show the change' |
| 1460 | grep -qF 'skipped cmp/fresh; nothing done' "$w/out" || fail 'review over a broken graph did not reach its prompt' |
| 1461 | if grep -q '^does ' "$w/out"; then fail 'review named a knot from a graph that does not read'; fi |
| 1462 | if grep -q 'graph' "$w/err"; then fail 'review spoke of a broken graph without -v'; fi |
| 1463 | printf 's\n' | as requester valley review -v cmp/fresh > "$w/out" 2> "$w/err" || fail 'review -v of cmp/fresh failed' |
| 1464 | grep -qF "project's plan graph could not be read, so no change shows a knot" "$w/err" \ |
| 1465 | || fail 'review -v did not say why it shows no knot' |
| 1466 | git -C "$origin" update-ref refs/the-valley/knots "$graph_tip" |
| 1467 | |
| 1468 | echo 'valley-ready: change records through real pushes passed' |
| 1469 | touch "$out" |