Skip to content

Commit 4ae7a86

Browse files
committed
CI: Re-enable clang-format-check workflow
1 parent 9c017f4 commit 4ae7a86

4 files changed

Lines changed: 90 additions & 41 deletions

File tree

.githooks/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
exec "$(git rev-parse --show-toplevel)/.github/scripts/clang-format-check.sh"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/sh
2+
#
3+
# Rejects changed lines that are not clang-format clean. Shared by the
4+
# pre-commit hook and by CI so both judge identically.
5+
#
6+
# clang-format-check.sh staged changes (pre-commit)
7+
# clang-format-check.sh <base-ref> changes since <base-ref> (CI)
8+
#
9+
# Only the changed lines are checked, so this does not demand a reformat
10+
# of surrounding code that predates the style file.
11+
#
12+
# git-clang-format spawns a separate clang-format, defaulting to the
13+
# unversioned one on PATH, so the wrapper and the binary are both pinned
14+
# here. A mismatched pair fails outright.
15+
#
16+
# Bypass:
17+
# git commit -n skips every hook
18+
# SKIP_CLANG_FORMAT=1 git ... skips just this check
19+
#
20+
21+
[ -n "$SKIP_CLANG_FORMAT" ] && exit 0
22+
23+
ver=${CLANG_FORMAT_VERSION:-18}
24+
bin="clang-format-$ver"
25+
wrapper="git-clang-format-$ver"
26+
27+
top=$(git rev-parse --show-toplevel 2>/dev/null) || exit 0
28+
[ -f "$top/.clang-format" ] || exit 0
29+
30+
# Skipping keeps a missing formatter from blocking commits. CI asserts the
31+
# binaries are present in a separate step, so it cannot skip silently.
32+
if ! command -v "$wrapper" >/dev/null 2>&1; then
33+
echo "clang-format: $wrapper not found, skipping" >&2
34+
exit 0
35+
fi
36+
37+
# git-clang-format needs a commit to diff against, so the very first
38+
# commit in a repo cannot be checked.
39+
git rev-parse --verify --quiet HEAD >/dev/null || exit 0
40+
41+
if [ -n "$1" ]; then
42+
scope=$1
43+
else
44+
scope=--staged
45+
fi
46+
47+
out=$("$wrapper" --binary "$bin" --diff -q "$scope" 2>&1)
48+
ret=$?
49+
50+
if [ $ret -eq 0 ]; then
51+
exit 0
52+
fi
53+
54+
if [ $ret -ne 1 ]; then
55+
echo "$out" >&2
56+
echo "clang-format: $wrapper failed (exit $ret)" >&2
57+
exit $ret
58+
fi
59+
60+
echo "$out"
61+
echo ""
62+
echo "ERROR: changed lines are not clang-format clean."
63+
echo ""
64+
# Not "&& git add -u": the wrapper exits 1 when it reformats something.
65+
echo "Fix with:"
66+
echo " $wrapper --binary $bin $scope; git add -u"
67+
echo ""
68+
echo "Or bypass with:"
69+
echo " SKIP_CLANG_FORMAT=1 git commit ..."
70+
exit 1

.github/workflows/clang-format-check.yml

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ permissions:
1313

1414
jobs:
1515
clang-format-check:
16-
name: Check PR formatting with clang-format-15
16+
name: Check PR formatting with clang-format-18
1717
runs-on: ubuntu-latest
1818
timeout-minutes: 5
1919

@@ -23,9 +23,15 @@ jobs:
2323
with:
2424
fetch-depth: 0 # Need full history to compare with main branch
2525

26-
- name: Install clang-format-15
26+
- name: Verify clang-format-18
2727
run: |
28-
sudo apt-get install -y clang-format-15
28+
# 18 is preinstalled on ubuntu-latest. use it to avoid an update+install
29+
command -v clang-format-18 >/dev/null || {
30+
echo "clang-format-18 is missing from the runner image"; exit 1; }
31+
command -v git-clang-format-18 >/dev/null || {
32+
echo "git-clang-format-18 is missing from the runner image"; exit 1; }
33+
clang-format-18 --version
34+
clang-format --version || true
2935
3036
- name: Fetch base branch
3137
env:
@@ -35,44 +41,8 @@ jobs:
3541
3642
- name: Check formatting
3743
env:
38-
BASE_SHA: ${{ github.event.pull_request.base.sha }}
3944
BASE_REF: ${{ github.event.pull_request.base.ref }}
4045
run: |
41-
echo "Running git-clang-format-15 to check for formatting issues..."
4246
echo "Comparing against base branch: $BASE_REF"
43-
44-
# Create a temporary file for the diff and ensure cleanup
45-
DIFF_FILE="$(mktemp)"
46-
trap 'rm -f "$DIFF_FILE"' EXIT
47-
48-
# Run git-clang-format against the PR base commit and capture status safely under set -e
49-
if git-clang-format-15 "$BASE_REF" > "$DIFF_FILE"; then
50-
status=0
51-
else
52-
status=$?
53-
fi
54-
55-
if [ "$status" -eq 0 ]; then
56-
echo "✅ Code is properly formatted!"
57-
exit 0
58-
elif [ "$status" -eq 1 ]; then
59-
echo "❌ Code formatting issues detected!"
60-
echo ""
61-
echo "The following changes would be made by clang-format-15:"
62-
echo "=================================================="
63-
cat "$DIFF_FILE"
64-
echo "=================================================="
65-
echo ""
66-
echo "Please run the following command locally on your feature branch and commit the changes:"
67-
echo " git-clang-format-15 $BASE_REF"
68-
exit 0
69-
# TEMPORARY DISABLE DUE TO BUGS
70-
#exit 1
71-
else
72-
echo "❌ git-clang-format-15 failed with exit code $status"
73-
echo "Output (if any):"
74-
cat "$DIFF_FILE"
75-
exit 0
76-
# TEMPORARY DISABLE DUE TO BUGS
77-
#exit 1
78-
fi
47+
# Same script the pre-commit hook runs, so local and CI agree.
48+
.github/scripts/clang-format-check.sh "$BASE_REF"

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ PKCS11 and AUTOSAR SHE.
2020
For a technical overview of wolfHSM and instructions on using wolfHSM in your application,
2121
please refer to the following resources.
2222

23+
## Formatting
24+
25+
Enable the pre-commit clang-format check, once per clone:
26+
27+
sudo apt-get install -y clang-format-18
28+
git config core.hooksPath .githooks
29+
2330
## Resources
2431

2532
- [wolfHSM Manual](https://www.wolfssl.com/documentation/manuals/wolfhsm/index.html)

0 commit comments

Comments
 (0)