diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 00000000..4ee72d78 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,24 @@ +name: pre-commit + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.x' + + - name: Install pre-commit + run: pip install pre-commit + + - name: Run pre-commit + run: pre-commit run --all-files diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..32312f80 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,21 @@ +# Exclude git submodules from all hooks +exclude: | + (?x)^( + test/benchmark/CGRA-Bench/.*| + test/benchmark/Zeonica_Testbench/.* + )$ + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v20.1.7 + hooks: + - id: clang-format + types_or: [c++, c] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 54fe7051..75a6357f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -19,4 +19,43 @@ To follow this style, you can use `clang-format` to generate the format configur $ clang-format -style=LLVM -dump-config > .clang-format ``` -Then use some format extentions in your editor to enable automatic code formatting (e.g., `clangd` in VS Code). \ No newline at end of file +Then use some format extentions in your editor to enable automatic code formatting (e.g., `clangd` in VS Code). + +## Pre-commit Hooks + +We use [pre-commit](https://pre-commit.com/) to automatically format code and check files before committing. This ensures consistent code style and catches common issues early. + +### Setup + +Install pre-commit and set up the git hooks: + +```sh +pip install pre-commit +pre-commit install +pre-commit install --hook-type pre-commit +``` + +### What it does + +The pre-commit hooks will automatically: +- Format C/C++ code using `clang-format` (with the project's `.clang-format` configuration) +- Remove trailing whitespace +- Ensure files end with a newline +- Validate YAML syntax +- Prevent committing large files + +### How it works + +Once installed, pre-commit hooks run automatically on your staged files every time you run `git commit`. If any issues are found, the commit will be interrupted and the hooks will attempt to fix them. You can then stage the fixed files and commit again. + +### Running manually + +To run pre-commit on all files manually (useful for initial setup or checking the entire codebase): + +```sh +pre-commit run --all-files +``` + +This is typically only needed when you first set up pre-commit or want to check files that weren't part of a commit. + +Please ensure your code passes all pre-commit checks before submitting a pull request. \ No newline at end of file diff --git a/README.md b/README.md index 0bd1ae51..3cd7e6be 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,28 @@ Build LLVM & Neura $ /workspace/llvm-project/build/bin/llvm-lit test.mlir -v ``` +Code Formatting with Pre-commit +-------------------------------------------------------- +This project uses [pre-commit](https://pre-commit.com/) to automatically format code and check files before committing. + +### Setup + +```sh +pip install pre-commit +pre-commit install +pre-commit install --hook-type pre-commit +``` + +Once installed, pre-commit hooks run automatically on your staged files every time you run `git commit`. + +To manually check the entire codebase: + +```sh +pre-commit run --all-files +``` + +See [CONTRIBUTING.md](CONTRIBUTING.md) for more details. + Sync `test/e2e` outputs into Zeonica_Testbench (submodule) -------------------------------------------------------- This repo vendors [`sarchlab/Zeonica_Testbench`](https://github.com/sarchlab/Zeonica_Testbench.git) as a git submodule at `test/benchmark/Zeonica_Testbench`.