From bf79c2fd71fe7b7f312e4bb352ef8819e29e0f8e Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 10:00:38 +0100 Subject: [PATCH 01/23] Add a read-only git wrapper, the reviewer's git environment and a worktree tripwire --- .github/workflows/ci.yml | 4 +- lib/git-ro | 33 ++++++++++ lib/guard.sh | 64 +++++++++++++++++++ tests/guard.sh | 132 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 231 insertions(+), 2 deletions(-) create mode 100755 lib/git-ro create mode 100644 lib/guard.sh create mode 100644 tests/guard.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a4827a5..b160da1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,8 +20,8 @@ jobs: - name: Install shfmt run: sudo apt-get update && sudo apt-get install -y shfmt - name: shellcheck - run: shellcheck gh-pr-review lib/*.sh tests/*.sh + run: shellcheck gh-pr-review lib/*.sh lib/git-ro tests/*.sh - name: shfmt - run: shfmt -d gh-pr-review lib/*.sh tests/*.sh + run: shfmt -d gh-pr-review lib/*.sh lib/git-ro tests/*.sh - name: tests run: for t in tests/*.sh; do bash "$t" || exit 1; done diff --git a/lib/git-ro b/lib/git-ro new file mode 100755 index 0000000..668c974 --- /dev/null +++ b/lib/git-ro @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# git for the reviewer: pi finds it as `git` first on its PATH (pi_env in lib/guard.sh). It runs the real git +# (GH_PR_REVIEW_REAL_GIT) for the commands that only read, and refuses the rest. A guard rail, not a wall: the +# tripwire around each pi call catches what gets past it. +set -euo pipefail + +real=${GH_PR_REVIEW_REAL_GIT:-} +if [[ -z "$real" || ! -x "$real" ]]; then + echo "git-ro: GH_PR_REVIEW_REAL_GIT does not name a git to run" >&2 + exit 1 +fi + +# The subcommand: the first argument after the global options. +i=1 sub="" +while [[ $i -le $# ]]; do + case "${!i}" in + -C | -c) i=$((i + 2)) ;; + --no-pager | -P | --git-dir=* | --work-tree=*) i=$((i + 1)) ;; + *) + sub=${!i} + break + ;; + esac +done + +case "$sub" in +show | diff | log | blame | grep | ls-files | ls-tree | cat-file | rev-parse | rev-list | merge-base | range-diff | \ + for-each-ref | describe | shortlog | status | help | version | --version | --help) + exec "$real" "$@" + ;; +esac +echo "git ${sub:-(no subcommand)} is not allowed in a read-only review" >&2 +exit 1 diff --git a/lib/guard.sh b/lib/guard.sh new file mode 100644 index 0000000..6edfa35 --- /dev/null +++ b/lib/guard.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash +# The reviewer's git sandbox: a read-only git on pi's PATH, no credentials, no hooks, and a tripwire on the review +# worktree around each pi call. Functions only. + +# Puts lib/git-ro in as `git`, for pi_env. +pi_bin() { # + mkdir -p "$1" + cp "$(dirname "${BASH_SOURCE[0]}")/git-ro" "$1/git" + chmod +x "$1/git" +} + +# Runs a command (pi) in the sandbox: the wrapper in first on PATH, the real git (the git on PATH now) in +# GH_PR_REVIEW_REAL_GIT, no credential helper, no hooks, no password prompt and no ssh. +pi_env() { # [args...] + local bin=$1 real + shift + real=$(command -v git) || { + echo "Error: git not found" >&2 + return 1 + } + env PATH="$bin:$PATH" GH_PR_REVIEW_REAL_GIT="$real" \ + GIT_CONFIG_COUNT=2 GIT_CONFIG_KEY_0=credential.helper GIT_CONFIG_VALUE_0= \ + GIT_CONFIG_KEY_1=core.hooksPath GIT_CONFIG_VALUE_1=/dev/null \ + GIT_TERMINAL_PROMPT=0 GIT_ASKPASS=false SSH_ASKPASS=false GIT_SSH_COMMAND=false "$@" +} + +# What the reviewer must not change. worktree.txt: the review worktree's HEAD (commit and branch) and status. +# clone.txt: the clone's refs but refs/pr-review/, its HEAD and its stash. Errors go in the files: they compare too. +tripwire_snapshot() { # + local g=(-c core.hooksPath=/dev/null -c core.fsmonitor=false) + mkdir -p "$3" + { + echo "HEAD $(git -C "$2" "${g[@]}" rev-parse HEAD 2>&1)" + echo "branch $(git -C "$2" "${g[@]}" symbolic-ref -q HEAD 2>&1)" + git -C "$2" "${g[@]}" status --porcelain 2>&1 || echo "status failed: $?" + } >"$3/worktree.txt" + { + { git -C "$1" "${g[@]}" for-each-ref --format='%(objectname) %(refname)' 2>&1 || echo "for-each-ref failed"; } | + { grep -v ' refs/pr-review/' || true; } + echo "HEAD $(git -C "$1" "${g[@]}" rev-parse HEAD 2>&1) $(git -C "$1" "${g[@]}" symbolic-ref -q HEAD 2>&1)" + git -C "$1" "${g[@]}" stash list 2>&1 || echo "stash list failed: $?" + } >"$3/clone.txt" +} + +# 0 when nothing changed. 4 when the worktree changed: prints what did. A change in the clone only (a commit or a +# branch I make while pi runs) is no stop: 0, with a warning. +tripwire_check() { # + local clone=same + cmp -s "$1/clone.txt" "$2/clone.txt" || clone=changed + if ! cmp -s "$1/worktree.txt" "$2/worktree.txt"; then + echo "The reviewer changed the review worktree (its HEAD or its files):" >&2 + { diff -u "$1/worktree.txt" "$2/worktree.txt" || true; } | tail -n +3 >&2 + if [[ $clone == changed ]]; then + echo "and the clone's refs, HEAD or stash:" >&2 + { diff -u "$1/clone.txt" "$2/clone.txt" || true; } | tail -n +3 >&2 + fi + return 4 + fi + if [[ $clone == changed ]]; then + echo "Warning: the clone's refs, HEAD or stash changed while the reviewer ran (the review worktree did not):" >&2 + { diff -u "$1/clone.txt" "$2/clone.txt" || true; } | tail -n +3 >&2 + fi + return 0 +} diff --git a/tests/guard.sh b/tests/guard.sh new file mode 100644 index 0000000..11da413 --- /dev/null +++ b/tests/guard.sh @@ -0,0 +1,132 @@ +#!/usr/bin/env bash +# Checks the reviewer's git sandbox with real git in a temp folder: lib/git-ro, pi_env and the tripwire. +# Conditions are single-quoted on purpose: expect() evals them. +# shellcheck disable=SC2016,SC2034,SC2329 # SC2329: ro runs inside expect's eval +set -euo pipefail +root=$(cd "$(dirname "$0")/.." && pwd) +# shellcheck source=lib/guard.sh +source "$root/lib/guard.sh" +# The script must reach its end: Bash 3.2 can stop on an error inside an EXIT trap with status 0. +finished=false +trap '[[ $finished == true ]] || { echo "FAIL stopped before the end"; exit 1; }' EXIT +fail=0 + +t=$(mktemp -d) +t=$(cd "$t" && pwd -P) +export HOME="$t/home" GIT_CONFIG_NOSYSTEM=1 GIT_AUTHOR_NAME=a GIT_AUTHOR_EMAIL=a@x GIT_COMMITTER_NAME=a \ + GIT_COMMITTER_EMAIL=a@x +mkdir -p "$HOME" +real=$(command -v git) + +expect() { # name condition + if eval "$2"; then echo "ok $1"; else + echo "FAIL $1 ($2)" + fail=1 + fi +} + +# The whole state of a repo the wrapper must not touch. +state() { # + git -C "$1" for-each-ref + git -C "$1" rev-parse HEAD + git -C "$1" status --porcelain + git -C "$1" stash list + git -C "$1" worktree list + git -C "$1" config --list --local +} + +repo="$t/repo" +git init -q "$repo" +git -C "$repo" checkout -q -b main +for n in 1 2 3; do + echo "$n" >"$repo/f" + git -C "$repo" add f + git -C "$repo" commit -qm "c$n" +done +git -C "$repo" branch side +git init -q --bare "$t/remote.git" +git -C "$repo" remote add origin "$t/remote.git" +echo dirty >"$repo/f" +pi_bin "$t/bin" + +# ── git-ro ── +ro() { # git args... (through the wrapper on pi's PATH, from the repo) + (cd "$repo" && pi_env "$t/bin" git "$@") >"$t/ro.out" 2>&1 +} +for args in "log --oneline" "diff" "-C $repo show --stat" "-c core.quotepath=off status" "--version" \ + "range-diff HEAD~2 HEAD~1 HEAD" "--no-pager log -1" "rev-parse HEAD"; do + expect "git-ro passes: $args" "ro $args" +done +before=$(state "$repo") +for args in "commit -qam x" "checkout -b evil" "branch -D side" "push origin main" "stash" "worktree add ../wt" \ + "fetch origin" "-C $repo reset --hard" "-c core.pager=cat --no-pager tag v1"; do + sub=${args##*-pager } + sub=${sub##*"$repo" } + sub=${sub%% *} + expect "git-ro refuses: $args" \ + "! ro $args && [[ \$(cat \"\$t/ro.out\") == 'git $sub is not allowed in a read-only review' ]]" +done +expect "git-ro: the repo is unchanged after the refused commands" '[[ "$(state "$repo")" == "$before" && ! -e "$t/wt" ]]' +expect "git-ro: nothing was pushed" '[[ -z "$(git -C "$t/remote.git" for-each-ref)" ]]' +expect "git-ro: an option where the subcommand goes is refused" '! ro --exec-path=/x status' +expect "git-ro: no real git, no run" \ + '! (cd "$repo" && GH_PR_REVIEW_REAL_GIT="" "$t/bin/git" log) >/dev/null 2>&1' + +# ── pi_env ── +printf '[credential]\n\thelper = store\n' >"$HOME/.gitconfig" +echo "https://u:s3cret@example.com" >"$HOME/.git-credentials" +cred=$'protocol=https\nhost=example.com\n' +expect "pi_env: credentials are there outside it (control)" \ + 'git credential fill <<<"$cred" 2>/dev/null | grep -q password=s3cret' +expect "pi_env: the real git's credential fill returns nothing" \ + '[[ -z "$(pi_env "$t/bin" bash -c "\"\$GH_PR_REVIEW_REAL_GIT\" credential fill" <<<"$cred" 2>/dev/null)" ]]' +rm "$HOME/.gitconfig" "$HOME/.git-credentials" +git init -q "$t/hooked" +printf '#!/bin/sh\ntouch "%s/hook.ran"\n' "$t" >"$t/hooked/.git/hooks/pre-commit" +chmod +x "$t/hooked/.git/hooks/pre-commit" +# shellcheck disable=SC2016 # expanded by the inner shell, where pi_env set it +pi_env "$t/bin" bash -c 'cd "$1" && "$GH_PR_REVIEW_REAL_GIT" commit -q --allow-empty -m h' _ "$t/hooked" +expect "pi_env: a hook does not run" '[[ ! -e "$t/hook.ran" ]]' +git -C "$t/hooked" commit -q --allow-empty -m h +expect "pi_env: the hook runs outside it (control)" '[[ -e "$t/hook.ran" ]]' +expect "pi_env: the wrapper is git on PATH" '[[ "$(pi_env "$t/bin" bash -c "command -v git")" == "$t/bin/git" ]]' +expect "pi_env: real git is the git outside it" '[[ "$(pi_env "$t/bin" printenv GH_PR_REVIEW_REAL_GIT)" == "$real" ]]' + +# ── tripwire ── +git -C "$repo" checkout -q -- f +git -C "$repo" worktree add -q --detach "$t/review" main +snap() { tripwire_snapshot "$repo" "$t/review" "$t/snap$1"; } +check() { # before after + rc=0 + tripwire_check "$t/snap$1" "$t/snap$2" >"$t/check.out" 2>&1 || rc=$? +} +snap 1 +snap 2 +check 1 2 +expect "tripwire: no change gives 0, quietly" '[[ $rc == 0 && ! -s "$t/check.out" ]]' +git -C "$repo" update-ref refs/pr-review/1/round-1 HEAD +snap 3 +check 2 3 +expect "tripwire: refs/pr-review/ is not watched" '[[ $rc == 0 && ! -s "$t/check.out" ]]' +git -C "$t/review" commit -q --allow-empty -m x +snap 4 +check 3 4 +expect "tripwire: a commit in the worktree gives 4, with the diff" \ + '[[ $rc == 4 ]] && grep -q "review worktree" "$t/check.out" && grep -q "^+HEAD" "$t/check.out"' +git -C "$repo" branch newb +snap 5 +check 4 5 +expect "tripwire: a new branch in the clone gives 0, with a warning" \ + '[[ $rc == 0 ]] && grep -q "^Warning" "$t/check.out" && grep -q "refs/heads/newb" "$t/check.out"' +touch "$t/review/new-file" +snap 6 +check 5 6 +expect "tripwire: a new file in the worktree gives 4" '[[ $rc == 4 ]] && grep -q "new-file" "$t/check.out"' +rm "$t/review/new-file" +git -C "$t/review" checkout -q -b evil +snap 7 +check 5 7 +expect "tripwire: a branch checked out in the worktree gives 4" '[[ $rc == 4 ]]' + +finished=true +exit "$fail" From 79b04105879beffa492bedf4007dd7977499d8d4 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 10:00:38 +0100 Subject: [PATCH 02/23] Run the reviewer with a read-only git and a worktree tripwire --- README.md | 8 ++++++++ gh-pr-review | 26 +++++++++++++++++++++----- tests/flow.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/passes.sh | 12 ++++++++++-- 4 files changed, 81 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 936e6a5..1e3526c 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,13 @@ record a preview, and exits with code 1. Another run's review on the same commit, by the same account, does not count. Run `gh pr-review rounds ` before retrying, so the round is not posted twice. +The reviewer (pi) works read-only. Its `git` is a wrapper that runs only the +commands that read (`show`, `diff`, `log`, `range-diff`, `status`, …) and +refuses the rest, with no credentials and no hooks. A snapshot of the review +checkout (its HEAD and `git status`) is taken before and after each model call: +if the reviewer changed it anyway, the run stops before posting, says what +changed, keeps the session folder, and exits with code 4. + `gh pr-review --check` runs the check alone and exits 0 (run), 3 (stop) or 5 (no new commits); `--check --json` prints the decision as JSON for scripts. `gh pr-review rounds ` shows the same decision under the history. @@ -329,6 +336,7 @@ scripts. `gh pr-review rounds ` shows the same decision under the history. | 0 | Done | | 1 | Error | | 3 | Stopped by the loop check | +| 4 | The reviewer changed the review checkout (its HEAD or its files); nothing posted, the session is kept | | 5 | No new commits since the last round | | 6 | History changed while the review ran (someone posted another round, or a round went away); nothing posted, the session is kept | diff --git a/gh-pr-review b/gh-pr-review index 5d0366c..b9a7bb7 100755 --- a/gh-pr-review +++ b/gh-pr-review @@ -22,8 +22,9 @@ # --doctor Check all prerequisites and exit # -h, --help Show this help # -# Exit codes: 0 done, 1 error, 3 stopped by the loop check, 5 no new commits since the last round, -# 6 the rounds changed while the review ran (nothing posted, the session is kept). +# Exit codes: 0 done, 1 error, 3 stopped by the loop check, 4 the reviewer changed the review checkout (nothing +# posted, the session is kept), 5 no new commits since the last round, 6 the rounds changed while the review ran +# (nothing posted, the session is kept). # # Examples: # gh pr-review https://github.com/owner/repo/pull/42 @@ -238,9 +239,24 @@ write_pr_context() { } # ── passes ────────────────────────────────────────────────────────────────────────── -# One headless pi call: prompt file in, model output file out. A failed call stops the run. -run_pi() { - if ! pi "${PI_ARGS[@]}" "$(cat "$1")" >"$2"; then +# One headless pi call: prompt file in, model output file out. A failed call stops the run. pi runs in the git +# sandbox (lib/guard.sh), between two snapshots of the review checkout: a reviewer that changed it stops the run with +# exit 4, before anything is posted. +run_pi() { # + local snap code=0 changed=0 + snap="${SESSION_DIR}/tripwire/$(basename "$2" .md)" + pi_bin "${SESSION_DIR}/bin" + tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/before" + pi_env "${SESSION_DIR}/bin" pi ${PI_ARGS[@]+"${PI_ARGS[@]}"} "$(cat "$1")" >"$2" || code=$? + tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/after" + tripwire_check "$snap/before" "$snap/after" || changed=$? + if [[ $changed -ne 0 ]]; then + echo "" >&2 + echo "Not posted (exit 4): the reviewer changed the review checkout, which must stay read-only." >&2 + echo " The checkout is left as it is. Session: ${SESSION_DIR}" >&2 + exit 4 + fi + if [[ $code -ne 0 ]]; then echo "" >&2 echo "Error: pi exited with a non-zero status." >&2 echo " Check the session directory for details: ${SESSION_DIR}" >&2 diff --git a/tests/flow.sh b/tests/flow.sh index 440c450..6c2c5ce 100644 --- a/tests/flow.sh +++ b/tests/flow.sh @@ -99,6 +99,16 @@ S cat >"$stub/bin/pi" <>"$stub/pi.calls" +# pi.evil: each pass tries git stash (refused); the code review then tries git checkout -b evil (refused) and runs the +# real git with the arguments in pi.evil (the tripwire's case). +if [[ -f "$stub/pi.evil" ]]; then + git stash >>"$stub/pi.git" 2>&1 || true + if [[ "\${*: -1}" != *"long-time maintainer"* ]]; then + git checkout -b evil >>"$stub/pi.git" 2>&1 || true + read -ra evil <"$stub/pi.evil" + "\$GH_PR_REVIEW_REAL_GIT" -C . "\${evil[@]}" >>"$stub/pi.git" 2>&1 + fi +fi if [[ "\${*: -1}" == *"long-time maintainer"* ]]; then [[ "${4:-}" == owner ]] && exit 7 printf '%s' "\${*: -1}" >"$stub/owner-prompt.txt"; cat "$stub/approach.out"; else [[ "${4:-}" == code ]] && exit 7 @@ -1403,5 +1413,37 @@ expect "record failed in round 3: round 4 has no previous-round rules and no pre expect "record failed in round 3: round 4, a clean full review, approves" \ '[[ $flag == --approve && $(jq -c "[.counts.open_blocking, .decision]" <<<"$record") == "[0,\"approve\"]" ]]' +# The reviewer's sandbox, on real git: the clone comes from a local bare repo (url..insteadOf through +# GIT_CONFIG_COUNT), never the network. The stub git can't see a change, so this case runs without it. +setup $'**Approach:** Sound' "$clean" +rm "$stub/bin/git" +export GIT_AUTHOR_NAME=a GIT_AUTHOR_EMAIL=a@x GIT_COMMITTER_NAME=a GIT_COMMITTER_EMAIL=a@x +git init -q --bare "$stub/r.git" +git -C "$stub/r.git" symbolic-ref HEAD refs/heads/main +git init -q "$stub/src" +git -C "$stub/src" commit -q --allow-empty -m base +git -C "$stub/src" push -q "$stub/r.git" HEAD:refs/heads/main +echo x >"$stub/src/d.js" +git -C "$stub/src" add d.js +git -C "$stub/src" commit -q -m change +git -C "$stub/src" push -q "$stub/r.git" HEAD:refs/pull/1/head +git -C "$stub/src" rev-parse HEAD >"$stub/heads" +export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="url.file://$stub/r.git.insteadOf" GIT_CONFIG_VALUE_0=https://github.com/o/r.git +echo "commit -q --allow-empty -m x" >"$stub/pi.evil" +run +expect "read-only reviewer: both passes get the read-only git" \ + '[[ $calls == 2 && $(grep -c "^git stash is not allowed in a read-only review$" "$stub/pi.git") == 2 ]] && + grep -q "^git checkout is not allowed in a read-only review$" "$stub/pi.git"' +expect "read-only reviewer: a commit past the wrapper exits 4, says what changed, posts nothing, keeps the session" \ + '[[ $rc == 4 && ! -e "$stub/posted.json" ]] && grep -q "Not posted (exit 4)" "$stub/out.log" && + grep -q "^+HEAD" "$stub/out.log" && [[ -s $(echo "$stub"/work/pr-review-sessions/pr-review-*/tripwire/review/after/worktree.txt) ]]' +rm "$stub/pi.git" +echo "branch side" >"$stub/pi.evil" +run +expect "read-only reviewer: a new branch in the clone only warns, and the review is posted" \ + '[[ $rc == 0 && -e "$stub/posted.json" ]] && grep -q "^Warning: the clone.s refs" "$stub/out.log" && + grep -q "refs/heads/side" "$stub/out.log"' +unset GIT_CONFIG_COUNT GIT_CONFIG_KEY_0 GIT_CONFIG_VALUE_0 + finished=true exit "$fail" diff --git a/tests/passes.sh b/tests/passes.sh index 04ed9b4..84580da 100644 --- a/tests/passes.sh +++ b/tests/passes.sh @@ -18,9 +18,10 @@ eq() { # name got want } in_lib() { # script (runs with lib sourced and the stub pi on PATH) - PATH="$stub:$PATH" bash -c "set -euo pipefail; source '$stub/lib.sh'; SESSION_DIR='$stub' MODEL=m; $1" 2>&1 || - echo "(exit $?)" + PATH="$stub:$PATH" bash -c "set -euo pipefail; source '$stub/lib.sh'; source '$root/lib/guard.sh' + SESSION_DIR='$stub' REVIEW_DIR='$stub/repo' MODEL=m; $1" 2>&1 || echo "(exit $?)" } +git init -q "$stub/repo" printf 'PROMPT' >"$stub/p.md" # shellcheck disable=SC2016 # the stub must expand $* when it runs, not now @@ -34,6 +35,13 @@ out=$(in_lib "PI_ARGS=(); run_pi '$stub/p.md' '$stub/o.md'; echo SHOULD-NOT-REAC eq "run_pi failure exits" "$([[ "$out" == *"(exit 1)"* && "$out" != *SHOULD-NOT-REACH* ]] && echo y)" y eq "run_pi failure names the session" "$([[ "$out" == *"$stub"* ]] && echo y)" y +# A reviewer that writes in the checkout, after its git refused to. +printf '#!/usr/bin/env bash\ngit stash\ntouch "%s/repo/x"\n' "$stub" >"$stub/pi" +out=$(in_lib "PI_ARGS=(); run_pi '$stub/p.md' '$stub/o.md'; echo SHOULD-NOT-REACH") +eq "run_pi: the reviewer's git is read-only" "$([[ "$out" == *"git stash is not allowed"* ]] && echo y)" y +eq "run_pi: a changed checkout exits 4 and says what changed" \ + "$([[ "$out" == *"(exit 4)"* && "$out" == *"+?? x"* && "$out" != *SHOULD-NOT-REACH* ]] && echo y)" y + boundary='' eq "approach, boundary, review, footer" "$(in_lib "assemble_review '' 'APPROACH' 'REVIEW'")" \ $'APPROACH\n\n'"$boundary"$'\nREVIEW\n\n---\n'"$footer" From 37661b470cc7f2cc3c7441496c088613e9eb4c47 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 11:25:54 +0100 Subject: [PATCH 03/23] Run the reviewer's git without optional locks --- lib/guard.sh | 8 +++++--- tests/guard.sh | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/guard.sh b/lib/guard.sh index 6edfa35..7e06168 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -10,7 +10,8 @@ pi_bin() { # } # Runs a command (pi) in the sandbox: the wrapper in first on PATH, the real git (the git on PATH now) in -# GH_PR_REVIEW_REAL_GIT, no credential helper, no hooks, no password prompt and no ssh. +# GH_PR_REVIEW_REAL_GIT, no credential helper, no hooks, no password prompt, no ssh, and no optional locks: `git status` +# does not rewrite the index. pi_env() { # [args...] local bin=$1 real shift @@ -21,13 +22,14 @@ pi_env() { # [args...] env PATH="$bin:$PATH" GH_PR_REVIEW_REAL_GIT="$real" \ GIT_CONFIG_COUNT=2 GIT_CONFIG_KEY_0=credential.helper GIT_CONFIG_VALUE_0= \ GIT_CONFIG_KEY_1=core.hooksPath GIT_CONFIG_VALUE_1=/dev/null \ - GIT_TERMINAL_PROMPT=0 GIT_ASKPASS=false SSH_ASKPASS=false GIT_SSH_COMMAND=false "$@" + GIT_TERMINAL_PROMPT=0 GIT_ASKPASS=false SSH_ASKPASS=false GIT_SSH_COMMAND=false \ + GIT_OPTIONAL_LOCKS=0 "$@" } # What the reviewer must not change. worktree.txt: the review worktree's HEAD (commit and branch) and status. # clone.txt: the clone's refs but refs/pr-review/, its HEAD and its stash. Errors go in the files: they compare too. tripwire_snapshot() { # - local g=(-c core.hooksPath=/dev/null -c core.fsmonitor=false) + local g=(--no-optional-locks -c core.hooksPath=/dev/null -c core.fsmonitor=false) mkdir -p "$3" { echo "HEAD $(git -C "$2" "${g[@]}" rev-parse HEAD 2>&1)" diff --git a/tests/guard.sh b/tests/guard.sh index 11da413..4444907 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -89,6 +89,25 @@ pi_env "$t/bin" bash -c 'cd "$1" && "$GH_PR_REVIEW_REAL_GIT" commit -q --allow-e expect "pi_env: a hook does not run" '[[ ! -e "$t/hook.ran" ]]' git -C "$t/hooked" commit -q --allow-empty -m h expect "pi_env: the hook runs outside it (control)" '[[ -e "$t/hook.ran" ]]' +# An index whose stat info is stale: `git status` refreshes it and writes it back, unless optional locks are off. +stale() { # : a different mtime each time, so the index is stale again even after a rewrite + touch -t "20000101000$2" "$1/f" + cksum <"$1/.git/index" +} +idx="$t/idx" +git init -q "$idx" +echo 1 >"$idx/f" +git -C "$idx" add f +git -C "$idx" commit -qm c +sum=$(stale "$idx" 1) +pi_env "$t/bin" bash -c 'cd "$1" && git status >/dev/null' _ "$idx" +expect "pi_env: git status leaves a stale index unchanged" '[[ "$(cksum <"$idx/.git/index")" == "$sum" ]]' +sum=$(stale "$idx" 2) +tripwire_snapshot "$idx" "$idx" "$t/snap-idx" +expect "tripwire: its snapshot leaves a stale index unchanged" '[[ "$(cksum <"$idx/.git/index")" == "$sum" ]]' +sum=$(stale "$idx" 3) +git -C "$idx" status >/dev/null +expect "pi_env: git status rewrites the stale index outside it (control)" '[[ "$(cksum <"$idx/.git/index")" != "$sum" ]]' expect "pi_env: the wrapper is git on PATH" '[[ "$(pi_env "$t/bin" bash -c "command -v git")" == "$t/bin/git" ]]' expect "pi_env: real git is the git outside it" '[[ "$(pi_env "$t/bin" printenv GH_PR_REVIEW_REAL_GIT)" == "$real" ]]' From 7d4e732213ccb2460d304948e7740eae6f4696d5 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 11:26:47 +0100 Subject: [PATCH 04/23] Skip more global options in git-ro to find the subcommand --- lib/git-ro | 9 ++++++--- tests/guard.sh | 16 +++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/git-ro b/lib/git-ro index 668c974..3d582ef 100755 --- a/lib/git-ro +++ b/lib/git-ro @@ -10,12 +10,15 @@ if [[ -z "$real" || ! -x "$real" ]]; then exit 1 fi -# The subcommand: the first argument after the global options. +# The subcommand: the first argument after the global options. Any other option before it is refused. i=1 sub="" while [[ $i -le $# ]]; do case "${!i}" in - -C | -c) i=$((i + 2)) ;; - --no-pager | -P | --git-dir=* | --work-tree=*) i=$((i + 1)) ;; + -C | -c | --git-dir | --work-tree) i=$((i + 2)) ;; + --no-pager | -P | -p | --paginate | --no-optional-locks | --literal-pathspecs | --no-replace-objects | \ + --git-dir=* | --work-tree=*) + i=$((i + 1)) + ;; *) sub=${!i} break diff --git a/tests/guard.sh b/tests/guard.sh index 4444907..e86ddeb 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -54,15 +54,21 @@ ro() { # git args... (through the wrapper on pi's PATH, from the repo) (cd "$repo" && pi_env "$t/bin" git "$@") >"$t/ro.out" 2>&1 } for args in "log --oneline" "diff" "-C $repo show --stat" "-c core.quotepath=off status" "--version" \ - "range-diff HEAD~2 HEAD~1 HEAD" "--no-pager log -1" "rev-parse HEAD"; do + "range-diff HEAD~2 HEAD~1 HEAD" "--no-pager log -1" "rev-parse HEAD" \ + "-p log -1" "--paginate log -1" "--no-optional-locks status" "--literal-pathspecs log -1" \ + "--no-replace-objects log -1" "--git-dir $repo/.git log -1" "--git-dir=$repo/.git log -1" \ + "--work-tree $repo --git-dir $repo/.git status" "--work-tree=$repo status"; do expect "git-ro passes: $args" "ro $args" done before=$(state "$repo") for args in "commit -qam x" "checkout -b evil" "branch -D side" "push origin main" "stash" "worktree add ../wt" \ - "fetch origin" "-C $repo reset --hard" "-c core.pager=cat --no-pager tag v1"; do - sub=${args##*-pager } - sub=${sub##*"$repo" } - sub=${sub%% *} + "fetch origin" "-C $repo reset --hard" "-c core.pager=cat --no-pager tag v1" "-p commit -qam x" \ + "--paginate commit -qam x" "--no-optional-locks commit -qam x" "--literal-pathspecs commit -qam x" \ + "--no-replace-objects commit -qam x" "--git-dir $repo/.git commit -qam x" "--work-tree $repo commit -qam x" \ + "--git-dir=$repo/.git commit -qam x" "--work-tree=$repo commit -qam x"; do + for sub in $args; do + case $sub in commit | checkout | branch | push | stash | worktree | fetch | reset | tag) break ;; esac + done expect "git-ro refuses: $args" \ "! ro $args && [[ \$(cat \"\$t/ro.out\") == 'git $sub is not allowed in a read-only review' ]]" done From 72f928b3616446b1fefe7f61883e25db20773c49 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 13:42:49 +0100 Subject: [PATCH 05/23] Show every untracked file and submodule change in the tripwire's status --- lib/guard.sh | 7 +++++-- tests/guard.sh | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/guard.sh b/lib/guard.sh index 7e06168..bd1335c 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -26,7 +26,9 @@ pi_env() { # [args...] GIT_OPTIONAL_LOCKS=0 "$@" } -# What the reviewer must not change. worktree.txt: the review worktree's HEAD (commit and branch) and status. +# What the reviewer must not change. worktree.txt: the review worktree's HEAD (commit and branch) and status, with +# every untracked file and every submodule whatever the config says (status.showUntrackedFiles, diff.ignoreSubmodules, +# submodule..ignore). # clone.txt: the clone's refs but refs/pr-review/, its HEAD and its stash. Errors go in the files: they compare too. tripwire_snapshot() { # local g=(--no-optional-locks -c core.hooksPath=/dev/null -c core.fsmonitor=false) @@ -34,7 +36,8 @@ tripwire_snapshot() { # { echo "HEAD $(git -C "$2" "${g[@]}" rev-parse HEAD 2>&1)" echo "branch $(git -C "$2" "${g[@]}" symbolic-ref -q HEAD 2>&1)" - git -C "$2" "${g[@]}" status --porcelain 2>&1 || echo "status failed: $?" + git -C "$2" "${g[@]}" status --porcelain --untracked-files=all --ignore-submodules=none 2>&1 || + echo "status failed: $?" } >"$3/worktree.txt" { { git -C "$1" "${g[@]}" for-each-ref --format='%(objectname) %(refname)' 2>&1 || echo "for-each-ref failed"; } | diff --git a/tests/guard.sh b/tests/guard.sh index e86ddeb..7067092 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -152,6 +152,38 @@ git -C "$t/review" checkout -q -b evil snap 7 check 5 7 expect "tripwire: a branch checked out in the worktree gives 4" '[[ $rc == 4 ]]' +git -C "$t/review" checkout -q --detach main +# Operator config that hides changes from a plain `git status`: the snapshot must see them anyway. +git -C "$repo" config status.showUntrackedFiles no +snap 8 +touch "$t/review/new-file" +snap 9 +check 8 9 +expect "tripwire: a new file gives 4 with status.showUntrackedFiles=no" \ + '[[ $rc == 4 ]] && grep -q "new-file" "$t/check.out"' +rm "$t/review/new-file" +git -C "$repo" config status.showUntrackedFiles normal +mkdir "$t/review/untracked" +touch "$t/review/untracked/a" +snap 10 +touch "$t/review/untracked/b" +snap 11 +check 10 11 +expect "tripwire: a new file in an untracked folder gives 4" '[[ $rc == 4 ]] && grep -q "untracked/b" "$t/check.out"' +rm -r "$t/review/untracked" +git -C "$repo" config --unset status.showUntrackedFiles +git init -q "$t/sub" +git -C "$t/sub" commit -q --allow-empty -m s +git init -q "$t/sup" +git -C "$t/sup" -c protocol.file.allow=always submodule add -q "$t/sub" s 2>/dev/null +git -C "$t/sup" commit -qm add +git -C "$t/sup" config diff.ignoreSubmodules all +tripwire_snapshot "$t/sup" "$t/sup" "$t/snap-sup1" +git -C "$t/sup/s" commit -q --allow-empty -m s2 +tripwire_snapshot "$t/sup" "$t/sup" "$t/snap-sup2" +rc=0 +tripwire_check "$t/snap-sup1" "$t/snap-sup2" >"$t/check.out" 2>&1 || rc=$? +expect "tripwire: a commit in a submodule gives 4 with diff.ignoreSubmodules=all" '[[ $rc == 4 ]]' finished=true exit "$fail" From 7b6863bb4ce75b5b153b22495efc9458dce047e3 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 13:51:21 +0100 Subject: [PATCH 06/23] Stop with exit 4 when the tripwire can't read the review checkout --- README.md | 6 ++++-- gh-pr-review | 16 +++++++++++++--- lib/guard.sh | 19 +++++++++++++------ tests/flow.sh | 13 ++++++++++++- tests/guard.sh | 11 +++++++++++ tests/passes.sh | 2 ++ 6 files changed, 55 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1e3526c..591f8bc 100644 --- a/README.md +++ b/README.md @@ -325,7 +325,9 @@ commands that read (`show`, `diff`, `log`, `range-diff`, `status`, …) and refuses the rest, with no credentials and no hooks. A snapshot of the review checkout (its HEAD and `git status`) is taken before and after each model call: if the reviewer changed it anyway, the run stops before posting, says what -changed, keeps the session folder, and exits with code 4. +changed, keeps the session folder, and exits with code 4. A checkout the +snapshot can't read (its HEAD or `git status` fails) stops the run the same +way: a change would go unseen. `gh pr-review --check` runs the check alone and exits 0 (run), 3 (stop) or 5 (no new commits); `--check --json` prints the decision as JSON for @@ -336,7 +338,7 @@ scripts. `gh pr-review rounds ` shows the same decision under the history. | 0 | Done | | 1 | Error | | 3 | Stopped by the loop check | -| 4 | The reviewer changed the review checkout (its HEAD or its files); nothing posted, the session is kept | +| 4 | The reviewer changed the review checkout (its HEAD or its files), or it could not be read; nothing posted, the session is kept | | 5 | No new commits since the last round | | 6 | History changed while the review ran (someone posted another round, or a round went away); nothing posted, the session is kept | diff --git a/gh-pr-review b/gh-pr-review index b9a7bb7..1829d51 100755 --- a/gh-pr-review +++ b/gh-pr-review @@ -241,14 +241,14 @@ write_pr_context() { # ── passes ────────────────────────────────────────────────────────────────────────── # One headless pi call: prompt file in, model output file out. A failed call stops the run. pi runs in the git # sandbox (lib/guard.sh), between two snapshots of the review checkout: a reviewer that changed it stops the run with -# exit 4, before anything is posted. +# exit 4, before anything is posted. So does a checkout a snapshot can't read: a change would go unseen. run_pi() { # local snap code=0 changed=0 snap="${SESSION_DIR}/tripwire/$(basename "$2" .md)" pi_bin "${SESSION_DIR}/bin" - tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/before" + tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/before" || unreadable_checkout "$snap/before" pi_env "${SESSION_DIR}/bin" pi ${PI_ARGS[@]+"${PI_ARGS[@]}"} "$(cat "$1")" >"$2" || code=$? - tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/after" + tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/after" || unreadable_checkout "$snap/after" tripwire_check "$snap/before" "$snap/after" || changed=$? if [[ $changed -ne 0 ]]; then echo "" >&2 @@ -264,6 +264,16 @@ run_pi() { # fi } +# Stops the run with exit 4: the review checkout could not be read (its snapshot says why), so a change by the +# reviewer could not be seen. +unreadable_checkout() { # + echo "" >&2 + echo "Not posted (exit 4): the review checkout could not be read, so a change by the reviewer could not be seen:" >&2 + sed 's/^/ /' "$1/worktree.txt" >&2 + echo " The checkout is left as it is. Session: ${SESSION_DIR}" >&2 + exit 4 +} + # The run's extra guidance as a prompt section. Empty in, empty out. instructions_section() { [[ -z "$INSTRUCTIONS" ]] && return 0 diff --git a/lib/guard.sh b/lib/guard.sh index bd1335c..d150dfb 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -28,16 +28,22 @@ pi_env() { # [args...] # What the reviewer must not change. worktree.txt: the review worktree's HEAD (commit and branch) and status, with # every untracked file and every submodule whatever the config says (status.showUntrackedFiles, diff.ignoreSubmodules, -# submodule..ignore). -# clone.txt: the clone's refs but refs/pr-review/, its HEAD and its stash. Errors go in the files: they compare too. +# submodule..ignore). clone.txt: the clone's refs but refs/pr-review/, its HEAD and its stash. Errors go in the +# files. 1 when the worktree's HEAD or status can't be read (a detached HEAD is fine): two such snapshots would compare +# equal, so the worktree can't be checked. tripwire_snapshot() { # - local g=(--no-optional-locks -c core.hooksPath=/dev/null -c core.fsmonitor=false) + local g=(--no-optional-locks -c core.hooksPath=/dev/null -c core.fsmonitor=false) ok=0 head branch code=0 mkdir -p "$3" + head=$(git -C "$2" "${g[@]}" rev-parse --verify HEAD 2>&1) || ok=1 + branch=$(git -C "$2" "${g[@]}" symbolic-ref -q HEAD 2>&1) || code=$? + [[ $code -le 1 ]] || ok=1 { - echo "HEAD $(git -C "$2" "${g[@]}" rev-parse HEAD 2>&1)" - echo "branch $(git -C "$2" "${g[@]}" symbolic-ref -q HEAD 2>&1)" - git -C "$2" "${g[@]}" status --porcelain --untracked-files=all --ignore-submodules=none 2>&1 || + echo "HEAD $head" + echo "branch $branch" + git -C "$2" "${g[@]}" status --porcelain --untracked-files=all --ignore-submodules=none 2>&1 || { echo "status failed: $?" + ok=1 + } } >"$3/worktree.txt" { { git -C "$1" "${g[@]}" for-each-ref --format='%(objectname) %(refname)' 2>&1 || echo "for-each-ref failed"; } | @@ -45,6 +51,7 @@ tripwire_snapshot() { # echo "HEAD $(git -C "$1" "${g[@]}" rev-parse HEAD 2>&1) $(git -C "$1" "${g[@]}" symbolic-ref -q HEAD 2>&1)" git -C "$1" "${g[@]}" stash list 2>&1 || echo "stash list failed: $?" } >"$3/clone.txt" + return "$ok" } # 0 when nothing changed. 4 when the worktree changed: prints what did. A change in the clone only (a commit or a diff --git a/tests/flow.sh b/tests/flow.sh index 6c2c5ce..5eb3241 100644 --- a/tests/flow.sh +++ b/tests/flow.sh @@ -1443,7 +1443,18 @@ run expect "read-only reviewer: a new branch in the clone only warns, and the review is posted" \ '[[ $rc == 0 && -e "$stub/posted.json" ]] && grep -q "^Warning: the clone.s refs" "$stub/out.log" && grep -q "refs/heads/side" "$stub/out.log"' -unset GIT_CONFIG_COUNT GIT_CONFIG_KEY_0 GIT_CONFIG_VALUE_0 +rm "$stub/pi.git" +echo "config status.showUntrackedFiles bogus" >"$stub/pi.evil" +run +expect "read-only reviewer: a checkout it leaves unreadable exits 4, says why, posts nothing" \ + '[[ $rc == 4 && $calls == 2 && ! -e "$stub/posted.json" ]] && grep -q "Not posted (exit 4)" "$stub/out.log" && + grep -q "could not be read" "$stub/out.log" && grep -q "status failed" "$stub/out.log"' +rm "$stub/pi.evil" +export GIT_CONFIG_COUNT=2 GIT_CONFIG_KEY_1=status.showUntrackedFiles GIT_CONFIG_VALUE_1=bogus +run +expect "read-only reviewer: a checkout it can't read before the call: exit 4, no model call, nothing posted" \ + '[[ $rc == 4 && $calls == 0 && ! -e "$stub/posted.json" ]] && grep -q "could not be read" "$stub/out.log"' +unset GIT_CONFIG_COUNT GIT_CONFIG_KEY_0 GIT_CONFIG_VALUE_0 GIT_CONFIG_KEY_1 GIT_CONFIG_VALUE_1 finished=true exit "$fail" diff --git a/tests/guard.sh b/tests/guard.sh index 7067092..639f281 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -172,6 +172,17 @@ check 10 11 expect "tripwire: a new file in an untracked folder gives 4" '[[ $rc == 4 ]] && grep -q "untracked/b" "$t/check.out"' rm -r "$t/review/untracked" git -C "$repo" config --unset status.showUntrackedFiles +# A worktree the snapshot can't read: no snapshot. Two failed snapshots would compare equal. +expect "tripwire: a snapshot of a detached HEAD succeeds" 'tripwire_snapshot "$repo" "$t/review" "$t/snap-ok"' +git -C "$repo" config status.showUntrackedFiles bogus +expect "tripwire: a snapshot fails when status fails" \ + '! tripwire_snapshot "$repo" "$t/review" "$t/snap-bad" 2>/dev/null && grep -q "^status failed" "$t/snap-bad/worktree.txt"' +git -C "$repo" config --unset status.showUntrackedFiles +git init -q "$t/unborn" +expect "tripwire: a snapshot fails when HEAD can't be read" \ + '! tripwire_snapshot "$t/unborn" "$t/unborn" "$t/snap-unborn" 2>/dev/null' +expect "tripwire: a snapshot fails when the worktree is gone" \ + '! tripwire_snapshot "$repo" "$t/nowhere" "$t/snap-gone" 2>/dev/null' git init -q "$t/sub" git -C "$t/sub" commit -q --allow-empty -m s git init -q "$t/sup" diff --git a/tests/passes.sh b/tests/passes.sh index 84580da..c1ecf57 100644 --- a/tests/passes.sh +++ b/tests/passes.sh @@ -21,7 +21,9 @@ in_lib() { # script (runs with lib sourced and the stub pi on PATH) PATH="$stub:$PATH" bash -c "set -euo pipefail; source '$stub/lib.sh'; source '$root/lib/guard.sh' SESSION_DIR='$stub' REVIEW_DIR='$stub/repo' MODEL=m; $1" 2>&1 || echo "(exit $?)" } +# The review checkout has a commit: the tripwire can't read an unborn HEAD. git init -q "$stub/repo" +git -C "$stub/repo" -c user.name=a -c user.email=a@x commit -q --allow-empty -m c printf 'PROMPT' >"$stub/p.md" # shellcheck disable=SC2016 # the stub must expand $* when it runs, not now From 5cb9d4376b3a6659184264001543c4c0690a7654 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 13:51:51 +0100 Subject: [PATCH 07/23] Keep the reviewer's git diff from refreshing the index --- lib/guard.sh | 7 ++++--- tests/guard.sh | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/guard.sh b/lib/guard.sh index d150dfb..85816f4 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -10,8 +10,8 @@ pi_bin() { # } # Runs a command (pi) in the sandbox: the wrapper in first on PATH, the real git (the git on PATH now) in -# GH_PR_REVIEW_REAL_GIT, no credential helper, no hooks, no password prompt, no ssh, and no optional locks: `git status` -# does not rewrite the index. +# GH_PR_REVIEW_REAL_GIT, no credential helper, no hooks, no password prompt, no ssh, no optional locks and no index +# refresh in `git diff`: `git status` and `git diff` do not rewrite the index. pi_env() { # [args...] local bin=$1 real shift @@ -20,8 +20,9 @@ pi_env() { # [args...] return 1 } env PATH="$bin:$PATH" GH_PR_REVIEW_REAL_GIT="$real" \ - GIT_CONFIG_COUNT=2 GIT_CONFIG_KEY_0=credential.helper GIT_CONFIG_VALUE_0= \ + GIT_CONFIG_COUNT=3 GIT_CONFIG_KEY_0=credential.helper GIT_CONFIG_VALUE_0= \ GIT_CONFIG_KEY_1=core.hooksPath GIT_CONFIG_VALUE_1=/dev/null \ + GIT_CONFIG_KEY_2=diff.autoRefreshIndex GIT_CONFIG_VALUE_2=false \ GIT_TERMINAL_PROMPT=0 GIT_ASKPASS=false SSH_ASKPASS=false GIT_SSH_COMMAND=false \ GIT_OPTIONAL_LOCKS=0 "$@" } diff --git a/tests/guard.sh b/tests/guard.sh index 639f281..d15d278 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -114,6 +114,14 @@ expect "tripwire: its snapshot leaves a stale index unchanged" '[[ "$(cksum <"$i sum=$(stale "$idx" 3) git -C "$idx" status >/dev/null expect "pi_env: git status rewrites the stale index outside it (control)" '[[ "$(cksum <"$idx/.git/index")" != "$sum" ]]' +# `git diff` refreshes the index on its own (diff.autoRefreshIndex), optional locks or not. +sum=$(stale "$idx" 4) +pi_env "$t/bin" bash -c 'cd "$1" && git diff >/dev/null' _ "$idx" +expect "pi_env: git diff leaves a stale index unchanged" '[[ "$(cksum <"$idx/.git/index")" == "$sum" ]]' +sum=$(stale "$idx" 5) +GIT_OPTIONAL_LOCKS=0 git -C "$idx" diff >/dev/null +expect "pi_env: git diff rewrites the stale index outside it, even without optional locks (control)" \ + '[[ "$(cksum <"$idx/.git/index")" != "$sum" ]]' expect "pi_env: the wrapper is git on PATH" '[[ "$(pi_env "$t/bin" bash -c "command -v git")" == "$t/bin/git" ]]' expect "pi_env: real git is the git outside it" '[[ "$(pi_env "$t/bin" printenv GH_PR_REVIEW_REAL_GIT)" == "$real" ]]' From d9e501633dc7195a57e553d772a10030a5459718 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 13:52:23 +0100 Subject: [PATCH 08/23] Turn off core.fsmonitor for the reviewer's git --- lib/guard.sh | 7 ++++--- tests/guard.sh | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/guard.sh b/lib/guard.sh index 85816f4..b0a2ad9 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -10,8 +10,8 @@ pi_bin() { # } # Runs a command (pi) in the sandbox: the wrapper in first on PATH, the real git (the git on PATH now) in -# GH_PR_REVIEW_REAL_GIT, no credential helper, no hooks, no password prompt, no ssh, no optional locks and no index -# refresh in `git diff`: `git status` and `git diff` do not rewrite the index. +# GH_PR_REVIEW_REAL_GIT, no credential helper, no hooks (core.fsmonitor included), no password prompt, no ssh, no +# optional locks and no index refresh in `git diff`: `git status` and `git diff` do not rewrite the index. pi_env() { # [args...] local bin=$1 real shift @@ -20,9 +20,10 @@ pi_env() { # [args...] return 1 } env PATH="$bin:$PATH" GH_PR_REVIEW_REAL_GIT="$real" \ - GIT_CONFIG_COUNT=3 GIT_CONFIG_KEY_0=credential.helper GIT_CONFIG_VALUE_0= \ + GIT_CONFIG_COUNT=4 GIT_CONFIG_KEY_0=credential.helper GIT_CONFIG_VALUE_0= \ GIT_CONFIG_KEY_1=core.hooksPath GIT_CONFIG_VALUE_1=/dev/null \ GIT_CONFIG_KEY_2=diff.autoRefreshIndex GIT_CONFIG_VALUE_2=false \ + GIT_CONFIG_KEY_3=core.fsmonitor GIT_CONFIG_VALUE_3=false \ GIT_TERMINAL_PROMPT=0 GIT_ASKPASS=false SSH_ASKPASS=false GIT_SSH_COMMAND=false \ GIT_OPTIONAL_LOCKS=0 "$@" } diff --git a/tests/guard.sh b/tests/guard.sh index d15d278..bea004f 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -122,6 +122,15 @@ sum=$(stale "$idx" 5) GIT_OPTIONAL_LOCKS=0 git -C "$idx" diff >/dev/null expect "pi_env: git diff rewrites the stale index outside it, even without optional locks (control)" \ '[[ "$(cksum <"$idx/.git/index")" != "$sum" ]]' +# A file system monitor is a hook core.hooksPath does not cover. +printf '#!/bin/sh\ntouch "%s/fsmonitor.ran"\n' "$t" >"$t/fsmonitor" +chmod +x "$t/fsmonitor" +git -C "$idx" config core.fsmonitor "$t/fsmonitor" +pi_env "$t/bin" bash -c 'cd "$1" && git status >/dev/null 2>&1' _ "$idx" +expect "pi_env: a core.fsmonitor hook does not run" '[[ ! -e "$t/fsmonitor.ran" ]]' +git -C "$idx" status >/dev/null 2>&1 +expect "pi_env: the core.fsmonitor hook runs outside it (control)" '[[ -e "$t/fsmonitor.ran" ]]' +git -C "$idx" config --unset core.fsmonitor expect "pi_env: the wrapper is git on PATH" '[[ "$(pi_env "$t/bin" bash -c "command -v git")" == "$t/bin/git" ]]' expect "pi_env: real git is the git outside it" '[[ "$(pi_env "$t/bin" printenv GH_PR_REVIEW_REAL_GIT)" == "$real" ]]' From 064414a1e13deae59dab38fe15bb6eed7273e564 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 14:02:40 +0100 Subject: [PATCH 09/23] Refuse to run the reviewer with a git that ignores GIT_CONFIG_COUNT --- README.md | 2 +- gh-pr-review | 10 ++++++++++ lib/guard.sh | 15 ++++++++++++++- tests/flow.sh | 18 +++++++++++++++++- tests/guard.sh | 10 ++++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 591f8bc..e91394d 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ gh pr-review https://github.com/owner/repo/pull/42 | What | Why | Get it | | --- | --- | --- | | `gh` (authenticated) | PR metadata, posting comments, this extension | `gh auth login` | -| `git`, `jq` | clone + JSON plumbing | `brew install jq` | +| `git` 2.31+, `jq` | clone + JSON plumbing (the reviewer's read-only git needs `GIT_CONFIG_COUNT`, git 2.31) | `brew install jq` | | Node 22+ | runtime for pi | https://nodejs.org | | `pi` | headless review agent | `npm install -g @earendil-works/pi-coding-agent` | | GitHub Copilot subscription | model access | run `pi`, then `/login` → GitHub Copilot | diff --git a/gh-pr-review b/gh-pr-review index 1829d51..62b8967 100755 --- a/gh-pr-review +++ b/gh-pr-review @@ -344,6 +344,16 @@ run_preflight() { "run: npm install -g @earendil-works/pi-coding-agent (requires Node 22+)" fi + # The reviewer's read-only git turns credential helpers and hooks off through GIT_CONFIG_COUNT (lib/guard.sh). + if command -v git &>/dev/null; then + if git_env_config; then + pf_ok "git reads config from the environment (2.31 or later)" + else + pf_fail "this git ignores GIT_CONFIG_COUNT, so the reviewer's git can't be made read-only" \ + "install git 2.31 or later" + fi + fi + if [[ "$MODEL" == github-copilot/* ]]; then if command -v jq &>/dev/null && [[ -f "$PI_AUTH_FILE" ]] && jq -e 'has("github-copilot")' "$PI_AUTH_FILE" &>/dev/null; then diff --git a/lib/guard.sh b/lib/guard.sh index b0a2ad9..394abe0 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -9,9 +9,17 @@ pi_bin() { # chmod +x "$1/git" } +# 0 when git reads config from the environment (GIT_CONFIG_COUNT: git 2.31 and later). pi_env turns credentials and +# hooks off that way; an older git ignores it. +git_env_config() { + [[ $(GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=gh-pr-review.probe GIT_CONFIG_VALUE_0=ok \ + git config --get gh-pr-review.probe 2>/dev/null) == ok ]] +} + # Runs a command (pi) in the sandbox: the wrapper in first on PATH, the real git (the git on PATH now) in # GH_PR_REVIEW_REAL_GIT, no credential helper, no hooks (core.fsmonitor included), no password prompt, no ssh, no -# optional locks and no index refresh in `git diff`: `git status` and `git diff` do not rewrite the index. +# optional locks and no index refresh in `git diff`: `git status` and `git diff` do not rewrite the index. Refuses +# (1) with a git that ignores GIT_CONFIG_COUNT. pi_env() { # [args...] local bin=$1 real shift @@ -19,6 +27,11 @@ pi_env() { # [args...] echo "Error: git not found" >&2 return 1 } + git_env_config || { + echo "Error: this git ignores GIT_CONFIG_COUNT, so the reviewer's git can't be made read-only:" \ + "install git 2.31 or later" >&2 + return 1 + } env PATH="$bin:$PATH" GH_PR_REVIEW_REAL_GIT="$real" \ GIT_CONFIG_COUNT=4 GIT_CONFIG_KEY_0=credential.helper GIT_CONFIG_VALUE_0= \ GIT_CONFIG_KEY_1=core.hooksPath GIT_CONFIG_VALUE_1=/dev/null \ diff --git a/tests/flow.sh b/tests/flow.sh index 5eb3241..6690289 100644 --- a/tests/flow.sh +++ b/tests/flow.sh @@ -20,7 +20,7 @@ setup() { # approach-output code-review-output [discussion-exit-code] [failing-p echo '{"comments":[],"reviews":[]}' >"$stub/discussion.json" echo '[]' >"$stub/rest-reviews.json" echo '[]' >"$stub/rest-comments.json" - rm -f "$stub/git.base" "$stub/git.missing" "$stub/git.rebased" "$stub/git.retargeted" "$stub/git.diff-fails" "$stub/git.far-base" "$stub/git.deepened" + rm -f "$stub/git.base" "$stub/git.missing" "$stub/git.rebased" "$stub/git.retargeted" "$stub/git.diff-fails" "$stub/git.far-base" "$stub/git.deepened" "$stub/git.old" # The PR head the Nth "pr view" returns: line N of heads, or its last line. The clone checks out line 1. echo head1 >"$stub/heads" cat >"$stub/bin/gh" <"$stub/bin/git" <<'S' #!/usr/bin/env bash s=$(cd "$(dirname "$0")/.." && pwd) H=$(head -1 "$s/heads" 2>/dev/null || echo aaaa) B=$(cat "$s/git.base" 2>/dev/null || echo bbbb) for a in "$@"; do [[ "$a" == clone ]] && mkdir -p "${@: -1}"; done case "$*" in +*"config --get gh-pr-review.probe") [[ -f "$s/git.old" || ${GIT_CONFIG_KEY_0:-} != gh-pr-review.probe ]] || echo "${GIT_CONFIG_VALUE_0:-}" ;; *"rev-parse --is-shallow-repository"*) echo false ;; *"rev-parse --verify"*) sha=${*: -1} @@ -1413,6 +1415,20 @@ expect "record failed in round 3: round 4 has no previous-round rules and no pre expect "record failed in round 3: round 4, a clean full review, approves" \ '[[ $flag == --approve && $(jq -c "[.counts.open_blocking, .decision]" <<<"$record") == "[0,\"approve\"]" ]]' +# A git that ignores GIT_CONFIG_COUNT can't keep the reviewer read-only: --doctor says so, and a run stops before pi. +setup $'**Approach:** Sound' "$clean" +touch "$stub/git.old" +run --doctor +expect "old git: --doctor reports a git that ignores GIT_CONFIG_COUNT" \ + '[[ $rc == 1 && $calls == 0 ]] && grep -q "git ignores GIT_CONFIG_COUNT" "$stub/out.log" && grep -q "2.31" "$stub/out.log"' +run +expect "old git: a run stops before any model call and posts nothing" \ + '[[ $rc == 1 && $calls == 0 && ! -e "$stub/posted.json" ]] && grep -q "git ignores GIT_CONFIG_COUNT" "$stub/out.log"' +rm "$stub/git.old" +run --doctor +expect "old git: --doctor passes the check with a git that reads GIT_CONFIG_COUNT (control)" \ + '[[ $rc == 0 ]] && grep -q "^✓ git reads config from the environment" "$stub/out.log"' + # The reviewer's sandbox, on real git: the clone comes from a local bare repo (url..insteadOf through # GIT_CONFIG_COUNT), never the network. The stub git can't see a change, so this case runs without it. setup $'**Approach:** Sound' "$clean" diff --git a/tests/guard.sh b/tests/guard.sh index bea004f..e46d38e 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -134,6 +134,16 @@ git -C "$idx" config --unset core.fsmonitor expect "pi_env: the wrapper is git on PATH" '[[ "$(pi_env "$t/bin" bash -c "command -v git")" == "$t/bin/git" ]]' expect "pi_env: real git is the git outside it" '[[ "$(pi_env "$t/bin" printenv GH_PR_REVIEW_REAL_GIT)" == "$real" ]]' +# A git older than 2.31 ignores GIT_CONFIG_COUNT: no credential helper or hook would be turned off. +mkdir -p "$t/oldgit" +printf '#!/bin/sh\nunset GIT_CONFIG_COUNT\nexec "%s" "$@"\n' "$real" >"$t/oldgit/git" +chmod +x "$t/oldgit/git" +expect "git_env_config: a git that reads GIT_CONFIG_COUNT passes" 'git_env_config' +expect "git_env_config: a git that ignores GIT_CONFIG_COUNT fails" '! PATH="$t/oldgit:$PATH" git_env_config' +PATH="$t/oldgit:$PATH" pi_env "$t/bin" touch "$t/old.ran" 2>"$t/old.err" || true +expect "pi_env: with a git that ignores GIT_CONFIG_COUNT, it refuses to run the command" \ + '[[ ! -e "$t/old.ran" ]] && grep -q "2.31" "$t/old.err"' + # ── tripwire ── git -C "$repo" checkout -q -- f git -C "$repo" worktree add -q --detach "$t/review" main From 59436c9fc2b1dfb3fda5fa0da1f1faea5a430138 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 14:02:55 +0100 Subject: [PATCH 10/23] Say what the reviewer's sandbox disables, and that pi keeps its gh access --- README.md | 14 ++++++++------ lib/guard.sh | 5 +++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e91394d..2fc28dc 100644 --- a/README.md +++ b/README.md @@ -322,12 +322,14 @@ commit, by the same account, does not count. Run The reviewer (pi) works read-only. Its `git` is a wrapper that runs only the commands that read (`show`, `diff`, `log`, `range-diff`, `status`, …) and -refuses the rest, with no credentials and no hooks. A snapshot of the review -checkout (its HEAD and `git status`) is taken before and after each model call: -if the reviewer changed it anyway, the run stops before posting, says what -changed, keeps the session folder, and exits with code 4. A checkout the -snapshot can't read (its HEAD or `git status` fails) stops the run the same -way: a change would go unseen. +refuses the rest. Git hooks, credential helpers and prompts are disabled. pi +keeps its authenticated `gh` access (with `--user`, that account's token), so +the wrapper is a guard rail against mistakes, not credential isolation. A +snapshot of the review checkout (its HEAD and `git status`) is taken before +and after each model call: if the reviewer changed it anyway, the run stops +before posting, says what changed, keeps the session folder, and exits with +code 4. A checkout the snapshot can't read (its HEAD or `git status` fails) +stops the run the same way: a change would go unseen. `gh pr-review --check` runs the check alone and exits 0 (run), 3 (stop) or 5 (no new commits); `--check --json` prints the decision as JSON for diff --git a/lib/guard.sh b/lib/guard.sh index 394abe0..9b9e89b 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash -# The reviewer's git sandbox: a read-only git on pi's PATH, no credentials, no hooks, and a tripwire on the review -# worktree around each pi call. Functions only. +# The reviewer's git sandbox: a read-only git on pi's PATH, no git credential helper or prompt, no hooks, and a +# tripwire on the review worktree around each pi call. A guard rail, not credential isolation: pi keeps its gh access. +# Functions only. # Puts lib/git-ro in as `git`, for pi_env. pi_bin() { # From 716f4c8a9654f710050949bec055f18807ac9cad Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 14:03:56 +0100 Subject: [PATCH 11/23] Keep the tripwire's clone warning in the session --- README.md | 2 ++ gh-pr-review | 5 +++-- lib/guard.sh | 12 ++++++++---- tests/flow.sh | 3 +++ tests/guard.sh | 7 +++++-- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2fc28dc..4bb45ae 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,8 @@ your current directory, so you can inspect or re-post later: - `round.json`: the round record (see below); `posted.md`: the exact body posted - `context/`: the PR description, the discussion and the inline review threads both passes read - `repo/`: the checkout +- `tripwire/`: the snapshots of the checkout around each model call; `warnings.txt`, when there is one: a change in + the clone only (its refs, HEAD or stash) while the reviewer ran Add `pr-review-sessions/` to `.gitignore` if you run it inside a repo. diff --git a/gh-pr-review b/gh-pr-review index 62b8967..f35202d 100755 --- a/gh-pr-review +++ b/gh-pr-review @@ -241,7 +241,8 @@ write_pr_context() { # ── passes ────────────────────────────────────────────────────────────────────────── # One headless pi call: prompt file in, model output file out. A failed call stops the run. pi runs in the git # sandbox (lib/guard.sh), between two snapshots of the review checkout: a reviewer that changed it stops the run with -# exit 4, before anything is posted. So does a checkout a snapshot can't read: a change would go unseen. +# exit 4, before anything is posted. So does a checkout a snapshot can't read: a change would go unseen. A change in +# the clone only is a warning, kept in the session's warnings.txt. run_pi() { # local snap code=0 changed=0 snap="${SESSION_DIR}/tripwire/$(basename "$2" .md)" @@ -249,7 +250,7 @@ run_pi() { # tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/before" || unreadable_checkout "$snap/before" pi_env "${SESSION_DIR}/bin" pi ${PI_ARGS[@]+"${PI_ARGS[@]}"} "$(cat "$1")" >"$2" || code=$? tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/after" || unreadable_checkout "$snap/after" - tripwire_check "$snap/before" "$snap/after" || changed=$? + tripwire_check "$snap/before" "$snap/after" "${SESSION_DIR}/warnings.txt" || changed=$? if [[ $changed -ne 0 ]]; then echo "" >&2 echo "Not posted (exit 4): the reviewer changed the review checkout, which must stay read-only." >&2 diff --git a/lib/guard.sh b/lib/guard.sh index 9b9e89b..420b93d 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -71,8 +71,8 @@ tripwire_snapshot() { # } # 0 when nothing changed. 4 when the worktree changed: prints what did. A change in the clone only (a commit or a -# branch I make while pi runs) is no stop: 0, with a warning. -tripwire_check() { # +# branch I make while pi runs) is no stop: 0, with a warning, also added to when given. +tripwire_check() { # [warnings-file] local clone=same cmp -s "$1/clone.txt" "$2/clone.txt" || clone=changed if ! cmp -s "$1/worktree.txt" "$2/worktree.txt"; then @@ -85,8 +85,12 @@ tripwire_check() { # return 4 fi if [[ $clone == changed ]]; then - echo "Warning: the clone's refs, HEAD or stash changed while the reviewer ran (the review worktree did not):" >&2 - { diff -u "$1/clone.txt" "$2/clone.txt" || true; } | tail -n +3 >&2 + { + echo "Warning: the clone's refs, HEAD or stash changed while the reviewer ran (the review worktree did not):" + { diff -u "$1/clone.txt" "$2/clone.txt" || true; } | tail -n +3 + } >"$2/warning.txt" + cat "$2/warning.txt" >&2 + [[ -z "${3:-}" ]] || cat "$2/warning.txt" >>"$3" fi return 0 } diff --git a/tests/flow.sh b/tests/flow.sh index 6690289..ecfd7db 100644 --- a/tests/flow.sh +++ b/tests/flow.sh @@ -1459,6 +1459,9 @@ run expect "read-only reviewer: a new branch in the clone only warns, and the review is posted" \ '[[ $rc == 0 && -e "$stub/posted.json" ]] && grep -q "^Warning: the clone.s refs" "$stub/out.log" && grep -q "refs/heads/side" "$stub/out.log"' +expect "read-only reviewer: the clone's warning is saved in the session" \ + 'grep -q "^Warning: the clone.s refs" "$stub"/work/pr-review-sessions/pr-review-*/warnings.txt && + grep -q "refs/heads/side" "$stub"/work/pr-review-sessions/pr-review-*/warnings.txt' rm "$stub/pi.git" echo "config status.showUntrackedFiles bogus" >"$stub/pi.evil" run diff --git a/tests/guard.sh b/tests/guard.sh index e46d38e..8da4b99 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -150,12 +150,12 @@ git -C "$repo" worktree add -q --detach "$t/review" main snap() { tripwire_snapshot "$repo" "$t/review" "$t/snap$1"; } check() { # before after rc=0 - tripwire_check "$t/snap$1" "$t/snap$2" >"$t/check.out" 2>&1 || rc=$? + tripwire_check "$t/snap$1" "$t/snap$2" "$t/warnings.txt" >"$t/check.out" 2>&1 || rc=$? } snap 1 snap 2 check 1 2 -expect "tripwire: no change gives 0, quietly" '[[ $rc == 0 && ! -s "$t/check.out" ]]' +expect "tripwire: no change gives 0, quietly" '[[ $rc == 0 && ! -s "$t/check.out" && ! -e "$t/warnings.txt" ]]' git -C "$repo" update-ref refs/pr-review/1/round-1 HEAD snap 3 check 2 3 @@ -170,6 +170,9 @@ snap 5 check 4 5 expect "tripwire: a new branch in the clone gives 0, with a warning" \ '[[ $rc == 0 ]] && grep -q "^Warning" "$t/check.out" && grep -q "refs/heads/newb" "$t/check.out"' +expect "tripwire: the clone's warning is saved too" \ + 'grep -q "^Warning" "$t/warnings.txt" && grep -q "refs/heads/newb" "$t/warnings.txt"' +rm -f "$t/warnings.txt" touch "$t/review/new-file" snap 6 check 5 6 From 55324a29676b75ca926209e4285021ef51537fb2 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 14:04:36 +0100 Subject: [PATCH 12/23] Isolate git config before the flow test's real-git fixture --- tests/flow.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/flow.sh b/tests/flow.sh index ecfd7db..2081608 100644 --- a/tests/flow.sh +++ b/tests/flow.sh @@ -1433,6 +1433,11 @@ expect "old git: --doctor passes the check with a git that reads GIT_CONFIG_COUN # GIT_CONFIG_COUNT), never the network. The stub git can't see a change, so this case runs without it. setup $'**Approach:** Sound' "$clean" rm "$stub/bin/git" +# The fixture repos and the run use this stub's git config only: no signing, hooks or other settings of mine. +unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_CONFIG \ + GIT_CONFIG_PARAMETERS GIT_CEILING_DIRECTORIES +export HOME="$stub/home" XDG_CONFIG_HOME="$stub/home/.config" GIT_CONFIG_NOSYSTEM=1 \ + GIT_CONFIG_GLOBAL="$stub/home/.gitconfig" export GIT_AUTHOR_NAME=a GIT_AUTHOR_EMAIL=a@x GIT_COMMITTER_NAME=a GIT_COMMITTER_EMAIL=a@x git init -q --bare "$stub/r.git" git -C "$stub/r.git" symbolic-ref HEAD refs/heads/main From 2c5a0295f85ceb29e8ee09ba8f71a53f7002e9f4 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 14:13:53 +0100 Subject: [PATCH 13/23] Refuse git describe --dirty and --broken in the reviewer's git: they write the index --- lib/git-ro | 12 ++++++++++++ tests/guard.sh | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/git-ro b/lib/git-ro index 3d582ef..8a164f5 100755 --- a/lib/git-ro +++ b/lib/git-ro @@ -26,6 +26,18 @@ while [[ $i -le $# ]]; do esac done +# describe --dirty and --broken refresh the index, and no setting turns that off (pi_env does for status and diff). +if [[ "$sub" == describe ]]; then + for arg in "${@:i+1}"; do + case "$arg" in + --dirty | --dirty=* | --broken | --broken=*) + echo "git describe --dirty and --broken are not allowed in a read-only review: they write the index" >&2 + exit 1 + ;; + esac + done +fi + case "$sub" in show | diff | log | blame | grep | ls-files | ls-tree | cat-file | rev-parse | rev-list | merge-base | range-diff | \ for-each-ref | describe | shortlog | status | help | version | --version | --help) diff --git a/tests/guard.sh b/tests/guard.sh index 8da4b99..8f58ba0 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -122,6 +122,21 @@ sum=$(stale "$idx" 5) GIT_OPTIONAL_LOCKS=0 git -C "$idx" diff >/dev/null expect "pi_env: git diff rewrites the stale index outside it, even without optional locks (control)" \ '[[ "$(cksum <"$idx/.git/index")" != "$sum" ]]' +# `git describe --dirty` (and --broken) refreshes the index too, and no setting turns that off: git-ro refuses them. +sum=$(stale "$idx" 6) +pi_env "$t/bin" bash -c 'cd "$1" && git describe --always --dirty >/dev/null 2>&1' _ "$idx" || true +expect "pi_env: git describe --dirty leaves a stale index unchanged" '[[ "$(cksum <"$idx/.git/index")" == "$sum" ]]' +sum=$(stale "$idx" 7) +GIT_OPTIONAL_LOCKS=0 git -c diff.autoRefreshIndex=false -C "$idx" describe --always --dirty >/dev/null +expect "pi_env: git describe --dirty rewrites the stale index outside it, whatever the settings (control)" \ + '[[ "$(cksum <"$idx/.git/index")" != "$sum" ]]' +for args in "describe --always" "describe --tags --always HEAD~1"; do + expect "git-ro passes: $args" "ro $args" +done +for args in "describe --dirty" "describe --always --dirty=-mod" "describe --broken" "-c x.y=z describe --tags --broken=-b"; do + expect "git-ro refuses: $args" \ + "! ro $args && grep -q '^git describe --dirty and --broken are not allowed in a read-only review' \"\$t/ro.out\"" +done # A file system monitor is a hook core.hooksPath does not cover. printf '#!/bin/sh\ntouch "%s/fsmonitor.ran"\n' "$t" >"$t/fsmonitor" chmod +x "$t/fsmonitor" From e0d727aff75d0e15a5ae825faef5ea071ad74aaa Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 17:10:34 +0100 Subject: [PATCH 14/23] Drop the settings a parent git passes on in the reviewer's sandbox --- lib/guard.sh | 7 ++++--- tests/guard.sh | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/guard.sh b/lib/guard.sh index 420b93d..b9d21c5 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -19,8 +19,9 @@ git_env_config() { # Runs a command (pi) in the sandbox: the wrapper in first on PATH, the real git (the git on PATH now) in # GH_PR_REVIEW_REAL_GIT, no credential helper, no hooks (core.fsmonitor included), no password prompt, no ssh, no -# optional locks and no index refresh in `git diff`: `git status` and `git diff` do not rewrite the index. Refuses -# (1) with a git that ignores GIT_CONFIG_COUNT. +# optional locks and no index refresh in `git diff`: `git status` and `git diff` do not rewrite the index. Drops the +# settings a parent git passes on (`git -c ...` running an alias), which would win over these. Refuses (1) with a git +# that ignores GIT_CONFIG_COUNT. pi_env() { # [args...] local bin=$1 real shift @@ -33,7 +34,7 @@ pi_env() { # [args...] "install git 2.31 or later" >&2 return 1 } - env PATH="$bin:$PATH" GH_PR_REVIEW_REAL_GIT="$real" \ + env -u GIT_CONFIG_PARAMETERS PATH="$bin:$PATH" GH_PR_REVIEW_REAL_GIT="$real" \ GIT_CONFIG_COUNT=4 GIT_CONFIG_KEY_0=credential.helper GIT_CONFIG_VALUE_0= \ GIT_CONFIG_KEY_1=core.hooksPath GIT_CONFIG_VALUE_1=/dev/null \ GIT_CONFIG_KEY_2=diff.autoRefreshIndex GIT_CONFIG_VALUE_2=false \ diff --git a/tests/guard.sh b/tests/guard.sh index 8f58ba0..dd56e55 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -146,6 +146,43 @@ expect "pi_env: a core.fsmonitor hook does not run" '[[ ! -e "$t/fsmonitor.ran" git -C "$idx" status >/dev/null 2>&1 expect "pi_env: the core.fsmonitor hook runs outside it (control)" '[[ -e "$t/fsmonitor.ran" ]]' git -C "$idx" config --unset core.fsmonitor +# Settings a parent git passes on (`git -c core.fsmonitor=... ` running gh-pr-review) come in +# GIT_CONFIG_PARAMETERS, which wins over GIT_CONFIG_COUNT: pi_env drops them. +mkdir -p "$t/phooks" +printf '#!/bin/sh\ntouch "%s/phook.ran"\n' "$t" >"$t/phooks/pre-commit" +chmod +x "$t/phooks/pre-commit" +echo "https://u:s3cret@example.com" >"$t/pcreds" +params="'core.fsmonitor'='$t/fsmonitor' 'credential.helper'='store --file=$t/pcreds' 'core.hooksPath'='$t/phooks'" +params+=" 'diff.autoRefreshIndex'='true'" +expect "pi_env: inherited config gives way to the sandbox's" \ + '[[ "$(GIT_CONFIG_PARAMETERS=$params pi_env "$t/bin" bash -c "cd \"\$1\" && + \"\$GH_PR_REVIEW_REAL_GIT\" config --get core.fsmonitor && \"\$GH_PR_REVIEW_REAL_GIT\" config --get-all credential.helper; + \"\$GH_PR_REVIEW_REAL_GIT\" config --get core.hooksPath && \"\$GH_PR_REVIEW_REAL_GIT\" config --get diff.autoRefreshIndex" \ + _ "$idx")" == $'"'"'false\n\n/dev/null\nfalse'"'"' ]]' +expect "pi_env: inherited config is there outside it (control)" \ + '[[ "$(cd "$idx" && GIT_CONFIG_PARAMETERS=$params git config --get core.fsmonitor)" == "$t/fsmonitor" ]]' +rm -f "$t/fsmonitor.ran" +GIT_CONFIG_PARAMETERS=$params pi_env "$t/bin" bash -c 'cd "$1" && git status >/dev/null 2>&1' _ "$idx" +expect "pi_env: an inherited core.fsmonitor hook does not run" '[[ ! -e "$t/fsmonitor.ran" ]]' +(cd "$idx" && GIT_CONFIG_PARAMETERS=$params git status >/dev/null 2>&1) +expect "pi_env: the inherited core.fsmonitor hook runs outside it (control)" '[[ -e "$t/fsmonitor.ran" ]]' +expect "pi_env: an inherited credential helper returns nothing" \ + '[[ -z "$(GIT_CONFIG_PARAMETERS=$params pi_env "$t/bin" bash -c "\"\$GH_PR_REVIEW_REAL_GIT\" credential fill" <<<"$cred" 2>/dev/null)" ]]' +expect "pi_env: the inherited credential helper returns the password outside it (control)" \ + 'GIT_CONFIG_PARAMETERS=$params git credential fill <<<"$cred" 2>/dev/null | grep -q password=s3cret' +# shellcheck disable=SC2016 # expanded by the inner shell, where pi_env set it +GIT_CONFIG_PARAMETERS=$params pi_env "$t/bin" bash -c 'cd "$1" && "$GH_PR_REVIEW_REAL_GIT" commit -q --allow-empty -m h' _ "$t/hooked" +expect "pi_env: an inherited core.hooksPath hook does not run" '[[ ! -e "$t/phook.ran" ]]' +(cd "$t/hooked" && GIT_CONFIG_PARAMETERS=$params git commit -q --allow-empty -m h 2>/dev/null) +expect "pi_env: the inherited core.hooksPath hook runs outside it (control)" '[[ -e "$t/phook.ran" ]]' +sum=$(stale "$idx" 8) +GIT_CONFIG_PARAMETERS=$params pi_env "$t/bin" bash -c 'cd "$1" && git diff >/dev/null' _ "$idx" +expect "pi_env: git diff leaves a stale index unchanged with an inherited diff.autoRefreshIndex" \ + '[[ "$(cksum <"$idx/.git/index")" == "$sum" ]]' +sum=$(stale "$idx" 9) +(cd "$idx" && GIT_CONFIG_PARAMETERS=$params GIT_OPTIONAL_LOCKS=0 git diff >/dev/null 2>&1) +expect "pi_env: git diff rewrites the stale index outside it with the inherited setting (control)" \ + '[[ "$(cksum <"$idx/.git/index")" != "$sum" ]]' expect "pi_env: the wrapper is git on PATH" '[[ "$(pi_env "$t/bin" bash -c "command -v git")" == "$t/bin/git" ]]' expect "pi_env: real git is the git outside it" '[[ "$(pi_env "$t/bin" printenv GH_PR_REVIEW_REAL_GIT)" == "$real" ]]' From 4d54f56245e64dc1e8b956f8794a32781d7ddf7e Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 17:11:44 +0100 Subject: [PATCH 15/23] Refuse abbreviated git describe --dirty and --broken in the reviewer's git --- lib/git-ro | 8 ++++---- tests/guard.sh | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/git-ro b/lib/git-ro index 8a164f5..0fb1e88 100755 --- a/lib/git-ro +++ b/lib/git-ro @@ -27,14 +27,14 @@ while [[ $i -le $# ]]; do done # describe --dirty and --broken refresh the index, and no setting turns that off (pi_env does for status and diff). +# Git takes any unique prefix of an option name (--dir, --brok=x): refused too, and so is an ambiguous --d. if [[ "$sub" == describe ]]; then for arg in "${@:i+1}"; do - case "$arg" in - --dirty | --dirty=* | --broken | --broken=*) + name=${arg%%=*} + if [[ ${#name} -gt 2 && (--dirty == "$name"* || --broken == "$name"*) ]]; then echo "git describe --dirty and --broken are not allowed in a read-only review: they write the index" >&2 exit 1 - ;; - esac + fi done fi diff --git a/tests/guard.sh b/tests/guard.sh index dd56e55..6f3ab91 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -97,7 +97,7 @@ git -C "$t/hooked" commit -q --allow-empty -m h expect "pi_env: the hook runs outside it (control)" '[[ -e "$t/hook.ran" ]]' # An index whose stat info is stale: `git status` refreshes it and writes it back, unless optional locks are off. stale() { # : a different mtime each time, so the index is stale again even after a rewrite - touch -t "20000101000$2" "$1/f" + touch -t "2000010100$(printf %02d "$2")" "$1/f" cksum <"$1/.git/index" } idx="$t/idx" @@ -130,12 +130,27 @@ sum=$(stale "$idx" 7) GIT_OPTIONAL_LOCKS=0 git -c diff.autoRefreshIndex=false -C "$idx" describe --always --dirty >/dev/null expect "pi_env: git describe --dirty rewrites the stale index outside it, whatever the settings (control)" \ '[[ "$(cksum <"$idx/.git/index")" != "$sum" ]]' -for args in "describe --always" "describe --tags --always HEAD~1"; do +# Git takes any unique prefix of an option (--dir, --brok=x): git-ro refuses those too, and an ambiguous --d. +sum=$(stale "$idx" 10) +GIT_OPTIONAL_LOCKS=0 git -c diff.autoRefreshIndex=false -C "$idx" describe --always --dir >/dev/null +expect "pi_env: git describe --dir rewrites the stale index outside it (control)" \ + '[[ "$(cksum <"$idx/.git/index")" != "$sum" ]]' +n=10 +for args in "describe --always" "describe --tags --always HEAD~1" "describe --debug --always" "describe --tags --always" \ + "describe --always --abbrev=7"; do expect "git-ro passes: $args" "ro $args" done -for args in "describe --dirty" "describe --always --dirty=-mod" "describe --broken" "-c x.y=z describe --tags --broken=-b"; do - expect "git-ro refuses: $args" \ - "! ro $args && grep -q '^git describe --dirty and --broken are not allowed in a read-only review' \"\$t/ro.out\"" +ro_idx() { # git args... (through the wrapper on pi's PATH, from the stale-index repo) + pi_env "$t/bin" bash -c 'cd "$1" && shift && git "$@"' _ "$idx" "$@" >"$t/ro.out" 2>&1 +} +for args in "describe --dirty" "describe --always --dirty=-mod" "describe --broken" "-c x.y=z describe --tags --broken=-b" \ + "describe --always --dir" "describe --always --dirt" "describe --always --di=-m" "describe --always --d" \ + "describe --always --br" "describe --always --brok=x" "describe --always --b" "describe --always --dirt=-m HEAD"; do + n=$((n + 1)) + sum=$(stale "$idx" "$n") + expect "git-ro refuses: $args, and the stale index is unchanged" \ + "! ro_idx $args && grep -q '^git describe --dirty and --broken are not allowed in a read-only review' \"\$t/ro.out\" && + [[ \"\$(cksum <\"\$idx/.git/index\")\" == \"\$sum\" ]]" done # A file system monitor is a hook core.hooksPath does not cover. printf '#!/bin/sh\ntouch "%s/fsmonitor.ran"\n' "$t" >"$t/fsmonitor" From 1b827981a9ff3504214ec275e1af555050d75bed Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 17:14:50 +0100 Subject: [PATCH 16/23] Stop the run when the tripwire's snapshot can't be saved --- README.md | 7 ++++--- gh-pr-review | 17 +++++++++++------ lib/guard.sh | 27 ++++++++++++++------------- tests/flow.sh | 14 ++++++++++++++ tests/guard.sh | 11 +++++++++++ 5 files changed, 54 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 4bb45ae..30d1418 100644 --- a/README.md +++ b/README.md @@ -330,8 +330,9 @@ the wrapper is a guard rail against mistakes, not credential isolation. A snapshot of the review checkout (its HEAD and `git status`) is taken before and after each model call: if the reviewer changed it anyway, the run stops before posting, says what changed, keeps the session folder, and exits with -code 4. A checkout the snapshot can't read (its HEAD or `git status` fails) -stops the run the same way: a change would go unseen. +code 4. A checkout the snapshot can't read (its HEAD or `git status` fails), +or a snapshot that can't be saved in the session folder, stops the run the +same way: a change would go unseen. `gh pr-review --check` runs the check alone and exits 0 (run), 3 (stop) or 5 (no new commits); `--check --json` prints the decision as JSON for @@ -342,7 +343,7 @@ scripts. `gh pr-review rounds ` shows the same decision under the history. | 0 | Done | | 1 | Error | | 3 | Stopped by the loop check | -| 4 | The reviewer changed the review checkout (its HEAD or its files), or it could not be read; nothing posted, the session is kept | +| 4 | The reviewer changed the review checkout (its HEAD or its files), or it could not be read or its snapshot saved, so a change could not be seen; nothing posted, the session is kept | | 5 | No new commits since the last round | | 6 | History changed while the review ran (someone posted another round, or a round went away); nothing posted, the session is kept | diff --git a/gh-pr-review b/gh-pr-review index f35202d..b17b92d 100755 --- a/gh-pr-review +++ b/gh-pr-review @@ -241,8 +241,8 @@ write_pr_context() { # ── passes ────────────────────────────────────────────────────────────────────────── # One headless pi call: prompt file in, model output file out. A failed call stops the run. pi runs in the git # sandbox (lib/guard.sh), between two snapshots of the review checkout: a reviewer that changed it stops the run with -# exit 4, before anything is posted. So does a checkout a snapshot can't read: a change would go unseen. A change in -# the clone only is a warning, kept in the session's warnings.txt. +# exit 4, before anything is posted. So does a checkout a snapshot can't read, or a snapshot that can't be saved: a +# change would go unseen. A change in the clone only is a warning, kept in the session's warnings.txt. run_pi() { # local snap code=0 changed=0 snap="${SESSION_DIR}/tripwire/$(basename "$2" .md)" @@ -265,12 +265,17 @@ run_pi() { # fi } -# Stops the run with exit 4: the review checkout could not be read (its snapshot says why), so a change by the -# reviewer could not be seen. +# Stops the run with exit 4: the review checkout could not be read (its snapshot says why), or its snapshot could +# not be saved, so a change by the reviewer could not be seen. unreadable_checkout() { # echo "" >&2 - echo "Not posted (exit 4): the review checkout could not be read, so a change by the reviewer could not be seen:" >&2 - sed 's/^/ /' "$1/worktree.txt" >&2 + if [[ -f "$1/worktree.txt" && -f "$1/clone.txt" ]]; then + echo "Not posted (exit 4): the review checkout could not be read, so a change by the reviewer could not be seen:" >&2 + sed 's/^/ /' "$1/worktree.txt" >&2 || true + else + echo "Not posted (exit 4): the snapshot of the review checkout could not be saved, so a change by the reviewer" \ + "could not be seen." >&2 + fi echo " The checkout is left as it is. Session: ${SESSION_DIR}" >&2 exit 4 } diff --git a/lib/guard.sh b/lib/guard.sh index b9d21c5..d6cd021 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -47,27 +47,28 @@ pi_env() { # [args...] # every untracked file and every submodule whatever the config says (status.showUntrackedFiles, diff.ignoreSubmodules, # submodule..ignore). clone.txt: the clone's refs but refs/pr-review/, its HEAD and its stash. Errors go in the # files. 1 when the worktree's HEAD or status can't be read (a detached HEAD is fine): two such snapshots would compare -# equal, so the worktree can't be checked. +# equal, so the worktree can't be checked. 1 too when or a file in it can't be written: no snapshot. Each +# step is checked: a caller's `|| ...` turns errexit off in here. tripwire_snapshot() { # - local g=(--no-optional-locks -c core.hooksPath=/dev/null -c core.fsmonitor=false) ok=0 head branch code=0 - mkdir -p "$3" + local g=(--no-optional-locks -c core.hooksPath=/dev/null -c core.fsmonitor=false) ok=0 head branch status clone code=0 head=$(git -C "$2" "${g[@]}" rev-parse --verify HEAD 2>&1) || ok=1 branch=$(git -C "$2" "${g[@]}" symbolic-ref -q HEAD 2>&1) || code=$? [[ $code -le 1 ]] || ok=1 - { - echo "HEAD $head" - echo "branch $branch" - git -C "$2" "${g[@]}" status --porcelain --untracked-files=all --ignore-submodules=none 2>&1 || { - echo "status failed: $?" - ok=1 - } - } >"$3/worktree.txt" - { + status=$(git -C "$2" "${g[@]}" status --porcelain --untracked-files=all --ignore-submodules=none 2>&1) || { + status+="${status:+$'\n'}status failed: $?" + ok=1 + } + clone=$( { git -C "$1" "${g[@]}" for-each-ref --format='%(objectname) %(refname)' 2>&1 || echo "for-each-ref failed"; } | { grep -v ' refs/pr-review/' || true; } echo "HEAD $(git -C "$1" "${g[@]}" rev-parse HEAD 2>&1) $(git -C "$1" "${g[@]}" symbolic-ref -q HEAD 2>&1)" git -C "$1" "${g[@]}" stash list 2>&1 || echo "stash list failed: $?" - } >"$3/clone.txt" + ) + if ! mkdir -p "$3" || ! printf 'HEAD %s\nbranch %s\n%s' "$head" "$branch" "${status:+$status$'\n'}" >"$3/worktree.txt" || + ! printf '%s\n' "$clone" >"$3/clone.txt"; then + echo "Error: could not save the tripwire's snapshot in $3" >&2 + return 1 + fi return "$ok" } diff --git a/tests/flow.sh b/tests/flow.sh index 2081608..a784213 100644 --- a/tests/flow.sh +++ b/tests/flow.sh @@ -1429,6 +1429,20 @@ run --doctor expect "old git: --doctor passes the check with a git that reads GIT_CONFIG_COUNT (control)" \ '[[ $rc == 0 ]] && grep -q "^✓ git reads config from the environment" "$stub/out.log"' +# A before-snapshot the run can't save: no baseline to compare with, so no model call. +real_mkdir=$(command -v mkdir) +cat >"$stub/bin/mkdir" <&2; exit 1 ;; esac +exec "$real_mkdir" "\$@" +S +chmod +x "$stub/bin/mkdir" +run +expect "tripwire: a before-snapshot that can't be saved exits 4, with no model call, nothing posted, the session named" \ + '[[ $rc == 4 && $calls == 0 && ! -e "$stub/posted.json" ]] && grep -q "Not posted (exit 4)" "$stub/out.log" && + grep -q "could not be saved" "$stub/out.log" && grep -q "Session: $stub/work/pr-review-sessions/pr-review-" "$stub/out.log"' +rm "$stub/bin/mkdir" + # The reviewer's sandbox, on real git: the clone comes from a local bare repo (url..insteadOf through # GIT_CONFIG_COUNT), never the network. The stub git can't see a change, so this case runs without it. setup $'**Approach:** Sound' "$clean" diff --git a/tests/guard.sh b/tests/guard.sh index 6f3ab91..ece2e0a 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -280,6 +280,17 @@ expect "tripwire: a snapshot fails when HEAD can't be read" \ '! tripwire_snapshot "$t/unborn" "$t/unborn" "$t/snap-unborn" 2>/dev/null' expect "tripwire: a snapshot fails when the worktree is gone" \ '! tripwire_snapshot "$repo" "$t/nowhere" "$t/snap-gone" 2>/dev/null' +# A snapshot that can't be saved is no snapshot either: run_pi calls it with errexit off, so it checks each step. +touch "$t/snap-file" +expect "tripwire: a snapshot fails when a file is where its folder goes" \ + '! tripwire_snapshot "$repo" "$t/review" "$t/snap-file" 2>/dev/null && [[ -f "$t/snap-file" && ! -s "$t/snap-file" ]]' +expect "tripwire: a snapshot fails when a file is where a parent folder goes" \ + '! tripwire_snapshot "$repo" "$t/review" "$t/snap-file/before" 2>/dev/null' +mkdir -p "$t/snap-wdir/worktree.txt" "$t/snap-cdir/clone.txt" +expect "tripwire: a snapshot fails when worktree.txt can't be written" \ + '! tripwire_snapshot "$repo" "$t/review" "$t/snap-wdir" 2>/dev/null' +expect "tripwire: a snapshot fails when clone.txt can't be written" \ + '! tripwire_snapshot "$repo" "$t/review" "$t/snap-cdir" 2>/dev/null' git init -q "$t/sub" git -C "$t/sub" commit -q --allow-empty -m s git init -q "$t/sup" From 3d23e0fc7050e43344e2c7676cfaa7eb9b9b4885 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 17:16:32 +0100 Subject: [PATCH 17/23] Clear inherited git config before the tests' real-git fixtures --- tests/flow.sh | 5 +++-- tests/guard.sh | 8 ++++++-- tests/passes.sh | 10 +++++++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/flow.sh b/tests/flow.sh index a784213..604fea4 100644 --- a/tests/flow.sh +++ b/tests/flow.sh @@ -1447,9 +1447,10 @@ rm "$stub/bin/mkdir" # GIT_CONFIG_COUNT), never the network. The stub git can't see a change, so this case runs without it. setup $'**Approach:** Sound' "$clean" rm "$stub/bin/git" -# The fixture repos and the run use this stub's git config only: no signing, hooks or other settings of mine. +# The fixture repos and the run use this stub's git config only: no signing, hooks or other settings of mine, from +# files or the environment. unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_CONFIG \ - GIT_CONFIG_PARAMETERS GIT_CEILING_DIRECTORIES + GIT_CONFIG_PARAMETERS GIT_CEILING_DIRECTORIES GIT_CONFIG_COUNT "${!GIT_CONFIG_KEY_@}" "${!GIT_CONFIG_VALUE_@}" export HOME="$stub/home" XDG_CONFIG_HOME="$stub/home/.config" GIT_CONFIG_NOSYSTEM=1 \ GIT_CONFIG_GLOBAL="$stub/home/.gitconfig" export GIT_AUTHOR_NAME=a GIT_AUTHOR_EMAIL=a@x GIT_COMMITTER_NAME=a GIT_COMMITTER_EMAIL=a@x diff --git a/tests/guard.sh b/tests/guard.sh index ece2e0a..1691af3 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -13,8 +13,12 @@ fail=0 t=$(mktemp -d) t=$(cd "$t" && pwd -P) -export HOME="$t/home" GIT_CONFIG_NOSYSTEM=1 GIT_AUTHOR_NAME=a GIT_AUTHOR_EMAIL=a@x GIT_COMMITTER_NAME=a \ - GIT_COMMITTER_EMAIL=a@x +# The test repos use this test's git config only: no signing, hooks or other settings of mine, from files or the +# environment. +unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_CONFIG \ + GIT_CONFIG_PARAMETERS GIT_CEILING_DIRECTORIES GIT_CONFIG_COUNT "${!GIT_CONFIG_KEY_@}" "${!GIT_CONFIG_VALUE_@}" +export HOME="$t/home" XDG_CONFIG_HOME="$t/home/.config" GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL="$t/home/.gitconfig" +export GIT_AUTHOR_NAME=a GIT_AUTHOR_EMAIL=a@x GIT_COMMITTER_NAME=a GIT_COMMITTER_EMAIL=a@x mkdir -p "$HOME" real=$(command -v git) diff --git a/tests/passes.sh b/tests/passes.sh index c1ecf57..a2b605c 100644 --- a/tests/passes.sh +++ b/tests/passes.sh @@ -21,9 +21,17 @@ in_lib() { # script (runs with lib sourced and the stub pi on PATH) PATH="$stub:$PATH" bash -c "set -euo pipefail; source '$stub/lib.sh'; source '$root/lib/guard.sh' SESSION_DIR='$stub' REVIEW_DIR='$stub/repo' MODEL=m; $1" 2>&1 || echo "(exit $?)" } +# The review checkout and run_pi use this stub's git config only: no signing, hooks or other settings of mine, from +# files or the environment. +unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_CONFIG \ + GIT_CONFIG_PARAMETERS GIT_CEILING_DIRECTORIES GIT_CONFIG_COUNT "${!GIT_CONFIG_KEY_@}" "${!GIT_CONFIG_VALUE_@}" +export HOME="$stub/home" XDG_CONFIG_HOME="$stub/home/.config" GIT_CONFIG_NOSYSTEM=1 \ + GIT_CONFIG_GLOBAL="$stub/home/.gitconfig" +export GIT_AUTHOR_NAME=a GIT_AUTHOR_EMAIL=a@x GIT_COMMITTER_NAME=a GIT_COMMITTER_EMAIL=a@x +mkdir -p "$HOME" # The review checkout has a commit: the tripwire can't read an unborn HEAD. git init -q "$stub/repo" -git -C "$stub/repo" -c user.name=a -c user.email=a@x commit -q --allow-empty -m c +git -C "$stub/repo" commit -q --allow-empty -m c printf 'PROMPT' >"$stub/p.md" # shellcheck disable=SC2016 # the stub must expand $* when it runs, not now From 5b66bbbc897baa658385e2a33e70ef7c9fe8bc0c Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 17:17:27 +0100 Subject: [PATCH 18/23] Probe for environment config without GIT_CONFIG or a parent git's settings --- lib/guard.sh | 7 ++++--- tests/guard.sh | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/guard.sh b/lib/guard.sh index d6cd021..b0d0746 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -11,10 +11,11 @@ pi_bin() { # } # 0 when git reads config from the environment (GIT_CONFIG_COUNT: git 2.31 and later). pi_env turns credentials and -# hooks off that way; an older git ignores it. +# hooks off that way; an older git ignores it. The probe runs without GIT_CONFIG (`git config` would read that file +# only) and without a parent git's -c settings (GIT_CONFIG_PARAMETERS, which win). git_env_config() { - [[ $(GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=gh-pr-review.probe GIT_CONFIG_VALUE_0=ok \ - git config --get gh-pr-review.probe 2>/dev/null) == ok ]] + [[ $(env -u GIT_CONFIG -u GIT_CONFIG_PARAMETERS GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=gh-pr-review.probe \ + GIT_CONFIG_VALUE_0=ok git config --get gh-pr-review.probe 2>/dev/null) == ok ]] } # Runs a command (pi) in the sandbox: the wrapper in first on PATH, the real git (the git on PATH now) in diff --git a/tests/guard.sh b/tests/guard.sh index 1691af3..9e1a64e 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -210,6 +210,11 @@ mkdir -p "$t/oldgit" printf '#!/bin/sh\nunset GIT_CONFIG_COUNT\nexec "%s" "$@"\n' "$real" >"$t/oldgit/git" chmod +x "$t/oldgit/git" expect "git_env_config: a git that reads GIT_CONFIG_COUNT passes" 'git_env_config' +# GIT_CONFIG points `git config` at one file, and a parent git's -c settings win: neither may fail the probe. +expect "git_env_config: a git that reads GIT_CONFIG_COUNT passes with GIT_CONFIG=/dev/null exported" \ + '(export GIT_CONFIG=/dev/null && git_env_config)' +expect "git_env_config: a git that reads GIT_CONFIG_COUNT passes with an inherited probe setting" \ + '(export GIT_CONFIG_PARAMETERS="'"'"'gh-pr-review.probe'"'"'='"'"'no'"'"'" && git_env_config)' expect "git_env_config: a git that ignores GIT_CONFIG_COUNT fails" '! PATH="$t/oldgit:$PATH" git_env_config' PATH="$t/oldgit:$PATH" pi_env "$t/bin" touch "$t/old.ran" 2>"$t/old.err" || true expect "pi_env: with a git that ignores GIT_CONFIG_COUNT, it refuses to run the command" \ From b7f56a2bfcf81419154f949017e30eb5682c1026 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 17:18:12 +0100 Subject: [PATCH 19/23] Name both causes of exit 4 in --help --- gh-pr-review | 6 +++--- tests/flow.sh | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/gh-pr-review b/gh-pr-review index b17b92d..c169f42 100755 --- a/gh-pr-review +++ b/gh-pr-review @@ -22,9 +22,9 @@ # --doctor Check all prerequisites and exit # -h, --help Show this help # -# Exit codes: 0 done, 1 error, 3 stopped by the loop check, 4 the reviewer changed the review checkout (nothing -# posted, the session is kept), 5 no new commits since the last round, 6 the rounds changed while the review ran -# (nothing posted, the session is kept). +# Exit codes: 0 done, 1 error, 3 stopped by the loop check, 4 the reviewer changed the review checkout, or it could +# not be read or its snapshot saved, so a change could not be seen (nothing posted, the session is kept), 5 no new +# commits since the last round, 6 the rounds changed while the review ran (nothing posted, the session is kept). # # Examples: # gh pr-review https://github.com/owner/repo/pull/42 diff --git a/tests/flow.sh b/tests/flow.sh index 604fea4..414ad2a 100644 --- a/tests/flow.sh +++ b/tests/flow.sh @@ -1442,6 +1442,9 @@ expect "tripwire: a before-snapshot that can't be saved exits 4, with no model c '[[ $rc == 4 && $calls == 0 && ! -e "$stub/posted.json" ]] && grep -q "Not posted (exit 4)" "$stub/out.log" && grep -q "could not be saved" "$stub/out.log" && grep -q "Session: $stub/work/pr-review-sessions/pr-review-" "$stub/out.log"' rm "$stub/bin/mkdir" +run --help +expect "--help: exit 4 names both causes, a changed checkout and one that could not be read or saved" \ + '[[ $rc == 0 && "$(tr -s " \n" " " <"$stub/out.log")" == *"4 the reviewer changed the review checkout, or it could not be read or its snapshot saved, so a change could not be seen (nothing posted, the session is kept)"* ]]' # The reviewer's sandbox, on real git: the clone comes from a local bare repo (url..insteadOf through # GIT_CONFIG_COUNT), never the network. The stub git can't see a change, so this case runs without it. From 211ca3fb25dbe94b1e309ffa9dfa1f178e73a997 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 18:54:09 +0100 Subject: [PATCH 20/23] Unset GIT_TEMPLATE_DIR in the real-git test fixtures --- tests/flow.sh | 3 ++- tests/guard.sh | 3 ++- tests/passes.sh | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/flow.sh b/tests/flow.sh index 0771f7f..6cb783d 100644 --- a/tests/flow.sh +++ b/tests/flow.sh @@ -1468,7 +1468,8 @@ rm "$stub/bin/git" # The fixture repos and the run use this stub's git config only: no signing, hooks or other settings of mine, from # files or the environment. unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_CONFIG \ - GIT_CONFIG_PARAMETERS GIT_CEILING_DIRECTORIES GIT_CONFIG_COUNT "${!GIT_CONFIG_KEY_@}" "${!GIT_CONFIG_VALUE_@}" + GIT_CONFIG_PARAMETERS GIT_CEILING_DIRECTORIES GIT_TEMPLATE_DIR GIT_CONFIG_COUNT "${!GIT_CONFIG_KEY_@}" \ + "${!GIT_CONFIG_VALUE_@}" export HOME="$stub/home" XDG_CONFIG_HOME="$stub/home/.config" GIT_CONFIG_NOSYSTEM=1 \ GIT_CONFIG_GLOBAL="$stub/home/.gitconfig" export GIT_AUTHOR_NAME=a GIT_AUTHOR_EMAIL=a@x GIT_COMMITTER_NAME=a GIT_COMMITTER_EMAIL=a@x diff --git a/tests/guard.sh b/tests/guard.sh index 9e1a64e..a9ab46d 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -16,7 +16,8 @@ t=$(cd "$t" && pwd -P) # The test repos use this test's git config only: no signing, hooks or other settings of mine, from files or the # environment. unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_CONFIG \ - GIT_CONFIG_PARAMETERS GIT_CEILING_DIRECTORIES GIT_CONFIG_COUNT "${!GIT_CONFIG_KEY_@}" "${!GIT_CONFIG_VALUE_@}" + GIT_CONFIG_PARAMETERS GIT_CEILING_DIRECTORIES GIT_TEMPLATE_DIR GIT_CONFIG_COUNT "${!GIT_CONFIG_KEY_@}" \ + "${!GIT_CONFIG_VALUE_@}" export HOME="$t/home" XDG_CONFIG_HOME="$t/home/.config" GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL="$t/home/.gitconfig" export GIT_AUTHOR_NAME=a GIT_AUTHOR_EMAIL=a@x GIT_COMMITTER_NAME=a GIT_COMMITTER_EMAIL=a@x mkdir -p "$HOME" diff --git a/tests/passes.sh b/tests/passes.sh index a2b605c..cc44a8b 100644 --- a/tests/passes.sh +++ b/tests/passes.sh @@ -24,7 +24,8 @@ in_lib() { # script (runs with lib sourced and the stub pi on PATH) # The review checkout and run_pi use this stub's git config only: no signing, hooks or other settings of mine, from # files or the environment. unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_CONFIG \ - GIT_CONFIG_PARAMETERS GIT_CEILING_DIRECTORIES GIT_CONFIG_COUNT "${!GIT_CONFIG_KEY_@}" "${!GIT_CONFIG_VALUE_@}" + GIT_CONFIG_PARAMETERS GIT_CEILING_DIRECTORIES GIT_TEMPLATE_DIR GIT_CONFIG_COUNT "${!GIT_CONFIG_KEY_@}" \ + "${!GIT_CONFIG_VALUE_@}" export HOME="$stub/home" XDG_CONFIG_HOME="$stub/home/.config" GIT_CONFIG_NOSYSTEM=1 \ GIT_CONFIG_GLOBAL="$stub/home/.gitconfig" export GIT_AUTHOR_NAME=a GIT_AUTHOR_EMAIL=a@x GIT_COMMITTER_NAME=a GIT_COMMITTER_EMAIL=a@x From ee518596163c74572f21d5ea280cea7305f4fb94 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 18:54:09 +0100 Subject: [PATCH 21/23] Pass the snapshot failure cause to unreadable_checkout --- gh-pr-review | 12 ++++++------ lib/guard.sh | 6 +++--- tests/guard.sh | 30 +++++++++++++++++++++--------- tests/passes.sh | 17 +++++++++++++++++ 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/gh-pr-review b/gh-pr-review index 2ac88bb..7fafab8 100755 --- a/gh-pr-review +++ b/gh-pr-review @@ -247,9 +247,9 @@ run_pi() { # local snap code=0 changed=0 snap="${SESSION_DIR}/tripwire/$(basename "$2" .md)" pi_bin "${SESSION_DIR}/bin" - tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/before" || unreadable_checkout "$snap/before" + tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/before" || unreadable_checkout "$snap/before" $? pi_env "${SESSION_DIR}/bin" pi ${PI_ARGS[@]+"${PI_ARGS[@]}"} "$(cat "$1")" >"$2" || code=$? - tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/after" || unreadable_checkout "$snap/after" + tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/after" || unreadable_checkout "$snap/after" $? tripwire_check "$snap/before" "$snap/after" "${SESSION_DIR}/warnings.txt" || changed=$? if [[ $changed -ne 0 ]]; then echo "" >&2 @@ -265,11 +265,11 @@ run_pi() { # fi } -# Stops the run with exit 4: the review checkout could not be read (its snapshot says why), or its snapshot could -# not be saved, so a change by the reviewer could not be seen. -unreadable_checkout() { # +# Stops the run with exit 4, so a change by the reviewer could not be seen. is tripwire_snapshot's exit code: +# 1, the review checkout could not be read (its snapshot says why); 2, the snapshot could not be saved. +unreadable_checkout() { # echo "" >&2 - if [[ -f "$1/worktree.txt" && -f "$1/clone.txt" ]]; then + if [[ "$2" == 1 ]]; then echo "Not posted (exit 4): the review checkout could not be read, so a change by the reviewer could not be seen:" >&2 sed 's/^/ /' "$1/worktree.txt" >&2 || true else diff --git a/lib/guard.sh b/lib/guard.sh index b0d0746..3309288 100644 --- a/lib/guard.sh +++ b/lib/guard.sh @@ -48,8 +48,8 @@ pi_env() { # [args...] # every untracked file and every submodule whatever the config says (status.showUntrackedFiles, diff.ignoreSubmodules, # submodule..ignore). clone.txt: the clone's refs but refs/pr-review/, its HEAD and its stash. Errors go in the # files. 1 when the worktree's HEAD or status can't be read (a detached HEAD is fine): two such snapshots would compare -# equal, so the worktree can't be checked. 1 too when or a file in it can't be written: no snapshot. Each -# step is checked: a caller's `|| ...` turns errexit off in here. +# equal, so the worktree can't be checked. 2 when or a file in it can't be written, even in part: no +# snapshot. Each step is checked: a caller's `|| ...` turns errexit off in here. tripwire_snapshot() { # local g=(--no-optional-locks -c core.hooksPath=/dev/null -c core.fsmonitor=false) ok=0 head branch status clone code=0 head=$(git -C "$2" "${g[@]}" rev-parse --verify HEAD 2>&1) || ok=1 @@ -68,7 +68,7 @@ tripwire_snapshot() { # if ! mkdir -p "$3" || ! printf 'HEAD %s\nbranch %s\n%s' "$head" "$branch" "${status:+$status$'\n'}" >"$3/worktree.txt" || ! printf '%s\n' "$clone" >"$3/clone.txt"; then echo "Error: could not save the tripwire's snapshot in $3" >&2 - return 1 + return 2 fi return "$ok" } diff --git a/tests/guard.sh b/tests/guard.sh index a9ab46d..c4a3e6d 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -279,28 +279,40 @@ check 10 11 expect "tripwire: a new file in an untracked folder gives 4" '[[ $rc == 4 ]] && grep -q "untracked/b" "$t/check.out"' rm -r "$t/review/untracked" git -C "$repo" config --unset status.showUntrackedFiles -# A worktree the snapshot can't read: no snapshot. Two failed snapshots would compare equal. +# A worktree the snapshot can't read: no snapshot (1). Two failed snapshots would compare equal. +snap_rc() { # : tripwire_snapshot's exit code + local rc=0 + tripwire_snapshot "$@" 2>/dev/null || rc=$? + echo "$rc" +} expect "tripwire: a snapshot of a detached HEAD succeeds" 'tripwire_snapshot "$repo" "$t/review" "$t/snap-ok"' git -C "$repo" config status.showUntrackedFiles bogus expect "tripwire: a snapshot fails when status fails" \ - '! tripwire_snapshot "$repo" "$t/review" "$t/snap-bad" 2>/dev/null && grep -q "^status failed" "$t/snap-bad/worktree.txt"' + '[[ $(snap_rc "$repo" "$t/review" "$t/snap-bad") == 1 ]] && grep -q "^status failed" "$t/snap-bad/worktree.txt"' git -C "$repo" config --unset status.showUntrackedFiles git init -q "$t/unborn" expect "tripwire: a snapshot fails when HEAD can't be read" \ - '! tripwire_snapshot "$t/unborn" "$t/unborn" "$t/snap-unborn" 2>/dev/null' + '[[ $(snap_rc "$t/unborn" "$t/unborn" "$t/snap-unborn") == 1 ]]' expect "tripwire: a snapshot fails when the worktree is gone" \ - '! tripwire_snapshot "$repo" "$t/nowhere" "$t/snap-gone" 2>/dev/null' -# A snapshot that can't be saved is no snapshot either: run_pi calls it with errexit off, so it checks each step. + '[[ $(snap_rc "$repo" "$t/nowhere" "$t/snap-gone") == 1 ]]' +# A snapshot that can't be saved is no snapshot either (2): run_pi calls it with errexit off, so it checks each step. touch "$t/snap-file" expect "tripwire: a snapshot fails when a file is where its folder goes" \ - '! tripwire_snapshot "$repo" "$t/review" "$t/snap-file" 2>/dev/null && [[ -f "$t/snap-file" && ! -s "$t/snap-file" ]]' + '[[ $(snap_rc "$repo" "$t/review" "$t/snap-file") == 2 && -f "$t/snap-file" && ! -s "$t/snap-file" ]]' expect "tripwire: a snapshot fails when a file is where a parent folder goes" \ - '! tripwire_snapshot "$repo" "$t/review" "$t/snap-file/before" 2>/dev/null' + '[[ $(snap_rc "$repo" "$t/review" "$t/snap-file/before") == 2 ]]' mkdir -p "$t/snap-wdir/worktree.txt" "$t/snap-cdir/clone.txt" expect "tripwire: a snapshot fails when worktree.txt can't be written" \ - '! tripwire_snapshot "$repo" "$t/review" "$t/snap-wdir" 2>/dev/null' + '[[ $(snap_rc "$repo" "$t/review" "$t/snap-wdir") == 2 ]]' expect "tripwire: a snapshot fails when clone.txt can't be written" \ - '! tripwire_snapshot "$repo" "$t/review" "$t/snap-cdir" 2>/dev/null' + '[[ $(snap_rc "$repo" "$t/review" "$t/snap-cdir") == 2 ]]' +# A partial write: worktree.txt is saved, clone.txt (a thousand refs) stops at the file-size limit, half written. +git init -q "$t/big" +git -C "$t/big" commit -q --allow-empty -m b +for i in $(seq 1000); do echo "create refs/heads/branch-with-a-long-name-$i HEAD"; done | git -C "$t/big" update-ref --stdin +expect "tripwire: a snapshot fails with 2 when clone.txt is cut short" \ + '[[ $( (trap "" XFSZ; ulimit -f 8; snap_rc "$t/big" "$t/big" "$t/snap-part") ) == 2 && -s "$t/snap-part/clone.txt" && + $(wc -l <"$t/snap-part/clone.txt") -lt 1000 ]] && grep -q "^HEAD" "$t/snap-part/worktree.txt"' git init -q "$t/sub" git -C "$t/sub" commit -q --allow-empty -m s git init -q "$t/sup" diff --git a/tests/passes.sh b/tests/passes.sh index cc44a8b..31ffc28 100644 --- a/tests/passes.sh +++ b/tests/passes.sh @@ -53,6 +53,23 @@ eq "run_pi: the reviewer's git is read-only" "$([[ "$out" == *"git stash is not eq "run_pi: a changed checkout exits 4 and says what changed" \ "$([[ "$out" == *"(exit 4)"* && "$out" == *"+?? x"* && "$out" != *SHOULD-NOT-REACH* ]] && echo y)" y +# A snapshot cut short (clone.txt stops at the file-size limit: a thousand refs) was not saved; a checkout status can't +# read could not be read. Both exit 4 before pi runs, with the session named. +git init -q "$stub/big" +git -C "$stub/big" commit -q --allow-empty -m b +for i in $(seq 1000); do echo "create refs/heads/branch-with-a-long-name-$i HEAD"; done | git -C "$stub/big" update-ref --stdin +printf '#!/usr/bin/env bash\necho PI-RAN\n' >"$stub/pi" +out=$(in_lib "REVIEW_DIR='$stub/big'; trap '' XFSZ; ulimit -f 8; PI_ARGS=(); run_pi '$stub/p.md' '$stub/o2.md'") +eq "run_pi: a snapshot cut short says it could not be saved, exits 4, names the session" \ + "$([[ "$out" == *"(exit 4)"* && "$out" == *"could not be saved"* && "$out" != *"could not be read"* && + "$out" == *"Session: $stub"* && -s "$stub/tripwire/o2/before/clone.txt" && ! -e "$stub/o2.md" ]] && echo y)" y +git -C "$stub/repo" config status.showUntrackedFiles bogus +out=$(in_lib "PI_ARGS=(); run_pi '$stub/p.md' '$stub/o3.md'") +eq "run_pi: an unreadable checkout says it could not be read, exits 4, names the session" \ + "$([[ "$out" == *"(exit 4)"* && "$out" == *"could not be read"* && "$out" == *"status failed"* && + "$out" != *"could not be saved"* && "$out" == *"Session: $stub"* && ! -e "$stub/o3.md" ]] && echo y)" y +git -C "$stub/repo" config --unset status.showUntrackedFiles + boundary='' eq "approach, boundary, review, footer" "$(in_lib "assemble_review '' 'APPROACH' 'REVIEW'")" \ $'APPROACH\n\n'"$boundary"$'\nREVIEW\n\n---\n'"$footer" From 3b80f5ae852017e61597ce023763512da2cf9dda Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 18:55:11 +0100 Subject: [PATCH 22/23] Skip the pattern --match and --exclude take in git-ro's describe check --- lib/git-ro | 12 ++++++++++-- tests/guard.sh | 11 ++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/git-ro b/lib/git-ro index 0fb1e88..2e202f3 100755 --- a/lib/git-ro +++ b/lib/git-ro @@ -27,11 +27,19 @@ while [[ $i -le $# ]]; do done # describe --dirty and --broken refresh the index, and no setting turns that off (pi_env does for status and diff). -# Git takes any unique prefix of an option name (--dir, --brok=x): refused too, and so is an ambiguous --d. +# Git takes any unique prefix of an option name (--dir, --brok=x): refused too, and so is an ambiguous --d. The +# pattern after --match or --exclude (or their prefixes --m, --exc) is no option: `--match --br` only reads. if [[ "$sub" == describe ]]; then + value=false for arg in "${@:i+1}"; do + if [[ $value == true ]]; then + value=false + continue + fi name=${arg%%=*} - if [[ ${#name} -gt 2 && (--dirty == "$name"* || --broken == "$name"*) ]]; then + if [[ $arg != *=* && ((${#name} -gt 2 && --match == "$name"*) || (${#name} -gt 4 && --exclude == "$name"*)) ]]; then + value=true + elif [[ ${#name} -gt 2 && (--dirty == "$name"* || --broken == "$name"*) ]]; then echo "git describe --dirty and --broken are not allowed in a read-only review: they write the index" >&2 exit 1 fi diff --git a/tests/guard.sh b/tests/guard.sh index c4a3e6d..7bd7c3f 100644 --- a/tests/guard.sh +++ b/tests/guard.sh @@ -150,13 +150,22 @@ ro_idx() { # git args... (through the wrapper on pi's PATH, from the stale-index } for args in "describe --dirty" "describe --always --dirty=-mod" "describe --broken" "-c x.y=z describe --tags --broken=-b" \ "describe --always --dir" "describe --always --dirt" "describe --always --di=-m" "describe --always --d" \ - "describe --always --br" "describe --always --brok=x" "describe --always --b" "describe --always --dirt=-m HEAD"; do + "describe --always --br" "describe --always --brok=x" "describe --always --b" "describe --always --dirt=-m HEAD" \ + "describe --always --match x --dirty" "describe --always --match=--br --broken"; do n=$((n + 1)) sum=$(stale "$idx" "$n") expect "git-ro refuses: $args, and the stale index is unchanged" \ "! ro_idx $args && grep -q '^git describe --dirty and --broken are not allowed in a read-only review' \"\$t/ro.out\" && [[ \"\$(cksum <\"\$idx/.git/index\")\" == \"\$sum\" ]]" done +# The pattern --match and --exclude take (or a unique prefix of theirs) is no option: these only read. +for args in "describe --always --match --br HEAD" "describe --always --exclude --dirty" "describe --always --mat --dir" \ + "describe --always --exc --broken=x --match --d"; do + n=$((n + 1)) + sum=$(stale "$idx" "$n") + expect "git-ro passes: $args, and the stale index is unchanged" \ + "ro_idx $args && [[ \"\$(cksum <\"\$idx/.git/index\")\" == \"\$sum\" ]]" +done # A file system monitor is a hook core.hooksPath does not cover. printf '#!/bin/sh\ntouch "%s/fsmonitor.ran"\n' "$t" >"$t/fsmonitor" chmod +x "$t/fsmonitor" From b0a2a54a6730127bda3ed7539411a435e9182f06 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sat, 28 Nov 2026 19:43:43 +0100 Subject: [PATCH 23/23] Leave the run folder to the exit handler on exit 4, and find run folders in the PR's state folder in the tests --- gh-pr-review | 4 ++-- tests/flow.sh | 8 ++++---- tests/passes.sh | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/gh-pr-review b/gh-pr-review index 25cc6a3..dd8365b 100755 --- a/gh-pr-review +++ b/gh-pr-review @@ -257,7 +257,7 @@ run_pi() { # if [[ $changed -ne 0 ]]; then echo "" >&2 echo "Not posted (exit 4): the reviewer changed the review checkout, which must stay read-only." >&2 - echo " The checkout is left as it is. Session: ${SESSION_DIR}" >&2 + echo " The checkout is left as it is." >&2 exit 4 fi if [[ $code -ne 0 ]]; then @@ -279,7 +279,7 @@ unreadable_checkout() { # echo "Not posted (exit 4): the snapshot of the review checkout could not be saved, so a change by the reviewer" \ "could not be seen." >&2 fi - echo " The checkout is left as it is. Session: ${SESSION_DIR}" >&2 + echo " The checkout is left as it is." >&2 exit 4 } diff --git a/tests/flow.sh b/tests/flow.sh index a74e558..89a9bbc 100644 --- a/tests/flow.sh +++ b/tests/flow.sh @@ -1541,7 +1541,7 @@ chmod +x "$stub/bin/mkdir" run expect "tripwire: a before-snapshot that can't be saved exits 4, with no model call, nothing posted, the session named" \ '[[ $rc == 4 && $calls == 0 && ! -e "$stub/posted.json" ]] && grep -q "Not posted (exit 4)" "$stub/out.log" && - grep -q "could not be saved" "$stub/out.log" && grep -q "Session: $stub/work/pr-review-sessions/pr-review-" "$stub/out.log"' + grep -q "could not be saved" "$stub/out.log" && grep -q "Session: $(prdir)/run-" "$stub/out.log"' rm "$stub/bin/mkdir" run --help expect "--help: exit 4 names both causes, a changed checkout and one that could not be read or saved" \ @@ -1577,7 +1577,7 @@ expect "read-only reviewer: both passes get the read-only git" \ grep -q "^git checkout is not allowed in a read-only review$" "$stub/pi.git"' expect "read-only reviewer: a commit past the wrapper exits 4, says what changed, posts nothing, keeps the session" \ '[[ $rc == 4 && ! -e "$stub/posted.json" ]] && grep -q "Not posted (exit 4)" "$stub/out.log" && - grep -q "^+HEAD" "$stub/out.log" && [[ -s $(echo "$stub"/work/pr-review-sessions/pr-review-*/tripwire/review/after/worktree.txt) ]]' + grep -q "^+HEAD" "$stub/out.log" && [[ -s $(echo "$(prdir)"/run-*/tripwire/review/after/worktree.txt) ]]' rm "$stub/pi.git" echo "branch side" >"$stub/pi.evil" run @@ -1585,8 +1585,8 @@ expect "read-only reviewer: a new branch in the clone only warns, and the review '[[ $rc == 0 && -e "$stub/posted.json" ]] && grep -q "^Warning: the clone.s refs" "$stub/out.log" && grep -q "refs/heads/side" "$stub/out.log"' expect "read-only reviewer: the clone's warning is saved in the session" \ - 'grep -q "^Warning: the clone.s refs" "$stub"/work/pr-review-sessions/pr-review-*/warnings.txt && - grep -q "refs/heads/side" "$stub"/work/pr-review-sessions/pr-review-*/warnings.txt' + 'grep -q "^Warning: the clone.s refs" "$(prdir)"/round-*/warnings.txt && + grep -q "refs/heads/side" "$(prdir)"/round-*/warnings.txt' rm "$stub/pi.git" echo "config status.showUntrackedFiles bogus" >"$stub/pi.evil" run diff --git a/tests/passes.sh b/tests/passes.sh index 31ffc28..2c79430 100644 --- a/tests/passes.sh +++ b/tests/passes.sh @@ -54,20 +54,20 @@ eq "run_pi: a changed checkout exits 4 and says what changed" \ "$([[ "$out" == *"(exit 4)"* && "$out" == *"+?? x"* && "$out" != *SHOULD-NOT-REACH* ]] && echo y)" y # A snapshot cut short (clone.txt stops at the file-size limit: a thousand refs) was not saved; a checkout status can't -# read could not be read. Both exit 4 before pi runs, with the session named. +# read could not be read. Both exit 4 before pi runs. (The run folder is named by the exit handler, outside run_pi.) git init -q "$stub/big" git -C "$stub/big" commit -q --allow-empty -m b for i in $(seq 1000); do echo "create refs/heads/branch-with-a-long-name-$i HEAD"; done | git -C "$stub/big" update-ref --stdin printf '#!/usr/bin/env bash\necho PI-RAN\n' >"$stub/pi" out=$(in_lib "REVIEW_DIR='$stub/big'; trap '' XFSZ; ulimit -f 8; PI_ARGS=(); run_pi '$stub/p.md' '$stub/o2.md'") -eq "run_pi: a snapshot cut short says it could not be saved, exits 4, names the session" \ +eq "run_pi: a snapshot cut short says it could not be saved, exits 4" \ "$([[ "$out" == *"(exit 4)"* && "$out" == *"could not be saved"* && "$out" != *"could not be read"* && - "$out" == *"Session: $stub"* && -s "$stub/tripwire/o2/before/clone.txt" && ! -e "$stub/o2.md" ]] && echo y)" y + -s "$stub/tripwire/o2/before/clone.txt" && ! -e "$stub/o2.md" ]] && echo y)" y git -C "$stub/repo" config status.showUntrackedFiles bogus out=$(in_lib "PI_ARGS=(); run_pi '$stub/p.md' '$stub/o3.md'") -eq "run_pi: an unreadable checkout says it could not be read, exits 4, names the session" \ +eq "run_pi: an unreadable checkout says it could not be read, exits 4" \ "$([[ "$out" == *"(exit 4)"* && "$out" == *"could not be read"* && "$out" == *"status failed"* && - "$out" != *"could not be saved"* && "$out" == *"Session: $stub"* && ! -e "$stub/o3.md" ]] && echo y)" y + "$out" != *"could not be saved"* && ! -e "$stub/o3.md" ]] && echo y)" y git -C "$stub/repo" config --unset status.showUntrackedFiles boundary=''