-
Notifications
You must be signed in to change notification settings - Fork 493
docs: add guide for using pixi with lefthook, pre-commit, and prek (#… #5891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yacine-DH
wants to merge
10
commits into
prefix-dev:main
Choose a base branch
from
Yacine-DH:docs/git-hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5521edb
docs: add guide for using pixi with lefthook, pre-commit, and prek (#…
Yacine-DH 690c988
Merge branch 'main' into docs/git-hooks
Yacine-DH 7fa46de
Merge branch 'main' into docs/git-hooks
Yacine-DH 1b992d1
Merge branch 'main' into docs/git-hooks
Yacine-DH 2fdd9f6
Merge branch 'main' into docs/git-hooks
Yacine-DH 698d28a
Update docs/integration/third_party/git_hooks.md
Yacine-DH fff4823
"docs: fix prek URL, rewrite prek section, add pre-commit.ci tipp"
Yacine-DH ab01093
link fix
Yacine-DH 2672726
Merge branch 'main' into docs/git-hooks
Yacine-DH fe398e7
Merge branch 'main' into docs/git-hooks
Yacine-DH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # Git Hooks | ||
|
|
||
| When working with `pixi`, you might want to use Git hooks to ensure that your code is formatted and linted properly before committing or pushing it. | ||
| Since `pixi` manages your environment, you can use these tools to run your checks within the `pixi` environment, ensuring that everyone on your team is using the same tools and versions without the overhead of downloading duplicate virtual environments. | ||
|
|
||
| We recommend using a Git hook manager to manage your hooks. The most popular ones are [Lefthook](https://github.com/evilmartians/lefthook), [pre-commit](https://pre-commit.com/), and [prek](https://github.com/j178/prek). | ||
| ## Lefthook | ||
|
|
||
| [Lefthook](https://github.com/evilmartians/lefthook) is a fast and powerful Git hooks manager for any type of project. | ||
|
|
||
| To use Lefthook with `pixi`, you can add it to your project's dependencies: | ||
|
|
||
| ```shell | ||
| pixi add lefthook | ||
| ``` | ||
|
|
||
| Then, initialize and configure `lefthook` by creating a `lefthook.yaml` file in the root of your project: | ||
|
|
||
| ```yaml title="lefthook.yaml" | ||
| # Run lefthook via pixi | ||
| lefthook: pixi run --no-progress lefthook | ||
| no_auto_install: true | ||
|
|
||
| # Use template for `run` so we don't have to repeat the flags | ||
| templates: | ||
| run: run --quiet --no-progress --as-is | ||
|
|
||
| pre-commit: | ||
| parallel: true | ||
| jobs: | ||
| - name: pixi-install | ||
| run: pixi install | ||
| - group: | ||
| parallel: true | ||
| jobs: | ||
| - name: ruff-check | ||
| glob: "*.{py,pyi}" | ||
| run: pixi {run} ruff check --fix --exit-non-zero-on-fix --force-exclude {staged_files} | ||
| - name: ruff-format | ||
| glob: "*.{py,pyi}" | ||
| run: pixi {run} ruff format --force-exclude {staged_files} | ||
| ``` | ||
|
|
||
| Make sure to install the hooks into your Git repository: | ||
|
|
||
| ```shell | ||
| pixi run lefthook install | ||
| ``` | ||
|
|
||
| With this configuration, Lefthook will use `pixi run` to execute your hooks, ensuring they run within the correct environment. The `--quiet` and `--no-progress` flags are useful to avoid cluttering the output. | ||
|
|
||
| ## pre-commit | ||
|
|
||
| [pre-commit](https://pre-commit.com/) is a framework for managing and maintaining multi-language pre-commit hooks. | ||
|
|
||
| You can add `pre-commit` to your project: | ||
|
|
||
| ```shell | ||
| pixi add pre-commit | ||
| ``` | ||
|
|
||
| Create a `.pre-commit-config.yaml` file in the root of your project: | ||
|
|
||
| ```yaml title=".pre-commit-config.yaml" | ||
| repos: | ||
| - repo: local | ||
| hooks: | ||
| - id: ruff-check | ||
| name: ruff-check | ||
| entry: pixi run --quiet --no-progress ruff check --force-exclude | ||
| language: system | ||
| types_or: [python, pyi] | ||
| require_serial: true | ||
| - id: ruff-format | ||
| name: ruff-format | ||
| entry: pixi run --quiet --no-progress ruff format --force-exclude | ||
| language: system | ||
| types_or: [python, pyi] | ||
| require_serial: true | ||
| ``` | ||
|
|
||
| Install the `pre-commit` hooks into your repository: | ||
|
|
||
| ```shell | ||
| pixi run pre-commit install | ||
| ``` | ||
|
|
||
| By defining the hooks as `local` and `language: system`, `pre-commit` will not try to manage the environments itself, but will instead rely on `pixi run` to execute the commands within the `pixi` environment. | ||
|
|
||
|
|
||
| !!! tip "Using pre-commit in CI" | ||
| This approach is **not compatible with [pre-commit.ci](https://pre-commit.ci)**, since `pixi` is not | ||
| pre-installed in that environment. | ||
|
|
||
| Instead, run your hooks directly in GitHub Actions using: | ||
|
|
||
| ```shell | ||
| pixi run pre-commit run --all-files --show-diff-on-failure | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| ## prek | ||
|
|
||
| [prek](https://github.com/j178/prek) is a faster, Rust-based, drop-in replacement for `pre-commit`. | ||
| It uses the **exact same** `.pre-commit-config.yaml` configuration format, so no changes to your | ||
| existing hook definitions are needed. | ||
|
|
||
| To use `prek` with `pixi`, add it to your project: | ||
|
|
||
| ```shell | ||
| pixi add prek | ||
| ``` | ||
|
|
||
| Install the git hooks: | ||
|
|
||
| ```shell | ||
| pixi run prek install | ||
| ``` | ||
|
|
||
| From this point on, `prek` will run your hooks on every commit using your existing | ||
| `.pre-commit-config.yaml` — no additional configuration is required. | ||
|
|
||
| !!! tip | ||
| If you are already using `pre-commit`, switching to `prek` is as simple as replacing | ||
| `pre-commit` with `prek` in your commands. Your existing `.pre-commit-config.yaml` | ||
| works without any modifications. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.