Commit 4fa3cbf8 authored by Vojko Pribudić's avatar Vojko Pribudić
Browse files

v0.8.0

parent 1ff2c1bf
Loading
Loading
Loading
Loading
+19 −2
Original line number Diff line number Diff line
@@ -84,9 +84,16 @@ $ pre-commit-update [OPTIONS]
```console
  Option:                                       Short description:

  -c, --config-file FILE                        Path to the configuration pyproject.toml
                                                file  [default: None]
  
  -d, --dry-run / -nd, --no-dry-run             Checks for the new versions without updating
                                                if enabled  [default: nd]
                                                
  -dm, --dry-run-mode [strict|warn]             Dry run mode: 'strict' prints the changes
                                                and exits, 'warn' only prints the changes
                                                [default: strict]

  -a, --all-versions / -na, --no-all-versions   Includes the alpha/beta versions when
                                                updating if enabled  [default: na]

@@ -130,10 +137,19 @@ Example of the `REPO_URL_TRIM`: `https://github.com/ambv/black` -> `black` (you
`--exclude`, `--keep` or `--bleeding-edge` option)


- `-c, --config-file FILE` - Path to the configuration pyproject.toml file. This can be an absolute or relative path. Make
sure that the path ends with `pyproject.toml`.


- `-d, --dry-run / -nd, --no-dry-run` - If enabled, updates will be fetched, but they won't be applied. Useful if you are only
curious about potential changes that would occur.


- `-dm, --dry-run-mode [strict|warn]` - If set to `strict`, the changes will be printed and the process will exit. If set to
`warn`, the changes will be printed but the process will not exit. This option is ignored if the `--dry-run` option is not
enabled!


- `-a, --all-versions / -na, --no-all-versions` - If enabled, all version tags will be checked. This may include alpha, beta
or release candidate versions. Keep in mind that this option is not the same as `-b, --bleeding-edge`!

@@ -224,7 +240,7 @@ You can also use `pre-commit-update` as a hook in your `pre-commit` hooks:

```yaml
- repo: https://gitlab.com/vojko.pribudic.foss/pre-commit-update
  rev: v0.6.1  # Insert the latest tag here
  rev: v0.8.0  # Insert the latest tag here
  hooks:
    - id: pre-commit-update
      # Example args (use your own configuration)
@@ -242,6 +258,7 @@ Example:
```toml
[tool.pre-commit-update]
dry_run = true
dry_run_mode = "strict"
all_versions = false
verbose = true
warnings = true
@@ -298,7 +315,7 @@ To do so, just add the following in your `.pre-commit-config.yaml` file:

