Skip to content

clang-format hook support --verbose output #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ Use -header-filter=.* to display errors from all non-system headers. Use -system

```

## Troubleshooting

### Debugging `clang-format` hook

If you encounter issues with the clang-format hook (such as exit code 247 or other errors), you can enable verbose output to show the list of processed files.

```yaml
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: v0.8.1
hooks:
- id: clang-format
args: [--style=file, --version=18, --verbose] # Add --verbose for detailed output
```

## Contributing

We welcome contributions! Whether it's fixing issues, suggesting improvements, or submitting pull requests, your support is greatly appreciated.
Expand Down
3 changes: 3 additions & 0 deletions cpp_linter_hooks/clang_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

parser = ArgumentParser()
parser.add_argument("--version", default=DEFAULT_CLANG_VERSION)
parser.add_argument(
"-v", "--verbose", action="store_true", help="Enable verbose output"
)


def run_clang_format(args=None) -> Tuple[int, str]:
Expand Down
6 changes: 6 additions & 0 deletions testing/pre-commit-config-verbose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: .
rev: HEAD
hooks:
- id: clang-format
args: [--style=file, --version=16, --verbose] # test with verbose output
2 changes: 1 addition & 1 deletion testing/run.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
rm -f result.txt
git restore testing/main.c

for config in testing/pre-commit-config.yaml testing/pre-commit-config-version.yaml; do
for config in testing/pre-commit-config.yaml testing/pre-commit-config-version.yaml testing/pre-commit-config-verbose.yaml; do
pre-commit clean
pre-commit run -c $config --files testing/main.c | tee -a result.txt || true
git restore testing/main.c
Expand Down
32 changes: 32 additions & 0 deletions tests/test_clang_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,35 @@ def test_run_clang_format_dry_run(args, expected_retval, tmp_path):
test_file = tmp_path / "main.c"
ret, _ = run_clang_format(["--dry-run", str(test_file)])
assert ret == -1 # Dry run should not fail


def test_run_clang_format_verbose(tmp_path):
"""Test that verbose option works and provides detailed output."""
# copy test file to tmp_path to prevent modifying repo data
test_file = tmp_path / "main.c"
test_file.write_bytes(Path("testing/main.c").read_bytes())

# Test with verbose flag
ret, output = run_clang_format(["--verbose", "--style=Google", str(test_file)])

# Should succeed
assert ret == 0
# Should have verbose output (will be printed to stderr, not returned)
# The function should still return successfully
assert test_file.read_text() == Path("testing/good.c").read_text()


def test_run_clang_format_verbose_error(tmp_path):
"""Test that verbose option provides useful error information."""
test_file = tmp_path / "main.c"
test_file.write_bytes(Path("testing/main.c").read_bytes())

# Test with verbose flag and invalid style
ret, output = run_clang_format(
["--verbose", "--style=InvalidStyle", str(test_file)]
)

# Should fail
assert ret != 0
# Should have error message in output
assert "Invalid value for -style" in output
Loading