mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 22:37:29 +00:00
Staleness was decided by reading gitdir inside the metadata dir. Under the agent sandbox that dir is pinned as read-only bind mounts, so a live worktree can read as having no gitdir at all -- prune then removed gitdir and HEAD and left the checkout as 'fatal: not a git repository' with no way back. Decide liveness from git's own worktree list plus each checkout's .git pointer instead, keep entries whose checkout base is not visible rather than guessing, drop the expected EBUSY noise, and report the pinned leftovers once with the command that clears them. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
108 lines
2.8 KiB
Bash
Executable File
108 lines
2.8 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}" ] && [ -e "${gitdir}" ]; then
|
|
continue
|
|
fi
|
|
|
|
if [ -n "${gitdir}" ]; then
|
|
base="$(dirname "$(dirname "${gitdir}")")"
|
|
if [ ! -d "${base}" ]; then
|
|
unverifiable+=("$(basename "${entry}") -> ${gitdir}")
|
|
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 base is not visible from here, so"
|
|
echo ">>> whether the checkout is gone cannot be decided:"
|
|
printf ' %s\n' "${unverifiable[@]}"
|
|
echo ">>> Re-run outside the sandbox to prune them."
|
|
fi
|