gunk-dev/the-valley

review-notes.sh

source88a5a486b52fmain's published tip history
25.0 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" TERM=xterm
5export GIT_CONFIG_NOSYSTEM=1
6unset VISUAL || true
7git config --global user.name valley-check
8git config --global user.email valley-check@localhost
9git config --global init.defaultBranch main
10
11# A project with an origin and one branch to review: two
12# files, changed twice and once, far enough apart that the
13# diff has two hunks in the first file and one in the second.
14# The commit's message has a body, and one line of it reads
15# like a diff header.
16git init --quiet --bare "$TMPDIR/origin.git"
17git init --quiet "$TMPDIR/work"
18cd "$TMPDIR/work" || exit 1
19git remote add origin "$TMPDIR/origin.git"
20seq 1 20 | sed 's/^/alpha /' > alpha.md
21seq 1 10 | sed 's/^/beta /' > beta.md
22git add -A
23git commit --quiet -m seed
24git push --quiet -u origin main
25git checkout --quiet -b scratch
26sed -i -e '3s/.*/alpha 3 changed/' -e '15s/.*/alpha 15 changed/' alpha.md
27sed -i -e '5s/.*/beta 5 changed/' beta.md
28git commit --quiet -am "touch both files" -m "Both files change, far enough apart
29for three hunks.
30
31diff --git a/body.md b/body.md"
32git push --quiet origin scratch
33git checkout --quiet main
34git fetch --quiet origin
35
36# A pager filter that restructures the diff, as delta's
37# rendering does: if v were handed what less displays, every
38# anchor below would be wrong.
39git config --global pager.diff "sed -e 's/^/| /'"
40export EDITOR="$reviewer"
41mkdir -p "$XDG_STATE_HOME"
42
43# wait_for <log> <pattern>: wait until <log> shows <pattern>, and fail
44# after a minute. A review reads every key from the one terminal, so a
45# verdict typed before the prompt that takes it would go to whatever held
46# the terminal then, such as less. Each log waited on here is written by
47# one review only, so no earlier review's output can match.
48wait_for() {
49 local i=0
50 until grep -q -- "$2" "$1" 2> /dev/null; do
51 if [ "$i" -ge 600 ]; then
52 printf 'review-notes: %s never showed %s\n' "$1" "$2" >&2
53 return 1
54 fi
55 sleep 0.1
56 i=$((i + 1))
57 done
58}
59
60# answer_after <log> <prompt> <command> <answers>: wait until the review
61# writing to <log> has asked <prompt>, run <command>, then answer. A page
62# is shown before its prompt, so the command runs after the page is shown
63# and before the answer.
64answer_after() {
65 wait_for "$1" "$2"
66 eval "$3"
67 printf '%b' "$4"
68}
69
70# v alone opens the editor, and the pager shows the notes,
71# rendered, once it returns; q then leaves the pager, and s is
72# the verdict. The verdict waits for its prompt, and the keys
73# stay open until the notes are printed. A wait that fails
74# still sends q, so the check fails rather than hang in less.
75# shellcheck disable=SC2094 # the answers wait on the review's own output
76{
77 printf v
78 if ! wait_for "$XDG_STATE_HOME/editor.log" 'editor got: ' \
79 || ! wait_for session.log '| a note above every file'; then
80 printf q
81 exit 1
82 fi
83 printf q
84 answer_after session.log '\]kip: ' : 's\n'
85 wait_for session.log '6 note(s) on scratch'
86} | script -qec "valley review scratch" /dev/null > session.log 2>&1
87sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' session.log > plain.log
88
89# The anchors, exactly: above every file, beside the body
90# line that reads like a diff header, on a file header above
91# the first hunk, mid-hunk, in a second hunk whose numbering
92# starts elsewhere, and in a second file.
93diff -u - "$XDG_STATE_HOME/valley/notes/scratch.md" <<EOF
94## scratch at $(git rev-parse --short origin/scratch)
95
96(unanchored): a note above every file
97(unanchored): a note beside the message
98alpha.md: alpha reads like two files
99alpha.md:3: why is 3 changed at all?
100alpha.md:13: and this hunk is fine
101beta.md:5: beta wants the same treatment
102
103EOF
104
105# The editor was given the buffer valley wrote, not the
106# rendering less displayed, and given a line to open at.
107grep -qE 'editor got: \+[0-9]+ .*/review\.diff$' \
108 "$XDG_STATE_HOME/editor.log"
109# The filter did run: what the pager showed was its output.
110# less showed no warning before editing the file it paged.
111grep -q '^| @@ ' plain.log
112if grep -q 'WARNING' plain.log; then exit 1; fi
113# The page, as first drawn, shows the commit's whole message:
114# its subject, then its body indented four spaces.
115grep -m1 -A2 '^| [0-9a-f]\{7,\} touch both files$' plain.log | sed 1d > message.log
116diff -u - message.log << 'EOF'
117| Both files change, far enough apart
118| for three hunks.
119EOF
120
121# q alone leaves the pager, and the editor never runs.
122rm "$XDG_STATE_HOME/editor.log"
123# shellcheck disable=SC2094 # the answers wait on the review's own output
124{
125 printf q
126 answer_after quit.log '\]kip: ' : 's\n'
127 wait_for quit.log 'skipped scratch; nothing done'
128} | script -qec "valley review scratch" /dev/null > quit.log 2>&1
129[ ! -e "$XDG_STATE_HOME/editor.log" ]
130if grep -q 'note(s) on scratch' quit.log; then exit 1; fi
131# The line the reviewer edited instead of inserting is gone,
132# and said so.
133grep -q 'changed or deleted diff line(s) ignored' plain.log
134if grep -q 'alpha SEVENTEEN' "$XDG_STATE_HOME/valley/notes/scratch.md"; then exit 1; fi
135
136# The verdict is unchanged, and the notes come after it.
137grep -q 'skipped scratch; nothing done' plain.log
138verdict="$(grep -n 'skipped scratch' plain.log | cut -d: -f1)"
139notes="$(grep -n '6 note(s) on scratch' plain.log | cut -d: -f1)"
140[ "$verdict" -lt "$notes" ]
141
142# Ctrl-C with the pager open, and SIGTERM at the prompt, each end the
143# review with status 130 and nothing of its own left in its TMPDIR. less
144# takes the Ctrl-C itself, and the review ends once less quits, before
145# any prompt.
146# signalled <case> <driver>: review scratch on a terminal, with a TMPDIR
147# of its own, its keys and signals sent by the driver, which is given the
148# session log and the review's pid file.
149signalled() {
150 local case="$1" status=0 tmp="$TMPDIR/$1-tmp" pid="$TMPDIR/$1.pid"
151 mkdir "$tmp"
152 # shellcheck disable=SC2094 # the driver waits on the review's own output
153 "$2" "$case.log" "$pid" \
154 | TMPDIR="$tmp" \
155 script -qec "sh -c 'echo \$\$ > $pid; exec valley review scratch'" /dev/null \
156 > "$case.log" 2>&1 || status=$?
157 [ "$status" = 130 ]
158 [ -z "$(ls -A "$tmp")" ]
159}
160# ended <pid file> [<key>]: wait for the review to end, for a minute at
161# most, sending the key meanwhile, so a pager still open takes it.
162ended() {
163 local i=0
164 while kill -0 "$(cat "$1")" 2> /dev/null; do
165 if [ "$i" -ge 600 ]; then
166 kill -KILL "$(cat "$1")"
167 return 1
168 fi
169 if [ -n "${2:-}" ] && [ $((i % 5)) = 0 ]; then printf '%s' "$2"; fi
170 sleep 0.1
171 i=$((i + 1))
172 done
173}
174pager_ctrl_c() {
175 wait_for "$1" '| @@ ' || true
176 printf '\003'
177 ended "$2" q
178}
179prompt_term() {
180 wait_for "$1" '| @@ ' && printf q && wait_for "$1" '\]kip: ' || true
181 kill -TERM "$(cat "$2")"
182 ended "$2"
183}
184signalled pager-int pager_ctrl_c
185if grep -q 'kip: ' pager-int.log; then exit 1; fi
186signalled prompt-term prompt_term
187
188# Piped, review is what it always was: the diff on stdout,
189# the verdict from stdin, and no notes to take.
190printf 's\n' | valley review scratch > piped.log
191grep -q '^diff --git a/alpha.md b/alpha.md$' piped.log
192grep -q 'skipped scratch; nothing done' piped.log
193if grep -q 'note(s) on scratch' piped.log; then exit 1; fi
194
195# The pending list of branches titles each by the subject of its first
196# own commit, on the line under its name, and cuts a long one short with …
197# to fit 80 columns when stdout is no terminal. Nothing it prints there is
198# an escape code.
199valley pending --branches > pending.log
200grep -A1 '^scratch ' pending.log | grep -q '^ touch both files$'
201git checkout --quiet -b longsubject origin/main
202git commit --quiet --allow-empty -m "$(printf 'x%.0s' $(seq 1 80))"
203git push --quiet origin longsubject
204git checkout --quiet main
205valley pending --branches > pending.log
206grep -A1 '^longsubject ' pending.log | grep -qE '^ x{77}…$'
207if grep -q $'\x1b' pending.log; then exit 1; fi
208
209# On a terminal, pending fits the terminal's width and sets each name in
210# bold. With NO_COLOR set, it prints no escape code.
211script -qec "stty cols 100; valley pending --branches" /dev/null < /dev/null > tty.log 2>&1
212grep -q $'^\x1b\\[1mlongsubject\x1b\\[0m ' tty.log
213grep -qE $'^ x{80}\r?$' tty.log
214NO_COLOR=1 script -qec "stty cols 100; valley pending --branches" /dev/null < /dev/null > tty.log 2>&1
215grep -q '^longsubject ' tty.log
216if grep -q $'\x1b' tty.log; then exit 1; fi
217
218# ----------------------------------------------------------------------
219# A stack: a branch, then two changes with records, each on the one
220# below, in a project whose floor requires nothing, so approving files a
221# request with no evidence. Its origin logs every push it receives, which
222# is how "publishes nothing" is read.
223stack="$TMPDIR/stack.git"
224pushlog="$TMPDIR/pushlog"
225git init --quiet --bare "$stack"
226cat > "$stack/hooks/pre-receive" << EOF
227#!/bin/sh
228echo '--- push' >> $pushlog
229cat >> $pushlog
230EOF
231chmod +x "$stack/hooks/pre-receive"
232git init --quiet "$TMPDIR/stackwork"
233cd "$TMPDIR/stackwork" || exit 1
234git remote add origin "$stack"
235git config valley.principal operator
236mkdir -p policy/instance
237cat > policy/instance/floor.cue << 'EOF'
238package verification
239
240floor: {
241 classes: everything: {
242 paths: "**": true
243 requires: {}
244 }
245 unclassified: {}
246}
247EOF
248git add -A
249git commit --quiet -m seed
250git push --quiet -u origin main
251from=main
252for layer in one two three; do
253 git checkout --quiet -b "$layer" "$from"
254 echo "layer $layer" > "$layer.md"
255 git add -A
256 git commit --quiet -m "layer $layer" -m "Why layer $layer is its own change."
257 git push --quiet origin "$layer"
258 from="$layer"
259done
260two="$(git rev-parse two)" three="$(git rev-parse three)"
261q=qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq p=pppppppppppppppppppppppppppppppp
262valley ready --change "$q" two > /dev/null
263valley ready --change "$p" three > /dev/null
264git checkout --quiet main
265requests="refs/the-valley/integration-requests/main"
266
267# s at the first layer, on a terminal, with a note written in its diff:
268# the note is kept under the first layer's branch and printed at the end,
269# and nothing is published.
270cat > "$TMPDIR/stack-editor" << 'EOF'
271#!/bin/sh
272f="$1"
273case "$1" in +*) f="$2" ;; esac
274sed -i '/^+layer one$/a one wants a second line' "$f"
275EOF
276chmod +x "$TMPDIR/stack-editor"
277: > "$pushlog"
278# shellcheck disable=SC2094 # the answers wait on the review's own output
279{
280 printf vq
281 answer_after stack-tty.log 'one, 1 of 3: \[n\]ext layer, .*\[s\]top? ' : 's\n'
282 wait_for stack-tty.log '1 note(s) on one'
283} | EDITOR="$TMPDIR/stack-editor" script -qec "valley review pppp" /dev/null > stack-tty.log 2>&1
284sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' stack-tty.log > stack-plain.log
285grep -q 'pppp is a stack of 3 changes; reviewing from the bottom, each against the one below it:$' stack-plain.log
286grep -qxF 'read each layer and go on with [n]; approving pppp at the top lands all 3. [a] on a lower layer lands it and the layers under it, and ends the review.' stack-plain.log
287grep -q '| change 1 of 3: one on main$' stack-plain.log
288grep -qF 'one, 1 of 3: [n]ext layer, [a]pprove one alone, [v]iew full diff, [s]top? ' stack-plain.log
289grep -q 'stopped reviewing pppp at change 1 of 3, one; nothing done' stack-plain.log
290if grep -q 'change 2 of 3' stack-plain.log; then exit 1; fi
291grep -qx 'one.md:1: one wants a second line' "$XDG_STATE_HOME/valley/notes/one.md"
292grep -q '1 note(s) on one, appended to .*/valley/notes/one.md:' stack-plain.log
293grep -q '^ one.md:1: one wants a second line$' stack-plain.log
294[ ! -e "$XDG_STATE_HOME/valley/notes/two.md" ]
295[ ! -s "$pushlog" ]
296
297# n, n, a: the layers are paged bottom up, each diff holds only its own
298# layer's file, and the top's request is filed at the top's head. Each
299# prompt names its layer and place, and what approving there lands.
300: > "$pushlog"
301printf 'n\nn\na\n' | valley review pppp > stack.out 2> stack.err
302grep -q '^ 1\. one (branch): layer one$' stack.out
303grep -q '^ 2\. qqqq (ready): layer two$' stack.out
304grep -q '^ 3\. pppp (ready): layer three$' stack.out
305grep -qxF 'read each layer and go on with [n]; approving pppp at the top lands all 3. [a] on a lower layer lands it and the layers under it, and ends the review.' stack.out
306grep -qF 'one, 1 of 3: [n]ext layer, [a]pprove one alone, [v]iew full diff, [s]top? ' stack.out
307grep -qF 'qqqq, 2 of 3: [n]ext layer, [a]pprove one and qqqq, [v]iew full diff, [s]top? ' stack.out
308grep -qF '[a]pprove all 3 [v]iew diff [r]eject pppp [s]top: ' stack.out
309# Piped, each page's first line follows the prompt before it.
310[ "$(grep -o 'change [0-9] of 3: [a-z]* on [a-z]*$' stack.out)" = "$(printf '%s\n' \
311 'change 1 of 3: one on main' 'change 2 of 3: qqqq on one' 'change 3 of 3: pppp on qqqq')" ]
312for n in 1 2 3; do
313 awk -v n="$n" '/change [0-9] of 3: /{ on = index($0, "change " n " of 3: ") > 0 } on' stack.out > "page$n"
314done
315[ "$(grep '^diff --git' page1)" = 'diff --git a/one.md b/one.md' ]
316[ "$(grep '^diff --git' page2)" = 'diff --git a/two.md b/two.md' ]
317[ "$(grep '^diff --git' page3)" = 'diff --git a/three.md b/three.md' ]
318# A layer's page shows its commit's whole message.
319grep -A1 '^[0-9a-f]\{7,\} layer two$' page2 | grep -qx ' Why layer two is its own change.'
320[ "$(git -C "$stack" rev-parse "$requests/c-$p")" = "$three" ]
321if git -C "$stack" rev-parse --verify --quiet "$requests/c-$q" > /dev/null; then exit 1; fi
322git -C "$stack" update-ref -d "$requests/c-$p"
323
324# a at the second layer files the second layer's request at its head, and
325# nothing for the top, whose review never starts: neither its page nor its
326# prompt is shown.
327: > "$pushlog"
328printf 'n\na\n' | valley review pppp > stack-two.out 2> stack-two.err
329grep -qF 'qqqq, 2 of 3: [n]ext layer, [a]pprove one and qqqq, [v]iew full diff, [s]top? ' stack-two.out
330[ "$(git -C "$stack" rev-parse "$requests/c-$q")" = "$two" ]
331if git -C "$stack" rev-parse --verify --quiet "$requests/c-$p" > /dev/null; then exit 1; fi
332grep -q "c-$q" "$pushlog"
333if grep -q "c-$p" "$pushlog"; then exit 1; fi
334if grep -q 'change 3 of 3' stack-two.out; then exit 1; fi
335if grep -q '3 of 3: ' stack-two.out; then exit 1; fi
336
337# A draft in the middle is named in the warning before anything is paged.
338# It is readied again after, since review offers no [a]pprove at or above
339# a draft, and the stacks below approve above it.
340valley unready qqqq > /dev/null 2>&1
341printf 's\n' | valley review pppp > stack-draft.out 2>&1
342grep -q '^warning: qqqq, change 2 of 3, is a draft\. The integrator refuses to land it or anything above it (refused-not-ready)\.$' stack-draft.out
343valley ready --change "$q" two > /dev/null
344
345# A change with no stack under it pages and prompts as it always has.
346printf 's\n' | valley review one > stack-one.out 2>&1
347grep -q '^branch one, log against origin/main:$' stack-one.out
348grep -q '^\[a\]pprove \[v\]iew diff \[r\]eject \[s\]kip: skipped one; nothing done$' stack-one.out
349if grep -q 'stack of\|change 1 of' stack-one.out; then exit 1; fi
350
351seed="$(git rev-parse main)" onehead="$(git rev-parse one)"
352
353# [a]pprove at the bottom, on a plain branch under the top, files that
354# branch's head under its own request name, and nothing for the top.
355: > "$pushlog"
356printf 'a\n' | valley review pppp > stack-a-one.out 2>&1
357[ "$(git -C "$stack" rev-parse "$requests/one")" = "$onehead" ]
358grep -q "$requests/one" "$pushlog"
359if grep -q "c-$p" "$pushlog"; then exit 1; fi
360
361# Once a page is shown, the head approved is the head shown. When the
362# branch moves on origin, and is fetched, after the page is shown and
363# before the answer, approving refuses, names both heads, and publishes
364# nothing: for a plain branch under the top, for a plain branch at the top
365# of a stack, and for a plain branch alone. The request each would file is
366# absent first, so a filing would push.
367git -C "$stack" update-ref -d "$requests/one"
368: > "$pushlog"
369# shellcheck disable=SC2094 # the answers wait on the review's own output
370answer_after moved-one.out 'one, 1 of 3: \[n\]ext' "git -C '$stack' update-ref refs/heads/one $seed; git fetch -q origin" 'a\ns\n' \
371 | valley review pppp > moved-one.out 2>&1
372grep -q "not approved · origin/one moved from ${onehead:0:7}[0-9a-f]*, the head shown, to ${seed:0:7}" moved-one.out
373[ ! -s "$pushlog" ]
374if git -C "$stack" rev-parse --verify --quiet "$requests/one" > /dev/null; then exit 1; fi
375git -C "$stack" update-ref refs/heads/one "$onehead"
376git fetch -q origin
377
378git checkout --quiet -b four three
379echo "layer four" > four.md
380git add four.md
381git commit --quiet -m "layer four"
382git push --quiet origin four
383fourhead="$(git rev-parse four)"
384git checkout --quiet -b solo main
385echo "solo" > solo.md
386git add solo.md
387git commit --quiet -m "solo"
388git push --quiet origin solo
389solohead="$(git rev-parse solo)"
390git checkout --quiet main
391
392: > "$pushlog"
393# shellcheck disable=SC2094 # the answers wait on the review's own output
394{
395 printf 'n\nn\nn\n'
396 answer_after moved-four.out '\[a\]pprove all 4 \[v\]iew diff \[r\]eject four \[s\]top:' "git -C '$stack' update-ref refs/heads/four $seed; git fetch -q origin" 'a\ns\n'
397} | valley review four > moved-four.out 2>&1
398grep -q '^four is a stack of 4 changes' moved-four.out
399# From the third layer up, a lower layer's prompt counts what approving
400# there lands. The top's prompt names the top, and says approving it
401# lands all 4.
402grep -qF 'pppp, 3 of 4: [n]ext layer, [a]pprove the 3 up to pppp, [v]iew full diff, [s]top? ' moved-four.out
403grep -qF '[a]pprove all 4 [v]iew diff [r]eject four [s]top: ' moved-four.out
404grep -q "not approved · origin/four moved from ${fourhead:0:7}[0-9a-f]*, the head shown, to ${seed:0:7}" moved-four.out
405grep -q 'stopped reviewing four at change 4 of 4; nothing done' moved-four.out
406[ ! -s "$pushlog" ]
407if git -C "$stack" rev-parse --verify --quiet "$requests/four" > /dev/null; then exit 1; fi
408
409: > "$pushlog"
410# shellcheck disable=SC2094 # the answers wait on the review's own output
411answer_after moved-solo.out '^\[a\]pprove \[v\]iew diff' "git -C '$stack' update-ref refs/heads/solo $seed; git fetch -q origin" 'a\ns\n' \
412 | valley review solo > moved-solo.out 2>&1
413grep -q '^branch solo, log against origin/main:$' moved-solo.out
414grep -q "not approved · origin/solo moved from ${solohead:0:7}[0-9a-f]*, the head shown, to ${seed:0:7}" moved-solo.out
415[ ! -s "$pushlog" ]
416if git -C "$stack" rev-parse --verify --quiet "$requests/solo" > /dev/null; then exit 1; fi
417
418# ----------------------------------------------------------------------
419# A stack whose bottom does not fast-forward from main. The top gains a
420# second version, and main moves on under the stack.
421git checkout --quiet three
422echo "layer three, again" >> three.md
423git commit --quiet -am "layer three again"
424git push --quiet origin three
425threeagain="$(git rev-parse three)"
426valley ready --change "$p" three > /dev/null
427git checkout --quiet main
428echo "main moves on" > moved.md
429git add moved.md
430git commit --quiet -m "main moves on"
431git push --quiet origin main
432
433# Approving the top is refused before any check runs, at its active head
434# and at an earlier version's head, and the refusal names the rebase that
435# fixes it. Nothing is published.
436: > "$pushlog"
437for head in "$threeagain" "$three"; do
438 if valley approve --head "$head" pppp > stale-approve.out 2>&1; then exit 1; fi
439 grep -qF 'not approved · stack is behind main (includes qqqq)' stale-approve.out
440 grep -qx ' run: valley rebase pppp' stale-approve.out
441 if grep -q 'requires no check' stale-approve.out; then exit 1; fi
442done
443[ ! -s "$pushlog" ]
444# The bottom layer has no change under it, so it is approved as it is.
445valley approve one > stale-bottom.out 2>&1
446grep -qx '✓ one approved · no checks required · landing on main' stale-bottom.out
447[ "$(git -C "$stack" rev-parse "$requests/one")" = "$onehead" ]
448git -C "$stack" update-ref -d "$requests/one"
449
450# review asks, before any layer is paged, whether to rebase the stack.
451# [s]top ends it there, and nothing is done.
452: > "$pushlog"
453printf 's\n' | valley review pppp > stale-stop.out 2>&1
454grep -qxF 'pppp · stack of 3 · behind main' stale-stop.out
455grep -qF '[b]ase onto main [v]iew diff [s]kip: stopped reviewing pppp before its first layer; nothing done' stale-stop.out
456if grep -q 'change 1 of 3' stale-stop.out; then exit 1; fi
457[ ! -s "$pushlog" ]
458
459# [b]ase rebases the whole stack. The review then lists the stack again
460# and pages the rebased layers, and [a]pprove at the top files the
461# request at the top's rebased head. A note written in [v]iew diff before
462# the rebase, beside the bottom layer's line, is printed once, under the
463# top's branch, since the whole diff it was written in is the top's. The
464# answers wait for each page and prompt, as on a terminal every key goes
465# to whatever holds it.
466# shellcheck disable=SC2094 # the answers wait on the review's own output
467{
468 answer_after stale-review.log '\]kip: ' : 'v\n'
469 answer_after stale-review.log 'log against origin/main:' : vq
470 answer_after "$XDG_STATE_HOME/valley/notes/three.md" 'one wants a second line' : 'b\n'
471 answer_after stale-review.log '| change 1 of 3: one on main' : q
472 answer_after stale-review.log 'one, 1 of 3: \[n\]ext' : 'n\n'
473 answer_after stale-review.log 'qqqq, 2 of 3: \[n\]ext' : 'n\n'
474 answer_after stale-review.log '| change 3 of 3: pppp on qqqq' : q
475 answer_after stale-review.log '\]pprove all 3' : 'a\n'
476 wait_for stale-review.log 'note(s) on three'
477} | EDITOR="$TMPDIR/stack-editor" script -qec "valley review pppp" /dev/null > stale-review.log 2>&1
478sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' stale-review.log > stale-review.out
479grep -qF '[b]ase onto main [v]iew diff [s]kip: ' stale-review.out
480grep -qF 'the stack under pppp, bottom first:' stale-review.out
481git fetch --quiet origin
482newone="$(git rev-parse origin/one)" newthree="$(git rev-parse origin/three)"
483[ "$newone" != "$onehead" ]
484git merge-base --is-ancestor origin/main "$newone"
485awk '/change 1 of 3: /{ on = 1 } /change 2 of 3: /{ on = 0 } on' stale-review.out > stale-page1
486grep -q "^| $(git rev-parse --short "$newone") layer one$" stale-page1
487if grep -q "$(git rev-parse --short "$onehead")" stale-page1; then exit 1; fi
488grep -qF '[a]pprove all 3 [v]iew diff [r]eject pppp [s]top: ' stale-review.out
489[ "$(git -C "$stack" rev-parse "$requests/c-$p")" = "$newthree" ]
490[ "$(grep -c 'one wants a second line' "$XDG_STATE_HOME/valley/notes/three.md")" = 1 ]
491[ "$(grep -c '^ one.md:1: one wants a second line$' stale-review.out)" = 1 ]
492grep -A1 '^1 note(s) on three, appended to .*/valley/notes/three.md:$' stale-review.out \
493 | grep -qx ' one.md:1: one wants a second line'
494# qqqq's v1 was approved earlier, and the rebase gave it v2 with the same
495# file delta, so its layer is printed as rebased with the same file
496# changes, and not paged. pppp has no approved version, so its layer is
497# paged in full.
498grep -qx "qqqq v2 at [0-9a-f]*: rebased since you approved v1 at $(git rev-parse --short "$two"), with the same file changes\." stale-review.out
499awk '/change 2 of 3: /{ on = 1 } /change 3 of 3: /{ on = 0 } on' stale-review.out > stale-page2
500if grep -q '^| diff --git' stale-page2; then exit 1; fi
501awk '/change 3 of 3: /{ on = 1 } on' stale-review.out > stale-page3
502[ "$(grep '^| diff --git' stale-page3)" = '| diff --git a/three.md b/three.md' ]
503
504# ----------------------------------------------------------------------
505# A note taken on an edited comparison. v1 of a change is approved, and
506# v2 edits line 9 of its file, so review pages v2 as edited since v1.
507# The page ends with what v2 changes beyond v1 carried onto main, in git's
508# ordinary form, and the note written beside the edited line there keeps
509# its file and line. The view is styled on a terminal, through a
510# structure-preserving renderer and through less alone, while the buffer
511# the editor opens stays plain.
512git checkout --quiet -b edited main
513seq 1 12 | sed 's/^/line /' > lines.md
514git add lines.md
515git commit --quiet -m "add the lines"
516git push --quiet origin edited
517m=mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
518valley ready --change "$m" edited > /dev/null
519git -C "$stack" update-ref "$requests/c-$m" "$(git rev-parse edited)"
520sed -i 's/^line 9$/line 9 edited/' lines.md
521git commit --quiet -am "edit line 9"
522git push --quiet origin edited
523valley ready --change "$m" edited > /dev/null
524git checkout --quiet main
525cat > "$TMPDIR/changed-editor" << 'EOF'
526#!/bin/sh
527f="$1"
528case "$1" in +*) f="$2" ;; esac
529if grep -q "$(printf '\033')" "$f"; then touch "$TMPDIR/escaped-buffer"; fi
530sed -i "/^+line 9 edited\$/a $NOTE" "$f"
531EOF
532chmod +x "$TMPDIR/changed-editor"
533esc="$(printf '\033')"
534for pager in cat less; do
535 git config --global pager.diff "$pager"
536 # shellcheck disable=SC2094 # the answers wait on the review's own output
537 {
538 printf vq
539 answer_after "changed-$pager.log" '\]kip: ' : 's\n'
540 wait_for "changed-$pager.log" '1 note(s) on edited'
541 } | NOTE="why edit line 9, through $pager?" EDITOR="$TMPDIR/changed-editor" LESSCHARSET=utf-8 \
542 script -qec "valley review mmmm" /dev/null > "changed-$pager.log" 2>&1
543 sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' "changed-$pager.log" > changed-plain.log
544 grep -q "mmmm v2 at [0-9a-f]*: edited since you approved v1 at " changed-plain.log
545 grep -q "── what v2 changes beyond v1 carried onto main " changed-plain.log
546 grep -q "${esc}\[1mmmmm v2 at [0-9a-f]*: ${esc}\[33medited${esc}\[39m since" "changed-$pager.log"
547 grep -q "${esc}\[36m── what v2 changes beyond " "changed-$pager.log"
548 grep -qx "lines.md:9: why edit line 9, through $pager?" "$XDG_STATE_HOME/valley/notes/edited.md"
549 grep -q "^ lines.md:9: why edit line 9, through $pager?\$" changed-plain.log
550done
551[ ! -e "$TMPDIR/escaped-buffer" ]
552
553touch "$out"