Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,18 @@ def __call__(
"action": "store_true",
"exclusive_group": "group1",
},
{
"name": ["--major"],
"help": "get just the major version",
"action": "store_true",
"exclusive_group": "group2",
},
{
"name": ["--minor"],
"help": "get just the minor version",
"action": "store_true",
"exclusive_group": "group2",
},
],
},
],
Expand Down
16 changes: 15 additions & 1 deletion commitizen/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
from commitizen import out
from commitizen.__version__ import __version__
from commitizen.config import BaseConfig
from commitizen.exceptions import NoVersionSpecifiedError
from commitizen.exceptions import NoVersionSpecifiedError, VersionSchemeUnknown
from commitizen.providers import get_provider
from commitizen.version_schemes import get_version_scheme


class VersionArgs(TypedDict, total=False):
report: bool
project: bool
verbose: bool
major: bool
minor: bool


class Version:
Expand Down Expand Up @@ -41,6 +44,17 @@ def __call__(self) -> None:
out.error("No project information in this project.")
return

try:
version_scheme = get_version_scheme(self.config.settings)
except VersionSchemeUnknown:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lee-W do you think I need this try/catch here? If get_version_scheme fails, then VersionSchemeUnknown would be propagated and properly displayed by commitizen_excepthook, right?

out.error("Unknown version scheme.")
_version = version_scheme(version)

if self.parameter.get("major"):
version = f"{_version.major}"
elif self.parameter.get("minor"):
version = f"{_version.minor}"

out.write(f"Project Version: {version}" if verbose else version)
return

Expand Down
4 changes: 3 additions & 1 deletion commitizen/version_schemes.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ def _get_prerelease(self) -> str:
def get_version_scheme(settings: Settings, name: str | None = None) -> VersionScheme:
"""
Get the version scheme as defined in the configuration
or from an overridden `name`
or from an overridden `name`.



:raises VersionSchemeUnknown: if the version scheme is not found.
"""
Expand Down
16 changes: 15 additions & 1 deletion docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ If you're a first-time contributor, please check out issues labeled [good first
```bash
git remote add upstream https://github.com/commitizen-tools/commitizen.git
```
4. Set up the development environment:
4. Set up the development environment (nix users go to [nix section](#nix)):
```bash
poetry install
```
Expand Down Expand Up @@ -70,6 +70,7 @@ If you're a first-time contributor, please check out issues labeled [good first
5. **Documentation**
- Update `docs/README.md` if needed
- For CLI help screenshots: `poetry doc:screenshots`
- Prefer [Google style documentation](https://github.com/google/styleguide/blob/gh-pages/pyguide.md#38-comments-and-docstrings), which works well with editors like VSCode and PyCharm
- **DO NOT** update `CHANGELOG.md` (automatically generated)
- **DO NOT** update version numbers (automatically handled)
6. **Pull Request**
Expand Down Expand Up @@ -153,3 +154,16 @@ flowchart TD
--modification-received-->
review
```

## Nix

If you have installed poetry globally, the project won't work because it requires `poethepoet` for command management.

You'll have to install poetry locally.

```sh
python -m venv .venv
. .venv/bin/activate
pip install -U pip && pip install poetry
poetry install
```
13 changes: 12 additions & 1 deletion docs/contributing_tldr.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Please check the [pyproject.toml](https://github.com/commitizen-tools/commitizen
### Code Changes

```bash
# Ensure you have the correct dependencies
# Ensure you have the correct dependencies, for nix user's see below
poetry install

# Make ruff happy
Expand All @@ -35,3 +35,14 @@ pytest -n auto <test_suite>
# Build the documentation locally and check for broken links
poetry doc
```

### Nix Users

If you are using Nix, you can install poetry locally by running:

```sh
python -m venv .venv
. .venv/bin/activate
pip install -U pip && pip install poetry
poetry install
```
82 changes: 45 additions & 37 deletions docs/images/cli_help/cz_version___help.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions tests/commands/test_version_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,40 @@ def test_version_command_shows_description_when_use_help_option(

out, _ = capsys.readouterr()
file_regression.check(out, extension=".txt")


@pytest.mark.parametrize(
"version, expected_version", (("1.0.0", "1\n"), ("2.1.3", "2\n"), ("0.0.1", "0\n"))
)
def test_version_just_major(config, capsys, version: str, expected_version: str):
config.settings["version"] = version
commands.Version(
config,
{
"report": False,
"project": True,
"verbose": False,
"major": True,
},
)()
captured = capsys.readouterr()
assert expected_version == captured.out


@pytest.mark.parametrize(
"version, expected_version",
(("1.0.0", "0\n"), ("2.1.3", "1\n"), ("0.0.1", "0\n"), ("0.1.0", "1\n")),
)
def test_version_just_minor(config, capsys, version: str, expected_version: str):
config.settings["version"] = version
commands.Version(
config,
{
"report": False,
"project": True,
"verbose": False,
"minor": True,
},
)()
captured = capsys.readouterr()
assert expected_version == captured.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
usage: cz version [-h] [-r | -p | -c | -v]
usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor]

get the version of the installed commitizen or the current project (default:
installed commitizen)
Expand All @@ -10,3 +10,5 @@ options:
-c, --commitizen get the version of the installed commitizen
-v, --verbose get the version of both the installed commitizen and the
current project
--major get just the major version
--minor get just the minor version