From e54781fd813625da1c5eef2a1aa19bc2b7b2a78a Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 25 Aug 2026 15:21:58 +0000 Subject: [PATCH] fix(branch_sweep): protect legacy trunks by name, not by containment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The first org-wide audit found a repo carrying a `master` branch alongside `main`. It landed in KEEP, but only because it happened to hold unique content — the sweep protected the *default* branch and nothing else, so a `master` fully folded into `main` after a rename is indistinguishable from spent work and would have been deleted. That is not an ordinary branch. Deleting it breaks every stale clone, bookmark, CI reference and doc link still pointing at the old trunk, and nothing about the branch's content says so — which is exactly why the skill's own recipe has always excluded both names (`grep -vE '^(main|master)$'`). Protecting only the default silently implemented half of a rule that was written whole. So `main` and `master` are now protected on NAME, whether or not either is this repo's default, alongside whatever the default actually is. Found by running the audit across 25 repos before deleting anything in any of them — the value of audit-first is that it surfaces this class of thing while it is still hypothetical. The test fails without the guard and passes with it. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01KwqicJpMqmcT5RVbyNwdKq --- bin/branch_sweep.sh | 15 +++++++++++++-- tests/test_branch_sweep.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/bin/branch_sweep.sh b/bin/branch_sweep.sh index a39d955d..3e2ba6b4 100755 --- a/bin/branch_sweep.sh +++ b/bin/branch_sweep.sh @@ -19,7 +19,7 @@ # view, so the safety gates from skills/repo_cleanup/SKILL.md are enforced here # rather than assumed of the caller: # -# * `main` / the default branch — never a candidate +# * `main`, `master`, and the default branch — never a candidate # * `archive/condemned/*` — PyAutoGut transit refs; # voiding these before their sweep-after date destroys the recovery path # the Gut exists to provide. The Gut voids them, not us. @@ -156,10 +156,21 @@ default_branch=$(g symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/ default_branch="${default_branch#origin/}" default_branch="${default_branch:-main}" +# Trunk names are protected whether or not they are THIS repo's default. +# A repo migrated main<-master keeps the old trunk as an ordinary branch, and +# once it is fully contained in main the sweep would happily delete it — +# breaking every stale clone, bookmark and doc that still points at it. The +# skill's own recipe has always excluded both names (`grep -vE +# '^(main|master)$'`); protecting only the default silently dropped half of +# that. Found by the first org-wide audit, where one repo's `master` survived +# on unique content alone. +is_trunk() { [[ "$1" == "$default_branch" || "$1" == "main" || "$1" == "master" ]]; } + # --- classify ---------------------------------------------------------------- safe=() keep=() protected=() while read -r b; do - [[ -n "$b" && "$b" != "HEAD" && "$b" != "$default_branch" ]] || continue + [[ -n "$b" && "$b" != "HEAD" ]] || continue + is_trunk "$b" && continue case "$b" in archive/condemned/*) protected+=("$b gut-transit-ref"); continue ;; esac diff --git a/tests/test_branch_sweep.py b/tests/test_branch_sweep.py index 28922c8b..449955b6 100644 --- a/tests/test_branch_sweep.py +++ b/tests/test_branch_sweep.py @@ -163,6 +163,36 @@ def test_delete_removes_only_the_contained_branch(world): assert {"main", "unmerged", "open-pr-head", "archive/condemned/something"} <= remaining +def test_a_legacy_trunk_is_protected_even_when_fully_merged(tmp_path): + """`master` after a main<-master migration is not an ordinary branch. + + It is protected on NAME, not on containment: once folded into main it looks + exactly like spent work, and deleting it breaks every stale clone, bookmark + and doc still pointing at it. The first org-wide audit found a repo whose + `master` survived only because it happened to carry unique content — luck, + not a gate. + """ + origin = tmp_path / "origin" + origin.mkdir() + subprocess.run(["git", "init", "-q", "-b", "main", str(origin)], check=True) + _git(origin, "config", "user.email", "t@t") + _git(origin, "config", "user.name", "t") + _commit(origin, "f", "base", "base") + # a legacy trunk fully contained in main — indistinguishable from spent work + _git(origin, "branch", "master") + + clone = tmp_path / "clone" + subprocess.run(["git", "clone", "-q", str(origin), str(clone)], check=True) + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + (bin_dir / "gh").write_text('#!/usr/bin/env bash\nexit 0\n') + (bin_dir / "gh").chmod(0o755) + + assert _sweep(clone, bin_dir, mode="delete").returncode == 0 + remaining = set(_git(origin, "for-each-ref", "--format=%(refname:short)", "refs/heads").split()) + assert "master" in remaining, "a legacy trunk must never be swept" + + def test_refuses_to_run_without_gh(world): """Blind to open PRs means blind to in-flight work: refuse, do not guess.""" clone, _, _ = world