cleanup-merged-branches

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Delete remote branches that belong to a merged PR. Destructive, so it runs as a dry-run first and never deletes without explicit confirmation.
删除对应已合并PR的远程分支。由于操作具有破坏性,因此会先以**试运行(dry-run)**模式运行,且必须经过明确确认才会执行删除操作。

Delete rule

删除规则

A branch is deletable iff all hold — encoded in
scripts/plan.sh
, the single source of truth:
  • it still exists on the remote,
  • it has a merged PR,
  • it has no open PR,
  • it is not protected.
Protected = the default branch +
staging
. Never delete these, even if a merged PR points at them. A branch with no PR, or only a closed-unmerged PR, is not deletable (it never "belongs to a merged PR"). Extend the protected set in
is_protected()
inside
scripts/plan.sh
, nowhere else.
一个分支可被删除的前提是同时满足以下所有条件——这些规则编码在
scripts/plan.sh
中,是唯一的判断依据:
  • 该分支仍存在于远程仓库,
  • 该分支关联的PR已合并,
  • 该分支没有处于开放状态的PR,
  • 该分支不属于受保护分支。
受保护分支指默认分支 +
staging
分支。即使这些分支关联的PR已合并,也绝不能删除它们。没有关联PR,或仅关联了未合并就关闭的PR的分支,不可被删除(因为它们不属于“对应已合并PR的分支”)。如需扩展受保护分支集合,请修改
scripts/plan.sh
中的
is_protected()
函数,不要在其他地方修改。

Steps

操作步骤

  1. Dry-run. Run
    bash scripts/plan.sh
    (pass a remote name as
    $1
    if not
    origin
    ). It prints deletable branches as
    <branch>  #<pr>  <title>
    , and deletes nothing. Done when you have the list. Empty output → report "nothing to clean up" and stop.
  2. Show + confirm. Show the full list to the user and ask for explicit go. Do not delete on a vague reply — only on a clear yes. This gate is mandatory every run.
  3. Delete. For each confirmed branch:
    git push origin --delete <branch>
    . Nothing outside the plan's list may be deleted.
  4. Report. List what was deleted; note any push that failed.
  1. 试运行(Dry-run)。运行
    bash scripts/plan.sh
    (如果远程仓库不是
    origin
    ,可将远程名称作为
    $1
    参数传入)。它会以
    <分支名>  #<PR编号>  <PR标题>
    的格式列出可删除的分支,且不会执行任何删除操作。获取到列表后即可完成此步骤。若输出为空,则提示“无需要清理的内容”并终止流程。
  2. 展示并确认。将完整列表展示给用户,并请求明确的执行许可。仅在得到明确的“同意”回复时才可执行删除,模糊的回复不能作为执行依据。每次运行都必须经过此确认环节。
  3. 执行删除。对每个确认删除的分支,执行命令:
    git push origin --delete <分支名>
    。绝不能删除不在试运行列表中的任何分支。
  4. 结果报告。列出已成功删除的分支;若有推送失败的情况,需特别说明。

Also: stale-open issues

额外说明:长期开放的议题

Closes #N
only auto-closes on merge into the default branch — a PR merged into an integration branch (e.g.
rebuild/v1
) leaves #N open forever. For each merged PR you clean up here, check its linked issue: if the work is on the default branch but the issue is still open, hand-close it (
gh issue close #N --reason completed --comment "merged via #<pr>"
). Same misfire hides "done" issues as open — this is where to catch them.
只有当PR合并到默认分支时,
Closes #N
才会自动关闭对应的议题#N——如果PR合并到集成分支(例如
rebuild/v1
),议题#N会一直处于开放状态。对于在此处清理的每个已合并PR,请检查其关联的议题:如果工作内容已合并到默认分支但议题仍处于开放状态,请执行
gh issue close #N --reason completed --comment "merged via #<pr>"
手动关闭它。这种情况会导致已完成的议题被标记为开放,需要在此环节进行排查处理。