diff --git a/.gitattributes b/.gitattributes index 8bfd419922d6b..10cff89899f11 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11,3 +11,6 @@ # make sure build.sh retains Unix line endings, even when checked out on windows. *.sh text eol=lf + +# Git hooks must have Unix line endings to work on all platforms +.githooks/* text eol=lf diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000000000..d0eb94432f156 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,17 @@ +#!/bin/sh +# Pre-commit hook: runs lintrunner on files being committed. +# Skip with: git commit --no-verify + +if ! command -v lintrunner >/dev/null 2>&1; then + echo "Warning: lintrunner not found - skipping pre-commit lint." + echo " Install: pip install -r requirements-lintrunner.txt && lintrunner init" + exit 0 +fi + +lintrunner --paths-cmd='git diff --cached --name-only --diff-filter=ACMR' + +if [ $? -ne 0 ]; then + echo "" + echo "Lint failed. Run 'lintrunner -a' to auto-fix, re-stage, and commit again." + exit 1 +fi diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2bd7a470f501e..9e1e7b9aa4f78 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,6 +9,18 @@ We're always looking for your help to improve the product (bug fixes, new featur * Make sure your PR adheres to the [PR Guidelines](./docs/PR_Guidelines.md) and [Coding Conventions and Standards](./docs/Coding_Conventions_and_Standards.md) established by the team. * If you're unsure about any of the above and want to contribute, you're welcome to [start a discussion](https://github.com/microsoft/onnxruntime/discussions) with the team. +## Git hooks (recommended) + +Enable the repo's pre-commit hook to run [lintrunner](https://github.com/suo/lintrunner) automatically before each commit: + +```sh +git config core.hooksPath .githooks +``` + +If lint issues are found, the commit is blocked. Run `lintrunner -a` to auto-fix, re-stage, and commit again. Use `git commit --no-verify` to bypass. + +> **Note:** `core.hooksPath` replaces Git's default hook directory (`.git/hooks/`). If you have existing custom hooks, you may need to integrate them manually. + ## Propose a new public API ONNX Runtime has a collection of [public APIs](./README.md#api-documentation). Some of these APIs make their way back into the Windows OS. We make compatibility commitments for these APIs and follow a structured process when adding to them. Please use the [Feature Request issue template](https://github.com/microsoft/onnxruntime/issues/new?template=feature_request.md) before starting any PRs that affect any of the public APIs.