Skip to content

Generic script/command version provider #1788

@benediktziegler

Description

@benediktziegler

Description

Projects using version handlers like hatch, which by themselves can have a configurable version handling, it would be great to have a generic CommandProvider.
The commitizen settings should just define which commands to run for getting and setting the version.

Possible Solution

Configure the provider via

[tool.commitizen]
...
version_provider = "command-provider"
provider_get_version = "hatch version"  # example for hatch
provider_set_version = "hatch version {version}"  # example for hatch

Simplified implementation:

from commitizen import cmd
from commitizen.providers import base_provider


class CommandProvider(base_provider.VersionProvider):
    """
    Base class for command-based version providers

    The commitizen configuration needs to specify the `provider_get_version` and `provider_set_version` script.
    The `provider_set_script` command must contain a `{version}` placeholder for the new version.
    """
    def get_version(self) -> str:
        script = self.config.settings.get("provider_get_script")
        if script is None:
            msg = "The 'provider_get_script' setting is missing in the commitizen configuration"
            raise KeyError(msg)
        result = cmd.run(script)
        return result.out.strip()

    def set_version(self, version: str) -> None:
        script = self.config.settings.get("provider_set_script")
        if script is None:
            msg = "The 'provider_set_script' setting is missing in the commitizen configuration"
            raise KeyError(msg)
        cmd.run(script.format(version=version))

Additional context

No response

Related issues

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions