gunk-dev/the-valley

review-notes.sh

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