Skip to content

Commit b3b3233

Browse files
committed
Limit clang-format CI to changed files
1 parent cb4a446 commit b3b3233

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

.github/workflows/ci.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- uses: actions/checkout@v4
15-
- uses: DoozyX/clang-format-lint-action@v0.15
1615
with:
17-
source: './app ./include ./src ./test'
16+
fetch-depth: 0
17+
- name: Install clang-format
18+
run: sudo apt-get update && sudo apt-get install -y clang-format-18
19+
- name: Run clang-format
20+
env:
21+
BASE_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }}
22+
HEAD_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
23+
run: ./scripts/ci/clang-format-check.sh "${BASE_SHA}" "${HEAD_SHA}"
1824
build-linux:
1925
strategy:
2026
matrix:

scripts/ci/clang-format-check.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
base_ref="${1:-}"
5+
head_ref="${2:-HEAD}"
6+
7+
if [[ -z "${base_ref}" || "${base_ref}" =~ ^0+$ ]]; then
8+
base_ref="$(git rev-parse "${head_ref}^")"
9+
fi
10+
11+
mapfile -t files < <(
12+
git diff --name-only --diff-filter=ACMR "${base_ref}" "${head_ref}" -- app include src test \
13+
| grep -E '\.(c|h|C|H|cpp|hpp|cc|hh|c\+\+|h\+\+|cxx|hxx)$' \
14+
|| true
15+
)
16+
17+
if [[ "${#files[@]}" -eq 0 ]]; then
18+
echo "No source files changed."
19+
exit 0
20+
fi
21+
22+
clang_format_bin="${CLANG_FORMAT_BIN:-clang-format-18}"
23+
24+
"${clang_format_bin}" --dry-run -Werror "${files[@]}"

0 commit comments

Comments
 (0)