Skip to content

Commit

Permalink
Trigger pre-commit hooks for pyproject.toml in subfolder (#8204)
Browse files Browse the repository at this point in the history
Signed-off-by: bentocin <[email protected]>
  • Loading branch information
bentocin authored Jul 26, 2023
1 parent 38efaea commit e8ca4b3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
5 changes: 3 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
entry: poetry check
language: python
pass_filenames: false
files: ^(.*/)?pyproject.toml$
files: ^(.*/)?pyproject\.toml$

- id: poetry-lock
name: poetry-lock
description: run poetry lock to update lock file
entry: poetry lock
language: python
pass_filenames: false
files: ^(.*/)?(poetry\.lock|pyproject\.toml)$

- id: poetry-export
name: poetry-export
description: run poetry export to sync lock file with requirements.txt
entry: poetry export
language: python
pass_filenames: false
files: ^poetry.lock$
files: ^(.*/)?poetry\.lock$
args: ["-f", "requirements.txt", "-o", "requirements.txt"]
30 changes: 21 additions & 9 deletions docs/pre-commit-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ the defaults are overwritten. You must fully specify all arguments for
your hook if you make use of `args:`.
{{% /note %}}

{{% note %}}
If the `pyproject.toml` file is not in the root directory, you can specify `args: ["-C", "./subdirectory"]`.
{{% /note %}}

## poetry-check

The `poetry-check` hook calls the `poetry check` command
Expand All @@ -34,10 +38,6 @@ to make sure the poetry configuration does not get committed in a broken state.
The hook takes the same arguments as the poetry command.
For more information see the [check command]({{< relref "cli#check" >}}).

{{% note %}}
If the `pyproject.toml` file is not in the root directory, you can specify `args: ["-C", "./subdirectory"]`.
{{% /note %}}

## poetry-lock

The `poetry-lock` hook calls the `poetry lock` command
Expand All @@ -48,7 +48,6 @@ to make sure the lock file is up-to-date when committing changes.
The hook takes the same arguments as the poetry command.
For more information see the [lock command]({{< relref "cli#lock" >}}).


## poetry-export

The `poetry-export` hook calls the `poetry export` command
Expand All @@ -64,7 +63,7 @@ The hook takes the same arguments as the poetry command.
For more information see the [export command]({{< relref "cli#export" >}}).

The default arguments are `args: ["-f", "requirements.txt", "-o", "requirements.txt"]`,
which will create/update the requirements.txt file in the current working directory.
which will create/update the `requirements.txt` file in the current working directory.

You may add `verbose: true` in your `.pre-commit-config.yaml` in order to output to the
console:
Expand All @@ -84,22 +83,35 @@ hooks:
args: ["--dev", "-f", "requirements.txt", "-o", "requirements.txt"]
```


## Usage

For more information on how to use pre-commit please see the [official documentation](https://pre-commit.com/).

A full `.pre-commit-config.yaml` example:
A minimalistic `.pre-commit-config.yaml` example:

```yaml
repos:
- repo: https://github.com/python-poetry/poetry
rev: '' # add version here
hooks:
- id: poetry-check
- id: poetry-lock
- id: poetry-export
```

A `.pre-commit-config.yaml` example for a monorepo setup or if the `pyproject.toml` file is not in the root directory:

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

## FAQ
Expand Down

0 comments on commit e8ca4b3

Please sign in to comment.