From 44743b6c89334a7b0c1ece02cad55b8410f399c2 Mon Sep 17 00:00:00 2001 From: david catalan Date: Sun, 29 Nov 2026 21:08:51 +0100 Subject: [PATCH 01/35] Add gh pr-review gc to remove the review worktrees and refs of closed PRs --- gh-pr-review | 1 + lib/gc.sh | 433 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/gc.sh | 350 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 784 insertions(+) create mode 100644 lib/gc.sh create mode 100644 tests/gc.sh diff --git a/gh-pr-review b/gh-pr-review index 0c20c83..8a6e9f1 100755 --- a/gh-pr-review +++ b/gh-pr-review @@ -4,6 +4,7 @@ # Usage: # gh pr-review [options] # gh pr-review rounds [--json] [-u ] +# gh pr-review gc [--dry-run] [--purge] [-u ] # gh pr-review --doctor # # Options: diff --git a/lib/gc.sh b/lib/gc.sh new file mode 100644 index 0000000..fe9faa8 --- /dev/null +++ b/lib/gc.sh @@ -0,0 +1,433 @@ +#!/usr/bin/env bash +# gh pr-review gc: removes the review worktrees and private refs of merged and closed PRs. Functions only. +# Uses state_root, lock_pid, lock_take, lock_release, path_absent and pr_json_get (lib/state.sh), rgit and clone_root +# (lib/clones.sh), pr_key, pr_refs, owned_worktree, reviews_check, clone_lock, missing_path, path_within and +# paths_overlap (lib/worktree.sh), and use_gh_user (lib/common.sh). +# +# The run's settings and results are globals: GC_DRY, GC_PURGE, GC_USER and GC_ROOT; the locks held (GC_PR_LOCK, +# GC_CLONE_LOCK), released on exit; and, for the PR at hand, GC_DONE (what was removed), GC_LEFT (why something was +# left: a failure) and GC_SKIP (why the PR was skipped, not an error). + +gc_usage() { + echo "Usage: gh pr-review gc [--dry-run] [--purge] [-u ]" + echo "Removes the review worktree and the private refs of each merged or closed PR in the state folder." + echo " --dry-run print what it would do, and change nothing" + echo " --purge also remove the PR's state folder: its runs, pr.json and the last loop check" + echo " -u the gh account that reads the PRs' states (default: the one in the PR's pr.json, else" + echo " GH_PR_REVIEW_USER, else the active one)" +} + +# The first line of an error message, without its "Error: ". +gc_why() { # + local line=${1%%$'\n'*} + printf '%s\n' "${line#Error: }" +} + +# Prints the PID of the live process that holds the lock folder ; fails when none does (no lock, no PID, or a +# PID that is gone). +gc_holder() { # + local pid + [[ -e "$1/pid" ]] && pid=$(lock_pid "$1") && [[ "$pid" =~ ^[0-9]+$ ]] && kill -0 "$pid" 2>/dev/null && + printf '%s\n' "$pid" +} + +# Adds to GC_DONE, the list of what was removed (or would be, with --dry-run). +gc_done() { # + GC_DONE="${GC_DONE:+$GC_DONE, }$1" +} + +# True when the physical path is where the run puts the PR's review worktree: //// +# /, with one folder. +gc_shape() { # + local rest=${1#"$2/${3%/*}/"} + [[ "$1" == "$2/${3%/*}/"* && "$rest" == */"${3##*/}" ]] || return 1 + rest=${rest%/*} + [[ -n "$rest" && "$rest" != */* && "$rest" != . && "$rest" != .. ]] +} + +# Removes the PR's review worktree, in pr.json, from (a physical path) with `git worktree remove +# --force`: the tool's checkout, maybe dirty from a review. Only when every check passes: it is a worktree of the +# clone and git, run in it, agrees (owned_worktree); its physical path is where the run puts it, in the checked +# (gc_shape), with no .git and no working tree on the way (reviews_check); it holds neither the state +# folder nor the PR's folder, nor is in the PR's folder. Otherwise it is left as it is, and GC_LEFT says why. A +# worktree whose folder is gone, but that the clone still lists at that very path (deleted outside git), has that one +# registration removed, the same way. Never `git worktree prune`: it would drop the registrations of the clone's other +# worktrees whose folders are missing (on a volume that is not mounted). Sets GC_WT to the worktree's physical path +# when it is the tool's, for gc_tidy. +gc_worktree() { # + local saved=$4 real list err why="" gone=false root + GC_WT="" + [[ -n "$saved" ]] || return 0 + if [[ "$saved" != /* ]]; then + why="it is not an absolute path" + elif path_absent "$saved"; then + if ! real=$(missing_path "$saved") || ! gc_shape "$real" "$2" "$3"; then + return 0 + fi + GC_WT=$real + list=$(rgit -C "$1" worktree list --porcelain) || { + GC_LEFT="could not list the worktrees of $1" + return 0 + } + grep -qxF "worktree $real" <<<"$list" || return 0 + gone=true + elif ! owned_worktree "$1" "$saved"; then + why="it is not a worktree of $1" + else + real=$(cd -P "$saved" && pwd -P) + root=$(missing_path "$(state_root)") || root="" + if ! gc_shape "$real" "$2" "$3"; then + why="it is not where the run puts it ($2/${3%/*}//${3##*/})" + elif ! err=$(reviews_check "$2" "${real%/*}" 2>&1); then + why=$(gc_why "$err") + elif [[ -z "$root" ]] || path_within "$root" "$real"; then + why="it holds the state folder $(state_root)" + elif paths_overlap "$real" "${PR_STATE_DIR:?}"; then + why="it and the PR's state folder, $PR_STATE_DIR, overlap" + else + GC_WT=$real + fi + fi + if [[ -n "$why" ]]; then + GC_LEFT="the worktree $saved was left as it is: $why" + return 0 + fi + if [[ $GC_DRY == false ]] && + ! err=$(rgit -C "$1" worktree remove --force --end-of-options "$real" 2>&1); then + GC_LEFT="git could not remove the worktree $saved: $(gc_why "$err")" + return 0 + fi + if [[ $gone == true ]]; then + gc_done "the worktree $saved (its folder was already gone)" + else + gc_done "the worktree $saved" + fi +} + +# Deletes the PR's private refs in , every ref under refs/pr-review// (pr_refs): its head, base and +# round pins. One `update-ref --stdin` transaction, each delete with the value just read, so all go or none. +gc_refs() { # + local refs list n err + refs=$(pr_refs "$2") || return 1 + if ! list=$(rgit -C "$1" for-each-ref --format='delete %(refname) %(objectname)' "$refs"); then + GC_LEFT="${GC_LEFT:+$GC_LEFT; }could not list the PR's refs in $1" + return 0 + fi + [[ -n "$list" ]] || return 0 + n=$(grep -c . <<<"$list") + if [[ $GC_DRY == false ]] && ! err=$(printf '%s\n' "$list" | rgit -C "$1" update-ref --stdin 2>&1); then + GC_LEFT="${GC_LEFT:+$GC_LEFT; }git could not delete the PR's refs in $1: $(gc_why "$err")" + return 0 + fi + if [[ $n == 1 ]]; then gc_done "1 ref"; else gc_done "$n refs"; fi +} + +# Removes the folders that are left empty in : the ones above the worktree (when given) up to +# , the folders above the PR's clone lock in .locks, .locks, and itself, each only when it is empty +# (rmdir). Nothing in them is ever deleted. +gc_tidy() { # [] + local d + if [[ -n "${3:-}" ]]; then + d=${3%/*} + while [[ "$d" == "$1/"* ]]; do + [[ ! -d "$d" ]] || rmdir "$d" 2>/dev/null || break + d=${d%/*} + done + fi + d="$1/.locks/${2%/*}" + while [[ "$d" == "$1/.locks"* ]]; do + [[ ! -d "$d" ]] || rmdir "$d" 2>/dev/null || break + d=${d%/*} + done + rmdir "$1" 2>/dev/null || true +} + +# Cleans up the PR in the clone that pr.json names, : under the clone's lock for the PR (clone_lock, which +# checks .reviews first), removes its review worktree (gc_worktree) and its refs (gc_refs), releases +# the lock, then removes the folders left empty (gc_tidy). A clone that is not there (deleted, or on a volume that is +# not mounted) is a skip; a path that is not a clone's top folder, or a .reviews that is not the tool's, leaves all. +gc_clone() { # + local clone reviews lock pid err + if [[ "$1" != /* ]]; then + GC_LEFT="pr.json's clone is not an absolute path, so nothing was removed: $1" + return 0 + fi + if path_absent "$1"; then + GC_SKIP="its clone is not there (deleted, or on a volume that is not mounted), so nothing was removed: $1" + return 0 + fi + if ! clone=$(cd -P "$1" 2>/dev/null && pwd -P) || [[ "$(clone_root "$clone" || true)" != "$clone" ]]; then + GC_LEFT="$1 is not the top folder of a clone, so nothing was removed" + return 0 + fi + reviews="$(dirname "$clone")/$(basename "$clone").reviews" + lock="$reviews/.locks/$2" + if pid=$(gc_holder "$lock"); then + GC_SKIP="a review of it is running (pid $pid)" + return 0 + fi + if [[ $GC_DRY == true ]]; then + err=$(reviews_check "$reviews" "$lock" 2>&1) || { + GC_LEFT="nothing was removed: $(gc_why "$err")" + return 0 + } + else + # In a subshell, so its messages can be read: the lock's pid file still holds this process's PID ($$). + if ! err=$(clone_lock "$clone" "$2" 2>&1); then + [[ -z "$err" ]] || printf '%s\n' "$err" >&2 + if pid=$(gc_holder "$lock"); then + GC_SKIP="a review of it is running (pid $pid)" + else + GC_LEFT="nothing was removed: $(gc_why "$err")" + fi + return 0 + fi + [[ -z "$err" ]] || printf '%s\n' "$err" >&2 + GC_CLONE_LOCK=$lock + fi + gc_worktree "$clone" "$reviews" "$2" "$3" + gc_refs "$clone" "$2" + [[ $GC_DRY == false ]] || return 0 + GC_CLONE_LOCK="" + lock_release "$lock" || GC_LEFT="${GC_LEFT:+$GC_LEFT; }could not release the clone's lock $lock" + gc_tidy "$reviews" "$2" "$GC_WT" +} + +# --purge: removes the PR's state folder . gc_pr found it as ////, its names +# those of a PR (pr_key), N a number; none of its folders below the state folder may be a symlink, and it must not +# overlap the clone or its .reviews. While the PR's lock is held, it deletes what runs put there: pr.json, +# loop-check.md (and their temporary files), and the run-, round- and preview- folders. Then it releases the lock, and +# removes the folder with rmdir, so it stays when it holds anything else (a file of yours, a lock a run took since), +# and so does that; then the , and folders, when empty. Never the state folder itself. +gc_purge() { # + local d e left="" + d=$1 + while [[ "$d" == "$GC_ROOT/"* ]]; do + if [[ -L "$d" || ! -d "$d" ]]; then + GC_LEFT="the state folder was left as it is: $d is a symlink, or not a folder" + return 0 + fi + d=${d%/*} + done + if [[ -n "$2" ]] && { paths_overlap "$1" "$2" || paths_overlap "$1" "$2.reviews"; }; then + GC_LEFT="the state folder was left as it is: it overlaps the clone $2 or its .reviews" + return 0 + fi + if [[ $GC_DRY == true ]]; then + gc_done "the state folder $1" + return 0 + fi + for e in "$1/pr.json" "$1"/pr.json.* "$1/loop-check.md" "$1"/loop-check.md.* "$1"/run-* "$1"/round-* \ + "$1"/preview-*; do + [[ -e "$e" || -L "$e" ]] || continue + rm -rf -- "$e" || { + GC_LEFT="could not remove $e, so the rest of the state folder was left" + return 0 + } + done + GC_PR_LOCK="" + lock_release "$1/lock" || { + GC_LEFT="the state folder's runs and pr.json were removed, but not its lock: $1/lock" + return 0 + } + if ! rmdir "$1" 2>/dev/null; then + for e in "$1"/* "$1"/.[!.]* "$1"/..?*; do + [[ ! -e "$e" && ! -L "$e" ]] || left="${left:+$left, }${e##*/}" + done + gc_done "the state folder's runs and pr.json" + GC_DONE="$GC_DONE; kept the state folder $1: it holds other files ($left)" + return 0 + fi + gc_done "the state folder $1" + d=${1%/*} + while [[ "$d" == "$GC_ROOT/"* ]]; do + rmdir "$d" 2>/dev/null || break + d=${d%/*} + done +} + +# Prints the PR's line and counts it: kept (open), removed (or would remove), skipped (GC_SKIP), or failed (GC_LEFT). +gc_report() { #