Skip to content
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

Feature: Add poetry as pre-commit hook #2511

Merged
merged 31 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
18443e7
added pre-commit hooks for check & lock
Cielquan Jun 6, 2020
691c250
added pre-commit hook for export requirements.txt
Cielquan Jun 6, 2020
42deba2
added pre-commit hooks to docs
Cielquan Jun 6, 2020
c41384e
fixed linting issues
Cielquan Jun 6, 2020
0538b80
separated pre-commit-hooks by one line
Cielquan Jun 6, 2020
7dac12d
fixed typo in cli.md
Cielquan Jun 6, 2020
66f2d2c
changed default of poetry-export hook to export to file
Cielquan Jun 6, 2020
d466aa2
renamed CLI command to poetry command in pre-commit-hooks.md
Cielquan Jun 6, 2020
100ce8b
updated docs for poetry-export hook to match new default
Cielquan Jun 6, 2020
31d5b4e
Added note for pre-commit args
Cielquan Jun 18, 2020
74a5e11
fixed linting issues in docs
Cielquan Jun 18, 2020
006d43d
docs: added example for exporting dev deps
Cielquan Nov 7, 2020
5ac36ba
Merge branch 'master' into feature/pre-commit-hooks
Cielquan May 12, 2021
a570e84
add pre-commit hook to check pre-commit-hooks file
Cielquan Jun 5, 2021
3cd355f
Merge branch 'master' into feature/pre-commit-hooks
Cielquan Jun 5, 2021
5c5ccda
update docs for new doc setup
Cielquan Jun 5, 2021
6c2db9f
remove readded mkdocs.yml
Cielquan Jun 5, 2021
fdb12c1
add yml config part to added docs file
Cielquan Jun 5, 2021
9a479f9
add 'files' constraint to pre-commit hooks
Cielquan Jun 5, 2021
c5a59a2
Merge branch 'master' into feature/pre-commit-hooks
Cielquan Nov 28, 2021
33576c6
Update docs/pre-commit-hooks.md
Cielquan Nov 29, 2021
7d1a99c
Update docs/pre-commit-hooks.md
Cielquan Nov 29, 2021
fd36526
Update docs/pre-commit-hooks.md
Cielquan Nov 29, 2021
e550c53
Update docs/pre-commit-hooks.md
Cielquan Nov 29, 2021
4b38d72
Update docs/pre-commit-hooks.md
Cielquan Nov 29, 2021
f317b2d
Update docs/cli.md
Cielquan Nov 29, 2021
bd41232
Update docs/pre-commit-hooks.md
Cielquan Nov 29, 2021
847d95b
Update docs/cli.md
Cielquan Nov 29, 2021
01696db
Update docs/cli.md
Cielquan Nov 29, 2021
e9816c3
Update docs/pre-commit-hooks.md
Cielquan Nov 29, 2021
0152439
Update docs/pre-commit-hooks.md
neersighted Nov 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ repos:
additional_dependencies:
- types-dataclasses
- types-requests

- repo: https://github.com/pre-commit/pre-commit
rev: v2.13.0
hooks:
- id: validate_manifest
26 changes: 26 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- id: poetry-check
name: poetry-check
description: run poetry check to validate config
entry: poetry check
language: python
language_version: python3
pass_filenames: false
Cielquan marked this conversation as resolved.
Show resolved Hide resolved
files: '^.*/pyproject\.toml$'

- id: poetry-lock
Cielquan marked this conversation as resolved.
Show resolved Hide resolved
name: poetry-lock
description: run poetry lock to update lock file
entry: poetry lock
language: python
language_version: python3
pass_filenames: false
Cielquan marked this conversation as resolved.
Show resolved Hide resolved

- id: poetry-export
name: poetry-export
description: run poetry export to sync lock file with requirements.txt
entry: poetry export
language: python
language_version: python3
pass_filenames: false
files: '^.*/poetry\.lock$'
args: ["-f", "requirements.txt", "-o", "requirements.txt"]
Cielquan marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ As such, `exit` should be used to properly exit the shell and the virtual enviro
The `check` command validates the structure of the `pyproject.toml` file
and returns a detailed report if there are any errors.

{{% note %}}
This command is also available as a `pre-commit` hook. See [pre-commit hooks](/docs/pre-commit-hooks#poetry-check) for more information.
Cielquan marked this conversation as resolved.
Show resolved Hide resolved
{{% /note %}}

```bash
poetry check
```
Expand All @@ -547,6 +551,7 @@ This command locks (without installing) the dependencies specified in `pyproject

{{% note %}}
By default, this will lock all dependencies to the latest available compatible versions. To only refresh the lock file, use the `--no-update` option.
This command is also available as a `pre-commit` hook. See [pre-commit hooks](/docs/pre-commit-hooks#poetry-lock) for more information.
Cielquan marked this conversation as resolved.
Show resolved Hide resolved
{{% /note %}}

```bash
Expand Down Expand Up @@ -595,6 +600,7 @@ poetry export -f requirements.txt --output requirements.txt

{{% note %}}
Only the `requirements.txt` format is currently supported.
This command is also available as a `pre-commit` hook. See [pre-commit hooks](/docs/pre-commit-hooks#poetry-export) for more information.
Cielquan marked this conversation as resolved.
Show resolved Hide resolved
{{% /note %}}

### Options
Expand Down
101 changes: 101 additions & 0 deletions docs/pre-commit-hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
title: "pre-commit hooks"
draft: false
type: docs
layout: single

menu:
docs:
weight: 120
---

# pre-commit hooks

`pre-commit` is a tool which manages running of
[git hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) scripts.
See the official documentation for more information: [pre-commit.com](https://pre-commit.com/)
Cielquan marked this conversation as resolved.
Show resolved Hide resolved

Below you can see the available `pre-commit` hooks you can use.
Cielquan marked this conversation as resolved.
Show resolved Hide resolved


{{% note %}}
If you specify the `args:` section for a hook in your pre-commit config
the default `args:` are overwritten. So if you want to add arguments
you need to specify the default ones in your config also.
Cielquan marked this conversation as resolved.
Show resolved Hide resolved
{{% /note %}}


## poetry-check

The `poetry-check` hook calls the `poetry check` command
to make sure the poetry configuration does not get committed in a broken state.

### Arguments

The hook takes the same arguments as the poetry command.
For more information see the [check command](/docs/cli#check).


## poetry-lock

The `poetry-lock` hook calls the `poetry lock` command
to make sure the lock file is up-to-date when committing changes.

### Arguments

The hook takes the same arguments as the poetry command.
For more information see the [lock command](/docs/cli#lock).


## poetry-export

The `poetry-export` hook calls the `poetry export` command
to sync your `requirements.txt` file with your current dependencies.

{{% note %}}
It is recommended to run the [`poetry-lock`](#poetry-lock) hook prior to this one.
{{% /note %}}

### Arguments

The hook takes the same arguments as the poetry command.
For more information see the [export command](/docs/cli#export).

`args: ["-f", "requirements.txt", "-o", "requirements.txt"]` are the default arguments
which will create/update the requirements.txt file in the current working directory.
Cielquan marked this conversation as resolved.
Show resolved Hide resolved

For output to the console change the arguments and add `verbose: true` to `poetry-export`
in your `.pre-commit-config.yaml` file like so:
Cielquan marked this conversation as resolved.
Show resolved Hide resolved

```yaml
hooks:
- id: poetry-export
args: ["-f", "requirements.txt"]
verbose: true
```

Or to put the `dev` dependencies into the `requirements.txt` also use this:
neersighted marked this conversation as resolved.
Show resolved Hide resolved

```yaml
hooks:
- id: poetry-export
args: ["--dev", "-f", "requirements.txt", "-o", "requirements.txt"]
```


## Usage

For how to use `pre-commit` please see the [official documentation](https://pre-commit.com/).
Cielquan marked this conversation as resolved.
Show resolved Hide resolved

Example `.pre-commit-config.yaml` file:
Cielquan marked this conversation as resolved.
Show resolved Hide resolved

```yaml
repos:
- repo: https://github.com/python-poetry/poetry
rev: '' # add version here
hooks:
- id: poetry-check
- id: poetry-lock
- id: poetry-export
args: ["-f", "requirements.txt", "-o", "requirements.txt"]
```