Skip to content

Comments

feat: detect squash/rebase-merged PRs for branch cleanup#402

Merged
atoomic merged 1 commit intosukria:mainfrom
atoomic:koan.atoomic/github-aware-branch-cleanup
Feb 23, 2026
Merged

feat: detect squash/rebase-merged PRs for branch cleanup#402
atoomic merged 1 commit intosukria:mainfrom
atoomic:koan.atoomic/github-aware-branch-cleanup

Conversation

@Koan-Bot
Copy link
Contributor

Summary

  • Problem: git branch --merged only detects fast-forward merges. Squash-merged and rebase-merged PRs (the most common strategies) produce different commit SHAs, so their local branches are never cleaned up — 90+ stale branches accumulated.
  • Solution: New get_github_merged_branches() queries gh pr list --state merged to batch-detect branches whose PRs are confirmed merged on GitHub. cleanup_merged_branches() now handles both git-detected (safe -d) and GitHub-detected (force -D) merges.
  • Impact: The sync report combines both detection sources and the unmerged list shrinks to only genuinely unmerged branches.

Changes

  • git_sync.py: Added get_github_merged_branches() method, extended cleanup_merged_branches() with two-phase cleanup (safe + force), updated build_sync_report() to integrate both sources
  • test_git_sync.py: 18 new tests covering GitHub detection, force-delete safety, deduplication, error handling, and report integration

Safety

  • Phase 1 (git-detected): uses git branch -d (safe delete, refuses if not merged)
  • Phase 2 (GitHub-detected): uses git branch -D (force delete), only for branches confirmed merged by GitHub API
  • Never deletes the current branch
  • Only targets branches matching the agent prefix
  • Gracefully degrades: if gh CLI is unavailable, falls back to git-only detection

Test plan

  • All 11 TestGetGithubMergedBranches tests pass (error handling, filtering, dedup)
  • All 6 TestCleanupWithGithubMerged tests pass (force-delete, skip logic, safety)
  • All existing tests updated for new get_github_merged_branches() call
  • Full suite: 6230 passed, 0 failures

🤖 Generated by Kōan (autonomous session)

git branch --merged only detects fast-forward merges where commits are
direct ancestors of the target branch. Squash-merged and rebase-merged
PRs (the most common merge strategies) have different commit SHAs, so
their local branches accumulate forever — 90+ stale branches in practice.

New get_github_merged_branches() queries gh pr list --state merged to
batch-detect branches whose PRs are confirmed merged on GitHub. The
cleanup_merged_branches() method now accepts both git-detected (safe -d)
and GitHub-detected (force -D) merged branches, with proper dedup.

The sync report combines both sources and removes cleaned branches from
the unmerged section.

18 new tests, 6230 total pass.
@atoomic atoomic marked this pull request as ready for review February 23, 2026 15:00
@atoomic atoomic merged commit 326a0b9 into sukria:main Feb 23, 2026
3 checks passed
@atoomic atoomic deleted the koan.atoomic/github-aware-branch-cleanup branch February 23, 2026 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants