-
-
Notifications
You must be signed in to change notification settings - Fork 304
feat(version): add MANUAL_VERSION, --next and --patch to version comm… #1724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v4-11-0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,21 +2,31 @@ | |
| import sys | ||
| from typing import TypedDict | ||
|
|
||
| from packaging.version import InvalidVersion | ||
|
|
||
| from commitizen import out | ||
| from commitizen.__version__ import __version__ | ||
| from commitizen.config import BaseConfig | ||
| from commitizen.exceptions import NoVersionSpecifiedError, VersionSchemeUnknown | ||
| from commitizen.providers import get_provider | ||
| from commitizen.version_increment import VersionIncrement | ||
| from commitizen.version_schemes import get_version_scheme | ||
|
|
||
|
|
||
| class VersionArgs(TypedDict, total=False): | ||
| manual_version: str | None | ||
| next: str | None | ||
|
|
||
| # Exclusive groups 1 | ||
| commitizen: bool | ||
| report: bool | ||
| project: bool | ||
| verbose: bool | ||
|
|
||
| # Exclusive groups 2 | ||
| major: bool | ||
| minor: bool | ||
| patch: bool | ||
|
|
||
|
|
||
| class Version: | ||
|
|
@@ -41,24 +51,49 @@ def __call__(self) -> None: | |
| if self.arguments.get("verbose"): | ||
| out.write(f"Installed Commitizen Version: {__version__}") | ||
|
|
||
| if not self.arguments.get("commitizen") and ( | ||
| self.arguments.get("project") or self.arguments.get("verbose") | ||
| if self.arguments.get("commitizen"): | ||
| out.write(__version__) | ||
| return | ||
|
|
||
| if ( | ||
| self.arguments.get("project") | ||
| or self.arguments.get("verbose") | ||
| or self.arguments.get("next") | ||
| or self.arguments.get("manual_version") | ||
| ): | ||
| version_str = self.arguments.get("manual_version") | ||
| if version_str is None: | ||
| try: | ||
| version_str = get_provider(self.config).get_version() | ||
| except NoVersionSpecifiedError: | ||
| out.error("No project information in this project.") | ||
| return | ||
| try: | ||
| version = get_provider(self.config).get_version() | ||
| except NoVersionSpecifiedError: | ||
| out.error("No project information in this project.") | ||
| return | ||
| try: | ||
| version_scheme = get_version_scheme(self.config.settings)(version) | ||
| version_scheme = get_version_scheme(self.config.settings) | ||
| except VersionSchemeUnknown: | ||
| out.error("Unknown version scheme.") | ||
| return | ||
|
|
||
| try: | ||
| version = version_scheme(version_str) | ||
| except InvalidVersion: | ||
| out.error("Invalid version.") | ||
| return | ||
|
|
||
| if next_str := self.arguments.get("next"): | ||
| next_increment = VersionIncrement.safe_cast(next_str) | ||
| # TODO: modify the interface of bump to accept VersionIncrement | ||
| version = version.bump(increment=str(next_increment)) # type: ignore[arg-type] | ||
|
|
||
| if self.arguments.get("major"): | ||
| version = f"{version_scheme.major}" | ||
| elif self.arguments.get("minor"): | ||
| version = f"{version_scheme.minor}" | ||
| out.write(version.major) | ||
| return | ||
| if self.arguments.get("minor"): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realize now that this creates problems with the (not)monotonic kind of versions (and possible non-semver). I'm not sure what to do about it. I think for now it's fine that if you diverge too much from semver in your custom version scheme, then you won't get the full range of features. |
||
| out.write(version.minor) | ||
| return | ||
| if self.arguments.get("patch"): | ||
| out.write(version.micro) | ||
| return | ||
|
|
||
| out.write( | ||
| f"Project Version: {version}" | ||
|
|
@@ -67,11 +102,12 @@ def __call__(self) -> None: | |
| ) | ||
| return | ||
|
|
||
| if self.arguments.get("major") or self.arguments.get("minor"): | ||
| out.error( | ||
| "Major or minor version can only be used with --project or --verbose." | ||
| ) | ||
| return | ||
| for argument in ("major", "minor", "patch"): | ||
| if self.arguments.get(argument): | ||
| out.error( | ||
| f"{argument} can only be used with MANUAL_VERSION, --project or --verbose." | ||
| ) | ||
| return | ||
|
|
||
| # If no arguments are provided, just show the installed commitizen version | ||
| out.write(__version__) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from enum import IntEnum | ||
|
|
||
|
|
||
| class VersionIncrement(IntEnum): | ||
| """An enumeration representing semantic versioning increments. | ||
| This class defines the four types of version increments according to semantic versioning: | ||
| - NONE: For commits that don't require a version bump (docs, style, etc.) | ||
| - PATCH: For backwards-compatible bug fixes | ||
| - MINOR: For backwards-compatible functionality additions | ||
| - MAJOR: For incompatible API changes | ||
| """ | ||
|
|
||
| NONE = 0 | ||
| PATCH = 1 | ||
| MINOR = 2 | ||
| MAJOR = 3 | ||
|
|
||
| def __str__(self) -> str: | ||
| return self.name | ||
|
|
||
| @classmethod | ||
| def safe_cast(cls, value: object) -> "VersionIncrement": | ||
| if not isinstance(value, str): | ||
| return VersionIncrement.NONE | ||
| try: | ||
| return cls[value] | ||
| except KeyError: | ||
| return VersionIncrement.NONE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what would happen here if I run
cz version --project --next? I would expect that to work