set -eu
: "${RK_REPO:?rk sets this; run this script through rk setup}"
: "${RK_TRUNK_BRANCH:?rk sets this; run this script through rk setup}"
for candidate in main develop; do
if [ "$candidate" = "$RK_TRUNK_BRANCH" ]; then
continue
fi
if gh api "repos/$RK_REPO/git/ref/heads/$candidate" >/dev/null 2>&1; then
status="$(gh api "repos/$RK_REPO/compare/$candidate...$RK_TRUNK_BRANCH" -q .status)"
case "$status" in
ahead | identical)
gh api -X DELETE "repos/$RK_REPO/git/refs/heads/$candidate"
echo "ok $candidate deleted"
;;
*)
echo "FAIL $candidate is not an ancestor of $RK_TRUNK_BRANCH ($status); deleting it would lose work" >&2
echo 'remediation: merge or move the work first, then rerun' >&2
exit 1
;;
esac
fi
done
echo 'check: prints nothing for the retired branches'
for candidate in main develop; do
gh api "repos/$RK_REPO/git/ref/heads/$candidate" -q .ref 2>/dev/null || true
done
echo 'ok single-trunk'