gunk-dev/the-valley

review-notes.sh

source7309e1d6b8bbmain's published tip history
23.8 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, its walk outcome interrupted, and nothing of
144# its own left in its TMPDIR. less takes the Ctrl-C itself, and the
145# review ends once less quits, before any prompt.
146# signalled <case> <driver>: review scratch on a terminal, with a TMPDIR
147# and a walk outcome of its own, its keys and signals sent by the driver,
148# which is given the session log and the review's pid file.
149signalled() {
150 local case="$1" status=0 tmp="$TMPDIR/$1-tmp" pid="$TMPDIR/$1.pid" outcome="$TMPDIR/$1.outcome"
151 mkdir "$tmp"
152 # shellcheck disable=SC2094 # the driver waits on the review's own output
153 "$2" "$case.log" "$pid" \
154 | TMPDIR="$tmp" VALLEY_WALK_OUTCOME="$outcome" \
155 script -qec "sh -c 'echo \$\$ > $pid; exec valley review scratch'" /dev/null \
156 > "$case.log" 2>&1 || status=$?
157 [ "$status" = 130 ]
158 [ "$(cat "$outcome")" = interrupted ]
159 [ -z "$(ls -A "$tmp")" ]
160}
161# ended <pid file> [<key>]: wait for the review to end, for a minute at
162# most, sending the key meanwhile, so a pager still open takes it.
163ended() {
164 local i=0
165 while kill -0 "$(cat "$1")" 2> /dev/null; do
166 if [ "$i" -ge 600 ]; then
167 kill -KILL "$(cat "$1")"
168 return 1
169 fi
170 if [ -n "${2:-}" ] && [ $((i % 5)) = 0 ]; then printf '%s' "$2"; fi
171 sleep 0.1
172 i=$((i + 1))
173 done
174}
175pager_ctrl_c() {
176 wait_for "$1" '| @@ ' || true
177 printf '\003'
178 ended "$2" q
179}
180prompt_term() {
181 wait_for "$1" '| @@ ' && printf q && wait_for "$1" '\]kip: ' || true
182 kill -TERM "$(cat "$2")"
183 ended "$2"
184}
185signalled pager-int pager_ctrl_c
186if grep -q 'kip: ' pager-int.log; then exit 1; fi
187signalled prompt-term prompt_term
188
189# Piped, review is what it always was: the diff on stdout,
190# the verdict from stdin, and no notes to take.
191printf 's\n' | valley review scratch > piped.log
192grep -q '^diff --git a/alpha.md b/alpha.md$' piped.log
193grep -q 'skipped scratch; nothing done' piped.log
194if grep -q 'note(s) on scratch' piped.log; then exit 1; fi
195
196# The pending list of branches titles each by the subject of its first
197# own commit, on the line under its name, and cuts a long one short with …
198# to fit 80 columns when stdout is no terminal. Nothing it prints there is
199# an escape code.
200valley pending --branches > pending.log
201grep -A1 '^scratch ' pending.log | grep -q '^ touch both files$'
202git checkout --quiet -b longsubject origin/main
203git commit --quiet --allow-empty -m "$(printf 'x%.0s' $(seq 1 80))"
204git push --quiet origin longsubject
205git checkout --quiet main
206valley pending --branches > pending.log
207grep -A1 '^longsubject ' pending.log | grep -qE '^ x{77}…$'
208if grep -q $'\x1b' pending.log; then exit 1; fi
209
210# On a terminal, pending fits the terminal's width and sets each name in
211# bold. With NO_COLOR set, it prints no escape code.
212script -qec "stty cols 100; valley pending --branches" /dev/null < /dev/null > tty.log 2>&1
213grep -q $'^\x1b\\[1mlongsubject\x1b\\[0m ' tty.log
214grep -qE $'^ x{80}\r?$' tty.log
215NO_COLOR=1 script -qec "stty cols 100; valley pending --branches" /dev/null < /dev/null > tty.log 2>&1
216grep -q '^longsubject ' tty.log
217if grep -q $'\x1b' tty.log; then exit 1; fi
218
219# ----------------------------------------------------------------------
220# A stack: a branch, then two changes with records, each on the one
221# below, in a project whose floor requires nothing, so approving files a
222# request with no evidence. Its origin logs every push it receives, which
223# is how "publishes nothing" is read.
224stack="$TMPDIR/stack.git"
225pushlog="$TMPDIR/pushlog"
226git init --quiet --bare "$stack"
227cat > "$stack/hooks/pre-receive" << EOF
228#!/bin/sh
229echo '--- push' >> $pushlog
230cat >> $pushlog
231EOF
232chmod +x "$stack/hooks/pre-receive"
233git init --quiet "$TMPDIR/stackwork"
234cd "$TMPDIR/stackwork" || exit 1
235git remote add origin "$stack"
236git config valley.principal operator
237mkdir -p policy/instance
238cat > policy/instance/floor.cue << 'EOF'
239package verification
240
241floor: {
242 classes: everything: {
243 paths: "**": true
244 requires: {}
245 }
246 unclassified: {}
247}
248EOF
249git add -A
250git commit --quiet -m seed
251git push --quiet -u origin main
252from=main
253for layer in one two three; do
254 git checkout --quiet -b "$layer" "$from"
255 echo "layer $layer" > "$layer.md"
256 git add -A
257 git commit --quiet -m "layer $layer" -m "Why layer $layer is its own change."
258 git push --quiet origin "$layer"
259 from="$layer"
260done
261two="$(git rev-parse two)" three="$(git rev-parse three)"
262q=qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq p=pppppppppppppppppppppppppppppppp
263valley ready --change "$q" two > /dev/null
264valley ready --change "$p" three > /dev/null
265git checkout --quiet main
266requests="refs/the-valley/integration-requests/main"
267
268# s at the first layer, on a terminal, with a note written in its diff:
269# the note is kept under the first layer's branch and printed at the end,
270# and nothing is published.
271cat > "$TMPDIR/stack-editor" << 'EOF'
272#!/bin/sh
273f="$1"
274case "$1" in +*) f="$2" ;; esac
275sed -i '/^+layer one$/a one wants a second line' "$f"
276EOF
277chmod +x "$TMPDIR/stack-editor"
278: > "$pushlog"
279# shellcheck disable=SC2094 # the answers wait on the review's own output
280{
281 printf vq
282 answer_after stack-tty.log 'one, 1 of 3: \[n\]ext layer, .*\[s\]top? ' : 's\n'
283 wait_for stack-tty.log '1 note(s) on one'
284} | EDITOR="$TMPDIR/stack-editor" script -qec "valley review pppp" /dev/null > stack-tty.log 2>&1
285sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' stack-tty.log > stack-plain.log
286grep -q 'pppp is a stack of 3 changes; reviewing from the bottom, each against the one below it:$' stack-plain.log
287grep -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
288grep -q '| change 1 of 3: one on main$' stack-plain.log
289grep -qF 'one, 1 of 3: [n]ext layer, [a]pprove one alone, [v]iew full diff, [s]top? ' stack-plain.log
290grep -q 'stopped reviewing pppp at change 1 of 3, one; nothing done' stack-plain.log
291if grep -q 'change 2 of 3' stack-plain.log; then exit 1; fi
292grep -qx 'one.md:1: one wants a second line' "$XDG_STATE_HOME/valley/notes/one.md"
293grep -q '1 note(s) on one, appended to .*/valley/notes/one.md:' stack-plain.log
294grep -q '^ one.md:1: one wants a second line$' stack-plain.log
295[ ! -e "$XDG_STATE_HOME/valley/notes/two.md" ]
296[ ! -s "$pushlog" ]
297
298# n, n, a: the layers are paged bottom up, each diff holds only its own
299# layer's file, and the top's request is filed at the top's head. Each
300# prompt names its layer and place, and what approving there lands.
301: > "$pushlog"
302printf 'n\nn\na\n' | valley review pppp > stack.out 2> stack.err
303grep -q '^ 1\. one (branch): layer one$' stack.out
304grep -q '^ 2\. qqqq (ready): layer two$' stack.out
305grep -q '^ 3\. pppp (ready): layer three$' stack.out
306grep -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
307grep -qF 'one, 1 of 3: [n]ext layer, [a]pprove one alone, [v]iew full diff, [s]top? ' stack.out
308grep -qF 'qqqq, 2 of 3: [n]ext layer, [a]pprove one and qqqq, [v]iew full diff, [s]top? ' stack.out
309grep -qF '[a]pprove all 3 [v]iew diff [r]eject pppp [s]top: ' stack.out
310# Piped, each page's first line follows the prompt before it.
311[ "$(grep -o 'change [0-9] of 3: [a-z]* on [a-z]*$' stack.out)" = "$(printf '%s\n' \
312 'change 1 of 3: one on main' 'change 2 of 3: qqqq on one' 'change 3 of 3: pppp on qqqq')" ]
313for n in 1 2 3; do
314 awk -v n="$n" '/change [0-9] of 3: /{ on = index($0, "change " n " of 3: ") > 0 } on' stack.out > "page$n"
315done
316[ "$(grep '^diff --git' page1)" = 'diff --git a/one.md b/one.md' ]
317[ "$(grep '^diff --git' page2)" = 'diff --git a/two.md b/two.md' ]
318[ "$(grep '^diff --git' page3)" = 'diff --git a/three.md b/three.md' ]
319# A layer's page shows its commit's whole message.
320grep -A1 '^[0-9a-f]\{7,\} layer two$' page2 | grep -qx ' Why layer two is its own change.'
321[ "$(git -C "$stack" rev-parse "$requests/c-$p")" = "$three" ]
322if git -C "$stack" rev-parse --verify --quiet "$requests/c-$q" > /dev/null; then exit 1; fi
323git -C "$stack" update-ref -d "$requests/c-$p"
324
325# a at the second layer files the second layer's request at its head, and
326# nothing for the top, whose review never starts: neither its page nor its
327# prompt is shown.
328: > "$pushlog"
329printf 'n\na\n' | valley review pppp > stack-two.out 2> stack-two.err
330grep -qF 'qqqq, 2 of 3: [n]ext layer, [a]pprove one and qqqq, [v]iew full diff, [s]top? ' stack-two.out
331[ "$(git -C "$stack" rev-parse "$requests/c-$q")" = "$two" ]
332if git -C "$stack" rev-parse --verify --quiet "$requests/c-$p" > /dev/null; then exit 1; fi
333grep -q "c-$q" "$pushlog"
334if grep -q "c-$p" "$pushlog"; then exit 1; fi
335if grep -q 'change 3 of 3' stack-two.out; then exit 1; fi
336if grep -q '3 of 3: ' stack-two.out; then exit 1; fi
337
338# A draft in the middle is named in the warning before anything is paged.
339# It is readied again after, since review offers no [a]pprove at or above
340# a draft, and the stacks below approve above it.
341valley unready qqqq > /dev/null 2>&1
342printf 's\n' | valley review pppp > stack-draft.out 2>&1
343grep -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
344valley ready --change "$q" two > /dev/null
345
346# A change with no stack under it pages and prompts as it always has.
347printf 's\n' | valley review one > stack-one.out 2>&1
348grep -q '^branch one, log against origin/main:$' stack-one.out
349grep -q '^\[a\]pprove \[v\]iew diff \[r\]eject \[s\]kip: skipped one; nothing done$' stack-one.out
350if grep -q 'stack of\|change 1 of' stack-one.out; then exit 1; fi
351
352seed="$(git rev-parse main)" onehead="$(git rev-parse one)"
353
354# [a]pprove at the bottom, on a plain branch under the top, files that
355# branch's head under its own request name, and nothing for the top.
356: > "$pushlog"
357printf 'a\n' | valley review pppp > stack-a-one.out 2>&1
358[ "$(git -C "$stack" rev-parse "$requests/one")" = "$onehead" ]
359grep -q "$requests/one" "$pushlog"
360if grep -q "c-$p" "$pushlog"; then exit 1; fi
361
362# Once a page is shown, the head approved is the head shown. When the
363# branch moves on origin, and is fetched, after the page is shown and
364# before the answer, approving refuses, names both heads, and publishes
365# nothing: for a plain branch under the top, for a plain branch at the top
366# of a stack, and for a plain branch alone. The request each would file is
367# absent first, so a filing would push.
368git -C "$stack" update-ref -d "$requests/one"
369: > "$pushlog"
370# shellcheck disable=SC2094 # the answers wait on the review's own output
371answer_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' \
372 | valley review pppp > moved-one.out 2>&1
373grep -q "not approved · origin/one moved from ${onehead:0:7}[0-9a-f]*, the head shown, to ${seed:0:7}" moved-one.out
374[ ! -s "$pushlog" ]
375if git -C "$stack" rev-parse --verify --quiet "$requests/one" > /dev/null; then exit 1; fi
376git -C "$stack" update-ref refs/heads/one "$onehead"
377git fetch -q origin
378
379git checkout --quiet -b four three
380echo "layer four" > four.md
381git add four.md
382git commit --quiet -m "layer four"
383git push --quiet origin four
384fourhead="$(git rev-parse four)"
385git checkout --quiet -b solo main
386echo "solo" > solo.md
387git add solo.md
388git commit --quiet -m "solo"
389git push --quiet origin solo
390solohead="$(git rev-parse solo)"
391git checkout --quiet main
392
393: > "$pushlog"
394# shellcheck disable=SC2094 # the answers wait on the review's own output
395{
396 printf 'n\nn\nn\n'
397 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'
398} | valley review four > moved-four.out 2>&1
399grep -q '^four is a stack of 4 changes' moved-four.out
400# From the third layer up, a lower layer's prompt counts what approving
401# there lands. The top's prompt names the top, and says approving it
402# lands all 4.
403grep -qF 'pppp, 3 of 4: [n]ext layer, [a]pprove the 3 up to pppp, [v]iew full diff, [s]top? ' moved-four.out
404grep -qF '[a]pprove all 4 [v]iew diff [r]eject four [s]top: ' moved-four.out
405grep -q "not approved · origin/four moved from ${fourhead:0:7}[0-9a-f]*, the head shown, to ${seed:0:7}" moved-four.out
406grep -q 'stopped reviewing four at change 4 of 4; nothing done' moved-four.out
407[ ! -s "$pushlog" ]
408if git -C "$stack" rev-parse --verify --quiet "$requests/four" > /dev/null; then exit 1; fi
409
410: > "$pushlog"
411# shellcheck disable=SC2094 # the answers wait on the review's own output
412answer_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' \
413 | valley review solo > moved-solo.out 2>&1
414grep -q '^branch solo, log against origin/main:$' moved-solo.out
415grep -q "not approved · origin/solo moved from ${solohead:0:7}[0-9a-f]*, the head shown, to ${seed:0:7}" moved-solo.out
416[ ! -s "$pushlog" ]
417if git -C "$stack" rev-parse --verify --quiet "$requests/solo" > /dev/null; then exit 1; fi
418
419# ----------------------------------------------------------------------
420# A stack whose bottom does not fast-forward from main. The top gains a
421# second version, and main moves on under the stack.
422git checkout --quiet three
423echo "layer three, again" >> three.md
424git commit --quiet -am "layer three again"
425git push --quiet origin three
426threeagain="$(git rev-parse three)"
427valley ready --change "$p" three > /dev/null
428git checkout --quiet main
429echo "main moves on" > moved.md
430git add moved.md
431git commit --quiet -m "main moves on"
432git push --quiet origin main
433
434# Approving the top is refused before any check runs, at its active head
435# and at an earlier version's head, and the refusal names the rebase that
436# fixes it. Nothing is published.
437: > "$pushlog"
438for head in "$threeagain" "$three"; do
439 if valley approve --head "$head" pppp > stale-approve.out 2>&1; then exit 1; fi
440 grep -qF 'not approved · stack is behind main (includes qqqq)' stale-approve.out
441 grep -qx ' run: valley rebase pppp' stale-approve.out
442 if grep -q 'requires no check' stale-approve.out; then exit 1; fi
443done
444[ ! -s "$pushlog" ]
445# The bottom layer has no change under it, so it is approved as it is.
446valley approve one > stale-bottom.out 2>&1
447grep -qx '✓ one approved · no checks required · landing on main' stale-bottom.out
448[ "$(git -C "$stack" rev-parse "$requests/one")" = "$onehead" ]
449git -C "$stack" update-ref -d "$requests/one"
450
451# review asks, before any layer is paged, whether to rebase the stack.
452# [s]top ends it there, and nothing is done.
453: > "$pushlog"
454printf 's\n' | valley review pppp > stale-stop.out 2>&1
455grep -qxF 'pppp · stack of 3 · behind main' stale-stop.out
456grep -qF '[b]ase onto main [v]iew diff [s]kip: stopped reviewing pppp before its first layer; nothing done' stale-stop.out
457if grep -q 'change 1 of 3' stale-stop.out; then exit 1; fi
458[ ! -s "$pushlog" ]
459
460# [b]ase rebases the whole stack. The review then lists the stack again
461# and pages the rebased layers, and [a]pprove at the top files the
462# request at the top's rebased head.
463printf 'b\nn\nn\na\n' | valley review pppp > stale-review.out 2>&1
464grep -qF '[b]ase onto main [v]iew diff [s]kip: ' stale-review.out
465grep -qF 'the stack under pppp, bottom first:' stale-review.out
466git fetch --quiet origin
467newone="$(git rev-parse origin/one)" newthree="$(git rev-parse origin/three)"
468[ "$newone" != "$onehead" ]
469git merge-base --is-ancestor origin/main "$newone"
470awk '/change 1 of 3: /{ on = 1 } /change 2 of 3: /{ on = 0 } on' stale-review.out > stale-page1
471grep -q "^$(git rev-parse --short "$newone") layer one$" stale-page1
472if grep -q "$(git rev-parse --short "$onehead")" stale-page1; then exit 1; fi
473grep -qF '[a]pprove all 3 [v]iew diff [r]eject pppp [s]top: ' stale-review.out
474[ "$(git -C "$stack" rev-parse "$requests/c-$p")" = "$newthree" ]
475# qqqq's v1 was approved earlier, and the rebase gave it v2 with the same
476# file delta, so its layer is printed as rebased with the same file
477# changes, and not paged. pppp has no approved version, so its layer is
478# paged in full.
479grep -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
480awk '/change 2 of 3: /{ on = 1 } /change 3 of 3: /{ on = 0 } on' stale-review.out > stale-page2
481if grep -q '^diff --git' stale-page2; then exit 1; fi
482awk '/change 3 of 3: /{ on = 1 } on' stale-review.out > stale-page3
483[ "$(grep '^diff --git' stale-page3)" = 'diff --git a/three.md b/three.md' ]
484
485# ----------------------------------------------------------------------
486# A note taken on an edited comparison. v1 of a change is approved, and
487# v2 edits line 9 of its file, so review pages v2 as edited since v1.
488# range-diff's hunks carry no line numbers, so the page ends with v2's
489# own diff of the file, and the note written beside the edited line there
490# keeps its file and line. The view is styled on a terminal, through a
491# structure-preserving renderer and through less alone, while the buffer
492# the editor opens stays plain.
493git checkout --quiet -b edited main
494seq 1 12 | sed 's/^/line /' > lines.md
495git add lines.md
496git commit --quiet -m "add the lines"
497git push --quiet origin edited
498m=mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
499valley ready --change "$m" edited > /dev/null
500git -C "$stack" update-ref "$requests/c-$m" "$(git rev-parse edited)"
501sed -i 's/^line 9$/line 9 edited/' lines.md
502git commit --quiet -am "edit line 9"
503git push --quiet origin edited
504valley ready --change "$m" edited > /dev/null
505git checkout --quiet main
506cat > "$TMPDIR/changed-editor" << 'EOF'
507#!/bin/sh
508f="$1"
509case "$1" in +*) f="$2" ;; esac
510if grep -q "$(printf '\033')" "$f"; then touch "$TMPDIR/escaped-buffer"; fi
511sed -i "/^+line 9 edited\$/a $NOTE" "$f"
512EOF
513chmod +x "$TMPDIR/changed-editor"
514esc="$(printf '\033')"
515for pager in cat less; do
516 git config --global pager.diff "$pager"
517 # shellcheck disable=SC2094 # the answers wait on the review's own output
518 {
519 printf vq
520 answer_after "changed-$pager.log" '\]kip: ' : 's\n'
521 wait_for "changed-$pager.log" '1 note(s) on edited'
522 } | NOTE="why edit line 9, through $pager?" EDITOR="$TMPDIR/changed-editor" LESSCHARSET=utf-8 \
523 script -qec "valley review mmmm" /dev/null > "changed-$pager.log" 2>&1
524 sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' "changed-$pager.log" > changed-plain.log
525 grep -q "mmmm v2 at [0-9a-f]*: edited since you approved v1 at " changed-plain.log
526 grep -q "── v2's diff of this file against main " changed-plain.log
527 grep -q "${esc}\[1mmmmm v2 at [0-9a-f]*: ${esc}\[33medited${esc}\[39m since" "changed-$pager.log"
528 grep -q "${esc}\[36m── git range-diff " "changed-$pager.log"
529 grep -qx "lines.md:9: why edit line 9, through $pager?" "$XDG_STATE_HOME/valley/notes/edited.md"
530 grep -q "^ lines.md:9: why edit line 9, through $pager?\$" changed-plain.log
531done
532[ ! -e "$TMPDIR/escaped-buffer" ]
533
534touch "$out"