Skip to content

Commit f2f71a7

Browse files
authored
Merge pull request #29 from wambitz/feature/28/fix-precommit-harmonize-ci-checks
feat: enhance CI/CD pipeline with pre-commit hooks and linting checks
2 parents b2da0b5 + b84c250 commit f2f71a7

5 files changed

Lines changed: 39 additions & 13 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
}
3636
},
3737

38+
"postCreateCommand": "pre-commit install --hook-type pre-push",
3839
"postStartCommand": "bash",
39-
4040
// Clean container name and setup
4141
"runArgs": [
4242
"--rm",

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ jobs:
66
steps:
77
- uses: actions/checkout@v4
88
- name: Install deps
9-
run: sudo apt-get update && sudo apt-get install -y cmake clang-format
9+
run: sudo apt-get update && sudo apt-get install -y cmake clang-format clang-tidy
10+
- name: Format check
11+
run: ./scripts/format.sh --check
1012
- name: Build
1113
run: mkdir -p build && cd build && cmake .. && make
14+
- name: Lint check
15+
run: ./scripts/lint.sh
1216
- name: Test
1317
run: cd build && ctest --output-on-failure

.pre-commit-config.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
repos:
2-
- repo: https://github.com/pre-commit/mirrors-clang-format
3-
rev: v14.0.0
2+
- repo: local
43
hooks:
54
- id: clang-format
6-
args: ["--style=file"]
5+
name: clang-format
6+
entry: ./scripts/format.sh --check
7+
language: system
8+
stages: [pre-push]
9+
pass_filenames: false
710
types: [c, c++]
8-
9-
- repo: local
10-
hooks:
11+
1112
- id: clang-tidy
1213
name: clang-tidy
13-
entry: ./scripts/lint_precommit.sh
14+
description: "Requires build directory with compile_commands.json to exist. Please build the project before pushing."
15+
entry: ./scripts/lint.sh
1416
language: system
17+
stages: [pre-push]
18+
pass_filenames: false
1519
types: [c, c++]

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ A modern, production-ready template for C++ development.
1818

1919
```bash
2020
git clone <your-fork> my_project && cd my_project
21+
22+
# Install pre-commit hooks (for code quality checks on push)
23+
pre-commit install --hook-type pre-push
24+
2125
./scripts/fetch_googletest.sh # optional, only if you need tests
2226
cmake -S . -B build # -DENABLE_TESTS=OFF to skip tests
2327
cmake --build build -j$(nproc)

scripts/format.sh

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22

33
# --------------------------------------------------------------------
44
# Format all C++ source/header files in the project using clang-format
5+
# Usage:
6+
# ./format.sh - Format files in-place
7+
# ./format.sh --check - Check formatting without modifying files
58
# --------------------------------------------------------------------
69

7-
#!/usr/bin/env bash
810
set -e
911

1012
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1113
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
1214

13-
echo "[INFO] Running clang-format on source files..."
15+
# Check if --check flag is passed
16+
CHECK_MODE=false
17+
if [[ "$1" == "--check" ]]; then
18+
CHECK_MODE=true
19+
echo "[INFO] Running clang-format in check mode (no modifications)..."
20+
else
21+
echo "[INFO] Running clang-format on source files..."
22+
fi
1423

1524
EXTENSIONS=("*.cpp" "*.hpp" "*.cc" "*.h" "*.cxx" "*.hxx")
1625
FOUND=false
@@ -20,8 +29,13 @@ for ext in "${EXTENSIONS[@]}"; do
2029

2130
for f in $FILES; do
2231
FOUND=true
23-
echo "Formatting $f"
24-
clang-format -i "$f"
32+
if $CHECK_MODE; then
33+
echo "Checking $f"
34+
clang-format --dry-run --Werror "$f"
35+
else
36+
echo "Formatting $f"
37+
clang-format -i "$f"
38+
fi
2539
done
2640
done
2741

0 commit comments

Comments
 (0)