diff --git a/src/pkgmgr/actions/branch/gc.py b/src/pkgmgr/actions/branch/gc.py index 9c4d7bd..3d54fca 100644 --- a/src/pkgmgr/actions/branch/gc.py +++ b/src/pkgmgr/actions/branch/gc.py @@ -8,6 +8,7 @@ import sys import time from collections.abc import Callable from dataclasses import asdict, dataclass, field +from functools import cache from graphlib import CycleError, TopologicalSorter from pkgmgr.actions.worktree.hooks import FINI_HOOK, hook_command, run_hook @@ -37,6 +38,7 @@ Snapshot = tuple[ dict[int, str], dict[str, tuple[str | None, int | None, int | None, bool]], Registrations, + Callable[[], dict[str, str]], ] # PROC_PID_INIT_INO: only the initial PID namespace sees every process of the host. @@ -665,7 +667,7 @@ def _build( tree: Worktree | None, snapshot: Snapshot, ) -> Item: - local, remote, cwds, tracking, registrations = snapshot + local, remote, cwds, tracking, registrations, held = snapshot upstream, ahead, behind, gone = tracking.get( branch or "", (None, None, None, False) ) @@ -759,7 +761,7 @@ def _build( and (time.time() - item.last_activity) / 3600 < repo.min_age ): item.skip = f"younger than {repo.min_age:g} h" - item.actions = _actions(repo, item, tree, checkout, dirty, registrations) + item.actions = _actions(repo, item, tree, checkout, dirty, registrations, held) if repo.unsafe: item.actions = [a for a in item.actions if not a.startswith("remote-branch:")] return item @@ -772,9 +774,10 @@ def _actions( checkout: bool, dirty: int, registrations: Registrations, + held: Callable[[], dict[str, str]], ) -> list[str]: if repo.scope == "local": - return _local_actions(repo, item, tree) + return _local_actions(repo, item, tree, held) tree_part: list[str] = [] if tree: if ( @@ -813,7 +816,12 @@ def _actions( return actions -def _local_actions(repo: Repo, item: Item, tree: Worktree | None) -> list[str]: +def _local_actions( + repo: Repo, + item: Item, + tree: Worktree | None, + held: Callable[[], dict[str, str]], +) -> list[str]: """ Settle the actions of an item planned with scope `local`. @@ -823,6 +831,8 @@ def _local_actions(repo: Repo, item: Item, tree: Worktree | None) -> list[str]: item: the classified item; a merged-clean one whose tip is an ancestor of none of them gets that as its skip. tree: the worktree holding its branch, or None. + held: the snapshot's _held_branches of the repository, read on the + first call and reused for every later item of that snapshot. Returns: `local-branch` for a merged-clean local branch that no worktree, @@ -832,9 +842,9 @@ def _local_actions(repo: Repo, item: Item, tree: Worktree | None) -> list[str]: """ if item.state != "merged-clean" or not item.local_sha or tree is not None: return [] - held = _held_branches(repo.repo).get(item.branch) - if held: - item.skip = item.skip or held + hold = held().get(item.branch) + if hold: + item.skip = item.skip or hold return [] targets = [repo.default, *repo.lines] if any(_is_ancestor(item.local_sha, ref, repo.repo) for ref in targets): @@ -854,6 +864,7 @@ def _snapshot(repo: Repo) -> tuple[list[Worktree], Snapshot]: cwds, tracking, _registrations(repo.repo), + cache(lambda: _held_branches(repo.repo)), ) diff --git a/tests/integration/test_branch_cleanup_local.py b/tests/integration/test_branch_cleanup_local.py index 2e9fb80..195cc57 100644 --- a/tests/integration/test_branch_cleanup_local.py +++ b/tests/integration/test_branch_cleanup_local.py @@ -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")