Skip to content

Commit dc8b6c7

Browse files
committed
CI: Add format check
Add --check flag to scripts/format.sh rather than calling clang-format directly in ci.yml, so the set of matched files stays consistent between local formatting and CI verification. Replace rg with find in the script to avoid requiring ripgrep in CI. Rename workflow from "build" to "ci" to match the filename and reflect that it now covers more than building. The format job runs in a debian:trixie container to pin clang-format 19. The script checks the version at startup so local dev catches mismatches early.
1 parent e795741 commit dc8b6c7

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

.github/workflows/ci.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: build
1+
name: ci
22

33
on:
44
pull_request:
@@ -11,6 +11,18 @@ permissions:
1111
checks: write
1212

1313
jobs:
14+
format:
15+
runs-on: ubuntu-latest
16+
container: debian:trixie
17+
steps:
18+
- name: Install tools
19+
run: apt-get update && apt-get install -y --no-install-recommends git clang-format
20+
21+
- uses: actions/checkout@v4
22+
23+
- name: Check formatting
24+
run: bash scripts/format.sh --check
25+
1426
build:
1527
strategy:
1628
fail-fast: false

scripts/format.sh

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
FILES=$(rg --files -g '*.{h,hpp,cc,cpp,cxx}' include src tests tools)
4+
REQUIRED_CF_VERSION=19
5+
6+
CF_VERSION=$(clang-format --version | sed -n 's/.*version \([0-9]*\)\..*/\1/p' | head -1)
7+
if [[ -z "${CF_VERSION}" ]]; then
8+
echo "error: could not determine clang-format version" >&2
9+
exit 1
10+
fi
11+
if [[ "${CF_VERSION}" -ne "${REQUIRED_CF_VERSION}" ]]; then
12+
echo "error: clang-format ${REQUIRED_CF_VERSION} required, found ${CF_VERSION}" >&2
13+
echo " apt install clang-format-${REQUIRED_CF_VERSION} (or see https://apt.llvm.org)" >&2
14+
exit 1
15+
fi
16+
17+
FILES=$(find include src tests tools -name '*.h' -o -name '*.hpp' -o -name '*.cc' -o -name '*.cpp' -o -name '*.cxx')
518

619
if [[ -z "${FILES}" ]]; then
720
echo "No source files found."
821
exit 0
922
fi
1023

11-
echo "Formatting files..."
12-
clang-format -i ${FILES}
24+
if [[ "${1:-}" == "--check" ]]; then
25+
echo "Checking formatting..."
26+
clang-format --dry-run -Werror ${FILES}
27+
else
28+
echo "Formatting files..."
29+
clang-format -i ${FILES}
30+
fi

0 commit comments

Comments
 (0)