| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- # Deletes branches that are done, in two ways:
- # - delete: when a pull request from a branch of this repository is merged, its branch is deleted. Pull requests from
- # forks are ignored. A branch closed without merging stays.
- # - merged-into-main: on every push to main, every branch whose commits are all in main and whose tip came into main
- # through a merge is deleted. This covers branches merged locally (git merge + git push) without a pull request.
- # A branch that points at a commit of main's own line (a new branch with no commits yet, or one merged as a fast
- # forward) stays, so a freshly created branch is not deleted before its first commit.
- # main and backup/* are never touched, and neither is a branch that is still the base of an open pull request (deleting
- # it through the API would close that pull request instead of retargeting it). No commit is ever lost: a branch is only
- # deleted when main already contains all of its commits.
- name: Delete merged branch
- on:
- pull_request:
- types: [closed]
- push:
- branches: [main]
- permissions:
- contents: write
- pull-requests: read
- jobs:
- delete:
- if: github.event_name == 'pull_request' && github.event.pull_request.merged == true && github.event.pull_request.head.repo.full_name == github.repository
- runs-on: ubuntu-latest
- steps:
- - name: Delete the merged branch
- env:
- GH_TOKEN: ${{ github.token }}
- BRANCH: ${{ github.event.pull_request.head.ref }}
- REPO: ${{ github.repository }}
- run: |
- # the branch name comes from the pull request: validate it as a plain name before using it anywhere
- if ! [[ $BRANCH =~ ^[A-Za-z0-9._/-]+$ ]] || [[ $BRANCH == -* || $BRANCH == *..* ]]; then
- echo "::error::Refusing '$BRANCH': not a plain branch name"; exit 1
- fi
- case "$BRANCH" in
- main|backup/*) echo "Keeping the long-lived branch $BRANCH"; exit 0 ;;
- esac
- open=$(gh pr list -R "$REPO" --base "$BRANCH" --state open --json number --jq 'length')
- if [ "$open" != 0 ]; then
- echo "Keeping $BRANCH: it is the base of $open open pull request(s)"; exit 0
- fi
- if ! gh api "repos/$REPO/git/refs/heads/$BRANCH" --silent 2>/dev/null; then
- echo "$BRANCH is already gone"; exit 0
- fi
- echo "Deleting $BRANCH"
- gh api -X DELETE "repos/$REPO/git/refs/heads/$BRANCH"
- merged-into-main:
- if: github.event_name == 'push'
- runs-on: ubuntu-latest
- # two pushes to main in a row: the second run waits instead of deleting in parallel
- concurrency:
- group: branch-autodelete-main
- cancel-in-progress: false
- steps:
- # full history of every branch, but no file contents: only the commit graph is needed
- - uses: actions/checkout@v5
- with:
- fetch-depth: 0
- filter: blob:none
- sparse-checkout: .github
- - name: Delete the branches merged into main
- env:
- GH_TOKEN: ${{ github.token }}
- REPO: ${{ github.repository }}
- run: |
- set -euo pipefail
- git rev-list --first-parent origin/main > "$RUNNER_TEMP/main-line"
- gh pr list -R "$REPO" --state open --limit 1000 --json baseRefName,headRefName,isCrossRepository \
- --jq '.[] | .baseRefName, (select(.isCrossRepository | not) | .headRefName)' | sort -u > "$RUNNER_TEMP/open-pr-branches"
- refspecs=()
- while read -r branch sha; do
- case "$branch" in
- HEAD|main|backup/*) continue ;;
- esac
- if grep -Fxq -- "$branch" "$RUNNER_TEMP/open-pr-branches"; then
- echo "Keeping $branch: it belongs to an open pull request"; continue
- fi
- git merge-base --is-ancestor "$sha" origin/main || continue
- if grep -Fxq "$sha" "$RUNNER_TEMP/main-line"; then
- echo "Keeping $branch: it points at a commit of main itself (new branch or fast forward)"; continue
- fi
- echo "Deleting $branch (${sha:0:9}, merged into main)"
- # the lease makes the delete fail if someone pushed to the branch since the fetch
- refspecs+=("--force-with-lease=refs/heads/$branch:$sha" ":refs/heads/$branch")
- done < <(git for-each-ref --format='%(refname:lstrip=3) %(objectname)' refs/remotes/origin)
- if [ ${#refspecs[@]} = 0 ]; then
- echo "Nothing to delete"; exit 0
- fi
- # one push per 50 branches (100 arguments) keeps the command line short; a rejected branch (lease or rule) does not stop the rest
- failed=0
- for ((i = 0; i < ${#refspecs[@]}; i += 100)); do
- git push origin "${refspecs[@]:i:100}" || failed=1
- done
- exit $failed
|