diff --git a/.githooks/commit-msg b/.githooks/commit-msg new file mode 100755 index 0000000..f4d8f72 --- /dev/null +++ b/.githooks/commit-msg @@ -0,0 +1,4 @@ +#!/bin/sh + +root=$(git rev-parse --show-toplevel) || exit 1 +exec "$root/.githooks/validate-commit-message" "$1" diff --git a/.githooks/validate-commit-message b/.githooks/validate-commit-message new file mode 100755 index 0000000..c823f3c --- /dev/null +++ b/.githooks/validate-commit-message @@ -0,0 +1,26 @@ +#!/bin/sh + +awk ' +BEGIN { + allowed = "co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>" +} +/^[[:space:]]*[Cc][Oo]-[Aa][Uu][Tt][Hh][Oo][Rr][Ee][Dd]-[Bb][Yy][[:space:]]*:/ { + line = tolower($0) + sub(/\r$/, "", line) + sub(/^[[:space:]]+/, "", line) + if (line != allowed) { + exit 42 + } +} +' "$1" +status=$? + +if [ "$status" -eq 42 ]; then + echo "Commit rejected: only Dependabot may be a co-author." >&2 + exit 1 +fi + +if [ "$status" -ne 0 ]; then + echo "Commit rejected: could not validate co-author trailers." >&2 + exit 1 +fi diff --git a/.github/workflows/commit-policy.yml b/.github/workflows/commit-policy.yml new file mode 100644 index 0000000..2da7120 --- /dev/null +++ b/.github/workflows/commit-policy.yml @@ -0,0 +1,33 @@ +name: Commit policy + +on: + pull_request: + merge_group: + +permissions: + contents: read + +jobs: + commit-messages: + name: Commit messages + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - name: Check out repository + uses: actions/checkout@v7.0.1 + with: + fetch-depth: 0 + + - name: Validate co-author trailers + env: + BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }} + HEAD_SHA: ${{ github.event.pull_request.head.sha || github.event.merge_group.head_sha }} + run: | + message_file=$(mktemp) + trap 'rm -f "$message_file"' EXIT + + while IFS= read -r commit; do + git log -1 --format=%B "$commit" > "$message_file" + .githooks/validate-commit-message "$message_file" + done < <(git rev-list "$BASE_SHA..$HEAD_SHA") diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f7f37fc..217ff71 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -57,3 +57,13 @@ Follow the [release checklist](./docs/releasing.md). A `chore(release): prepare @askrjs/cli X.Y.Z` change must move the Unreleased entries into a dated version section; the normal `npm run check` gate verifies that the package version exists in the changelog. + +## Git hooks + +Enable the repository hooks after cloning: + +```shell +git config core.hooksPath .githooks +``` + +Commit messages may not contain `Co-authored-by:` trailers. The canonical Dependabot co-author trailer is the only exception. Pull requests run the same validation on GitHub, so the policy applies even when a contributor has not enabled local hooks.