-
-
Notifications
You must be signed in to change notification settings - Fork 335
feat(version): add MANUAL_VERSION, --next and --patch to version command #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: master
Are you sure you want to change the base?
Changes from all commits
4482093
54db154
5212b31
55ee3b6
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,22 +2,32 @@ | |
| 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.tags import TagRules | ||
| from commitizen.version_schemes import get_version_scheme | ||
| from commitizen.version_increment import VersionIncrement | ||
| from commitizen.version_schemes import Increment, 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 | ||
| tag: bool | ||
|
|
||
|
|
||
|
|
@@ -43,40 +53,82 @@ 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) | ||
| scheme_factory = get_version_scheme(self.config.settings) | ||
| except VersionSchemeUnknown: | ||
| out.error("Unknown version scheme.") | ||
| return | ||
|
|
||
| try: | ||
| version = scheme_factory(version_str) | ||
| except InvalidVersion: | ||
| out.error(f"Invalid version: '{version_str}'") | ||
| return | ||
|
|
||
| if next_increment_str := self.arguments.get("next"): | ||
| if next_increment_str == "USE_GIT_COMMITS": | ||
| out.error("--next USE_GIT_COMMITS is not implemented yet.") | ||
| return | ||
|
|
||
| next_increment = VersionIncrement.from_value(next_increment_str) | ||
| increment: Increment | None | ||
| if next_increment == VersionIncrement.NONE: | ||
| increment = None | ||
| elif next_increment == VersionIncrement.PATCH: | ||
| increment = "PATCH" | ||
| elif next_increment == VersionIncrement.MINOR: | ||
| increment = "MINOR" | ||
| else: | ||
| increment = "MAJOR" | ||
| version = version.bump(increment=increment) | ||
|
|
||
| if self.arguments.get("major"): | ||
| version = f"{version_scheme.major}" | ||
| elif self.arguments.get("minor"): | ||
| version = f"{version_scheme.minor}" | ||
| elif self.arguments.get("tag"): | ||
| 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 | ||
|
Comment on lines
+107
to
+110
|
||
|
|
||
| display_version: str | ||
| if self.arguments.get("tag"): | ||
| tag_rules = TagRules.from_settings(self.config.settings) | ||
| version = tag_rules.normalize_tag(version_scheme) | ||
| display_version = tag_rules.normalize_tag(version) | ||
| else: | ||
| display_version = str(version) | ||
|
|
||
| out.write( | ||
| f"Project Version: {version}" | ||
| f"Project Version: {display_version}" | ||
| if self.arguments.get("verbose") | ||
| else version | ||
| else display_version | ||
| ) | ||
| 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 self.arguments.get("tag"): | ||
| out.error("Tag can only be used with --project or --verbose.") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from enum import IntEnum | ||
|
|
||
|
|
||
| class VersionIncrement(IntEnum): | ||
bearomorphism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Semantic versioning bump increments. | ||
|
|
||
| IntEnum keeps a total order compatible with NONE < PATCH < MINOR < MAJOR | ||
| for comparisons across the codebase. | ||
|
|
||
| - NONE: no bump (docs-only / style commits, etc.) | ||
| - PATCH: backwards-compatible bug fixes | ||
| - MINOR: backwards-compatible features | ||
| - MAJOR: incompatible API changes | ||
| """ | ||
|
|
||
| NONE = 0 | ||
| PATCH = 1 | ||
| MINOR = 2 | ||
| MAJOR = 3 | ||
|
|
||
| def __str__(self) -> str: | ||
| return self.name | ||
|
|
||
| @classmethod | ||
| def from_value(cls, value: object) -> VersionIncrement: | ||
| if not isinstance(value, str): | ||
| return VersionIncrement.NONE | ||
| try: | ||
| return cls[value] | ||
| except KeyError: | ||
| return VersionIncrement.NONE | ||
Uh oh!
There was an error while loading. Please reload this page.