refactor(worktree): load the reclaim merge cache only when it is used

reclaim_worktrees loaded the git-cherry merge cache on every call, dry
runs included, though a dry run never revalidates. It also never saved
the verdicts revalidation added, so the next audit re-ran `git cherry`
for any tree whose head moved after the audit (gate r2 efficiency, Low).
Load it only for a real run and save it once after the loop when it
grew, as audit_worktrees already does. Verdicts are unchanged: the cache
is only a memo.
This commit is contained in:
kshitijk4poor
2026-10-03 20:15:39 +05:30
committed by kshitij
parent a2ecf83336
commit 05ab8766e7

View File

@@ -290,7 +290,9 @@ def reclaim_worktrees(
if records is None: if records is None:
records = audit_worktrees(repo_root, with_sizes=False) records = audit_worktrees(repo_root, with_sizes=False)
merge_cache = _ops._load_worktree_merge_cache() # The cache only memoizes revalidation's `git cherry`; dry runs never revalidate.
merge_cache = {} if dry_run else _ops._load_worktree_merge_cache()
cache_size_before = len(merge_cache)
remote_heads = ( remote_heads = (
_ops._fetch_remote_branch_heads(repo_root) _ops._fetch_remote_branch_heads(repo_root)
if not dry_run and any(r.verdict == "reap-keep-branch" for r in records) else None) if not dry_run and any(r.verdict == "reap-keep-branch" for r in records) else None)
@@ -358,6 +360,8 @@ def reclaim_worktrees(
if not dry_run: if not dry_run:
with contextlib.suppress(Exception): with contextlib.suppress(Exception):
_git(["worktree", "prune"], cwd=repo_root, timeout=15) _git(["worktree", "prune"], cwd=repo_root, timeout=15)
if len(merge_cache) != cache_size_before:
_ops._save_worktree_merge_cache(merge_cache)
return actions return actions