```yaml
- repo: https://gitlab.com/vojko.pribudic.foss/pre-commit-update
  rev: v0.6.0  # Version that works for you
  rev: v0.7.0  # Version that works for you
  hooks:
    - id: pre-commit-update
      args: [--keep, pre-commit-update]
+141 −117

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
[project]
name = "pre-commit-update"
version = "0.7.0"
version = "0.8.0"
description = "Simple CLI tool and a pre-commit hook to check and update pre-commit hooks."
authors = [
    {name = "Vojko Pribudić", email = "dmanthing@gmail.com"}
+60 −7
Original line number Diff line number Diff line
import os
import sys
from typing import Any, Optional
from typing import Any, Literal, Optional

import click

@@ -22,6 +22,21 @@ from .utils import (
)


def __get_config_file(config_file: Optional[str]) -> Optional[str]:
    if not config_file:
        return None

    if not config_file.endswith("pyproject.toml"):
        raise click.ClickException(
            "The configuration file must be a pyproject.toml file"
        )

    if config_file == "pyproject.toml":
        config_file = None

    return config_file


def __preview(
    *, defaults: dict, toml_params: dict, cmd_params: dict, final_params: dict
) -> None:
@@ -49,7 +64,9 @@ def __preview(

def __run(
    *,
    config_file: Optional[str],
    dry_run: bool,
    dry_run_mode: Optional[Literal["strict", "warn"]] = None,
    all_versions: bool,
    verbose: bool,
    warnings: bool,
@@ -66,11 +83,14 @@ def __run(
    try:
        message_manager: MessageManager = MessageManager()
        yaml_manager: YAMLManager = YAMLManager(
            os.path.join(os.getcwd(), ".pre-commit-config.yaml")
            os.path.join(os.getcwd(), ".pre-commit-config.yaml"),
            config_file,
        )
        option_manager: OptionManager = OptionManager(
            warnings,
            dry_run,
            dry_run_mode,
            all_versions,
            warnings,
            exclude,
            keep,
            bleeding_edge,
@@ -110,7 +130,15 @@ def __run(
            message_manager.output_messages(message_manager.update)

            if dry_run:
                raise click.ClickException(get_color("Changes detected", "red"))
                dry_run_message: str = get_color("Changes detected", "red")
                if (
                    not option_manager.dry_run_mode
                    or option_manager.dry_run_mode == "strict"
                ):
                    raise click.ClickException(dry_run_message)

                click.echo(dry_run_message)
                return

            yaml_manager.data["repos"] = repo_manager.repos_data
            yaml_manager.dump()
@@ -128,6 +156,15 @@ def __run(


@click.command(context_settings=dict(help_option_names=["-h", "--help"]))
@click.option(
    "-c",
    "--config-file",
    type=click.Path(exists=True, dir_okay=False),
    required=False,
    show_default=True,
    default=None,
    help="Path to the configuration pyproject.toml file",
)
@click.option(
    "-d/-nd",
    "--dry-run/--no-dry-run",
@@ -136,6 +173,14 @@ def __run(
    default=False,
    help="Checks for the new versions without updating if enabled",
)
@click.option(
    "-dm",
    "--dry-run-mode",
    type=click.Choice(["strict", "warn"], case_sensitive=False),
    show_default=True,
    default="strict",
    help="Dry run mode: 'strict' prints the changes and exits, 'warn' only prints the changes",
)
@click.option(
    "-a/-na",
    "--all-versions/--no-all-versions",
@@ -212,10 +257,18 @@ def __run(
@click.pass_context
def cli(ctx: click.Context, **_: Any) -> None:
    defaults: dict = {p.name: p.default for p in ctx.command.params}
    toml_params: dict = get_toml_config(defaults)
    toml_params.pop("yaml", None)

    if not defaults.get("dry_run"):
        defaults.pop("dry_run_mode", None)

    cmd_params: dict = get_passed_params(ctx)
    final_params: dict = {**toml_params, **cmd_params}
    config_file: Optional[str] = __get_config_file(
        cmd_params.get("config_file") or defaults["config_file"]
    )
    toml_params: dict = get_toml_config(defaults, toml_path=config_file)
    toml_params.pop("config_file", None)
    toml_params.pop("yaml", None)
    final_params: dict = {**toml_params, **cmd_params, "config_file": config_file}

    if final_params.pop("preview", False):
        __preview(
+26 −2
Original line number Diff line number Diff line
from collections import Counter
from typing import Literal, Optional

from ..utils import get_converted_iterable
from . import MessageManager
@@ -7,22 +8,30 @@ from . import MessageManager
class OptionManager:
    def __init__(
        self,
        warnings: bool,
        dry_run: bool,
        dry_run_mode: Optional[Literal["strict", "warn"]],
        all_versions: bool,
        warnings: bool,
        exclude: tuple,
        keep: tuple,
        bleeding_edge: tuple,
        tag_prefix: tuple[tuple[str, str]],
        repo_trims: set,
    ) -> None:
        self.__warnings: bool = warnings
        self.__dry_run: bool = dry_run
        self.__dry_run_mode: Optional[Literal["strict", "warn"]] = dry_run_mode
        self.__all_versions: bool = all_versions
        self.__warnings: bool = warnings
        self.__exclude: list = list(sorted(exclude))
        self.__keep: list = list(sorted(keep))
        self.__bleeding_edge: list = list(sorted(bleeding_edge))
        self.__tag_prefix: list[list[str]] = get_converted_iterable(sorted(tag_prefix, key=lambda x: x[0]), list)  # type: ignore
        self.__repo_trims: set = repo_trims

    @property
    def dry_run_mode(self) -> Optional[Literal["strict", "warn"]]:
        return self.__dry_run_mode

    @property
    def exclude(self) -> tuple:
        return tuple(self.__exclude)
@@ -55,6 +64,20 @@ class OptionManager:
        for index in sorted(indexes[1:] if preserve_first else indexes, reverse=True):
            del lst[index]

    def __validate_dry_run(self, message_manager: MessageManager) -> None:
        if not self.__dry_run and self.__dry_run_mode:
            message_manager.add_warning_message(
                "*", "--dry-run-mode option ignored (--dry-run not enabled)"
            )
            self.__dry_run_mode = None
            return
        if self.dry_run_mode and self.__dry_run_mode not in ("strict", "warn"):
            message_manager.add_warning_message(
                "*",
                f"--dry-run-mode option ignored ({self.__dry_run_mode} - not supported)",
            )
            self.__dry_run_mode = None

    def __validate_invalid_repo_trims(self, message_manager: MessageManager) -> None:
        lists_to_update: tuple = (
            self.__exclude,
@@ -161,6 +184,7 @@ class OptionManager:
                )

    def validate(self, message_manager: MessageManager) -> None:
        self.__validate_dry_run(message_manager)
        self.__validate_invalid_repo_trims(message_manager)
        self.__validate_duplicate_repo_trims(message_manager)
        self.__validate_exclusive_repo_trims(message_manager)
Loading