Skip to content

Commit 5bccc50

Browse files
committed
refactor: fix repeat install clang tools
1 parent c42c695 commit 5bccc50

File tree

5 files changed

+10
-18
lines changed

5 files changed

+10
-18
lines changed

.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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ repos:
5454
args: [--checks=.clang-tidy] # Loads checks from .clang-tidy file
5555
```
5656

57+
> [!TIP]
58+
> Install the latest version of `clang-format` and `clang-tidy` 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):

cpp_linter_hooks/clang_format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
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+
ensure_installed("clang-format", hook_args.version)
19+
command = ["clang-format", "-i"]
2020

2121
# Add verbose flag if requested
2222
if hook_args.verbose:

cpp_linter_hooks/clang_tidy.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
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+
ensure_installed("clang-tidy", hook_args.version)
15+
command = ["clang-tidy"] + other_args
1716

1817
retval = 0
1918
output = ""

cpp_linter_hooks/util.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,6 @@ def _resolve_install(tool: str, version: Optional[str]) -> Optional[Path]:
187187
else DEFAULT_CLANG_TIDY_VERSION
188188
)
189189

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-
198190
path = shutil.which(tool)
199191
if path:
200192
runtime_version = _get_runtime_version(tool)
@@ -219,12 +211,10 @@ def is_installed(tool: str) -> Optional[Path]:
219211
return None
220212

221213

222-
def ensure_installed(tool: str, version: Optional[str] = None) -> str:
214+
def ensure_installed(tool: str, version: Optional[str] = None) -> None:
223215
"""Ensure a tool is installed, resolving its version if necessary."""
224216
LOG.info("Ensuring %s is installed", tool)
225217
tool_path = _resolve_install(tool, version)
226218
if tool_path:
227219
LOG.info("%s available at %s", tool, tool_path)
228-
return tool
229220
LOG.warning("%s not found and could not be installed", tool)
230-
return tool

0 commit comments

Comments
 (0)