Skip to content

feat(scripts): cross-repo GitHub branch hygiene defaults#46

Merged
mlorentedev merged 1 commit into
mainfrom
feat/gh-branch-hygiene
May 18, 2026
Merged

feat(scripts): cross-repo GitHub branch hygiene defaults#46
mlorentedev merged 1 commit into
mainfrom
feat/gh-branch-hygiene

Conversation

@mlorentedev
Copy link
Copy Markdown
Owner

Summary

Adds scripts/init-repo-github-defaults.{sh,ps1} -- small idempotent tool that enables delete_branch_on_merge: true on any GitHub repo via gh api. Plus bats coverage and a cross-project vault pattern.

Why

The 2026-05-18 marathon session (PRs #38-#45) left 8 orphan branches on origin because gh pr merge was used without --delete-branch on some calls and the repo did not have delete_branch_on_merge enabled. The retro-cleanup took 30 seconds but the policy fix prevents recurrence permanently.

This is a cross-project pattern -- every GitHub repo benefits. The script is portable; the vault pattern (00_meta/patterns/github-branch-hygiene.md) documents the when/why/how.

What

  • scripts/init-repo-github-defaults.sh -- bash, idempotent. Derives owner/name from origin remote (ssh + https forms), --dry-run, --help, --repo override for cross-repo backfill.
  • scripts/init-repo-github-defaults.ps1 -- PowerShell parity. ASCII-only per project convention (PSScriptAnalyzer rule PSUseBOMForUnicodeEncodedFile). -DryRun switch.
  • tests/init-repo-github-defaults.bats -- 17 assertions: syntax, idempotent early-exit, --dry-run, --help, unknown-arg rejection, ASCII check on .ps1, parity between .sh and .ps1.
  • Vault: _meta/patterns/github-branch-hygiene.md (created via Hive MCP) + entry in _meta/patterns/_index.md under "Git & CI/CD".

Usage

# Apply to current repo (origin auto-derived):
./scripts/init-repo-github-defaults.sh

# Apply to a different repo:
./scripts/init-repo-github-defaults.sh --repo owner/name

# Dry run:
./scripts/init-repo-github-defaults.sh --dry-run

Idempotent: if the setting is already enabled, the script prints [OK] Already enabled, nothing to do and exits 0.

Test plan

  • bats tests/init-repo-github-defaults.bats -- 17/17 green locally
  • bash -n + zsh -n on the .sh clean
  • --dry-run on this repo reports [OK] Already enabled (the dotfiles repo had the setting backfilled manually during the same session)
  • ASCII-only check on .ps1 (PSScriptAnalyzer convention)
  • CI green (lint, lint-powershell, test, integration)
  • Post-merge manual: run on one of your other GitHub repos with --repo owner/name to validate cross-repo backfill works

Out of scope (deferred)

  • Bulk audit script that lists all your repos and applies the setting. YAGNI for now; a follow-up if useful.
  • Other GitHub repo settings (squash-only merge, branch protection, etc.). One concern per script.
  • GitLab / Codeberg / Bitbucket parity. Same concept, different APIs.

Cross-links

Adds init-repo-github-defaults.{sh,ps1} -- a small idempotent tool that
enables `delete_branch_on_merge: true` on any GitHub repo via gh API,
plus a one-time PATCH for repos that drifted before this script existed.

Why
---
2026-05-18 marathon session: PRs #38-#45 merged via a mix of gh CLI and
the GitHub web UI. The CLI invocations did not include --delete-branch
on every call; the web UI honoured the repo's setting, which was
disabled. Result: 8 orphan branches on origin after the session, all
stale (their content was already squashed into main).

The retro-cleanup took 30 seconds (loop of `git push origin --delete`),
but the policy fix prevents recurrence permanently -- one PATCH per repo
and GitHub auto-deletes the head branch on every future merge.

What
----
- scripts/init-repo-github-defaults.sh  -- bash, idempotent, derives
                                           owner/name from origin remote
                                           (handles ssh + https forms),
                                           --dry-run, --help, --repo
                                           override, --repo for cross-
                                           repo backfill
- scripts/init-repo-github-defaults.ps1 -- PowerShell parity, ASCII-only
                                           per project convention
                                           (PSScriptAnalyzer rule
                                           PSUseBOMForUnicodeEncodedFile),
                                           -DryRun switch
- tests/init-repo-github-defaults.bats   -- 17 assertions: syntax, idem-
                                           potence early-exit, --dry-run,
                                           --help, unknown-arg
                                           rejection, ASCII-only on .ps1,
                                           parity between .sh and .ps1

Cross-project pattern documented in vault:
  00_meta/patterns/github-branch-hygiene.md
  (added to the patterns _index.md under Git & CI/CD)

Tests: 17/17 green locally. dotfiles repo itself already has the setting
enabled (applied manually during the same session). Re-running the
script reports `Already enabled, nothing to do` and exits 0.

When to use
-----------
- New repo creation: run after `gh repo create`. Catches the setting
  before the first PR.
- Existing repos: backfill with `--repo owner/name`. Idempotent on
  already-enabled repos.
- Long-lived: the patches in the script are the only persistent state;
  the script itself can be re-run any time without side effects.

Out of scope
------------
- Bulk audit of all the user's repos (script supports one --repo at a
  time; a wrapper that lists all repos and applies the setting could be
  added later, YAGNI for now)
- Other GitHub repo settings (squash/rebase/merge allowed flags,
  default branch protection, etc.) -- this script does ONE thing well;
  additional settings get their own scripts or future iterations
- GitLab / Codeberg / Bitbucket parity -- same concept, different APIs;
  separate per-vendor scripts when needed
@mlorentedev mlorentedev merged commit 984bb6d into main May 18, 2026
5 checks passed
@mlorentedev mlorentedev deleted the feat/gh-branch-hygiene branch May 18, 2026 04:11
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.

1 participant