From e8ca4b32ceeb1648eaba6bed49e8b36dcfbf0cc0 Mon Sep 17 00:00:00 2001 From: Nico Bentenrieder Date: Wed, 26 Jul 2023 18:36:32 +0200 Subject: [PATCH] Trigger pre-commit hooks for pyproject.toml in subfolder (#8204) Signed-off-by: bentocin --- .pre-commit-hooks.yaml | 5 +++-- docs/pre-commit-hooks.md | 30 +++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 08f733c18e8..be3bca9dbcc 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -4,7 +4,7 @@ entry: poetry check language: python pass_filenames: false - files: ^(.*/)?pyproject.toml$ + files: ^(.*/)?pyproject\.toml$ - id: poetry-lock name: poetry-lock @@ -12,6 +12,7 @@ entry: poetry lock language: python pass_filenames: false + files: ^(.*/)?(poetry\.lock|pyproject\.toml)$ - id: poetry-export name: poetry-export @@ -19,5 +20,5 @@ entry: poetry export language: python pass_filenames: false - files: ^poetry.lock$ + files: ^(.*/)?poetry\.lock$ args: ["-f", "requirements.txt", "-o", "requirements.txt"] diff --git a/docs/pre-commit-hooks.md b/docs/pre-commit-hooks.md index baff210811c..49aae8d9ccd 100644 --- a/docs/pre-commit-hooks.md +++ b/docs/pre-commit-hooks.md @@ -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 @@ -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 @@ -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 @@ -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: @@ -84,12 +83,23 @@ 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: @@ -97,9 +107,11 @@ repos: 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