-
Notifications
You must be signed in to change notification settings - Fork 300
pre-commit introduction #2980
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
base: master
Are you sure you want to change the base?
pre-commit introduction #2980
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces pre-commit hooks to automate code quality checks and formatting before commits. It configures multiple linters and formatters for Python, C/C++, and configuration files to maintain consistent code quality standards.
Key Changes
- Added pre-commit configuration with 9 different hooks for Python, C/C++, YAML, JSON, Markdown, and CMake files
- Configured Ruff for Python linting/formatting, mypy for type checking, and clang-format/clang-tidy for C++ code quality
- Set up project-specific rules and exclusions for Ruff and mypy to balance strictness with practicality
Reviewed Changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
.pre-commit-config.yaml |
Configures pre-commit hooks for various languages and file types with specific tool versions |
pyproject.toml |
Adds Ruff configuration for Python linting and formatting with project-specific rule customizations |
mypy.ini |
Sets up mypy static type checking configuration with strict type checking options enabled |
.clang-tidy |
Configures C++ static analysis checks with specific rule inclusions and exclusions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
e883a6f to
c345800
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Required for C++ linting and formatting
sudo apt install clang-format clang-tidy # Linux
# or
brew install clang-format llvm # macOSWhat happens on Windows?
sudo apt install clang-format clang-tidy
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
clang-format-18 : Depends: libllvm18 (= 1:18.1.3-1) but 1:18.1.3-1ubuntu1 is to be installed
clang-tidy-18 : Depends: libllvm18 (= 1:18.1.3-1) but 1:18.1.3-1ubuntu1 is to be installed
Depends: clang-tools-18 but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
Do you know the solution for Ubuntu24?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like not a specific Ubuntu 24 problem. I've installed them on two Ubuntu 24s and it's okay. I think it's some kind of a broken dependency which can be fixed through apt. Maybe try using an official LLVM repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to silence pre-commit if no issues found?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried searching, but not sure. Where are you planning to use it? I don't think it's a good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every time I run ls, my alias also runs git commit and git reset to add the current state to reflog
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a trailing space, say, src/cpp/src/utils.cpp's content results in the whole file being reformatted. That Shouldn't happen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've searched, but couldn't find any easy way to avoid this. I can forbit committing files with whitespace-only changes. But changing whitespaces should also trigger reformatting in my opinion - otherwise we can get inconsistencies in formatting through the project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case you should introduce this separately and in multiple PRs so minor contributions don't end up rewriting the whole file
c345800 to
386f67a
Compare
462994b to
9917912
Compare
9917912 to
f602d0f
Compare
Add Pre-commit Hooks for Code Quality and Consistency
This PR introduces pre-commit hooks to automate code quality checks and formatting across the codebase. The hooks run automatically on staged files before each commit.
How to Run Pre-commit Manually
Important: Pre-commit only lints changed/staged files, not the entire codebase.
Prerequisites
Before using pre-commit hooks, ensure the following tools are installed:
Added Linters and Their Benefits
General Hooks (pre-commit-hooks)
Benefits: Prevents basic formatting inconsistencies and security issues before they enter the codebase.
Ruff (Python)
Fast Python linter and formatter that replaces multiple tools (flake8, black, isort, etc.).
Benefits: Catches common Python errors, enforces style consistency, and automatically fixes issues. 10-100x faster than traditional Python linters.
clang-format (C/C++)
Automatic C++ code formatter for consistent style.
Benefits: Eliminates style debates and ensures uniform formatting across the C++ codebase.
clang-tidy (C/C++)
Static analysis tool for C++ that catches bugs, performance issues, and style violations.
Benefits: Detects potential bugs, memory leaks, and anti-patterns. Enforces modern C++ best practices and improves code safety.
mypy (Python)
Static type checker for Python.
Benefits: Catches type-related bugs before runtime, improves code documentation through type hints, and enhances IDE autocomplete.
absolufy-imports (Python)
Converts relative imports to absolute imports.
Benefits: Makes imports more explicit and prevents import resolution issues in larger codebases.
Prettier (JSON/YAML/Markdown/JS/TS)
Opinionated code formatter for web-related files.
Benefits: Ensures consistent formatting for configuration and documentation files.
cmake-format (CMake)
Formatter for CMake files.
Benefits: Maintains readable and consistent CMake build configurations.
yamllint (YAML)
YAML linter for configuration files.
Benefits: Catches syntax errors and enforces YAML best practices in CI/CD configs.