Skip to content

Latest commit

 

History

History
178 lines (134 loc) · 5.08 KB

File metadata and controls

178 lines (134 loc) · 5.08 KB

Branch Cleanup Implementation Summary

Problem Statement

The pollinations/pollinations repository had accumulated 2,878 remote branches, many of which were:

  • Stale from merged pull requests
  • Old feature branches
  • Abandoned work

This created repository hygiene issues and made it difficult to navigate active development.

Solution Implemented

1. Automated GitHub Actions Workflow

File: .github/workflows/cleanup-stale-branches.yml

Features:

  • Dry-run mode: Preview deletions before executing
  • Batch processing: Process branches in configurable batches (default 50)
  • Multiple filters:
    • merged-prs: Branches from merged pull requests (safest)
    • old-issue: Issue branches older than 6 months
    • old-feature: Feature branches older than 6 months
    • all: All branches older than 6 months
  • Safety measures:
    • Protected branch list (main, master, develop, staging, production)
    • Rate limiting between deletions
    • Artifact uploads for audit trail
  • Manual trigger: workflow_dispatch only (no automatic execution)

2. Comprehensive Documentation

File: BRANCH_CLEANUP.md

Includes:

  • How to use the GitHub Actions workflow
  • Manual cleanup instructions with bash scripts
  • Branch categorization guide
  • Safety measures and best practices
  • Troubleshooting section
  • Expected outcomes

3. Analysis Helper Script

File: scripts/analyze-branches.sh

A bash script to quickly analyze branch types:

  • Count total branches
  • Identify protected branches
  • Count by category (issue-numbered, feature, copilot, add-*)
  • Sample recent vs old branches
  • Provide cleanup recommendations

Usage Instructions

For Maintainers

  1. Initial Analysis:

    ./scripts/analyze-branches.sh
  2. First Cleanup (Dry Run):

    • Go to Actions → "Clean Up Stale Branches"
    • Configure:
      • dry_run: true
      • branch_filter: merged-prs
      • batch_size: 50
    • Click "Run workflow"
    • Download and review the artifact
  3. Execute Cleanup:

    • Same as above but set dry_run: false
    • Repeat as needed for remaining branches
  4. Review Progress:

    git fetch --all --prune
    git branch -r | grep -v '\->' | wc -l

Recommended Cleanup Order

  1. Merged PR branches (safest) - issue-numbered branches like 664-feature-name
  2. Old feature branches - feature/fix/hotfix branches > 6 months old
  3. Old issue branches - issue branches > 6 months old
  4. Other old branches - after careful review

Safety Guarantees

The implementation includes multiple safety layers:

  1. Hard-coded protected branches that can never be deleted
  2. Dry-run mode is the default
  3. Manual trigger only - no automatic cleanup
  4. Batch processing prevents overwhelming the system
  5. Audit trail via workflow artifacts
  6. Rate limiting to avoid API issues

Expected Outcomes

Target: Reduce from ~2,878 branches to ~50-100 active branches

Benefits:

  • Improved git performance
  • Easier navigation of active work
  • Better repository hygiene
  • Clearer view of ongoing development

Technical Notes

Branch Categories Identified

Based on analysis of the repository:

  1. Issue-numbered branches (e.g., 664-feature, 1234-fix-bug)

    • Typically created from GitHub issues/PRs
    • Safe to delete if PR is merged or closed
  2. Feature branches (e.g., feature/xyz, fix/abc, hotfix/urgent)

    • Development branches
    • Check for open PRs before deleting
  3. Add- branches* (e.g., add-new-project)

    • Usually project submissions
    • Check if PR was merged
  4. Copilot branches (e.g., copilot/task-name)

    • GitHub Copilot generated branches
    • Review activity before deleting
  5. Other patterns (e.g., hacktoberfest/*, date-based, etc.)

    • Case-by-case review needed

Workflow Logic

The workflow script:

  1. Fetches all remote branches
  2. Filters based on selected category
  3. Checks against protected branch list
  4. For merged-prs filter: validates PR merge status via GitHub API
  5. For age-based filters: checks last commit date
  6. Limits to batch size
  7. Deletes (or just lists in dry-run mode)
  8. Saves list to artifact

Maintenance

The cleanup workflow should be run periodically:

  • Monthly: For merged PR branches
  • Quarterly: For old feature branches
  • Annually: Full review of all branches

Files Changed

.github/workflows/cleanup-stale-branches.yml  (new)
BRANCH_CLEANUP.md                             (new)
scripts/analyze-branches.sh                   (new)

Security Review

✅ CodeQL analysis passed with 0 alerts ✅ No secrets or credentials in code ✅ Requires write permissions (controlled by repository settings) ✅ Manual trigger only (no automatic execution)

Next Steps

For repository maintainers:

  1. Review this implementation
  2. Test with dry-run mode
  3. Execute cleanup in batches
  4. Monitor progress
  5. Schedule periodic cleanups

Note: This implementation provides the tools but does not automatically delete any branches. All deletions must be manually triggered by repository maintainers with appropriate permissions.