Skip to content

Commit 0ce8da5

Browse files
authored
Merge pull request #40 from wermos/precommit
Added `pre-commit` Action and `pre-commit` formatting fixes. Also run clang-format on source.
2 parents bbefdea + d55e0e6 commit 0ce8da5

27 files changed

+605
-249
lines changed

.codespellignore

Whitespace-only changes.

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,3 @@ jobs:
9797
set -x
9898
[[ ! -z "${{ matrix.config.asan_options }}" ]] && export ASAN_OPTIONS="${{ matrix.config.asan_options }}"
9999
ctest --build-config Asan --output-on-failure --test-dir .build
100-

.github/workflows/pre-commit.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Lint Check (pre-commit)
2+
3+
on:
4+
# We have to use pull_request_target here as pull_request does not grant
5+
# enough permission for reviewdog
6+
pull_request_target:
7+
push:
8+
9+
jobs:
10+
pre-commit-push:
11+
name: Pre-Commit check on Push
12+
runs-on: ubuntu-latest
13+
if: ${{ github.event_name == 'push' }}
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: 3.13
23+
24+
# We wish to run pre-commit on all files instead of the changes
25+
# only made in the push commit.
26+
#
27+
# So linting error persists when there's formatting problem.
28+
- uses: pre-commit/[email protected]
29+
30+
pre-commit-pr:
31+
name: Pre-Commit check on PR
32+
runs-on: ubuntu-latest
33+
if: ${{ github.event_name == 'pull_request_target' }}
34+
35+
permissions:
36+
contents: read
37+
checks: write
38+
issues: write
39+
pull-requests: write
40+
41+
steps:
42+
- name: Checkout repository
43+
uses: actions/checkout@v4
44+
45+
# pull_request_target checkout the base of the repo
46+
# We need to checkout the actual pr to lint the changes.
47+
- name: Checkout pr
48+
run: gh pr checkout ${{ github.event.number }}
49+
env:
50+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
51+
52+
- name: Set up Python
53+
uses: actions/setup-python@v5
54+
with:
55+
python-version: 3.13
56+
57+
# we only lint on the changed file in PR.
58+
- name: Get Changed Files
59+
id: changed-files
60+
uses: tj-actions/changed-files@v45
61+
62+
# See:
63+
# https://github.com/tj-actions/changed-files?tab=readme-ov-file#using-local-git-directory-
64+
- uses: pre-commit/[email protected]
65+
id: run-pre-commit
66+
with:
67+
extra_args: --files ${{ steps.changed-files.outputs.all_changed_files }}
68+
69+
# Review dog posts the suggested change from pre-commit to the pr.
70+
- name: suggester / pre-commit
71+
uses: reviewdog/action-suggester@v1
72+
if: ${{ failure() && steps.run-pre-commit.conclusion == 'failure' }}
73+
with:
74+
tool_name: pre-commit
75+
level: warning
76+
reviewdog_flags: "-fail-level=error"

.gitmodules

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

.pre-commit-config.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v5.0.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-yaml
10+
- id: check-added-large-files
11+
12+
# Clang-format for C++
13+
# This brings in a portable version of clang-format.
14+
# See also: https://github.com/ssciwr/clang-format-wheel
15+
- repo: https://github.com/pre-commit/mirrors-clang-format
16+
rev: v19.1.4
17+
hooks:
18+
- id: clang-format
19+
types_or: [c++, c]
20+
21+
# CMake linting and formatting
22+
- repo: https://github.com/BlankSpruce/gersemi
23+
rev: 0.17.1
24+
hooks:
25+
- id: gersemi
26+
name: CMake linting
27+
28+
# Markdown linting
29+
# Config file: .markdownlint.yaml
30+
- repo: https://github.com/igorshubovych/markdownlint-cli
31+
rev: v0.43.0
32+
hooks:
33+
- id: markdownlint
34+
35+
- repo: https://github.com/codespell-project/codespell
36+
rev: v2.3.0
37+
hooks:
38+
- id: codespell
39+
files: ^.*\.(cmake|cpp|hpp|txt|md|json|in|yaml|yml)$
40+
args: ["--ignore-words", ".codespellignore" ]

CMakeLists.txt

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
cmake_minimum_required(VERSION 3.27...3.31)
77

8-
project(
9-
beman.iterator_interface
10-
VERSION 0.0.0
11-
LANGUAGES CXX)
8+
project(beman.iterator_interface VERSION 0.0.0 LANGUAGES CXX)
129

1310
# Local helpers: required to include CompilerFeatureTest.
1411
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
@@ -24,76 +21,83 @@ beman_iterator_check_deducing_this(COMPILER_SUPPORTS_DEDUCING_THIS)
2421
set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets)
2522

2623
option(
27-
BEMAN_ITERATOR_INTERFACE_USE_DEDUCING_THIS
28-
"Make use of C++23 \"deducing this\" feature (P0847R7). Turn this off for non-conforming compilers."
29-
${COMPILER_SUPPORTS_DEDUCING_THIS})
24+
BEMAN_ITERATOR_INTERFACE_USE_DEDUCING_THIS
25+
"Make use of C++23 \"deducing this\" feature (P0847R7). Turn this off for non-conforming compilers."
26+
${COMPILER_SUPPORTS_DEDUCING_THIS}
27+
)
3028

3129
option(
32-
BEMAN_ITERATOR_INTERFACE_BUILD_TESTS
33-
"Enable building tests and test infrastructure. Default: ON. Values: {ON, OFF}."
34-
${PROJECT_IS_TOP_LEVEL})
35-
36-
option(BEMAN_ITERATOR_INTERFACE_BUILD_EXAMPLES
37-
"Enable building examples. Default: ON. Values: {ON, OFF}."
38-
${PROJECT_IS_TOP_LEVEL})
39-
40-
if(BEMAN_ITERATOR_INTERFACE_USE_DEDUCING_THIS
41-
AND NOT COMPILER_SUPPORTS_DEDUCING_THIS)
42-
message(
43-
WARNING
44-
"Building with C++23 \"deducing this\" feature (P0847R7) despite of the compiler's lack of actual support for it."
45-
)
30+
BEMAN_ITERATOR_INTERFACE_BUILD_TESTS
31+
"Enable building tests and test infrastructure. Default: ON. Values: {ON, OFF}."
32+
${PROJECT_IS_TOP_LEVEL}
33+
)
34+
35+
option(
36+
BEMAN_ITERATOR_INTERFACE_BUILD_EXAMPLES
37+
"Enable building examples. Default: ON. Values: {ON, OFF}."
38+
${PROJECT_IS_TOP_LEVEL}
39+
)
40+
41+
if(
42+
BEMAN_ITERATOR_INTERFACE_USE_DEDUCING_THIS
43+
AND NOT COMPILER_SUPPORTS_DEDUCING_THIS
44+
)
45+
message(
46+
WARNING
47+
"Building with C++23 \"deducing this\" feature (P0847R7) despite of the compiler's lack of actual support for it."
48+
)
4649
endif()
4750

4851
configure_file(
49-
"${PROJECT_SOURCE_DIR}/include/beman/iterator_interface/config.hpp.in"
50-
"${PROJECT_BINARY_DIR}/include/beman/iterator_interface/config.hpp" @ONLY)
52+
"${PROJECT_SOURCE_DIR}/include/beman/iterator_interface/config.hpp.in"
53+
"${PROJECT_BINARY_DIR}/include/beman/iterator_interface/config.hpp"
54+
@ONLY
55+
)
5156

5257
if(BEMAN_ITERATOR_INTERFACE_BUILD_TESTS)
53-
# Fetch GoogleTest
54-
FetchContent_Declare(
55-
googletest
56-
EXCLUDE_FROM_ALL
57-
GIT_REPOSITORY https://github.com/google/googletest.git
58-
GIT_TAG e39786088138f2749d64e9e90e0f9902daa77c40 # release-1.15.0
59-
)
60-
FetchContent_MakeAvailable(googletest)
58+
# Fetch GoogleTest
59+
FetchContent_Declare(
60+
googletest
61+
EXCLUDE_FROM_ALL
62+
GIT_REPOSITORY https://github.com/google/googletest.git
63+
GIT_TAG
64+
e39786088138f2749d64e9e90e0f9902daa77c40 # release-1.15.0
65+
)
66+
FetchContent_MakeAvailable(googletest)
6167
endif()
6268

6369
# Create the library target and named header set for beman.iterator_interface
6470
add_library(beman.iterator_interface STATIC)
6571
add_library(beman::iterator_interface ALIAS beman.iterator_interface)
6672

6773
target_sources(
68-
beman.iterator_interface
69-
PUBLIC FILE_SET
70-
beman_iterator_interface_headers
71-
TYPE
72-
HEADERS
73-
BASE_DIRS
74-
${PROJECT_BINARY_DIR}/include
75-
${PROJECT_SOURCE_DIR}/include
76-
FILES
77-
${PROJECT_BINARY_DIR}/include/beman/iterator_interface/config.hpp)
74+
beman.iterator_interface
75+
PUBLIC
76+
FILE_SET beman_iterator_interface_headers
77+
TYPE HEADERS
78+
BASE_DIRS ${PROJECT_BINARY_DIR}/include ${PROJECT_SOURCE_DIR}/include
79+
FILES ${PROJECT_BINARY_DIR}/include/beman/iterator_interface/config.hpp
80+
)
7881

7982
add_subdirectory(src/beman/iterator_interface)
8083
add_subdirectory(include/beman/iterator_interface)
8184

8285
if(BEMAN_ITERATOR_INTERFACE_BUILD_TESTS)
83-
enable_testing()
84-
add_subdirectory(tests/beman/iterator_interface)
86+
enable_testing()
87+
add_subdirectory(tests/beman/iterator_interface)
8588
endif()
8689

8790
if(BEMAN_ITERATOR_INTERFACE_BUILD_EXAMPLES)
88-
add_subdirectory(examples)
91+
add_subdirectory(examples)
8992
endif()
9093

9194
# Coverage
9295
configure_file("cmake/gcovr.cfg.in" gcovr.cfg @ONLY)
9396

9497
add_custom_target(
95-
process_coverage
96-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
97-
COMMENT "Running gcovr to process coverage results"
98-
COMMAND mkdir -p coverage
99-
COMMAND gcovr --config gcovr.cfg .)
98+
process_coverage
99+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
100+
COMMENT "Running gcovr to process coverage results"
101+
COMMAND mkdir -p coverage
102+
COMMAND gcovr --config gcovr.cfg .
103+
)

0 commit comments

Comments
 (0)