Files
computer-playbook/scripts/system/worktree/prune.sh
T
kevinveenbirkenbachandClaude Opus 5 005775a333 fix(worktree): prune only what is provably gone
841a5e0e1 claimed to keep entries whose checkout cannot be inspected, but the check resolved dirname(dirname(gitdir)) -- the checkout's PARENT, e.g. /tmp -- which always exists. The branch was dead code and every entry fell through to `rm -f gitdir HEAD`, including a live worktree whose metadata git can no longer read: that path drops it from the liveness list and it was then unregistered, which is the reported symptom.

Make the checkout directory itself the criterion, and refuse to conclude anything when its parent is not traversable -- `test -d` succeeds on a directory without +x, so a first attempt using -d alone reproduced the same blind spot. Reproduced in throwaway repos: a live worktree with a 0-byte commondir keeps its registration, a deleted checkout is pruned, and a metadata corpse without gitdir is cleaned.

Remaining limit: `git worktree prune`, which the script runs first, shares the blind spot and wins where it can unlink; this guard therefore only decides in the sandbox-pinned case that produced the bug.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 22:47:30 +02:00

106 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# shellcheck shell=bash
#
# Drop the registrations of worktrees whose checkout directory is gone, so the
# branches they still claim can be checked out again.
#
# `git worktree prune` alone is not enough under the agent sandbox: it deletes
# the metadata directory as a whole, and the sandbox holds some of its files
# (config.worktree, commondir) as read-only bind mounts, so the unlink fails
# with EBUSY and the entry survives. Removing gitdir + HEAD unregisters the
# worktree without needing the pinned files gone.
#
# Use `make worktree-prune` rather than calling this script directly.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)"
cd "${REPO_ROOT}"
if ! git worktree prune -v 2>/dev/null; then
echo ">>> git could not delete every stale entry; clearing the metadata by hand"
fi
meta_dir="$(git rev-parse --git-common-dir)/worktrees"
if [ ! -d "${meta_dir}" ]; then
echo "No stale worktree registrations left."
exit 0
fi
live_ids=()
while IFS= read -r line; do
case "${line}" in
"worktree "*) ;;
*) continue ;;
esac
checkout="${line#worktree }"
[ -f "${checkout}/.git" ] || continue
live_ids+=("$(basename "$(sed -n 's/^gitdir: //p' "${checkout}/.git")")")
done < <(git worktree list --porcelain)
is_live() {
local known
for known in ${live_ids[@]+"${live_ids[@]}"}; do
[ "${known}" = "${1}" ] && return 0
done
return 1
}
pruned=0
held=()
unverifiable=()
for entry in "${meta_dir}"/*; do
[ -d "${entry}" ] || continue
if is_live "$(basename "${entry}")"; then
continue
fi
gitdir=""
if [ -f "${entry}/gitdir" ]; then
gitdir="$(cat "${entry}/gitdir")"
fi
if [ -n "${gitdir}" ]; then
checkout="$(dirname "${gitdir}")"
base="$(dirname "${checkout}")"
if [ -e "${checkout}" ] || [ ! -d "${base}" ] || [ ! -x "${base}" ]; then
unverifiable+=("$(basename "${entry}") -> ${checkout}")
continue
fi
fi
registered=false
if [ -e "${entry}/gitdir" ] || [ -e "${entry}/HEAD" ]; then
registered=true
fi
rm -f "${entry}/gitdir" "${entry}/HEAD"
if ! rm -rf "${entry}" 2>/dev/null; then
held+=("${entry}")
fi
if [ "${registered}" = true ]; then
echo "Pruned $(basename "${entry}")"
pruned=$((pruned + 1))
fi
done
if [ "${pruned}" -eq 0 ] && [ "${#unverifiable[@]}" -eq 0 ]; then
echo "No stale worktree registrations left."
fi
if [ "${#held[@]}" -gt 0 ]; then
echo ">>> Unregistered, but the sandbox pins these metadata dirs; git ignores them now:"
printf ' %s\n' "${held[@]}"
echo ">>> Clear them outside the sandbox: rm -rf ${held[*]}"
fi
if [ "${#unverifiable[@]}" -gt 0 ]; then
echo ">>> Kept registered — their checkout is still on disk (or its parent is not"
echo ">>> readable from here), so 'gone' cannot be established:"
printf ' %s\n' "${unverifiable[@]}"
echo ">>> Release them with 'make worktree-down branch=<name>' instead."
fi