gunk-dev/the-valley

review-notes.sh

sourceea258e0e0163main's published tip history
12.4 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 -q '| change 1 of 3: one on main$' stack-plain.log
170grep -q 'one: \[n\]ext, \[a\]pprove up to here, \[s\]kip?' stack-plain.log
171grep -q 'skipped pppp at change 1 of 3, one; nothing done' stack-plain.log
172if grep -q 'change 2 of 3' stack-plain.log; then exit 1; fi
173grep -qx 'one.md:1: one wants a second line' "$XDG_STATE_HOME/valley/notes/one.md"
174grep -q '1 note(s) on one, appended to .*/valley/notes/one.md:' stack-plain.log
175grep -q '^ one.md:1: one wants a second line$' stack-plain.log
176[ ! -e "$XDG_STATE_HOME/valley/notes/two.md" ]
177[ ! -s "$pushlog" ]
178
179# n, n, a: the layers are paged bottom up, each diff holds only its own
180# layer's file, and the top's request is filed at the top's head.
181: > "$pushlog"
182printf 'n\nn\na\n' | valley review pppp > stack.out 2> stack.err
183grep -q '^ 1\. one (branch): layer one$' stack.out
184grep -q '^ 2\. qqqq (ready): layer two$' stack.out
185grep -q '^ 3\. pppp (ready): layer three$' stack.out
186# Piped, each page's first line follows the prompt before it.
187[ "$(grep -o 'change [0-9] of 3: [a-z]* on [a-z]*$' stack.out)" = "$(printf '%s\n' \
188 'change 1 of 3: one on main' 'change 2 of 3: qqqq on one' 'change 3 of 3: pppp on qqqq')" ]
189for n in 1 2 3; do
190 awk -v n="$n" '/change [0-9] of 3: /{ on = index($0, "change " n " of 3: ") > 0 } on' stack.out > "page$n"
191done
192[ "$(grep '^diff --git' page1)" = 'diff --git a/one.md b/one.md' ]
193[ "$(grep '^diff --git' page2)" = 'diff --git a/two.md b/two.md' ]
194[ "$(grep '^diff --git' page3)" = 'diff --git a/three.md b/three.md' ]
195grep -q '^three: \[a\]pprove, \[r\]eject, \[s\]kip?' stack.out
196[ "$(git -C "$stack" rev-parse "$requests/c-$p")" = "$three" ]
197if git -C "$stack" rev-parse --verify --quiet "$requests/c-$q" > /dev/null; then exit 1; fi
198git -C "$stack" update-ref -d "$requests/c-$p"
199
200# a at the second layer files the second layer's request at its head, and
201# nothing for the top.
202: > "$pushlog"
203printf 'n\na\n' | valley review pppp > stack-two.out 2> stack-two.err
204[ "$(git -C "$stack" rev-parse "$requests/c-$q")" = "$two" ]
205if git -C "$stack" rev-parse --verify --quiet "$requests/c-$p" > /dev/null; then exit 1; fi
206grep -q "c-$q" "$pushlog"
207if grep -q "c-$p" "$pushlog"; then exit 1; fi
208if grep -q 'change 3 of 3' stack-two.out; then exit 1; fi
209
210# A draft in the middle is named in the warning before anything is paged.
211valley unready qqqq > /dev/null 2>&1
212printf 's\n' | valley review pppp > stack-draft.out 2>&1
213grep -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
214
215# A change with no stack under it pages and prompts as it always has.
216printf 's\n' | valley review one > stack-one.out 2>&1
217grep -q '^branch one, log against origin/main:$' stack-one.out
218grep -q '^one: \[a\]pprove, \[r\]eject, \[s\]kip? skipped one; nothing done$' stack-one.out
219if grep -q 'stack of\|change 1 of' stack-one.out; then exit 1; fi
220
221# answer_after <log> <prompt> <command> <answers>: wait until the review
222# writing to <log> has asked <prompt>, run <command>, then answer. A page
223# is shown before its prompt, so the command runs after the page is shown
224# and before the answer.
225answer_after() {
226 local i=0
227 until grep -q -- "$2" "$1" 2> /dev/null || [ "$i" -ge 600 ]; do
228 sleep 0.1
229 i=$((i + 1))
230 done
231 eval "$3"
232 printf '%b' "$4"
233}
234seed="$(git rev-parse main)" onehead="$(git rev-parse one)"
235
236# [a]pprove up to here on a plain branch under the top files that
237# branch's head under its own request name, and nothing for the top.
238: > "$pushlog"
239printf 'a\n' | valley review pppp > stack-a-one.out 2>&1
240[ "$(git -C "$stack" rev-parse "$requests/one")" = "$onehead" ]
241grep -q "$requests/one" "$pushlog"
242if grep -q "c-$p" "$pushlog"; then exit 1; fi
243
244# Once a page is shown, the head approved is the head shown. When the
245# branch moves on origin, and is fetched, after the page is shown and
246# before the answer, approving refuses, names both heads, and publishes
247# nothing: for a plain branch under the top, for a plain branch at the top
248# of a stack, and for a plain branch alone. The request each would file is
249# absent first, so a filing would push.
250git -C "$stack" update-ref -d "$requests/one"
251: > "$pushlog"
252# shellcheck disable=SC2094 # the answers wait on the review's own output
253answer_after moved-one.out 'one: \[n\]ext' "git -C '$stack' update-ref refs/heads/one $seed; git fetch -q origin" 'a\ns\n' \
254 | valley review pppp > moved-one.out 2>&1
255grep -q "origin/one is at $seed, and is no longer at $onehead, the head shown; nothing published" moved-one.out
256[ ! -s "$pushlog" ]
257if git -C "$stack" rev-parse --verify --quiet "$requests/one" > /dev/null; then exit 1; fi
258git -C "$stack" update-ref refs/heads/one "$onehead"
259git fetch -q origin
260
261git checkout --quiet -b four three
262echo "layer four" > four.md
263git add four.md
264git commit --quiet -m "layer four"
265git push --quiet origin four
266fourhead="$(git rev-parse four)"
267git checkout --quiet -b solo main
268echo "solo" > solo.md
269git add solo.md
270git commit --quiet -m "solo"
271git push --quiet origin solo
272solohead="$(git rev-parse solo)"
273git checkout --quiet main
274
275: > "$pushlog"
276# shellcheck disable=SC2094 # the answers wait on the review's own output
277{
278 printf 'n\nn\nn\n'
279 answer_after moved-four.out 'four: \[a\]pprove' "git -C '$stack' update-ref refs/heads/four $seed; git fetch -q origin" 'a\ns\n'
280} | valley review four > moved-four.out 2>&1
281grep -q '^four is a stack of 4 changes' moved-four.out
282grep -q "origin/four is at $seed, and is no longer at $fourhead, the head shown; nothing published" moved-four.out
283[ ! -s "$pushlog" ]
284if git -C "$stack" rev-parse --verify --quiet "$requests/four" > /dev/null; then exit 1; fi
285
286: > "$pushlog"
287# shellcheck disable=SC2094 # the answers wait on the review's own output
288answer_after moved-solo.out 'solo: \[a\]pprove' "git -C '$stack' update-ref refs/heads/solo $seed; git fetch -q origin" 'a\ns\n' \
289 | valley review solo > moved-solo.out 2>&1
290grep -q '^branch solo, log against origin/main:$' moved-solo.out
291grep -q "origin/solo is at $seed, and is no longer at $solohead, the head shown; nothing published" moved-solo.out
292[ ! -s "$pushlog" ]
293if git -C "$stack" rev-parse --verify --quiet "$requests/solo" > /dev/null; then exit 1; fi
294
295touch "$out"