gunk-dev/the-valley

review-notes.sh

source0b2daf24367fmain's published tip history
3.1 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
82touch "$out"