perf(branch): read the held branches once per cleanup plan
branch cleanup --local reads the held-branches map lazily once per snapshot instead of once per item, so a plan reads it once and every apply-time re-check still reads it fresh; a branch held after the plan is still skipped. On a scratch repository with 60 merged branches and 101 git dirs the plan drops from 27.1 s to 2.3 s with a byte-identical JSON plan. Tests run once after the merge (operator order 2026-11-14). 🎯 Fix: 85 % 💥 Break: 3 % 💸 Tokens: 10 % ⏱️ Velocity: 5 % 🛡️ Security: 100 % 🏆 Score: 100 🚦 Gate: ⛔ fix 85 % < 90 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
757903fdd5
commit
e017af0cd1
2 files changed
+64
-7
No files matched your search
@@ -489,6 +489,52 @@ class TestBranchCleanupLocal(unittest.TestCase):
|
||||
heads["refs/heads/local-done"],
|
||||
)
|
||||
|
||||
def test_plan_reads_the_held_branches_once_per_repository(self) -> None:
|
||||
self._set_head()
|
||||
extra = ("more-a", "more-b", "more-c")
|
||||
for name in extra:
|
||||
git(self.repo, "branch", name, "done")
|
||||
|
||||
with patch.object(gc, "_held_branches", wraps=gc._held_branches) as held:
|
||||
run = self._cleanup("local", apply=False)
|
||||
|
||||
self.assertEqual(held.call_count, 1)
|
||||
self.assertEqual(
|
||||
{name for name, i in run.items.items() if i["actions"]},
|
||||
{"done", "local-done", *extra},
|
||||
)
|
||||
|
||||
def test_branch_held_after_the_plan_is_not_deleted(self) -> None:
|
||||
self._set_head()
|
||||
real = gc._rebuild
|
||||
changes = [
|
||||
lambda: git(
|
||||
self.repo, "symbolic-ref", "refs/tags/pin", "refs/heads/local-done"
|
||||
)
|
||||
]
|
||||
|
||||
def late(repo: gc.Repo, item: gc.Item) -> gc.Item:
|
||||
if changes:
|
||||
changes.pop()()
|
||||
return real(repo, item)
|
||||
|
||||
with (
|
||||
patch.object(gc, "_held_branches", wraps=gc._held_branches) as held,
|
||||
patch.object(gc, "_rebuild", side_effect=late),
|
||||
):
|
||||
run = self._cleanup("local", apply=True)
|
||||
|
||||
self.assertEqual(held.call_count, 3)
|
||||
self.assertEqual(
|
||||
self._results(run),
|
||||
{
|
||||
"done": "done",
|
||||
"local-done": "skipped: changed since plan "
|
||||
"(target of symbolic ref refs/tags/pin)",
|
||||
},
|
||||
)
|
||||
self.assertIn("refs/heads/local-done", self._refs(self.repo, "refs/heads"))
|
||||
|
||||
def test_delete_never_follows_a_symref_made_after_the_check(self) -> None:
|
||||
planned = git(self.repo, "rev-parse", "held-done")
|
||||
|
||||
|
||||
Reference in new issue
Block a user