Skip to content
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
44743b6
Add gh pr-review gc to remove the review worktrees and refs of closed…
catalan-adobe Nov 29, 2026
a19caf6
Document where reviews run and gh pr-review gc; --doctor prints the s…
catalan-adobe Nov 29, 2026
843f165
gc --purge deletes only what runs make, by name and type: look-alikes…
catalan-adobe Nov 30, 2026
69d6e96
gc deletes the PR's refs with --no-deref: a symbolic ref there is del…
catalan-adobe Nov 30, 2026
0783a16
A run marks its state folder (.gh-pr-review-state); gc leaves a revie…
catalan-adobe Nov 30, 2026
8ee370d
gc --purge takes a PR folder without pr.json as the tool's only with …
catalan-adobe Nov 30, 2026
4b8a695
gc removes the registration of a worktree whose folder is gone only w…
catalan-adobe Nov 30, 2026
8346847
gc finds the PR folders whose names start with a dot (a repo named .g…
catalan-adobe Nov 30, 2026
f903649
gc reports a folder rmdir can't remove for another reason than holdin…
catalan-adobe Nov 30, 2026
f404908
gc --dry-run makes the run's checks, its locks' and --purge's plan in…
catalan-adobe Nov 30, 2026
7488f82
--doctor notes a state folder, or the folder it would be made in, tha…
catalan-adobe Nov 30, 2026
9679ee3
README: rewrap the gc section
catalan-adobe Nov 30, 2026
bb11b2e
Rewrap the comments of lib/gc.sh to 120 columns, and name its new glo…
catalan-adobe Nov 30, 2026
b3b3a71
gc removes a review worktree only when it is clean, with git worktree…
catalan-adobe Nov 30, 2026
72fcdd5
A run marks each PR's folder too (.gh-pr-review-pr, the PR's key), an…
catalan-adobe Nov 30, 2026
f5a5d94
Runs write their temporary files under names that are the tool's (.pr…
catalan-adobe Nov 30, 2026
104d060
gc --purge checks every run folder it would delete before it deletes …
catalan-adobe Nov 30, 2026
3457236
One read-only lock check: lock_state and lock_probe (lib/state.sh) de…
catalan-adobe Nov 30, 2026
33d75dd
gc names a folder it can't list while looking for PRs, goes on with t…
catalan-adobe Nov 30, 2026
194439d
gc_tidy: a folder above the worktree that holds another PR's worktree…
catalan-adobe Nov 30, 2026
1bcf4a1
gc keeps the review history: --purge is removed (it moves to a later …
catalan-adobe Nov 30, 2026
fec461c
gc leaves a review worktree that holds a .git below it other than its…
catalan-adobe Nov 30, 2026
efe0e3f
gc leaves a review worktree with files git status does not look at: f…
catalan-adobe Nov 30, 2026
c91eb12
lock_probe takes a dead run's lock whose folder is read-only for what…
catalan-adobe Nov 30, 2026
1ba885a
clone_lock makes <clone>.reviews 0700 by stripping the known /.locks/…
catalan-adobe Nov 30, 2026
afd768f
gc looks for another checkout's .git as git does: for each folder bel…
catalan-adobe Nov 30, 2026
b911f01
gc leaves a review worktree with files in the folder of a submodule t…
catalan-adobe Nov 30, 2026
69b54f1
gc leaves a review worktree that alone names commits no ref that stay…
catalan-adobe Nov 30, 2026
c863449
tests/gc.sh checks that no line names h/x/y#7 or h/x/y#8 by their ful…
catalan-adobe Nov 30, 2026
5d439f3
README: delete a PR's history by hand only once gc has removed its wo…
catalan-adobe Nov 30, 2026
cfee709
gc and the locks tell a folder's emptiness, or that it holds its pid …
catalan-adobe Nov 30, 2026
8d9fb02
The run records in pr.json the commit it made the review worktree at …
catalan-adobe Nov 30, 2026
67fb6fe
gc counts as keeping a commit only what it leaves: every ref but the …
catalan-adobe Nov 30, 2026
08ccb24
gc checks the git folder of a worktree whose folder was deleted outsi…
catalan-adobe Nov 30, 2026
c6e1cd6
gc names every commit ID a worktree would lose, not three and how man…
catalan-adobe Nov 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 200 additions & 55 deletions README.md

Large diffs are not rendered by default.

39 changes: 38 additions & 1 deletion gh-pr-review
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Usage:
# gh pr-review <PR-URL | PR-number> [options]
# gh pr-review rounds <PR-URL | PR-number> [--json] [-u <login>]
# gh pr-review gc [--dry-run] [-u <login>]
# gh pr-review --doctor
#
# Options:
Expand All @@ -21,7 +22,7 @@
# --full Review the whole PR again (round 2 and later still re-check the earlier findings)
# --clone <path> Review in a worktree of this local clone (default: found under the config's roots, ~/repos)
# --fresh Review in a throwaway depth-50 clone in the run folder instead of a local clone
# --doctor Check all prerequisites and exit
# --doctor Check all prerequisites, print the state folder, config file and clone roots, 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, or it could
Expand Down Expand Up @@ -400,9 +401,45 @@ run_preflight() {
fi
}

# --doctor: where the tool keeps its files, without a scan: the state folder (with a note when it, or the folder it
# would be made in, is not writable and searchable), the config file, and the folders a clone lookup scans. A config
# file that is not one JSON object fails the check: a run that looks up a clone stops on it.
doctor_paths() {
local state dir file roots
state=$(state_root)
dir=$state
while [[ ! -e "$dir" && "$dir" == */* ]]; do dir=${dir%/*}; done
if [[ -d "$state" && -w "$state" && -x "$state" ]] ||
[[ ! -e "$state" && -d "${dir:-/}" && -w "${dir:-/}" && -x "${dir:-/}" ]]; then
echo " state folder: $state"
elif [[ -e "$state" ]]; then
echo " state folder: $state (note: not writable and searchable, so a review can't keep its runs or take its lock)"
else
echo " state folder: $state (note: ${dir:-/} is not writable and searchable, so it can't be made)"
fi
file=$(config_file)
if [[ -e "$file" ]]; then
echo " config file: $file"
else
echo " config file: none ($file is optional)"
fi
if ! command -v jq &>/dev/null; then
echo " clone roots: unknown (needs jq)"
return 0
fi
if ! roots=$(clone_roots 2>/dev/null); then
pf_fail "invalid config file (not one JSON object): $file" "fix it, or remove it"
echo " clone roots: unknown (the config file is invalid)"
return 0
fi
roots=$(jq -r 'map(strings) | join(", ")' <<<"$roots")
echo " clone roots: ${roots:-none (no scan for clones)}"
}

run_preflight

if [[ "$DOCTOR" == "true" ]]; then
doctor_paths
if [[ "$PREFLIGHT_FAILURES" -eq 0 ]]; then
echo ""
echo "All checks passed — try: gh pr-review <PR-URL | PR-number>"
Expand Down
12 changes: 9 additions & 3 deletions lib/clones.sh
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ remember_clone() { # <host/owner/repo> <dir>
fi
}

# Prints the folders a lookup scans for clones, the config's `roots`, as a JSON array: missing, null or "" is the
# default, ["~/repos"]; a string is one folder; [] turns the scan off, and so does a value of another type.
clone_roots() {
config_get '.roots | if . == null or . == "" then null elif type == "string" then [.] elif type == "array"
then . else [] end' '["~/repos"]'
}

# Sets CLONE_DIR and CLONE_KIND (override, config, scan or cache) for a repo. The order: CLONE_OVERRIDE
# (--clone), the config `clones` map, clones.json, a scan of the config `roots`, then the cache clone. The repo's key,
# and the keys of the clones map, are compared as repo_key gives them. A cache clone is made from <url>, by default
Expand Down Expand Up @@ -259,10 +266,9 @@ find_clone() { # <host> <owner> <repo> [<url>]
return 0
fi

# A value of the wrong type: no clones, no roots. Missing, null or "" roots: the default; [] turns the scan off.
# A value of the wrong type: no clones.
clones=$(config_get '.clones | objects' '{}') || return 1
roots=$(config_get '.roots | if . == null or . == "" then null elif type == "string" then [.] elif type == "array"
then . else [] end' '["~/repos"]') || return 1
roots=$(clone_roots) || return 1
path=""
while IFS= read -r name; do
[[ "$(repo_key "$name" || true)" == "$key" ]] || continue
Expand Down
694 changes: 694 additions & 0 deletions lib/gc.sh

Large diffs are not rendered by default.

205 changes: 170 additions & 35 deletions lib/state.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ lock_claim() { # <dir>
(set -C && echo "$$" >"$1/pid") 2>/dev/null || return 1
[[ "$(ls -A "$1" 2>/dev/null)" != pid ]] || return 0
rm -f "$1/pid"
echo "Error: not a lock folder (it has no pid file, and holds other files); it was left as it is: $1" >&2
lock_why "$1" "" notlock || true
return 2
}

Expand Down Expand Up @@ -113,9 +113,130 @@ lock_refuse() { # <dir> <what> <reason> [<note>]
return 1
}

# What the lock folder <dir> is, as lock_take finds it when its claim fails, read only: the one place its decisions
# are made. Prints one of: free (not there, and the folder it goes in can be changed and searched: mkdir makes it);
# empty (a folder without a pid file, holding nothing, whose pid file can be made: claimed; also one whose pid file
# just went); cannot (it can't be made or claimed: the folder it goes in, or the empty lock folder, can't be changed or
# searched, it is not a folder, or its pid file is a symlink that leads nowhere);
# notlock (a folder without a pid file that holds other files: not a lock); nopid (a pid file without a PID, once
# lock_pid has waited for it); live <pid> (held by a process that is alive); dead <pid> (its run is gone: taken over).
lock_state() { # <dir>
local pid
if [[ ! -e "$1" && ! -L "$1" ]]; then
if [[ -d "${1%/*}" && -w "${1%/*}" && -x "${1%/*}" ]]; then echo free; else echo cannot; fi
return 0
fi
if [[ ! -d "$1" || ! -r "$1" || ! -x "$1" ]]; then
echo cannot
return 0
fi
# A pid file that is a symlink leading nowhere can't be read, nor made: no claim ever gets it.
if [[ -L "$1/pid" && ! -e "$1/pid" ]]; then
echo cannot
return 0
fi
if [[ ! -e "$1/pid" ]]; then
if [[ -n "$(ls -A "$1" 2>/dev/null)" ]]; then
echo notlock
elif [[ -w "$1" ]]; then
echo empty
else
echo cannot
fi
return 0
fi
# Released meanwhile: claimed again.
pid=$(lock_pid "$1") || {
echo empty
return 0
}
if [[ ! "$pid" =~ ^[0-9]+$ ]]; then
echo nopid
elif kill -0 "$pid" 2>/dev/null; then
echo "live $pid"
else
echo "dead $pid"
fi
}

# What lock_take would do with the lock folder <lock> as it is, read only (lock_state of the lock, and of
# <lock>.takeover for a lock whose run is gone). Prints one of: free, empty, or takeover <pid> (a lock whose run is
# gone, that the run takes over), which lock_take takes; else why it would refuse it: cannot, notlock, nopid or live
# <pid>, as lock_state; taking <pid> (another run is taking it over), takeover-dead <pid> (a run stopped while taking it
# over), takeover-nopid or takeover-notlock (<lock>.takeover is so); cannot-remove (the dead run's lock can't be
# removed: the folder it is in, or the lock folder itself, is read-only, so its pid file can't be deleted, which
# lock_remove does first, whatever else the lock holds); dead-notlock <pid> (the dead run's lock holds other files
# than its pid file).
lock_probe() { # <lock>
local state take
state=$(lock_state "$1")
[[ "$state" == "dead "* ]] || {
echo "$state"
return 0
}
take=$(lock_state "$1.takeover")
case "$take" in
"live "*) echo "taking ${take#live }" ;;
"dead "*) echo "takeover-dead ${take#dead }" ;;
nopid | notlock) echo "takeover-$take" ;;
cannot) echo cannot ;;
*)
if [[ ! -w "${1%/*}" || ! -w "$1" ]]; then
echo cannot-remove
elif [[ "$(ls -A "$1" 2>/dev/null)" != pid ]]; then
echo "dead-notlock ${state#dead }"
else
echo "takeover ${state#dead }"
fi
;;
esac
}

# Prints the error lock_take prints when it refuses the lock folder <lock> for <verdict> (lock_probe's, with its <pid>),
# and fails. <what> names the run that holds it ("review of this PR").
lock_why() { # <lock> <what> <verdict> [<pid>]
local take="$1.takeover" nolock="not a lock folder (it has no pid file, and holds other files); it was left as it is"
case "$3" in
cannot) echo "Error: could not take the lock: $1" >&2 ;;
notlock) echo "Error: $nolock: $1" >&2 ;;
nopid)
lock_refuse "$1" "$2" "the lock has no PID, so whether its run is still going can't be checked" \
"A run killed while taking it leaves it so"
;;
live) echo "Error: another $2 is running (pid ${4:-}). Lock: $1" >&2 ;;
taking) echo "Error: another run is taking over the lock (pid ${4:-}). Lock: $1" >&2 ;;
takeover-nopid)
lock_refuse "$take" "$2" "the takeover lock has no PID, so whether its run is still going can't be checked" \
"A run killed while taking it leaves it so"
;;
takeover-dead)
lock_refuse "$take" "$2" "a run (pid ${4:-}) was stopped while taking over the lock, and left its takeover lock"
;;
takeover-notlock) echo "Error: $nolock: $take" >&2 ;;
cannot-remove) echo "Error: could not remove the lock: $1" >&2 ;;
dead-notlock)
echo "Error: not a lock folder (it holds other files than the pid file of a run that is gone): that pid file" \
"was removed, and the other files were left as they are: $1" >&2
;;
esac
return 1
}

# Fails, printing the error lock_take would print, when lock_take could not take the lock folder <lock> as it is
# (lock_probe); changes nothing. For a preview (gc --dry-run): races and failures of the file system are not foreseen.
lock_check() { # <lock> <what>
local verdict
verdict=$(lock_probe "$1")
case "$verdict" in
free | empty | "takeover "*) return 0 ;;
esac
lock_why "$1" "$2" "${verdict%% *}" "${verdict#* }"
}

# Takes the lock folder <lock>, whose parent is there, with this process's PID in <lock>/pid. <what> names the run
# that holds it, in the messages ("review of this PR"). The run that makes the pid file holds the lock (lock_claim);
# only a PID says who owns a lock, never its age. An empty lock folder without a pid file is no one's: claimed. One
# only a PID says who owns a lock, never its age. Each claim is atomic; when it fails, what the lock is decides
# (lock_state), and the messages are lock_why's. An empty lock folder without a pid file is no one's: claimed. One
# without a pid file that holds other files is not a lock: refused at once, and left as it is (lock_claim). While the
# PID in the lock is alive, another run is refused, or with <wait> waits for it, that many seconds at most. A lock whose
# PID is gone (a killed run) is taken over, unless its folder holds other files than the pid file: then it is not a
Expand All @@ -126,27 +247,29 @@ lock_refuse() { # <dir> <what> <reason> [<note>]
# replaced only when it still holds the same dead PID, so a run that saw that PID never removes the lock another run
# just took. Nothing else removes a <lock>.takeover: while its PID is alive, another run waits (~10 s at most, then is
# refused); one whose PID is gone, or whose pid file stays empty ~2 s, was left by a run killed while taking over:
# refused, with the command that removes it.
# refused, with the command that removes it. A lock that can't be made or claimed is tried 100 times, then refused.
lock_take() { # <lock> <what> [<wait>]
local lock="$1" take="$1.takeover" pid tpid n rc tries=0 waited=0 taking=false
local lock="$1" take="$1.takeover" state pid tpid n rc tries=0 waited=0 taking=false
while :; do
rc=0
lock_claim "$lock" || rc=$?
[[ $rc != 0 ]] || break
[[ $rc != 2 ]] || return 1
tries=$((tries + 1))
if [[ $tries -gt 100 ]]; then
echo "Error: could not take the lock: $lock" >&2
lock_why "$lock" "$2" cannot
return 1
fi
# Released meanwhile: try again.
pid=$(lock_pid "$lock") || continue
[[ -d "$lock" ]] || continue
if [[ ! "$pid" =~ ^[0-9]+$ ]]; then
lock_refuse "$lock" "$2" "the lock has no PID, so whether its run is still going can't be checked" \
"A run killed while taking it leaves it so" || return 1
fi
if kill -0 "$pid" 2>/dev/null; then
state=$(lock_state "$lock")
pid=${state#* }
case "$state" in
# Released or made meanwhile, or not claimable now: claim again (lock_claim refuses one that is not a lock).
free | empty | cannot | notlock) continue ;;
nopid)
lock_why "$lock" "$2" nopid
return 1
;;
"live "*)
# Waiting, 0.2 s at a time: each turn is a fresh try.
if [[ $waited -lt $((${3:-0} * 5)) ]]; then
[[ $waited -gt 0 ]] || echo "Waiting for another $2 (pid $pid). Lock: $lock" >&2
Expand All @@ -155,30 +278,38 @@ lock_take() { # <lock> <what> [<wait>]
sleep 0.2
continue
fi
echo "Error: another $2 is running (pid $pid). Lock: $lock" >&2
lock_why "$lock" "$2" live "$pid"
return 1
fi
;;
esac
# Its run is gone (dead): take it over, under lock.takeover.
rc=0
lock_claim "$take" || rc=$?
[[ $rc != 2 ]] || return 1
if [[ $rc != 0 ]]; then
# Another run is taking over: wait while its PID lives, until it releases lock.takeover.
n=0
while tpid=$(lock_pid "$take") && [[ -d "$take" ]]; do
n=$((n + 1))
if [[ $n -gt 100 ]]; then
echo "Error: another run is taking over the lock (pid $tpid). Lock: $lock" >&2
while :; do
state=$(lock_state "$take")
tpid=${state#* }
case "$state" in
"live "*) ;;
nopid)
lock_why "$lock" "$2" takeover-nopid
return 1
fi
if [[ ! "$tpid" =~ ^[0-9]+$ ]]; then
lock_refuse "$take" "$2" "the takeover lock has no PID, so whether its run is still going can't be checked" \
"A run killed while taking it leaves it so" || return 1
fi
if ! kill -0 "$tpid" 2>/dev/null; then
;;
"dead "*)
# Gone, and lock.takeover still its: its run never released it.
[[ "$(cat "$take/pid" 2>/dev/null || true)" == "$tpid" ]] || continue
lock_refuse "$take" "$2" "a run (pid $tpid) was stopped while taking over the lock, and left its takeover lock" ||
return 1
lock_why "$lock" "$2" takeover-dead "$tpid"
return 1
;;
*) break ;;
esac
n=$((n + 1))
if [[ $n -gt 100 ]]; then
lock_why "$lock" "$2" taking "$tpid"
return 1
fi
sleep 0.1
done
Expand All @@ -190,8 +321,7 @@ lock_take() { # <lock> <what> [<wait>]
rc=0
lock_remove "$lock" || rc=$?
if [[ $rc != 0 ]]; then
[[ $rc != 2 ]] || echo "Error: not a lock folder (it holds other files than the pid file of a run that is" \
"gone): that pid file was removed, and the other files were left as they are: $lock" >&2
[[ $rc != 2 ]] || lock_why "$lock" "$2" dead-notlock || true
lock_release "$take" || true
return 1
fi
Expand Down Expand Up @@ -286,15 +416,19 @@ pr_json_get() { # <key>
}

# Sets <key> to the string <value> in <PR_STATE_DIR>/pr.json, made when missing. A file that is not one JSON object
# is an error that names it, and is left as it is.
# is an error that names it, and is left as it is. Written to .pr.json.tmp.<pid> next to it, then renamed: a name that
# is the tool's, never one a file of yours may have. Removed when the write fails.
pr_json_set() { # <key> <value>
local file="${PR_STATE_DIR:?}/pr.json" json='{}'
local file="${PR_STATE_DIR:?}/pr.json" tmp="$PR_STATE_DIR/.pr.json.tmp.$$" json='{}'
pr_state_mkdir || return 1
if [[ -e "$file" ]]; then
pr_json_check "$file" || return 1
json=$(jq -c . "$file") || return 1
fi
jq --arg k "$1" --arg v "$2" '.[$k] = $v' <<<"$json" >"$file.$$" && mv "$file.$$" "$file"
if ! jq --arg k "$1" --arg v "$2" '.[$k] = $v' <<<"$json" >"$tmp" || ! mv "$tmp" "$file"; then
rm -f "$tmp"
return 1
fi
}

# Makes the run folder <PR_STATE_DIR>/run-<UTC time>, 0700, and prints its path. The time is free among the run- and
Expand Down Expand Up @@ -330,13 +464,14 @@ run_dir_finish() { # <run-dir> <K | preview>
echo "$1"
}

# Writes the loop check's diagnosis (loop_text) to <PR_STATE_DIR>/loop-check.md: to a temporary file next to it, moved
# in once complete, so a failed or interrupted check keeps the last diagnosis, and two checks never mix their output.
# Writes the loop check's diagnosis (loop_text) to <PR_STATE_DIR>/loop-check.md: to a temporary file next to it,
# .loop-check.md.tmp.XXXXXX (a name that is the tool's, never one a file of yours may have), moved in once complete,
# so a failed or interrupted check keeps the last diagnosis, and two checks never mix their output.
# A loop-check.md that is there and not a file (a folder, which mv would move the temporary file into) is not
# replaced. A failure is a warning only.
loop_check_save() { # <decision-json>
local file="${PR_STATE_DIR:?}/loop-check.md" tmp=""
if ! pr_state_mkdir || ! tmp=$(mktemp "$file.XXXXXX") || ! loop_text "$1" >"$tmp" ||
if ! pr_state_mkdir || ! tmp=$(mktemp "$PR_STATE_DIR/.loop-check.md.tmp.XXXXXX") || ! loop_text "$1" >"$tmp" ||
[[ -e "$file" && ! -f "$file" ]] || ! mv "$tmp" "$file"; then
[[ -z "$tmp" ]] || rm -f "$tmp"
echo "Warning: could not write $file" >&2
Expand Down
Loading