Skip to content

Conversation

@sgonorov
Copy link
Contributor

@sgonorov sgonorov commented Nov 6, 2025

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.

# Install pre-commit hooks (one-time setup)
pre-commit install

# Run on all staged files
pre-commit run

# Run on all files in the repository
pre-commit run --all-files

# Run on specific files
pre-commit run --files path/to/file1.cpp path/to/file2.py

Prerequisites

Before using pre-commit hooks, ensure the following tools are installed:

# Required for C++ linting and formatting
sudo apt install clang-format clang-tidy  # Linux
# or
brew install clang-format llvm  # macOS

Added Linters and Their Benefits

General Hooks (pre-commit-hooks)

  • Fixes trailing whitespace, line endings (LF), and ensures files end with newlines
  • Prevents commits to master branch and catches merge conflicts
  • Detects private keys and large files (>1MB)
  • Validates JSON, YAML, TOML, and Python AST syntax

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.

@Wovchena Wovchena requested a review from Copilot November 7, 2025 06:10
Copy link
Contributor

Copilot AI left a 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.

@github-actions github-actions bot added the category: GHA CI based on Github actions label Nov 7, 2025
@sgonorov sgonorov requested a review from Wovchena November 7, 2025 08:57
@sgonorov sgonorov marked this pull request as ready for review November 7, 2025 08:57
@sgonorov sgonorov force-pushed the pre_commit_linting_and_formatting branch from e883a6f to c345800 Compare November 10, 2025 07:44
Copy link
Collaborator

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  # macOS

What 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?

Copy link
Contributor Author

@sgonorov sgonorov Nov 10, 2025

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.

Copy link
Collaborator

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?

Copy link
Contributor Author

@sgonorov sgonorov Nov 10, 2025

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.

Copy link
Collaborator

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

Copy link
Collaborator

@Wovchena Wovchena Nov 10, 2025

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

Copy link
Contributor Author

@sgonorov sgonorov Nov 10, 2025

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.

Copy link
Collaborator

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

@sgonorov sgonorov force-pushed the pre_commit_linting_and_formatting branch from c345800 to 386f67a Compare November 11, 2025 06:53
@sgonorov sgonorov requested a review from Wovchena November 11, 2025 07:34
@sgonorov sgonorov force-pushed the pre_commit_linting_and_formatting branch 2 times, most recently from 462994b to 9917912 Compare November 13, 2025 08:57
@sgonorov sgonorov force-pushed the pre_commit_linting_and_formatting branch from 9917912 to f602d0f Compare November 13, 2025 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: cmake / build Cmake scripts category: GHA CI based on Github actions no-match-files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants