From cc4e6509c5f46c5cfffd3c6716a6fad1bd30ae6e Mon Sep 17 00:00:00 2001 From: Adam Kudrna Date: Mon, 30 Nov 2026 19:32:49 +0100 Subject: [PATCH] Block `amend!` commits without a third-party action Replace `xt0rted/block-autosquash-commits-action` with a `git log` check, which blocks `amend!` commits besides `fixup!` and `squash!`. The job keeps its name because branch protection requires it. --- .github/workflows/git.yml | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/.github/workflows/git.yml b/.github/workflows/git.yml index 855d0a7ea..cceb5dfee 100644 --- a/.github/workflows/git.yml +++ b/.github/workflows/git.yml @@ -2,13 +2,37 @@ name: Git Checks on: [ pull_request ] +permissions: + contents: read + jobs: + # Fails while the pull request has commits that git rebase --autosquash would fold: fixup!, + # squash! and amend!. block-merge-with-autosquash-commits: name: Block merge with autosquash commits runs-on: ubuntu-24.04 + timeout-minutes: 5 steps: - - name: Block merge with autosquash commits - uses: xt0rted/block-autosquash-commits-action@v2 + - name: Clone repository + uses: actions/checkout@v6 with: - repo-token: ${{ secrets.GITHUB_TOKEN }} + fetch-depth: 0 + persist-credentials: false + + - name: Block merge with autosquash commits + shell: bash + env: + BASE: ${{ github.event.pull_request.base.sha }} + HEAD: ${{ github.event.pull_request.head.sha }} + run: | + # git log runs on its own, so its failure fails the job instead of passing as no match. + log=$(git log --format='%h %s' "$BASE..$HEAD") + commits=$(grep -E '^[0-9a-f]+ (fixup|squash|amend)! ' <<<"$log") || exit 0 + echo "::error::Squash these commits before merge: git rebase -i --autosquash origin/${GITHUB_BASE_REF}" + # Subjects are untrusted: stop them from being read as workflow commands. + token=$(openssl rand -hex 16) + echo "::stop-commands::$token" + printf '%s\n' "$commits" + echo "::$token::" + exit 1