Skip to content

Commit 09e7f7c

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

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

.github/workflows/build_test.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ 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: Lint commits
131+
run: ./scripts/opentitan/lint-commits.sh
132+
124133
test-clang:
125134
runs-on: ubuntu-24.04
126135
needs: build-clang

scripts/opentitan/lint-commits.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)