|
| 1 | +#!/usr/bin/env sh |
| 2 | +# Copyright lowRISC. |
| 3 | + |
| 4 | +# This script checks that commit messages are in the correct format: |
| 5 | +# |
| 6 | +# 1. The title must be of the form `[ot] path/to/change: description`. |
| 7 | +# 2. The message must end with `Signed-off-by: Name <email@address>`. |
| 8 | +# |
| 9 | +# It is intended to be run from CI, however you can run it manually with: |
| 10 | +# |
| 11 | +# GITHUB_BASE_REF="HEAD~5" lint-commits.sh |
| 12 | +# |
| 13 | +# Where `GITHUB_BASE_REF` is the first commit to start linting from. |
| 14 | + |
| 15 | +set -e |
| 16 | + |
| 17 | +# Check the commit title has the correct prefixes. |
| 18 | +lint_title() { |
| 19 | + commit="$1" |
| 20 | + |
| 21 | + title="$(git show "$commit" -s --format="format:%s")" |
| 22 | + short_hash="$(git show "$commit" -s --format="format:%h")" |
| 23 | + |
| 24 | + example="[ot] hw/opentitan: fix i2c register address" |
| 25 | + |
| 26 | + if ! echo "$title" | grep -P -q '^\[ot\]'; then |
| 27 | + echo "::error::${short_hash}: commit titles must have the prefix '[ot]'" >&2 |
| 28 | + echo "Example:" >&2 |
| 29 | + echo " ${example}" >&2 |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | + |
| 33 | + if ! echo "$title" | grep -P -q '^\[ot\]\s+[^:]+:'; then |
| 34 | + echo "::error::${short_hash}: commit titles must contain the path of the change" >&2 |
| 35 | + echo "Example:" >&2 |
| 36 | + echo " ${example}" >&2 |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | +# Check the commit is signed off. |
| 42 | +lint_signed_off_by() { |
| 43 | + commit="$1" |
| 44 | + |
| 45 | + signed_off_by="$(git show "$commit" -s --format="format:%(trailers:key=Signed-off-by)")" |
| 46 | + short_hash="$(git show "$commit" -s --format="format:%h")" |
| 47 | + |
| 48 | + if [ -z "$signed_off_by" ]; then |
| 49 | + echo "::error::${short_hash}: is missing a 'Signed-off-by' line" >&2 |
| 50 | + echo "Hint:" >&2 |
| 51 | + echo "Use \`git commit -s\` to add sign-offs" >&2 |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +if [ -z "$GITHUB_BASE_REF" ]; then |
| 57 | + echo "ERROR: This script is intended to be run in CI." >&2 |
| 58 | + echo "If you'd like to run it locally, set \`GITHUB_BASE_REF\` to the first commit to lint from." >&2 |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | + |
| 62 | +exit_code=0 |
| 63 | + |
| 64 | +# Lint each commit from the merge target to now. |
| 65 | +for commit in $(git log "${GITHUB_BASE_REF}..HEAD" --format="format:%H"); do |
| 66 | + lint_title "$commit" || exit_code=1 |
| 67 | + lint_signed_off_by "$commit" || exit_code=1 |
| 68 | +done |
| 69 | + |
| 70 | +exit $exit_code |
0 commit comments