diff --git a/.github/workflows/git.yml b/.github/workflows/git.yml index 855d0a7e..cceb5dfe 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