gunk-dev/the-valley

valley-ready.sh

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