From 9e9ea472c797bb327f2a7bc29d33a55f68f63301 Mon Sep 17 00:00:00 2001 From: Q Liu <52538137+LIUQyou@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:53:15 +0800 Subject: [PATCH] [format] Add pre-commit check (#438) * pre-commit check add * update README and precommit check * update README and precommit check and pre-commit install * update README and precommit check and pre-commit install --------- Co-authored-by: LiuQun --- .pre-commit-config.yaml | 59 +++++++++++++++++++++++++++++++++++++++++ README.md | 6 +++++ requirements.txt | 2 +- 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..0f65b75427 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,59 @@ +repos: + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v19.1.3 # Use the version of clang-format you have installed + hooks: + - id: clang-format + name: clang-format C++ code + files: \.(cpp|hpp|cc|cxx|h|c|hxx)$ + args: [--style=llvm] # You can set your preferred style here + + # - repo: https://github.com/pocc/pre-commit-hooks + # rev: v1.3.5 # Use the latest stable version + # hooks: + # - id: clang-tidy + # name: clang-tidy C++ code + # files: \.(cpp|hpp|cc|cxx|h|c|hxx)$ + # args: + # - --quiet + # - --checks=*,-clang-diagnostic-*,-clang-analyzer-* + # - --extra-arg=-x + # - --extra-arg=c++ + # - --extra-arg=-std=c++17 + # - --warnings-as-errors=* + # - --extra-arg=-I. + + - repo: https://github.com/psf/black + rev: 24.10.0 # Use the latest stable version + hooks: + - id: black + language_version: python3.10 + + # a comprehensive tool for checking the style and quality of Python code. + # It combines three popular Python tools: + # PyFlakes: Checks for logical errors in the code. + # pycodestyle (formerly known as pep8): Checks for adherence to the PEP 8 style guide. + # McCabe Complexity Checker: Measures the complexity of your code. + - repo: https://github.com/PyCQA/flake8 + rev: 6.1.0 # Set the rev to match the desired flake8 version + hooks: + - id: flake8 + args: + - --max-line-length=88 # Adjust as per your style guide + - --ignore=F821,F403,F405,F401,W503,E203,E402,E401,W605,E712,E711,F841 + # W503: Line break before binary operator + # E203: Whitespace before colon + # E402: Module level import not at top of file + # E401: Multiple imports on one line + # W605: Invalid escape sequence + # E712: Comparison to True/False + # E711: Comparison to None + # F841: Unused variable + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 # Updated to the latest version + hooks: + - id: end-of-file-fixer + - id: trailing-whitespace + - id: check-merge-conflict + - id: check-yaml + - id: check-added-large-files diff --git a/README.md b/README.md index 2e44658b02..771a0a3175 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,12 @@ This program should be a drop-in replacement for `mlir-lsp-server`, supporting n After modification, your editor should have correct completion and error prompts for new dialects such as `rvv` and `gemmini`. +### pre-commit checks + +The .pre-commit-config.yaml file checks code format and style on each commit, using tools such as clang-format, black, and flake8. You can also run these checks without committing by using "pre-commit run --all-files". This ensures consistent coding standards and prevents common errors before pushing changes. + +To get started, you should install pre-commit (e.g., pip install pre-commit) and verify that clang-format, black, and flake8 are available. On Linux, you can use your package manager for clang-format, and pip for Python tools. If you need to revert any unwanted formatting changes, you can use "git stash" or "git restore ." (for all files) or "git restore " (for a specific file), or revert the commit through your Git history. + ## Examples The purpose of the examples is to give users a better understanding of how to use the passes and the interfaces in buddy-mlir. Currently, we provide three types of examples. diff --git a/requirements.txt b/requirements.txt index 55726a11f8..28586a4356 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,4 +16,4 @@ PyYAML certifi idna diffusers - +pre-commit