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/README.md b/README.md index ba5d8d8..53a6275 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 | @@ -132,6 +132,8 @@ A run folder holds: - `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 Earlier versions wrote to `./pr-review-sessions/`; those folders are left alone. The optional config file is `${XDG_CONFIG_HOME:-~/.config}/gh-pr-review/config.json`. @@ -335,6 +337,18 @@ 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. 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), +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 scripts. `gh pr-review rounds ` shows the same decision under the history. @@ -344,6 +358,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 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 ef6a465..dd8365b 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, 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 @@ -241,9 +242,25 @@ 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. 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)" + pi_bin "${SESSION_DIR}/bin" + 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" "${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 + echo " The checkout is left as it is." >&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 @@ -251,6 +268,21 @@ run_pi() { fi } +# 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 [[ "$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 + 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." >&2 + exit 4 +} + # The run's extra guidance as a prompt section. Empty in, empty out. instructions_section() { [[ -z "$INSTRUCTIONS" ]] && return 0 @@ -321,6 +353,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/git-ro b/lib/git-ro new file mode 100755 index 0000000..2e202f3 --- /dev/null +++ b/lib/git-ro @@ -0,0 +1,56 @@ +#!/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. Any other option before it is refused. +i=1 sub="" +while [[ $i -le $# ]]; do + case "${!i}" in + -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 + ;; + esac +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. 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 [[ $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 + 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) + 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..3309288 --- /dev/null +++ b/lib/guard.sh @@ -0,0 +1,99 @@ +#!/usr/bin/env bash +# 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() { # + mkdir -p "$1" + cp "$(dirname "${BASH_SOURCE[0]}")/git-ro" "$1/git" + 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. 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() { + [[ $(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 +# 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. 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 + real=$(command -v git) || { + 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 -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 \ + 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 "$@" +} + +# 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. 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. 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 + branch=$(git -C "$2" "${g[@]}" symbolic-ref -q HEAD 2>&1) || code=$? + [[ $code -le 1 ]] || ok=1 + 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: $?" + ) + 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 2 + fi + return "$ok" +} + +# 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, 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 + 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):" + { 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 9b0df06..89a9bbc 100644 --- a/tests/flow.sh +++ b/tests/flow.sh @@ -28,7 +28,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} @@ -107,6 +109,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 @@ -1504,5 +1516,89 @@ 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"' + +# 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: $(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" \ + '[[ $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. +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, 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_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 +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 "$(prdir)"/run-*/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"' +expect "read-only reviewer: the clone's warning is saved in the session" \ + '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 +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 new file mode 100644 index 0000000..7bd7c3f --- /dev/null +++ b/tests/guard.sh @@ -0,0 +1,339 @@ +#!/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) +# 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_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" +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" \ + "-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" "-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 +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" ]]' +# 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 "2000010100$(printf %02d "$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" ]]' +# `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" ]]' +# `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" ]]' +# 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 +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" \ + "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" +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 +# 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" ]]' + +# 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' +# 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" \ + '[[ ! -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 +snap() { tripwire_snapshot "$repo" "$t/review" "$t/snap$1"; } +check() { # before after + rc=0 + 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" && ! -e "$t/warnings.txt" ]]' +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"' +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 +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 ]]' +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 +# 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" \ + '[[ $(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" \ + '[[ $(snap_rc "$t/unborn" "$t/unborn" "$t/snap-unborn") == 1 ]]' +expect "tripwire: a snapshot fails when the worktree is gone" \ + '[[ $(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" \ + '[[ $(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" \ + '[[ $(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" \ + '[[ $(snap_rc "$repo" "$t/review" "$t/snap-wdir") == 2 ]]' +expect "tripwire: a snapshot fails when clone.txt can't be written" \ + '[[ $(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" +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" diff --git a/tests/passes.sh b/tests/passes.sh index 04ed9b4..2c79430 100644 --- a/tests/passes.sh +++ b/tests/passes.sh @@ -18,9 +18,21 @@ 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 $?)" } +# 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_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 +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" commit -q --allow-empty -m c printf 'PROMPT' >"$stub/p.md" # shellcheck disable=SC2016 # the stub must expand $* when it runs, not now @@ -34,6 +46,30 @@ 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 + +# 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. (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" \ + "$([[ "$out" == *"(exit 4)"* && "$out" == *"could not be saved"* && "$out" != *"could not be read"* && + -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" \ + "$([[ "$out" == *"(exit 4)"* && "$out" == *"could not be read"* && "$out" == *"status failed"* && + "$out" != *"could not be saved"* && ! -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"