From b41cbb08c643f45025e4b58fd7854d02f4edfd23 Mon Sep 17 00:00:00 2001 From: Alex Kremer Date: Wed, 22 Apr 2026 23:20:14 +0100 Subject: [PATCH] chore: Add pre-commit hook to fix include style (#6995) Co-authored-by: Ayaz Salikhov --- .pre-commit-config.yaml | 24 ++++++++++++------- bin/pre-commit/fix_include_style.py | 37 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) create mode 100755 bin/pre-commit/fix_include_style.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 232e6ca5a3..1c0dc94550 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,6 +20,22 @@ repos: - id: check-merge-conflict args: [--assume-in-merge] + - repo: local + hooks: + - id: clang-tidy + name: "clang-tidy (enable with: TIDY=1)" + entry: ./bin/pre-commit/clang_tidy_check.py + language: python + types_or: [c++, c] + exclude: ^include/xrpl/protocol_autogen + pass_filenames: false # script determines the staged files itself + - id: fix-include-style + name: fix include style + entry: ./bin/pre-commit/fix_include_style.py + language: python + types_or: [c++, c] + exclude: ^include/xrpl/protocol_autogen/(transactions|ledger_entries)/ + - repo: https://github.com/pre-commit/mirrors-clang-format rev: cd481d7b0bfb5c7b3090c21846317f9a8262e891 # frozen: v22.1.0 hooks: @@ -67,14 +83,6 @@ repos: - repo: local hooks: - - id: clang-tidy - name: "clang-tidy (enable with: TIDY=1)" - entry: ./bin/pre-commit/clang_tidy_check.py - language: python - types_or: [c++, c] - exclude: ^include/xrpl/protocol_autogen - pass_filenames: false # script determines the staged files itself - - id: nix-fmt name: Format Nix files entry: | diff --git a/bin/pre-commit/fix_include_style.py b/bin/pre-commit/fix_include_style.py new file mode 100755 index 0000000000..ca59107271 --- /dev/null +++ b/bin/pre-commit/fix_include_style.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +""" +Converts quoted includes (#include "...") to angle-bracket includes +(#include <...>), which is the required style in this project. + +Usage: ./bin/pre-commit/fix_include_style.py ... +""" + +import re +import sys +from pathlib import Path + +PATTERN = re.compile(r'^(\s*#include\s*)"([^"]+)"', re.MULTILINE) + + +def fix_includes(path: Path) -> bool: + original = path.read_text(encoding="utf-8") + fixed = PATTERN.sub(r"\1<\2>", original) + if fixed != original: + path.write_text(fixed, encoding="utf-8") + return False + return True + + +def main() -> int: + files = [Path(f) for f in sys.argv[1:]] + success = True + + for path in files: + success &= fix_includes(path) + + return 0 if success else 1 + + +if __name__ == "__main__": + sys.exit(main())