Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
description: Automatically install any specific version of clang-format and format C/C++ code
entry: clang-format-hook
language: python
files: \.(h\+\+|h|hh|hxx|hpp|c|cc|cpp|c\+\+|cxx)$
types_or: [c++, c]
require_serial: false

- id: clang-tidy
name: clang-tidy
description: Automatically install any specific version of clang-tidy and diagnose/fix typical programming errors
entry: clang-tidy-hook
language: python
files: \.(h\+\+|h|hh|hxx|hpp|c|cc|cpp|c\+\+|cxx)$
types_or: [c++, c]
require_serial: false
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Add this configuration to your `.pre-commit-config.yaml` file:
```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.1.0 # Use the tag or commit you want
rev: v1.1.2 # Use the tag or commit you want
hooks:
- id: clang-format
args: [--style=Google] # Other coding style: LLVM, GNU, Chromium, Microsoft, Mozilla, WebKit.
Expand All @@ -46,32 +46,32 @@ To use custom configurations like `.clang-format` and `.clang-tidy`:
```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.1.0
rev: v1.1.2
hooks:
- id: clang-format
args: [--style=file] # Loads style from .clang-format file
- id: clang-tidy
args: [--checks=.clang-tidy] # Loads checks from .clang-tidy file
```

> [!TIP]
> 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.

### Custom Clang Tool Version

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

```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.1.0
rev: v1.1.2
hooks:
- id: clang-format
args: [--style=file, --version=21] # Specifies version
- id: clang-tidy
args: [--checks=.clang-tidy, --version=21] # Specifies version
```

> [!NOTE]
> 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).

## Output

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

```yaml
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.1.0
rev: v1.1.2
hooks:
- id: clang-format
args: [--style=file, --version=21]
Expand All @@ -177,7 +177,7 @@ This approach ensures that only modified files are checked, further speeding up
```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.1.0
rev: v1.1.2
hooks:
- id: clang-format
args: [--style=file, --version=21, --verbose] # Add -v or --verbose for detailed output
Expand Down
7 changes: 4 additions & 3 deletions cpp_linter_hooks/clang_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from argparse import ArgumentParser
from typing import Tuple

from .util import ensure_installed, DEFAULT_CLANG_FORMAT_VERSION
from cpp_linter_hooks.util import _resolve_install, DEFAULT_CLANG_FORMAT_VERSION


parser = ArgumentParser()
Expand All @@ -15,8 +15,9 @@

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

# Add verbose flag if requested
if hook_args.verbose:
Expand Down
8 changes: 4 additions & 4 deletions cpp_linter_hooks/clang_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from argparse import ArgumentParser
from typing import Tuple

from .util import ensure_installed, DEFAULT_CLANG_TIDY_VERSION
from cpp_linter_hooks.util import _resolve_install, DEFAULT_CLANG_TIDY_VERSION


parser = ArgumentParser()
Expand All @@ -11,9 +11,9 @@

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

retval = 0
output = ""
Expand Down
75 changes: 8 additions & 67 deletions cpp_linter_hooks/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@ def get_version_from_dependency(tool: str) -> Optional[str]:
return None
with open(pyproject_path, "rb") as f:
data = tomllib.load(f)
# First try project.optional-dependencies.tools
optional_deps = data.get("project", {}).get("optional-dependencies", {})
tools_deps = optional_deps.get("tools", [])
for dep in tools_deps:
if dep.startswith(f"{tool}=="):
return dep.split("==")[1]

# Fallback to project.dependencies for backward compatibility
dependencies = data.get("project", {}).get("dependencies", [])
for dep in dependencies:
# Check build-system.requires
build_system = data.get("build-system", {})
requires = build_system.get("requires", [])
for dep in requires:
if dep.startswith(f"{tool}=="):
return dep.split("==")[1]
return None
Expand Down Expand Up @@ -148,29 +142,16 @@ def parse_version(v: str):
return None


def _get_runtime_version(tool: str) -> Optional[str]:
"""Get the runtime version of a tool."""
try:
output = subprocess.check_output([tool, "--version"], text=True)
if tool == "clang-tidy":
lines = output.strip().splitlines()
if len(lines) > 1:
return lines[1].split()[-1]
elif tool == "clang-format":
return output.strip().split()[-1]
except Exception:
return None


def _install_tool(tool: str, version: str) -> Optional[Path]:
"""Install a tool using pip."""
"""Install a tool using pip, suppressing output."""
try:
subprocess.check_call(
[sys.executable, "-m", "pip", "install", f"{tool}=={version}"]
[sys.executable, "-m", "pip", "install", f"{tool}=={version}"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
return shutil.which(tool)
except subprocess.CalledProcessError:
LOG.error("Failed to install %s==%s", tool, version)
return None


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

# Additional safety check in case DEFAULT versions are None
if user_version is None:
user_version = (
DEFAULT_CLANG_FORMAT_VERSION
if tool == "clang-format"
else DEFAULT_CLANG_TIDY_VERSION
)

path = shutil.which(tool)
if path:
runtime_version = _get_runtime_version(tool)
if runtime_version and user_version not in runtime_version:
LOG.info(
"%s version mismatch (%s != %s), reinstalling...",
tool,
runtime_version,
user_version,
)
return _install_tool(tool, user_version)
return Path(path)

return _install_tool(tool, user_version)


def is_installed(tool: str) -> Optional[Path]:
"""Check if a tool is installed and return its path."""
path = shutil.which(tool)
if path:
return Path(path)
return None


def ensure_installed(tool: str, version: Optional[str] = None) -> str:
"""Ensure a tool is installed, resolving its version if necessary."""
LOG.info("Ensuring %s is installed", tool)
tool_path = _resolve_install(tool, version)
if tool_path:
LOG.info("%s available at %s", tool, tool_path)
return tool
LOG.warning("%s not found and could not be installed", tool)
return tool
8 changes: 1 addition & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=45", "setuptools-scm"]
requires = ["setuptools>=45", "setuptools-scm", "clang-format==21.1.0", "clang-tidy==21.1.0"]
build-backend = "setuptools.build_meta"

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

[project.optional-dependencies]
# only clang tools can added to this section to make hooks work
tools = [
"clang-format==21.1.0",
"clang-tidy==21.1.0",
]

dev = [
"coverage",
"pre-commit",
Expand Down
2 changes: 1 addition & 1 deletion testing/benchmark_hook_1.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v1.1.0
rev: v1.1.2
hooks:
- id: clang-format
args: [--style=file, --version=21]
Loading
Loading