| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" TERM=xterm |
| 5 | export GIT_CONFIG_NOSYSTEM=1 |
| 6 | unset VISUAL || true |
| 7 | git config --global user.name valley-check |
| 8 | git config --global user.email valley-check@localhost |
| 9 | git 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. |
| 16 | git init --quiet --bare "$TMPDIR/origin.git" |
| 17 | git init --quiet "$TMPDIR/work" |
| 18 | cd "$TMPDIR/work" || exit 1 |
| 19 | git remote add origin "$TMPDIR/origin.git" |
| 20 | seq 1 20 | sed 's/^/alpha /' > alpha.md |
| 21 | seq 1 10 | sed 's/^/beta /' > beta.md |
| 22 | git add -A |
| 23 | git commit --quiet -m seed |
| 24 | git push --quiet -u origin main |
| 25 | git checkout --quiet -b scratch |
| 26 | sed -i -e '3s/.*/alpha 3 changed/' -e '15s/.*/alpha 15 changed/' alpha.md |
| 27 | sed -i -e '5s/.*/beta 5 changed/' beta.md |
| 28 | git commit --quiet -am "touch both files" -m "Both files change, far enough apart |
| 29 | for three hunks. |
| 30 | |
| 31 | diff --git a/body.md b/body.md" |
| 32 | git push --quiet origin scratch |
| 33 | git checkout --quiet main |
| 34 | git 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. |
| 39 | git config --global pager.diff "sed -e 's/^/| /'" |
| 40 | export EDITOR="$reviewer" |
| 41 | mkdir -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. |
| 48 | wait_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. |
| 64 | answer_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 |
| 87 | sed -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. |
| 93 | diff -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 |
| 98 | alpha.md: alpha reads like two files |
| 99 | alpha.md:3: why is 3 changed at all? |
| 100 | alpha.md:13: and this hunk is fine |
| 101 | beta.md:5: beta wants the same treatment |
| 102 | |
| 103 | EOF |
| 104 | |
| 105 | # The editor was given the buffer valley wrote, not the |
| 106 | # rendering less displayed, and given a line to open at. |
| 107 | grep -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. |
| 111 | grep -q '^| @@ ' plain.log |
| 112 | if 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. |
| 115 | grep -m1 -A2 '^| [0-9a-f]\{7,\} touch both files$' plain.log | sed 1d > message.log |
| 116 | diff -u - message.log << 'EOF' |
| 117 | | Both files change, far enough apart |
| 118 | | for three hunks. |
| 119 | EOF |
| 120 | |
| 121 | # q alone leaves the pager, and the editor never runs. |
| 122 | rm "$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" ] |
| 130 | if 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. |
| 133 | grep -q 'changed or deleted diff line(s) ignored' plain.log |
| 134 | if 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. |
| 137 | grep -q 'skipped scratch; nothing done' plain.log |
| 138 | verdict="$(grep -n 'skipped scratch' plain.log | cut -d: -f1)" |
| 139 | notes="$(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. |
| 149 | signalled() { |
| 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. |
| 163 | ended() { |
| 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 | } |
| 175 | pager_ctrl_c() { |
| 176 | wait_for "$1" '| @@ ' || true |
| 177 | printf '\003' |
| 178 | ended "$2" q |
| 179 | } |
| 180 | prompt_term() { |
| 181 | wait_for "$1" '| @@ ' && printf q && wait_for "$1" '\]kip: ' || true |
| 182 | kill -TERM "$(cat "$2")" |
| 183 | ended "$2" |
| 184 | } |
| 185 | signalled pager-int pager_ctrl_c |
| 186 | if grep -q 'kip: ' pager-int.log; then exit 1; fi |
| 187 | signalled 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. |
| 191 | printf 's\n' | valley review scratch > piped.log |
| 192 | grep -q '^diff --git a/alpha.md b/alpha.md$' piped.log |
| 193 | grep -q 'skipped scratch; nothing done' piped.log |
| 194 | if 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. |
| 200 | valley pending --branches > pending.log |
| 201 | grep -A1 '^scratch ' pending.log | grep -q '^ touch both files$' |
| 202 | git checkout --quiet -b longsubject origin/main |
| 203 | git commit --quiet --allow-empty -m "$(printf 'x%.0s' $(seq 1 80))" |
| 204 | git push --quiet origin longsubject |
| 205 | git checkout --quiet main |
| 206 | valley pending --branches > pending.log |
| 207 | grep -A1 '^longsubject ' pending.log | grep -qE '^ x{77}…$' |
| 208 | if 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. |
| 212 | script -qec "stty cols 100; valley pending --branches" /dev/null < /dev/null > tty.log 2>&1 |
| 213 | grep -q $'^\x1b\\[1mlongsubject\x1b\\[0m ' tty.log |
| 214 | grep -qE $'^ x{80}\r?$' tty.log |
| 215 | NO_COLOR=1 script -qec "stty cols 100; valley pending --branches" /dev/null < /dev/null > tty.log 2>&1 |
| 216 | grep -q '^longsubject ' tty.log |
| 217 | if 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. |
| 224 | stack="$TMPDIR/stack.git" |
| 225 | pushlog="$TMPDIR/pushlog" |
| 226 | git init --quiet --bare "$stack" |
| 227 | cat > "$stack/hooks/pre-receive" << EOF |
| 228 | #!/bin/sh |
| 229 | echo '--- push' >> $pushlog |
| 230 | cat >> $pushlog |
| 231 | EOF |
| 232 | chmod +x "$stack/hooks/pre-receive" |
| 233 | git init --quiet "$TMPDIR/stackwork" |
| 234 | cd "$TMPDIR/stackwork" || exit 1 |
| 235 | git remote add origin "$stack" |
| 236 | git config valley.principal operator |
| 237 | mkdir -p policy/instance |
| 238 | cat > policy/instance/floor.cue << 'EOF' |
| 239 | package verification |
| 240 | |
| 241 | floor: { |
| 242 | classes: everything: { |
| 243 | paths: "**": true |
| 244 | requires: {} |
| 245 | } |
| 246 | unclassified: {} |
| 247 | } |
| 248 | EOF |
| 249 | git add -A |
| 250 | git commit --quiet -m seed |
| 251 | git push --quiet -u origin main |
| 252 | from=main |
| 253 | for 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" |
| 260 | done |
| 261 | two="$(git rev-parse two)" three="$(git rev-parse three)" |
| 262 | q=qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq p=pppppppppppppppppppppppppppppppp |
| 263 | valley ready --change "$q" two > /dev/null |
| 264 | valley ready --change "$p" three > /dev/null |
| 265 | git checkout --quiet main |
| 266 | requests="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. |
| 271 | cat > "$TMPDIR/stack-editor" << 'EOF' |
| 272 | #!/bin/sh |
| 273 | f="$1" |
| 274 | case "$1" in +*) f="$2" ;; esac |
| 275 | sed -i '/^+layer one$/a one wants a second line' "$f" |
| 276 | EOF |
| 277 | chmod +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 |
| 285 | sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' stack-tty.log > stack-plain.log |
| 286 | grep -q 'pppp is a stack of 3 changes; reviewing from the bottom, each against the one below it:$' stack-plain.log |
| 287 | grep -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 |
| 288 | grep -q '| change 1 of 3: one on main$' stack-plain.log |
| 289 | grep -qF 'one, 1 of 3: [n]ext layer, [a]pprove one alone, [v]iew full diff, [s]top? ' stack-plain.log |
| 290 | grep -q 'stopped reviewing pppp at change 1 of 3, one; nothing done' stack-plain.log |
| 291 | if grep -q 'change 2 of 3' stack-plain.log; then exit 1; fi |
| 292 | grep -qx 'one.md:1: one wants a second line' "$XDG_STATE_HOME/valley/notes/one.md" |
| 293 | grep -q '1 note(s) on one, appended to .*/valley/notes/one.md:' stack-plain.log |
| 294 | grep -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" |
| 302 | printf 'n\nn\na\n' | valley review pppp > stack.out 2> stack.err |
| 303 | grep -q '^ 1\. one (branch): layer one$' stack.out |
| 304 | grep -q '^ 2\. qqqq (ready): layer two$' stack.out |
| 305 | grep -q '^ 3\. pppp (ready): layer three$' stack.out |
| 306 | grep -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 |
| 307 | grep -qF 'one, 1 of 3: [n]ext layer, [a]pprove one alone, [v]iew full diff, [s]top? ' stack.out |
| 308 | grep -qF 'qqqq, 2 of 3: [n]ext layer, [a]pprove one and qqqq, [v]iew full diff, [s]top? ' stack.out |
| 309 | grep -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')" ] |
| 313 | for 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" |
| 315 | done |
| 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. |
| 320 | grep -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" ] |
| 322 | if git -C "$stack" rev-parse --verify --quiet "$requests/c-$q" > /dev/null; then exit 1; fi |
| 323 | git -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" |
| 329 | printf 'n\na\n' | valley review pppp > stack-two.out 2> stack-two.err |
| 330 | grep -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" ] |
| 332 | if git -C "$stack" rev-parse --verify --quiet "$requests/c-$p" > /dev/null; then exit 1; fi |
| 333 | grep -q "c-$q" "$pushlog" |
| 334 | if grep -q "c-$p" "$pushlog"; then exit 1; fi |
| 335 | if grep -q 'change 3 of 3' stack-two.out; then exit 1; fi |
| 336 | if 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. |
| 341 | valley unready qqqq > /dev/null 2>&1 |
| 342 | printf 's\n' | valley review pppp > stack-draft.out 2>&1 |
| 343 | grep -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 |
| 344 | valley ready --change "$q" two > /dev/null |
| 345 | |
| 346 | # A change with no stack under it pages and prompts as it always has. |
| 347 | printf 's\n' | valley review one > stack-one.out 2>&1 |
| 348 | grep -q '^branch one, log against origin/main:$' stack-one.out |
| 349 | grep -q '^\[a\]pprove \[v\]iew diff \[r\]eject \[s\]kip: skipped one; nothing done$' stack-one.out |
| 350 | if grep -q 'stack of\|change 1 of' stack-one.out; then exit 1; fi |
| 351 | |
| 352 | seed="$(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" |
| 357 | printf 'a\n' | valley review pppp > stack-a-one.out 2>&1 |
| 358 | [ "$(git -C "$stack" rev-parse "$requests/one")" = "$onehead" ] |
| 359 | grep -q "$requests/one" "$pushlog" |
| 360 | if 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. |
| 368 | git -C "$stack" update-ref -d "$requests/one" |
| 369 | : > "$pushlog" |
| 370 | # shellcheck disable=SC2094 # the answers wait on the review's own output |
| 371 | answer_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 |
| 373 | grep -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" ] |
| 375 | if git -C "$stack" rev-parse --verify --quiet "$requests/one" > /dev/null; then exit 1; fi |
| 376 | git -C "$stack" update-ref refs/heads/one "$onehead" |
| 377 | git fetch -q origin |
| 378 | |
| 379 | git checkout --quiet -b four three |
| 380 | echo "layer four" > four.md |
| 381 | git add four.md |
| 382 | git commit --quiet -m "layer four" |
| 383 | git push --quiet origin four |
| 384 | fourhead="$(git rev-parse four)" |
| 385 | git checkout --quiet -b solo main |
| 386 | echo "solo" > solo.md |
| 387 | git add solo.md |
| 388 | git commit --quiet -m "solo" |
| 389 | git push --quiet origin solo |
| 390 | solohead="$(git rev-parse solo)" |
| 391 | git 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 |
| 399 | grep -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. |
| 403 | grep -qF 'pppp, 3 of 4: [n]ext layer, [a]pprove the 3 up to pppp, [v]iew full diff, [s]top? ' moved-four.out |
| 404 | grep -qF '[a]pprove all 4 [v]iew diff [r]eject four [s]top: ' moved-four.out |
| 405 | grep -q "not approved · origin/four moved from ${fourhead:0:7}[0-9a-f]*, the head shown, to ${seed:0:7}" moved-four.out |
| 406 | grep -q 'stopped reviewing four at change 4 of 4; nothing done' moved-four.out |
| 407 | [ ! -s "$pushlog" ] |
| 408 | if 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 |
| 412 | answer_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 |
| 414 | grep -q '^branch solo, log against origin/main:$' moved-solo.out |
| 415 | grep -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" ] |
| 417 | if 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. |
| 422 | git checkout --quiet three |
| 423 | echo "layer three, again" >> three.md |
| 424 | git commit --quiet -am "layer three again" |
| 425 | git push --quiet origin three |
| 426 | threeagain="$(git rev-parse three)" |
| 427 | valley ready --change "$p" three > /dev/null |
| 428 | git checkout --quiet main |
| 429 | echo "main moves on" > moved.md |
| 430 | git add moved.md |
| 431 | git commit --quiet -m "main moves on" |
| 432 | git 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" |
| 438 | for 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 |
| 443 | done |
| 444 | [ ! -s "$pushlog" ] |
| 445 | # The bottom layer has no change under it, so it is approved as it is. |
| 446 | valley approve one > stale-bottom.out 2>&1 |
| 447 | grep -qx '✓ one approved · no checks required · landing on main' stale-bottom.out |
| 448 | [ "$(git -C "$stack" rev-parse "$requests/one")" = "$onehead" ] |
| 449 | git -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" |
| 454 | printf 's\n' | valley review pppp > stale-stop.out 2>&1 |
| 455 | grep -qxF 'pppp · stack of 3 · behind main' stale-stop.out |
| 456 | grep -qF '[b]ase onto main [v]iew diff [s]kip: stopped reviewing pppp before its first layer; nothing done' stale-stop.out |
| 457 | if 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. A note written in [v]iew diff before |
| 463 | # the rebase, beside the bottom layer's line, is printed once, under the |
| 464 | # top's branch, since the whole diff it was written in is the top's. The |
| 465 | # answers wait for each page and prompt, as on a terminal every key goes |
| 466 | # to whatever holds it. |
| 467 | # shellcheck disable=SC2094 # the answers wait on the review's own output |
| 468 | { |
| 469 | answer_after stale-review.log '\]kip: ' : 'v\n' |
| 470 | answer_after stale-review.log 'log against origin/main:' : vq |
| 471 | answer_after "$XDG_STATE_HOME/valley/notes/three.md" 'one wants a second line' : 'b\n' |
| 472 | answer_after stale-review.log '| change 1 of 3: one on main' : q |
| 473 | answer_after stale-review.log 'one, 1 of 3: \[n\]ext' : 'n\n' |
| 474 | answer_after stale-review.log 'qqqq, 2 of 3: \[n\]ext' : 'n\n' |
| 475 | answer_after stale-review.log '| change 3 of 3: pppp on qqqq' : q |
| 476 | answer_after stale-review.log '\]pprove all 3' : 'a\n' |
| 477 | wait_for stale-review.log 'note(s) on three' |
| 478 | } | EDITOR="$TMPDIR/stack-editor" script -qec "valley review pppp" /dev/null > stale-review.log 2>&1 |
| 479 | sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' stale-review.log > stale-review.out |
| 480 | grep -qF '[b]ase onto main [v]iew diff [s]kip: ' stale-review.out |
| 481 | grep -qF 'the stack under pppp, bottom first:' stale-review.out |
| 482 | git fetch --quiet origin |
| 483 | newone="$(git rev-parse origin/one)" newthree="$(git rev-parse origin/three)" |
| 484 | [ "$newone" != "$onehead" ] |
| 485 | git merge-base --is-ancestor origin/main "$newone" |
| 486 | awk '/change 1 of 3: /{ on = 1 } /change 2 of 3: /{ on = 0 } on' stale-review.out > stale-page1 |
| 487 | grep -q "^| $(git rev-parse --short "$newone") layer one$" stale-page1 |
| 488 | if grep -q "$(git rev-parse --short "$onehead")" stale-page1; then exit 1; fi |
| 489 | grep -qF '[a]pprove all 3 [v]iew diff [r]eject pppp [s]top: ' stale-review.out |
| 490 | [ "$(git -C "$stack" rev-parse "$requests/c-$p")" = "$newthree" ] |
| 491 | [ "$(grep -c 'one wants a second line' "$XDG_STATE_HOME/valley/notes/three.md")" = 1 ] |
| 492 | [ "$(grep -c '^ one.md:1: one wants a second line$' stale-review.out)" = 1 ] |
| 493 | grep -A1 '^1 note(s) on three, appended to .*/valley/notes/three.md:$' stale-review.out \ |
| 494 | | grep -qx ' one.md:1: one wants a second line' |
| 495 | # qqqq's v1 was approved earlier, and the rebase gave it v2 with the same |
| 496 | # file delta, so its layer is printed as rebased with the same file |
| 497 | # changes, and not paged. pppp has no approved version, so its layer is |
| 498 | # paged in full. |
| 499 | grep -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 |
| 500 | awk '/change 2 of 3: /{ on = 1 } /change 3 of 3: /{ on = 0 } on' stale-review.out > stale-page2 |
| 501 | if grep -q '^| diff --git' stale-page2; then exit 1; fi |
| 502 | awk '/change 3 of 3: /{ on = 1 } on' stale-review.out > stale-page3 |
| 503 | [ "$(grep '^| diff --git' stale-page3)" = '| diff --git a/three.md b/three.md' ] |
| 504 | |
| 505 | # ---------------------------------------------------------------------- |
| 506 | # A note taken on an edited comparison. v1 of a change is approved, and |
| 507 | # v2 edits line 9 of its file, so review pages v2 as edited since v1. |
| 508 | # range-diff's hunks carry no line numbers, so the page ends with v2's |
| 509 | # own diff of the file, and the note written beside the edited line there |
| 510 | # keeps its file and line. The view is styled on a terminal, through a |
| 511 | # structure-preserving renderer and through less alone, while the buffer |
| 512 | # the editor opens stays plain. |
| 513 | git checkout --quiet -b edited main |
| 514 | seq 1 12 | sed 's/^/line /' > lines.md |
| 515 | git add lines.md |
| 516 | git commit --quiet -m "add the lines" |
| 517 | git push --quiet origin edited |
| 518 | m=mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm |
| 519 | valley ready --change "$m" edited > /dev/null |
| 520 | git -C "$stack" update-ref "$requests/c-$m" "$(git rev-parse edited)" |
| 521 | sed -i 's/^line 9$/line 9 edited/' lines.md |
| 522 | git commit --quiet -am "edit line 9" |
| 523 | git push --quiet origin edited |
| 524 | valley ready --change "$m" edited > /dev/null |
| 525 | git checkout --quiet main |
| 526 | cat > "$TMPDIR/changed-editor" << 'EOF' |
| 527 | #!/bin/sh |
| 528 | f="$1" |
| 529 | case "$1" in +*) f="$2" ;; esac |
| 530 | if grep -q "$(printf '\033')" "$f"; then touch "$TMPDIR/escaped-buffer"; fi |
| 531 | sed -i "/^+line 9 edited\$/a $NOTE" "$f" |
| 532 | EOF |
| 533 | chmod +x "$TMPDIR/changed-editor" |
| 534 | esc="$(printf '\033')" |
| 535 | for pager in cat less; do |
| 536 | git config --global pager.diff "$pager" |
| 537 | # shellcheck disable=SC2094 # the answers wait on the review's own output |
| 538 | { |
| 539 | printf vq |
| 540 | answer_after "changed-$pager.log" '\]kip: ' : 's\n' |
| 541 | wait_for "changed-$pager.log" '1 note(s) on edited' |
| 542 | } | NOTE="why edit line 9, through $pager?" EDITOR="$TMPDIR/changed-editor" LESSCHARSET=utf-8 \ |
| 543 | script -qec "valley review mmmm" /dev/null > "changed-$pager.log" 2>&1 |
| 544 | sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' "changed-$pager.log" > changed-plain.log |
| 545 | grep -q "mmmm v2 at [0-9a-f]*: edited since you approved v1 at " changed-plain.log |
| 546 | grep -q "── v2's diff of this file against main " changed-plain.log |
| 547 | grep -q "${esc}\[1mmmmm v2 at [0-9a-f]*: ${esc}\[33medited${esc}\[39m since" "changed-$pager.log" |
| 548 | grep -q "${esc}\[36m── git range-diff " "changed-$pager.log" |
| 549 | grep -qx "lines.md:9: why edit line 9, through $pager?" "$XDG_STATE_HOME/valley/notes/edited.md" |
| 550 | grep -q "^ lines.md:9: why edit line 9, through $pager?\$" changed-plain.log |
| 551 | done |
| 552 | [ ! -e "$TMPDIR/escaped-buffer" ] |
| 553 | |
| 554 | touch "$out" |