Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions bin/branch_sweep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions tests/test_branch_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading