Skip to content

Commit b6c75c0

Browse files
authored
Merge branch 'main' into copilot/fix-110
2 parents d84cd71 + d56ca2a commit b6c75c0

File tree

9 files changed

+43
-273
lines changed

9 files changed

+43
-273
lines changed

.github/workflows/codspeed.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
run: pip install -e .[dev]
3636

3737
- name: Run benchmarks
38-
uses: CodSpeedHQ/action@6eeb021fd0f305388292348b775d96d95253adf4 #v3
38+
uses: CodSpeedHQ/action@653fdc30e6c40ffd9739e40c8a0576f4f4523ca1 #v3
3939
with:
4040
token: ${{ secrets.CODSPEED_TOKEN }}
4141
run: pytest tests/ --codspeed

.pre-commit-hooks.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
description: Automatically install any specific version of clang-format and format C/C++ code
44
entry: clang-format-hook
55
language: python
6-
files: \.(h\+\+|h|hh|hxx|hpp|c|cc|cpp|c\+\+|cxx)$
6+
types_or: [c++, c]
77
require_serial: false
88

99
- id: clang-tidy
1010
name: clang-tidy
1111
description: Automatically install any specific version of clang-tidy and diagnose/fix typical programming errors
1212
entry: clang-tidy-hook
1313
language: python
14-
files: \.(h\+\+|h|hh|hxx|hpp|c|cc|cpp|c\+\+|cxx)$
14+
types_or: [c++, c]
1515
require_serial: false

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Add this configuration to your `.pre-commit-config.yaml` file:
3131
```yaml
3232
repos:
3333
- repo: https://github.com/cpp-linter/cpp-linter-hooks
34-
rev: v1.1.0 # Use the tag or commit you want
34+
rev: v1.1.2 # Use the tag or commit you want
3535
hooks:
3636
- id: clang-format
3737
args: [--style=Google] # Other coding style: LLVM, GNU, Chromium, Microsoft, Mozilla, WebKit.
@@ -46,32 +46,32 @@ To use custom configurations like `.clang-format` and `.clang-tidy`:
4646
```yaml
4747
repos:
4848
- repo: https://github.com/cpp-linter/cpp-linter-hooks
49-
rev: v1.1.0
49+
rev: v1.1.2
5050
hooks:
5151
- id: clang-format
5252
args: [--style=file] # Loads style from .clang-format file
5353
- id: clang-tidy
5454
args: [--checks=.clang-tidy] # Loads checks from .clang-tidy file
5555
```
5656

57+
> [!TIP]
58+
> By default, the latest version of [`clang-format`](https://pypi.org/project/clang-format/#history) and [`clang-tidy`](https://pypi.org/project/clang-tidy/#history) will be installed if not specified. You can specify the version using the `--version` argument in the `args` list as shown below.
59+
5760
### Custom Clang Tool Version
5861

5962
To use specific versions of clang-format and clang-tidy (using Python wheel packages):
6063

6164
```yaml
6265
repos:
6366
- repo: https://github.com/cpp-linter/cpp-linter-hooks
64-
rev: v1.1.0
67+
rev: v1.1.2
6568
hooks:
6669
- id: clang-format
6770
args: [--style=file, --version=21] # Specifies version
6871
- id: clang-tidy
6972
args: [--checks=.clang-tidy, --version=21] # Specifies version
7073
```
7174

72-
> [!NOTE]
73-
> Starting from version **v1.0.0**, this pre-commit hook now relies on Python wheel packages — [clang-format](https://pypi.org/project/clang-format/) and [clang-tidy](https://pypi.org/project/clang-tidy/) — instead of the [clang-tools binaries](https://github.com/cpp-linter/clang-tools-static-binaries). The wheel packages are lighter, easier to install, and offer better cross-platform compatibility. For more information, see the [detailed migration notes](docs/migration-notes.md).
74-
7575
## Output
7676

7777
### clang-format Output
@@ -151,7 +151,7 @@ Use -header-filter=.* to display errors from all non-system headers. Use -system
151151

152152
```yaml
153153
- repo: https://github.com/cpp-linter/cpp-linter-hooks
154-
rev: v1.1.0
154+
rev: v1.1.2
155155
hooks:
156156
- id: clang-format
157157
args: [--style=file, --version=21]
@@ -177,7 +177,7 @@ This approach ensures that only modified files are checked, further speeding up
177177
```yaml
178178
repos:
179179
- repo: https://github.com/cpp-linter/cpp-linter-hooks
180-
rev: v1.1.0
180+
rev: v1.1.2
181181
hooks:
182182
- id: clang-format
183183
args: [--style=file, --version=21, --verbose] # Add -v or --verbose for detailed output

cpp_linter_hooks/clang_format.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from argparse import ArgumentParser
44
from typing import Tuple
55

6-
from .util import ensure_installed, DEFAULT_CLANG_FORMAT_VERSION
6+
from cpp_linter_hooks.util import _resolve_install, DEFAULT_CLANG_FORMAT_VERSION
77

88

99
parser = ArgumentParser()
@@ -15,8 +15,9 @@
1515

1616
def run_clang_format(args=None) -> Tuple[int, str]:
1717
hook_args, other_args = parser.parse_known_args(args)
18-
tool_name = ensure_installed("clang-format", hook_args.version)
19-
command = [tool_name, "-i"]
18+
if hook_args.version:
19+
_resolve_install("clang-format", hook_args.version)
20+
command = ["clang-format", "-i"]
2021

2122
# Add verbose flag if requested
2223
if hook_args.verbose:

cpp_linter_hooks/clang_tidy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from argparse import ArgumentParser
33
from typing import Tuple
44

5-
from .util import ensure_installed, DEFAULT_CLANG_TIDY_VERSION
5+
from cpp_linter_hooks.util import _resolve_install, DEFAULT_CLANG_TIDY_VERSION
66

77

88
parser = ArgumentParser()
@@ -11,9 +11,9 @@
1111

1212
def run_clang_tidy(args=None) -> Tuple[int, str]:
1313
hook_args, other_args = parser.parse_known_args(args)
14-
tool_name = ensure_installed("clang-tidy", hook_args.version)
15-
command = [tool_name]
16-
command.extend(other_args)
14+
if hook_args.version:
15+
_resolve_install("clang-tidy", hook_args.version)
16+
command = ["clang-tidy"] + other_args
1717

1818
retval = 0
1919
output = ""

cpp_linter_hooks/util.py

Lines changed: 8 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,10 @@ def get_version_from_dependency(tool: str) -> Optional[str]:
2020
return None
2121
with open(pyproject_path, "rb") as f:
2222
data = tomllib.load(f)
23-
# First try project.optional-dependencies.tools
24-
optional_deps = data.get("project", {}).get("optional-dependencies", {})
25-
tools_deps = optional_deps.get("tools", [])
26-
for dep in tools_deps:
27-
if dep.startswith(f"{tool}=="):
28-
return dep.split("==")[1]
29-
30-
# Fallback to project.dependencies for backward compatibility
31-
dependencies = data.get("project", {}).get("dependencies", [])
32-
for dep in dependencies:
23+
# Check build-system.requires
24+
build_system = data.get("build-system", {})
25+
requires = build_system.get("requires", [])
26+
for dep in requires:
3327
if dep.startswith(f"{tool}=="):
3428
return dep.split("==")[1]
3529
return None
@@ -148,29 +142,16 @@ def parse_version(v: str):
148142
return None
149143

150144

151-
def _get_runtime_version(tool: str) -> Optional[str]:
152-
"""Get the runtime version of a tool."""
153-
try:
154-
output = subprocess.check_output([tool, "--version"], text=True)
155-
if tool == "clang-tidy":
156-
lines = output.strip().splitlines()
157-
if len(lines) > 1:
158-
return lines[1].split()[-1]
159-
elif tool == "clang-format":
160-
return output.strip().split()[-1]
161-
except Exception:
162-
return None
163-
164-
165145
def _install_tool(tool: str, version: str) -> Optional[Path]:
166-
"""Install a tool using pip."""
146+
"""Install a tool using pip, suppressing output."""
167147
try:
168148
subprocess.check_call(
169-
[sys.executable, "-m", "pip", "install", f"{tool}=={version}"]
149+
[sys.executable, "-m", "pip", "install", f"{tool}=={version}"],
150+
stdout=subprocess.DEVNULL,
151+
stderr=subprocess.DEVNULL,
170152
)
171153
return shutil.which(tool)
172154
except subprocess.CalledProcessError:
173-
LOG.error("Failed to install %s==%s", tool, version)
174155
return None
175156

176157

@@ -187,44 +168,4 @@ def _resolve_install(tool: str, version: Optional[str]) -> Optional[Path]:
187168
else DEFAULT_CLANG_TIDY_VERSION
188169
)
189170

190-
# Additional safety check in case DEFAULT versions are None
191-
if user_version is None:
192-
user_version = (
193-
DEFAULT_CLANG_FORMAT_VERSION
194-
if tool == "clang-format"
195-
else DEFAULT_CLANG_TIDY_VERSION
196-
)
197-
198-
path = shutil.which(tool)
199-
if path:
200-
runtime_version = _get_runtime_version(tool)
201-
if runtime_version and user_version not in runtime_version:
202-
LOG.info(
203-
"%s version mismatch (%s != %s), reinstalling...",
204-
tool,
205-
runtime_version,
206-
user_version,
207-
)
208-
return _install_tool(tool, user_version)
209-
return Path(path)
210-
211171
return _install_tool(tool, user_version)
212-
213-
214-
def is_installed(tool: str) -> Optional[Path]:
215-
"""Check if a tool is installed and return its path."""
216-
path = shutil.which(tool)
217-
if path:
218-
return Path(path)
219-
return None
220-
221-
222-
def ensure_installed(tool: str, version: Optional[str] = None) -> str:
223-
"""Ensure a tool is installed, resolving its version if necessary."""
224-
LOG.info("Ensuring %s is installed", tool)
225-
tool_path = _resolve_install(tool, version)
226-
if tool_path:
227-
LOG.info("%s available at %s", tool, tool_path)
228-
return tool
229-
LOG.warning("%s not found and could not be installed", tool)
230-
return tool

pyproject.toml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["setuptools>=45", "setuptools-scm"]
2+
requires = ["setuptools>=45", "setuptools-scm", "clang-format==21.1.0", "clang-tidy==21.1.0"]
33
build-backend = "setuptools.build_meta"
44

55
requires-python = ">=3.9"
@@ -46,12 +46,6 @@ source = "https://github.com/cpp-linter/cpp-linter-hooks"
4646
tracker = "https://github.com/cpp-linter/cpp-linter-hooks/issues"
4747

4848
[project.optional-dependencies]
49-
# only clang tools can added to this section to make hooks work
50-
tools = [
51-
"clang-format==21.1.0",
52-
"clang-tidy==21.1.0",
53-
]
54-
5549
dev = [
5650
"coverage",
5751
"pre-commit",

testing/benchmark_hook_1.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/cpp-linter/cpp-linter-hooks
3-
rev: v1.1.0
3+
rev: v1.1.2
44
hooks:
55
- id: clang-format
66
args: [--style=file, --version=21]

0 commit comments

Comments
 (0)