mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
chore: sweep other merged branches in post-merge cleanup (#3873)
* chore: sweep other merged branches in post-merge cleanup Branches from earlier PRs pile up whenever a cleanup is skipped. The skill now checks every other local and origin branch, finds the PRs that contain its tip by commit, and deletes it only when a merged PR has that tip as its head or in its history and no open PR uses it. next-major/*, gh-pages and branches in worktrees are left alone, and anything without a merged PR is kept and reported. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * chore: delete a branch on origin only while it is at the tip that was checked A plain `push --delete` removes whatever the branch points to when the push runs, so commits pushed after the check would go unchecked. Both remote deletes now lease on the checked tip: the PR's head in step 5, and the origin tip in step 6. Git refuses the delete with "stale info" if the branch has moved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
---
|
||||
name: post-merge-cleanup
|
||||
description: Reset the repo after a PR has merged, ready for the next piece of work. Removes the branch's worktree if it has one, goes back to master, pulls, deletes the merged branch locally and on origin, and runs npm install. Use when the user says their PR is merged and wants to clean up or start the next task.
|
||||
description: Reset the repo after a PR has merged, ready for the next piece of work. Removes the branch's worktree if it has one, goes back to master, pulls, deletes the merged branch locally and on origin, deletes any other branches whose PRs have merged, and runs npm install. Use when the user says their PR is merged and wants to clean up or start the next task, or wants old merged branches cleared out.
|
||||
allowed-tools: Bash, ExitWorktree
|
||||
argument-hint: "[branch]"
|
||||
---
|
||||
|
||||
# Post-Merge Cleanup
|
||||
|
||||
Bring the repo back to a clean, up-to-date `master` after a PR has merged. The branch to clean up is `$ARGUMENTS` if given, otherwise the branch checked out in the current directory.
|
||||
Bring the repo back to a clean, up-to-date `master` after a PR has merged. The branch to clean up is `$ARGUMENTS` if given, otherwise the branch checked out in the current directory. Branches from earlier PRs are swept up too, because they pile up whenever a cleanup is skipped.
|
||||
|
||||
PRs here are squash merged, so the branch's commits never appear on `master` and `git branch -d` always refuses. Merge status therefore comes from GitHub, and the safety checks below are what make it safe to force-delete. If any check fails, stop and tell the user what you found. Never force anything past a failed check.
|
||||
|
||||
@@ -30,7 +30,7 @@ git worktree list --porcelain # which checkou
|
||||
gh pr list --state merged --head "$BRANCH" --json number,title,headRefOid,mergedAt
|
||||
git rev-parse "$BRANCH"
|
||||
```
|
||||
Stop if no merged PR is listed. Stop if the local tip differs from the PR's `headRefOid`, because it has commits that never reached the PR.
|
||||
Stop if no merged PR is listed. Stop if the local tip differs from the PR's `headRefOid` (`PR_HEAD`), because it has commits that never reached the PR.
|
||||
2. **No uncommitted work in the worktree** (only when `WORKTREE` exists). `git -C "$WORKTREE" status --porcelain` must be empty. Removing the worktree would delete anything listed, including untracked files.
|
||||
3. **No tracked changes in `MAIN`.** `git -C "$MAIN" status --porcelain --untracked-files=no` must be empty. Untracked files in `MAIN`, such as plan notes, are fine because they carry over to `master`.
|
||||
|
||||
@@ -61,24 +61,57 @@ Skip this step if there is no `BRANCH`.
|
||||
|
||||
```bash
|
||||
git -C "$MAIN" branch -D "$BRANCH" # skip if ExitWorktree already deleted it
|
||||
git -C "$MAIN" ls-remote --exit-code --heads origin "$BRANCH" && git -C "$MAIN" push origin --delete "$BRANCH"
|
||||
git -C "$MAIN" ls-remote --exit-code --heads origin "$BRANCH" && git -C "$MAIN" push origin --delete --force-with-lease="$BRANCH:$PR_HEAD" "$BRANCH"
|
||||
git -C "$MAIN" fetch --prune
|
||||
```
|
||||
|
||||
GitHub often deletes the remote branch on merge. `ls-remote` exits non-zero when it is already gone, and then there is nothing to push.
|
||||
GitHub often deletes the remote branch on merge. `ls-remote` exits non-zero when it is already gone, and then there is nothing to push. The lease makes the delete fail with `stale info` if someone pushed to the branch after the PR merged. Then stop and report it.
|
||||
|
||||
## 6. Reinstall dependencies
|
||||
## 6. Delete other merged branches
|
||||
|
||||
List every other branch, locally and on origin, and the branches checked out in worktrees:
|
||||
|
||||
```bash
|
||||
git -C "$MAIN" for-each-ref --format='%(refname:short) %(objectname)' refs/heads
|
||||
git -C "$MAIN" for-each-ref --format='%(refname:lstrip=3) %(objectname)' refs/remotes/origin
|
||||
git -C "$MAIN" worktree list --porcelain
|
||||
```
|
||||
|
||||
Leave these alone without checking them: `master`, `gh-pages`, `HEAD`, `next-major/*` (breaking changes saved for the next major, which have no PR), and any branch checked out in a worktree.
|
||||
|
||||
Check each remaining branch `B`. A branch that is both local and on origin is checked once per tip, because the two can differ. Find the PRs that contain its tip `TIP`. The lookup goes by commit, so it also finds the PR for a branch whose name differs from the PR's head, such as `pr-3768` fetched from a fork PR or a local copy of a Dependabot branch:
|
||||
|
||||
```bash
|
||||
gh api "repos/{owner}/{repo}/commits/$TIP/pulls" --jq '.[] | select(.merged_at) | "\(.number) \(.head.sha)"'
|
||||
gh pr list --state open --head "$B" --json number
|
||||
```
|
||||
|
||||
Delete `B` only when a merged PR is listed, no open PR has `B` as its head, and `TIP` is the merged PR's head or an earlier commit of it. The earlier-commit case comes up when the PR gained commits after the branch was copied:
|
||||
|
||||
```bash
|
||||
git -C "$MAIN" fetch -q origin "pull/$PR/head" # only when TIP is not the PR's head
|
||||
git -C "$MAIN" merge-base --is-ancestor "$TIP" FETCH_HEAD
|
||||
git -C "$MAIN" branch -D "$B" # local tip
|
||||
git -C "$MAIN" push origin --delete --force-with-lease="$B:$TIP" "$B" # tip on origin
|
||||
```
|
||||
|
||||
The lease makes the delete fail with `stale info` if the branch on origin has moved since its tip was checked. Keep that branch and report it.
|
||||
|
||||
Keep every other branch and list it in the report with its last commit date and subject. It may hold work that never reached a PR, so delete it only when the user says to.
|
||||
|
||||
## 7. Reinstall dependencies
|
||||
|
||||
```bash
|
||||
cd "$MAIN" && npm install
|
||||
```
|
||||
|
||||
## 7. Report
|
||||
## 8. Report
|
||||
|
||||
Keep the report short:
|
||||
|
||||
- the worktree removed, if any
|
||||
- the branch deleted, both locally and on origin
|
||||
- the other merged branches deleted in step 6, local and on origin, and the branches kept, with the reason for each
|
||||
- the `master` commit you are now on (`git log --oneline -1`)
|
||||
- whether `npm install` succeeded
|
||||
- `git status --short`. Call out `package-lock.json` if `npm install` changed it.
|
||||
|
||||
Reference in New Issue
Block a user