Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 345
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
80732e032ef677dc843c42913d601985e542ca5b7eFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -21,6 +21,7 @@ | ||
| InvalidCommandArgumentError, | ||
| NoCommandFoundError, | ||
| ) | ||
| from commitizen.version_increment import VersionIncrement | ||
| logger = logging.getLogger(__name__) | ||
| @@ -542,13 +543,19 @@ def __call__( | ||
| }, | ||
| { | ||
| "name": ["--major"], | ||
| "help": "Output just the major version. Must be used with --project or --verbose.", | ||
| "help": ( | ||
| "Output just the major version. Must be used with MANUAL_VERSION, " | ||
| "--project, or --verbose." | ||
| ), | ||
| "action": "store_true", | ||
| "exclusive_group": "group2", | ||
| }, | ||
| { | ||
| "name": ["--minor"], | ||
| "help": "Output just the minor version. Must be used with --project or --verbose.", | ||
| "help": ( | ||
| "Output just the minor version. Must be used with MANUAL_VERSION, " | ||
| "--project, or --verbose." | ||
| ), | ||
| "action": "store_true", | ||
| "exclusive_group": "group2", | ||
| }, | ||
| @@ -558,6 +565,33 @@ def __call__( | ||
| "action": "store_true", | ||
| "exclusive_group": "group2", | ||
| }, | ||
| { | ||
| "name": ["--patch"], | ||
| "help": ( | ||
| "Output the patch version only. Must be used with MANUAL_VERSION, " | ||
| "--project, or --verbose." | ||
| ), | ||
| "action": "store_true", | ||
| "exclusive_group": "group2", | ||
| }, | ||
| { | ||
| "name": ["--next"], | ||
| "help": "Output the next version.", | ||
| "type": str, | ||
| "nargs": "?", | ||
| "default": None, | ||
| "const": "USE_GIT_COMMITS", | ||
| "choices": ["USE_GIT_COMMITS"] | ||
| + [str(increment) for increment in VersionIncrement], | ||
bearomorphism marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| "exclusive_group": "group2", | ||
| }, | ||
| { | ||
| "name": "manual_version", | ||
| "type": str, | ||
| "nargs": "?", | ||
| "help": "Use the version provided instead of the version from the project. Can be used to test the selected version scheme.", | ||
| "metavar": "MANUAL_VERSION", | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| 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,85 @@ 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": | ||
| # TODO: implement USE_GIT_COMMITS by deriving the increment from | ||
| # git history. This requires refactoring the bump logic out of | ||
| # `commitizen/commands/bump.py` so it can be reused here. See #1678. | ||
| out.error("--next USE_GIT_COMMITS is not implemented yet.") | ||
bearomorphism marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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"): | ||
bearomorphism marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| out.write(version.minor) | ||
| return | ||
| if self.arguments.get("patch"): | ||
| out.write(version.micro) | ||
| return | ||
bearomorphism marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| """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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.