gunk-dev/the-valley

valley-ready.sh

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