Skip to content

Commit ef5662a

Browse files
authored
SWDEV-123456 - add clang-format hook and github action (#126)
1 parent d0cf32a commit ef5662a

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
RANGE=""
6+
7+
while [[ $# -gt 0 ]]; do
8+
echo $1
9+
echo $2
10+
case "$1" in
11+
--range)
12+
RANGE="$2"
13+
shift 2
14+
;;
15+
*)
16+
echo "Unknown arg $1" >&2
17+
exit 64
18+
;;
19+
esac
20+
done
21+
22+
regex='\.(c|cc|cpp|cxx|h|hh|hpp|hxx)$'
23+
24+
if [[ -n $RANGE ]]; then
25+
files=$(git diff --name-only "$RANGE" | grep -E "$regex" || true)
26+
else
27+
files=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "$regex" || true)
28+
fi
29+
echo "Checking $files"
30+
[[ -z $files ]] && exit 0
31+
32+
clang_bin="${CLANG_FORMAT:-clang-format}"
33+
if ! command -v "$clang_bin" >/dev/null 2>&1; then
34+
if [[ -x "/c/Program Files/LLVM/bin/clang-format.exe" ]]; then
35+
clang_bin="/c/Program Files/LLVM/bin/clang-format.exe"
36+
fi
37+
fi
38+
39+
"$clang_bin" -style=file --dry-run -fallback-style=none -n -Werror $files

.github/hooks/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
exec "$(git rev-parse --show-toplevel)/.github/hooks/clang-format-check.sh"

.github/workflows/clang-format.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Clang format check
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
format:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
with:
12+
fetch-depth: 0
13+
14+
- name: Install clang-format
15+
run: |
16+
sudo apt update && sudo apt install -y clang-format
17+
18+
- name: Run clang-format-check
19+
id: clang-format
20+
run: |
21+
chmod +x .github/hooks/clang-format-check.sh
22+
./.github/hooks/clang-format-check.sh --range "${{ github.event.before }}..${{ github.sha }}"

0 commit comments

Comments
 (0)