gunk-dev/the-valley

review-notes.sh

source40320586268emain's published tip history
20.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.
14git init --quiet --bare "$TMPDIR/origin.git"
15git init --quiet "$TMPDIR/work"
16cd "$TMPDIR/work" || exit 1
17git remote add origin "$TMPDIR/origin.git"
18seq 1 20 | sed 's/^/alpha /' > alpha.md
19seq 1 10 | sed 's/^/beta /' > beta.md
20git add -A
21git commit --quiet -m seed
22git push --quiet -u origin main
23git checkout --quiet -b scratch
24sed -i -e '3s/.*/alpha 3 changed/' -e '15s/.*/alpha 15 changed/' alpha.md
25sed -i -e '5s/.*/beta 5 changed/' beta.md
26git commit --quiet -am "touch both files"
27git push --quiet origin scratch
28git checkout --quiet main
29git fetch --quiet origin
30
31# A pager filter that restructures the diff, as delta's
32# rendering does: if v were handed what less displays, every
33# anchor below would be wrong.
34git config --global pager.diff "sed -e 's/^/| /'"
35export EDITOR="$reviewer"
36mkdir -p "$XDG_STATE_HOME"
37
38# wait_for <log> <pattern>: wait until <log> shows <pattern>, and fail
39# after a minute. A review reads every key from the one terminal, so a
40# verdict typed before the prompt that takes it would go to whatever held
41# the terminal then, such as less. Each log waited on here is written by
42# one review only, so no earlier review's output can match.
43wait_for() {
44 local i=0
45 until grep -q -- "$2" "$1" 2> /dev/null; do
46 if [ "$i" -ge 600 ]; then
47 printf 'review-notes: %s never showed %s\n' "$1" "$2" >&2
48 return 1
49 fi
50 sleep 0.1
51 i=$((i + 1))
52 done
53}
54
55# answer_after <log> <prompt> <command> <answers>: wait until the review
56# writing to <log> has asked <prompt>, run <command>, then answer. A page
57# is shown before its prompt, so the command runs after the page is shown
58# and before the answer.
59answer_after() {
60 wait_for "$1" "$2"
61 eval "$3"
62 printf '%b' "$4"
63}
64
65# v opens the editor, RETURN dismisses less's LESSOPEN
66# warning, q leaves the pager, s is the verdict. The verdict
67# waits for its prompt, and the keys stay open until the
68# notes are printed.
69# shellcheck disable=SC2094 # the answers wait on the review's own output
70{
71 printf 'v\nq'
72 answer_after session.log 'scratch: \[a\]pprove, \[v\]iew full diff, \[r\]eject, \[s\]kip? ' : 's\n'
73 wait_for session.log '5 note(s) on scratch'
74} | script -qec "valley review scratch" /dev/null > session.log 2>&1
75sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' session.log > plain.log
76
77# The anchors, exactly: above every file, on a file header
78# above the first hunk, mid-hunk, in a second hunk whose
79# numbering starts elsewhere, and in a second file.
80diff -u - "$XDG_STATE_HOME/valley/notes/scratch.md" <<EOF
81## scratch at $(git rev-parse --short origin/scratch)
82
83(unanchored): a note above every file
84alpha.md: alpha reads like two files
85alpha.md:3: why is 3 changed at all?
86alpha.md:13: and this hunk is fine
87beta.md:5: beta wants the same treatment
88
89EOF
90
91# The editor was given the buffer valley wrote, not the
92# rendering less displayed, and given a line to open at.
93grep -qE 'editor got: \+[0-9]+ .*/review\.diff$' \
94 "$XDG_STATE_HOME/editor.log"
95# The filter did run: what the pager showed was its output.
96grep -q '^| @@ ' plain.log
97# The line the reviewer edited instead of inserting is gone,
98# and said so.
99grep -q 'changed or deleted diff line(s) ignored' plain.log
100if grep -q 'alpha SEVENTEEN' "$XDG_STATE_HOME/valley/notes/scratch.md"; then exit 1; fi
101
102# The verdict is unchanged, and the notes come after it.
103grep -q 'skipped scratch; nothing done' plain.log
104verdict="$(grep -n 'skipped scratch' plain.log | cut -d: -f1)"
105notes="$(grep -n '5 note(s) on scratch' plain.log | cut -d: -f1)"
106[ "$verdict" -lt "$notes" ]
107
108# Piped, review is what it always was: the diff on stdout,
109# the verdict from stdin, and no notes to take.
110printf 's\n' | valley review scratch > piped.log
111grep -q '^diff --git a/alpha.md b/alpha.md$' piped.log
112grep -q 'skipped scratch; nothing done' piped.log
113if grep -q 'note(s) on scratch' piped.log; then exit 1; fi
114
115# The pending list titles each branch by the subject of its first own
116# commit, on the line under its name, and cuts a long one short with … to
117# fit 80 columns when stdout is no terminal. Nothing it prints there is an
118# escape code.
119valley pending > pending.log
120grep -A1 '^scratch ' pending.log | grep -q '^ touch both files$'
121git checkout --quiet -b longsubject origin/main
122git commit --quiet --allow-empty -m "$(printf 'x%.0s' $(seq 1 80))"
123git push --quiet origin longsubject
124git checkout --quiet main
125valley pending > pending.log
126grep -A1 '^longsubject ' pending.log | grep -qE '^ x{77}…$'
127if grep -q $'\x1b' pending.log; then exit 1; fi
128
129# On a terminal, pending fits the terminal's width and sets each name in
130# bold. With NO_COLOR set, it prints no escape code.
131script -qec "stty cols 100; valley pending" /dev/null < /dev/null > tty.log 2>&1
132grep -q $'^\x1b\\[1mlongsubject\x1b\\[0m ' tty.log
133grep -qE $'^ x{80}\r?$' tty.log
134NO_COLOR=1 script -qec "stty cols 100; valley pending" /dev/null < /dev/null > tty.log 2>&1
135grep -q '^longsubject ' tty.log
136if grep -q $'\x1b' tty.log; then exit 1; fi
137
138# ----------------------------------------------------------------------
139# A stack: a branch, then two changes with records, each on the one
140# below, in a project whose floor requires nothing, so approving files a
141# request with no evidence. Its origin logs every push it receives, which
142# is how "publishes nothing" is read.
143stack="$TMPDIR/stack.git"
144pushlog="$TMPDIR/pushlog"
145git init --quiet --bare "$stack"
146cat > "$stack/hooks/pre-receive" << EOF
147#!/bin/sh
148echo '--- push' >> $pushlog
149cat >> $pushlog
150EOF
151chmod +x "$stack/hooks/pre-receive"
152git init --quiet "$TMPDIR/stackwork"
153cd "$TMPDIR/stackwork" || exit 1
154git remote add origin "$stack"
155git config valley.principal operator
156mkdir -p policy/instance
157cat > policy/instance/floor.cue << 'EOF'
158package verification
159
160floor: {
161 classes: everything: {
162 paths: "**": true
163 requires: {}
164 }
165 unclassified: {}
166}
167EOF
168git add -A
169git commit --quiet -m seed
170git push --quiet -u origin main
171from=main
172for layer in one two three; do
173 git checkout --quiet -b "$layer" "$from"
174 echo "layer $layer" > "$layer.md"
175 git add -A
176 git commit --quiet -m "layer $layer"
177 git push --quiet origin "$layer"
178 from="$layer"
179done
180two="$(git rev-parse two)" three="$(git rev-parse three)"
181q=qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq p=pppppppppppppppppppppppppppppppp
182valley ready --change "$q" two > /dev/null
183valley ready --change "$p" three > /dev/null
184git checkout --quiet main
185requests="refs/the-valley/integration-requests/main"
186
187# s at the first layer, on a terminal, with a note written in its diff:
188# the note is kept under the first layer's branch and printed at the end,
189# and nothing is published.
190cat > "$TMPDIR/stack-editor" << 'EOF'
191#!/bin/sh
192f="$1"
193case "$1" in +*) f="$2" ;; esac
194sed -i '/^+layer one$/a one wants a second line' "$f"
195EOF
196chmod +x "$TMPDIR/stack-editor"
197: > "$pushlog"
198# shellcheck disable=SC2094 # the answers wait on the review's own output
199{
200 printf 'v\nq'
201 answer_after stack-tty.log 'one, 1 of 3: \[n\]ext layer, .*\[s\]top? ' : 's\n'
202 wait_for stack-tty.log '1 note(s) on one'
203} | EDITOR="$TMPDIR/stack-editor" script -qec "valley review pppp" /dev/null > stack-tty.log 2>&1
204sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' stack-tty.log > stack-plain.log
205grep -q 'pppp is a stack of 3 changes; reviewing from the bottom, each against the one below it:$' stack-plain.log
206grep -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
207grep -q '| change 1 of 3: one on main$' stack-plain.log
208grep -qF 'one, 1 of 3: [n]ext layer, [a]pprove one alone, [v]iew full diff, [s]top? ' stack-plain.log
209grep -q 'stopped reviewing pppp at change 1 of 3, one; nothing done' stack-plain.log
210if grep -q 'change 2 of 3' stack-plain.log; then exit 1; fi
211grep -qx 'one.md:1: one wants a second line' "$XDG_STATE_HOME/valley/notes/one.md"
212grep -q '1 note(s) on one, appended to .*/valley/notes/one.md:' stack-plain.log
213grep -q '^ one.md:1: one wants a second line$' stack-plain.log
214[ ! -e "$XDG_STATE_HOME/valley/notes/two.md" ]
215[ ! -s "$pushlog" ]
216
217# n, n, a: the layers are paged bottom up, each diff holds only its own
218# layer's file, and the top's request is filed at the top's head. Each
219# prompt names its layer and place, and what approving there lands.
220: > "$pushlog"
221printf 'n\nn\na\n' | valley review pppp > stack.out 2> stack.err
222grep -q '^ 1\. one (branch): layer one$' stack.out
223grep -q '^ 2\. qqqq (ready): layer two$' stack.out
224grep -q '^ 3\. pppp (ready): layer three$' stack.out
225grep -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
226grep -qF 'one, 1 of 3: [n]ext layer, [a]pprove one alone, [v]iew full diff, [s]top? ' stack.out
227grep -qF 'qqqq, 2 of 3: [n]ext layer, [a]pprove one and qqqq, [v]iew full diff, [s]top? ' stack.out
228grep -qF 'pppp, 3 of 3: [a]pprove all 3, [v]iew full diff, [r]eject pppp, [s]top? ' stack.out
229# Piped, each page's first line follows the prompt before it.
230[ "$(grep -o 'change [0-9] of 3: [a-z]* on [a-z]*$' stack.out)" = "$(printf '%s\n' \
231 'change 1 of 3: one on main' 'change 2 of 3: qqqq on one' 'change 3 of 3: pppp on qqqq')" ]
232for n in 1 2 3; do
233 awk -v n="$n" '/change [0-9] of 3: /{ on = index($0, "change " n " of 3: ") > 0 } on' stack.out > "page$n"
234done
235[ "$(grep '^diff --git' page1)" = 'diff --git a/one.md b/one.md' ]
236[ "$(grep '^diff --git' page2)" = 'diff --git a/two.md b/two.md' ]
237[ "$(grep '^diff --git' page3)" = 'diff --git a/three.md b/three.md' ]
238[ "$(git -C "$stack" rev-parse "$requests/c-$p")" = "$three" ]
239if git -C "$stack" rev-parse --verify --quiet "$requests/c-$q" > /dev/null; then exit 1; fi
240git -C "$stack" update-ref -d "$requests/c-$p"
241
242# a at the second layer files the second layer's request at its head, and
243# nothing for the top, whose review never starts: neither its page nor its
244# prompt is shown.
245: > "$pushlog"
246printf 'n\na\n' | valley review pppp > stack-two.out 2> stack-two.err
247grep -qF 'qqqq, 2 of 3: [n]ext layer, [a]pprove one and qqqq, [v]iew full diff, [s]top? ' stack-two.out
248[ "$(git -C "$stack" rev-parse "$requests/c-$q")" = "$two" ]
249if git -C "$stack" rev-parse --verify --quiet "$requests/c-$p" > /dev/null; then exit 1; fi
250grep -q "c-$q" "$pushlog"
251if grep -q "c-$p" "$pushlog"; then exit 1; fi
252if grep -q 'change 3 of 3' stack-two.out; then exit 1; fi
253if grep -q '3 of 3: ' stack-two.out; then exit 1; fi
254
255# A draft in the middle is named in the warning before anything is paged.
256# It is readied again after, since review offers no [a]pprove at or above
257# a draft, and the stacks below approve above it.
258valley unready qqqq > /dev/null 2>&1
259printf 's\n' | valley review pppp > stack-draft.out 2>&1
260grep -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
261valley ready --change "$q" two > /dev/null
262
263# A change with no stack under it pages and prompts as it always has.
264printf 's\n' | valley review one > stack-one.out 2>&1
265grep -q '^branch one, log against origin/main:$' stack-one.out
266grep -q '^one: \[a\]pprove, \[v\]iew full diff, \[r\]eject, \[s\]kip? skipped one; nothing done$' stack-one.out
267if grep -q 'stack of\|change 1 of' stack-one.out; then exit 1; fi
268
269seed="$(git rev-parse main)" onehead="$(git rev-parse one)"
270
271# [a]pprove at the bottom, on a plain branch under the top, files that
272# branch's head under its own request name, and nothing for the top.
273: > "$pushlog"
274printf 'a\n' | valley review pppp > stack-a-one.out 2>&1
275[ "$(git -C "$stack" rev-parse "$requests/one")" = "$onehead" ]
276grep -q "$requests/one" "$pushlog"
277if grep -q "c-$p" "$pushlog"; then exit 1; fi
278
279# Once a page is shown, the head approved is the head shown. When the
280# branch moves on origin, and is fetched, after the page is shown and
281# before the answer, approving refuses, names both heads, and publishes
282# nothing: for a plain branch under the top, for a plain branch at the top
283# of a stack, and for a plain branch alone. The request each would file is
284# absent first, so a filing would push.
285git -C "$stack" update-ref -d "$requests/one"
286: > "$pushlog"
287# shellcheck disable=SC2094 # the answers wait on the review's own output
288answer_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' \
289 | valley review pppp > moved-one.out 2>&1
290grep -q "origin/one is at $seed, and is no longer at $onehead, the head shown; nothing published" moved-one.out
291[ ! -s "$pushlog" ]
292if git -C "$stack" rev-parse --verify --quiet "$requests/one" > /dev/null; then exit 1; fi
293git -C "$stack" update-ref refs/heads/one "$onehead"
294git fetch -q origin
295
296git checkout --quiet -b four three
297echo "layer four" > four.md
298git add four.md
299git commit --quiet -m "layer four"
300git push --quiet origin four
301fourhead="$(git rev-parse four)"
302git checkout --quiet -b solo main
303echo "solo" > solo.md
304git add solo.md
305git commit --quiet -m "solo"
306git push --quiet origin solo
307solohead="$(git rev-parse solo)"
308git checkout --quiet main
309
310: > "$pushlog"
311# shellcheck disable=SC2094 # the answers wait on the review's own output
312{
313 printf 'n\nn\nn\n'
314 answer_after moved-four.out 'four, 4 of 4: \[a\]pprove all 4, \[v\]iew full diff, \[r\]eject four, \[s\]top?' "git -C '$stack' update-ref refs/heads/four $seed; git fetch -q origin" 'a\ns\n'
315} | valley review four > moved-four.out 2>&1
316grep -q '^four is a stack of 4 changes' moved-four.out
317# From the third layer up, a lower layer's prompt counts what approving
318# there lands. The top's prompt names the top, and says approving it
319# lands all 4.
320grep -qF 'pppp, 3 of 4: [n]ext layer, [a]pprove the 3 up to pppp, [v]iew full diff, [s]top? ' moved-four.out
321grep -qF 'four, 4 of 4: [a]pprove all 4, [v]iew full diff, [r]eject four, [s]top? ' moved-four.out
322grep -q "origin/four is at $seed, and is no longer at $fourhead, the head shown; nothing published" moved-four.out
323grep -q 'stopped reviewing four at change 4 of 4; nothing done' moved-four.out
324[ ! -s "$pushlog" ]
325if git -C "$stack" rev-parse --verify --quiet "$requests/four" > /dev/null; then exit 1; fi
326
327: > "$pushlog"
328# shellcheck disable=SC2094 # the answers wait on the review's own output
329answer_after moved-solo.out 'solo: \[a\]pprove' "git -C '$stack' update-ref refs/heads/solo $seed; git fetch -q origin" 'a\ns\n' \
330 | valley review solo > moved-solo.out 2>&1
331grep -q '^branch solo, log against origin/main:$' moved-solo.out
332grep -q "origin/solo is at $seed, and is no longer at $solohead, the head shown; nothing published" moved-solo.out
333[ ! -s "$pushlog" ]
334if git -C "$stack" rev-parse --verify --quiet "$requests/solo" > /dev/null; then exit 1; fi
335
336# ----------------------------------------------------------------------
337# A stack whose bottom does not fast-forward from main. The top gains a
338# second version, and main moves on under the stack.
339git checkout --quiet three
340echo "layer three, again" >> three.md
341git commit --quiet -am "layer three again"
342git push --quiet origin three
343threeagain="$(git rev-parse three)"
344valley ready --change "$p" three > /dev/null
345git checkout --quiet main
346echo "main moves on" > moved.md
347git add moved.md
348git commit --quiet -m "main moves on"
349git push --quiet origin main
350
351# Approving the top is refused before any check runs, at its active head
352# and at an earlier version's head, and the refusal names the rebase that
353# fixes it. Nothing is published.
354: > "$pushlog"
355for head in "$threeagain" "$three"; do
356 if valley approve --head "$head" pppp > stale-approve.out 2>&1; then exit 1; fi
357 grep -qF 'does not fast-forward from main, and the commits it would bring hold a version of qqqq, not landed.' stale-approve.out
358 grep -qF 'nothing published. Rebase the stack first: valley rebase pppp' stale-approve.out
359 if grep -q 'requires no check' stale-approve.out; then exit 1; fi
360done
361[ ! -s "$pushlog" ]
362# The bottom layer has no change under it, so it is approved as it is.
363valley approve one > stale-bottom.out 2>&1
364grep -q 'requires no check' stale-bottom.out
365[ "$(git -C "$stack" rev-parse "$requests/one")" = "$onehead" ]
366git -C "$stack" update-ref -d "$requests/one"
367
368# review asks, before any layer is paged, whether to rebase the stack.
369# [s]top ends it there, and nothing is done.
370: > "$pushlog"
371printf 's\n' | valley review pppp > stale-stop.out 2>&1
372grep -qF 'one, at the bottom, does not fast-forward from main: [b]ase all 3 onto main, [s]top? stopped reviewing pppp before its first layer; nothing done' stale-stop.out
373if grep -q 'change 1 of 3' stale-stop.out; then exit 1; fi
374[ ! -s "$pushlog" ]
375
376# [b]ase rebases the whole stack. The review then lists the stack again
377# and pages the rebased layers, and [a]pprove at the top files the
378# request at the top's rebased head.
379printf 'b\nn\nn\na\n' | valley review pppp > stale-review.out 2>&1
380grep -qF 'one, at the bottom, does not fast-forward from main: [b]ase all 3 onto main, [s]top? ' stale-review.out
381grep -qF 'the stack under pppp, bottom first:' stale-review.out
382git fetch --quiet origin
383newone="$(git rev-parse origin/one)" newthree="$(git rev-parse origin/three)"
384[ "$newone" != "$onehead" ]
385git merge-base --is-ancestor origin/main "$newone"
386awk '/change 1 of 3: /{ on = 1 } /change 2 of 3: /{ on = 0 } on' stale-review.out > stale-page1
387grep -q "^$(git rev-parse --short "$newone") layer one$" stale-page1
388if grep -q "$(git rev-parse --short "$onehead")" stale-page1; then exit 1; fi
389grep -qF 'pppp, 3 of 3: [a]pprove all 3, [v]iew full diff, [r]eject pppp, [s]top? ' stale-review.out
390[ "$(git -C "$stack" rev-parse "$requests/c-$p")" = "$newthree" ]
391# qqqq's v1 was approved earlier, and the rebase gave it v2 with the same
392# file delta, so its layer is printed as a clean restack of v1, and not
393# paged. pppp has no approved version, so its layer is paged in full.
394grep -qx "qqqq v2 at [0-9a-f]*: clean restack of approved v1 at $(git rev-parse --short "$two")\." stale-review.out
395awk '/change 2 of 3: /{ on = 1 } /change 3 of 3: /{ on = 0 } on' stale-review.out > stale-page2
396if grep -q '^diff --git' stale-page2; then exit 1; fi
397awk '/change 3 of 3: /{ on = 1 } on' stale-review.out > stale-page3
398[ "$(grep '^diff --git' stale-page3)" = 'diff --git a/three.md b/three.md' ]
399
400# ----------------------------------------------------------------------
401# A note taken on a changed comparison. v1 of a change is approved, and
402# v2 edits line 9 of its file, so review pages v2 as changed since v1.
403# range-diff's hunks carry no line numbers, so the page ends with v2's
404# own diff of the file, and the note written beside the edited line there
405# keeps its file and line.
406git checkout --quiet -b edited main
407seq 1 12 | sed 's/^/line /' > lines.md
408git add lines.md
409git commit --quiet -m "add the lines"
410git push --quiet origin edited
411m=mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
412valley ready --change "$m" edited > /dev/null
413git -C "$stack" update-ref "$requests/c-$m" "$(git rev-parse edited)"
414sed -i 's/^line 9$/line 9 edited/' lines.md
415git commit --quiet -am "edit line 9"
416git push --quiet origin edited
417valley ready --change "$m" edited > /dev/null
418git checkout --quiet main
419cat > "$TMPDIR/changed-editor" << 'EOF'
420#!/bin/sh
421f="$1"
422case "$1" in +*) f="$2" ;; esac
423sed -i '/^+line 9 edited$/a why edit line 9?' "$f"
424EOF
425chmod +x "$TMPDIR/changed-editor"
426# shellcheck disable=SC2094 # the answers wait on the review's own output
427{
428 printf 'v\nq'
429 answer_after changed-tty.log '\[s\]kip? ' : 's\n'
430 wait_for changed-tty.log '1 note(s) on edited'
431} | EDITOR="$TMPDIR/changed-editor" script -qec "valley review mmmm" /dev/null > changed-tty.log 2>&1
432sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' changed-tty.log > changed-plain.log
433grep -q "| mmmm v2 at [0-9a-f]*: changed since approved v1 at " changed-plain.log
434grep -qx 'lines.md:9: why edit line 9?' "$XDG_STATE_HOME/valley/notes/edited.md"
435grep -q '^ lines.md:9: why edit line 9?$' changed-plain.log
436
437touch "$out"