Home

Awesome

sync-pre-commit-lock

Tests pre-commit.ci status OpenSSF Best Practices pypi version License Python version pdm-managed Ruff

PDM and Poetry plugin to sync your pre-commit versions with your lockfile and automatically install pre-commit hooks.

Sample output

Features

Supported versions

Installation

For PDM

Install it just like any other PDM plugin:

pdm self add "sync-pre-commit-lock"

Optionally, you can also specify the plugin in your project pyproject.toml, to make it installable with pdm install --plugins:

[tool.pdm]
plugins = [
    "sync-pre-commit-lock"
]

Note: we have an extra group [pdm], that adds PDM version constraints. While it's safer, it might result in PDM being installed twice.

For Poetry

Install like any other Poetry plugin, e.g.:

poetry self add "sync-pre-commit-lock[poetry]"

Only Poetry 1.6.0+ is supported.

Configuration

This plugin is configured using the tool.sync-pre-commit-lock section in your pyproject.toml file.

Here is the default configuration:

[tool.sync-pre-commit-lock]
# Run `pre-commit install` automatically if applicable
automatically-install-hooks = true
# Should we sync your pre-commit versions with your lockfile (when running lock, add, update, remove, etc.)?
disable-sync-from-lock = false
# Packages to ignore when syncing from lock
ignore = []
# Name of the pre-commit config file to sync with
pre-commit-config-file = ".pre-commit-config.yaml"
# Additional mapping of URLs to python packages
# Default is empty, but will merge with the default mapping
# "rev" indicates the format of the Git tags
dependency-mapping = {"package-name"= {"repo"= "https://github.com/example/package-name", "rev"= "v${rev}"}}

Note: the dependency-mapping is merged with the default mapping, so you don't need to specify the default mapping if you want to add a new mapping. Repos urls will be normalized to http(s), with the trailing slash removed.

From environment

Some settings are overridable by environment variables with the following SYNC_PRE_COMMIT_LOCK_* prefixed environment variables:

toml settingenvironmentformat
automatically-install-hooksSYNC_PRE_COMMIT_LOCK_INSTALLbool as string (true, 1...)
disable-sync-from-lockSYNC_PRE_COMMIT_LOCK_DISABLEDbool as string (true, 1...)
ignoreSYNC_PRE_COMMIT_LOCK_IGNOREcomma-separated list
pre-commit-config-fileSYNC_PRE_COMMIT_LOCK_PRE_COMMIT_FILEstr

Usage

Once installed, and optionally configured, the plugin usage should be transparent, and trigger when you run applicable PDM or Poetry commands, like pdm lock, or poetry lock.

There should be a message in the output, when the sync or install or pre-commit is triggered.

You can manually trigger the sync with the CLI command:

pdm sync-pre-commit

or

poetry sync-pre-commit

Both commands support --dry-run and verbosity options.

PDM Github Action support

If you use pdm-project/update-deps-actions Github Action, you can get automatically update your .pre-commit-config.yaml file by adding the plugin in your pyproject.toml and setting a flag in your workflow:

# In your workflow
      - name: Update dependencies
        uses: pdm-project/update-deps-action@main
        with:
          # Whether to install PDM plugins before update (defaults to "false")
          install-plugins: "true"
# In your pyproject.toml
[tool.pdm]
plugins = ["sync-pre-commit-lock"]

Supported packages for pre-commits

Here is the list of default packages supported by this plugin, from db.py. You can add more packages using the dependency-mapping configuration.

<!-- GENERATED-PACKAGES-LIST --> <!-- @generated by scripts/db_md.py --> <!-- END-GENERATED-PACKAGES-LIST -->

Note: pdm or poetry version will be added, from the current instance version, and not from the lockfile.

Improvement ideas

Feel free to open an issue or a PR if you have any idea, or if you want to help!

Release / CI / DX

Features or fixes

Inspiration

This project is inspired by @floatingpurr's sync_with_pdm and sync_with_poetry.

The code to install pre-commit hooks automatically is adapted from @vstrimaitis's poetry-pre-commit-plugin, licensed under GPL-3.