Skip to content

Commit 392aff9

Browse files
feat: pre commit (#134)
1 parent f7aecef commit 392aff9

4 files changed

Lines changed: 121 additions & 8 deletions

File tree

.github/workflows/lint.yml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,28 @@ jobs:
1010
lint:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- name: Checkout Code
14-
uses: actions/checkout@v3
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
16+
- name: Install dependencies
17+
run: sudo apt-get install -y clang-format cmake ccache
18+
19+
- name: Restore ccache
20+
uses: actions/cache@v4
1521
with:
16-
fetch-depth: 0
22+
path: ~/.cache/ccache
23+
key: ccache-${{ runner.os }}-${{ hashFiles('cpl/inc/**', 'tests/**/*.cpp', 'CMakeLists.txt', 'cpl/CMakeLists.txt', 'tests/CMakeLists.txt') }}
24+
restore-keys: ccache-${{ runner.os }}-
25+
26+
- name: Configure ccache
27+
run: ccache --set-config max_size=500M
1728

18-
- name: Check Formatting
19-
run: |
20-
find cpl/inc cpl/src tests/cpl -regex '.*\.\(cpp\|h\|hpp\)' -exec clang-format -i --style=file {} +
21-
git diff --exit-code || (echo 'Formatting issues found. Please run clang-format.' && exit 1)
29+
- name: Run lint
30+
run: scripts/lint.sh
31+
32+
- name: Save ccache
33+
uses: actions/cache/save@v4
34+
if: always()
35+
with:
36+
path: ~/.cache/ccache
37+
key: ccache-${{ runner.os }}-${{ hashFiles('cpl/inc/**', 'tests/**/*.cpp', 'CMakeLists.txt', 'cpl/CMakeLists.txt', 'tests/CMakeLists.txt') }}

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ CMAKE_FLAGS = \
1919
-DCMAKE_EXPORT_COMPILE_COMMANDS=$(COMPILE_COMMANDS)
2020

2121
.PHONY: build test bench tools \
22-
format clean distclean help
22+
format install-hooks clean distclean help
2323

2424
configure:
2525
@mkdir -p "$(BUILD_DIR)"
@@ -53,6 +53,10 @@ tools: build
5353
format:
5454
@clang-format -i -style=file $(shell git ls-files '*.hpp' '*.h' '*.cpp')
5555

56+
install-hooks:
57+
@ln -sf ../../scripts/pre-commit .git/hooks/pre-commit
58+
@echo "Pre-commit hook installed"
59+
5660
clean:
5761
@cmake --build "$(BUILD_DIR)" --target clean || true
5862

@@ -72,6 +76,7 @@ help:
7276
@echo " bench-out - output the most recent benchmark run"
7377
@echo " tools - build project-defined tools (BUILD_TOOLS=ON)"
7478
@echo " format - run clang-format on all source files"
79+
@echo " install-hooks - install pre-commit hook (calls scripts/lint.sh)"
7580
@echo " clean - clean build artifacts"
7681
@echo " distclean - clean entire build dir"
7782
@echo ""

scripts/lint.sh

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright (c) Brandon Pacewic
4+
# SPDX-License-Identifier: MIT
5+
6+
set -e
7+
8+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
9+
ROOT="$DIR/../"
10+
cd $ROOT
11+
12+
RED='\033[0;31m'
13+
GREEN='\033[0;32m'
14+
NC='\033[0m'
15+
16+
FAILED=0
17+
FAST=0
18+
19+
while [[ $# -gt 0 ]]; do
20+
case $1 in
21+
--fast)
22+
FAST=1
23+
shift
24+
;;
25+
*)
26+
echo "Unknown option: $1"
27+
echo "Usage: $0 [--fast]"
28+
echo " --fast skip build and tests (format + file checks only)"
29+
exit 1
30+
;;
31+
esac
32+
done
33+
34+
function run_check() {
35+
local name="$1"
36+
local cmd="$2"
37+
38+
local name_len=${#name}
39+
local dots_len=$((46 - name_len))
40+
local dots=$(printf '%*s' "$dots_len" | tr ' ' '.')
41+
42+
printf "%s%s" "$name" "$dots"
43+
44+
set +e
45+
log=$(eval "$cmd" 2>&1)
46+
result=$?
47+
set -e
48+
49+
if [[ $result -eq 0 ]]; then
50+
echo -e "[${GREEN}ok${NC}]"
51+
else
52+
echo -e "[${RED}FAIL${NC}]"
53+
echo "$log"
54+
FAILED=1
55+
fi
56+
}
57+
58+
function check_large_files() {
59+
local limit=$((250 * 1024))
60+
local found=0
61+
while IFS= read -r file; do
62+
local size
63+
size=$(wc -c < "$file")
64+
if [[ "$size" -gt "$limit" ]]; then
65+
echo "Large file ($(( size / 1024 ))KB): $file"
66+
found=1
67+
fi
68+
done < <(git ls-files)
69+
return "$found"
70+
}
71+
72+
run_check "check_default_branch" "[[ \$(git rev-parse --abbrev-ref HEAD) != mega ]]"
73+
run_check "clang_format" "git ls-files '*.cpp' '*.h' '*.hpp' | xargs clang-format --dry-run --Werror"
74+
run_check "check_large_files" "check_large_files"
75+
run_check "check_merge_conflicts" "! git ls-files | xargs grep -lP '^(<{7}|={7}|>{7})' 2>/dev/null | grep -q ."
76+
77+
if [[ "$FAST" -eq 0 ]]; then
78+
run_check "build" \
79+
"cmake -B build -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Debug && cmake --build build -j\$(nproc)"
80+
run_check "tests" "ctest --test-dir build --output-on-failure"
81+
fi
82+
83+
echo ""
84+
if [[ $FAILED -eq 1 ]]; then
85+
echo -e "${RED}Lint failed${NC}"
86+
exit 1
87+
else
88+
echo -e "${GREEN}All checks passed${NC}"
89+
fi

scripts/pre-commit

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
scripts/lint.sh

0 commit comments

Comments
 (0)