Skip to content

Commit 0c40cda

Browse files
committed
[ot] scripts/opentitan: lint-commits: lint commits in CI
Signed-off-by: James Wainwright <james.wainwright@lowrisc.org>
1 parent 2ce9880 commit 0c40cda

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

.github/workflows/build_test.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ jobs:
121121
run: |
122122
scripts/opentitan/ot-tidy.sh --ci -p build-clang
123123
124+
lint-commits:
125+
runs-on: ubuntu-latest
126+
steps:
127+
- uses: actions/checkout@v4
128+
with:
129+
fetch-depth: 0 # Don't shallow clone, we need to see all commits.
130+
- name: Create local branch
131+
run: git branch "${{ github.base_ref }}" "origin/${{ github.base_ref }}"
132+
- name: Lint commits
133+
run: ./scripts/opentitan/lint-commits.sh
134+
124135
test-clang:
125136
runs-on: ubuntu-24.04
126137
needs: build-clang

scripts/opentitan/lint-commits.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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() {
25+
echo "Got:" >&2
26+
echo " ${title}" >&2
27+
echo
28+
echo "Example:" >&2
29+
echo " [ot] hw/opentitan: ot_hmac: fix i2c register address" >&2
30+
}
31+
32+
if ! echo "$title" | grep -P -q '^\[ot\]'; then
33+
echo "::error::${short_hash}: commit titles must have the prefix '[ot]'" >&2
34+
example
35+
exit 1
36+
fi
37+
38+
if ! echo "$title" | grep -P -q '^\[ot\]\s+[^:]+:'; then
39+
echo "::error::${short_hash}: commit titles must contain the path of the change" >&2
40+
example
41+
exit 1
42+
fi
43+
44+
if ! echo "$title" | grep -P -q '^\[ot\]\s+[^:]+:\s+[^:]+:'; then
45+
echo "::error::${short_hash}: commit titles must state the changed component" >&2
46+
example
47+
exit 1
48+
fi
49+
}
50+
51+
# Check the commit is signed off.
52+
lint_signed_off_by() {
53+
commit="$1"
54+
55+
signed_off_by="$(git show "$commit" -s --format="format:%(trailers:key=Signed-off-by)")"
56+
short_hash="$(git show "$commit" -s --format="format:%h")"
57+
58+
if [ -z "$signed_off_by" ]; then
59+
echo "::error::${short_hash}: is missing a 'Signed-off-by' line" >&2
60+
echo "Hint:" >&2
61+
echo "Use \`git commit -s\` to add sign-offs" >&2
62+
exit 1
63+
fi
64+
}
65+
66+
if [ -z "$GITHUB_BASE_REF" ]; then
67+
echo "ERROR: This script is intended to be run in CI." >&2
68+
echo "If you'd like to run it locally, set \`GITHUB_BASE_REF\` to the first commit to lint from." >&2
69+
exit 1
70+
fi
71+
72+
exit_code=0
73+
74+
# Lint each commit from the merge target to now.
75+
for commit in $(git log "${GITHUB_BASE_REF}..HEAD" --format="format:%H" --no-merges); do
76+
lint_title "$commit" || exit_code=1
77+
lint_signed_off_by "$commit" || exit_code=1
78+
done
79+
80+
exit $exit_code

0 commit comments

Comments
 (0)