diff --git a/bumpversion/scm.py b/bumpversion/scm.py index 8b324fab..ba1d5a5f 100644 --- a/bumpversion/scm.py +++ b/bumpversion/scm.py @@ -2,6 +2,7 @@ import logging import os +import re import subprocess from dataclasses import dataclass from pathlib import Path @@ -25,6 +26,7 @@ class SCMInfo: distance_to_latest_tag: Optional[int] = None current_version: Optional[str] = None branch_name: Optional[str] = None + short_branch_name: Optional[str] = None dirty: Optional[bool] = None def __str__(self): @@ -245,11 +247,12 @@ def latest_tag_info(cls, tag_pattern: str) -> SCMInfo: git_cmd = ["git", "rev-parse", "--abbrev-ref", "HEAD"] result = subprocess.run(git_cmd, text=True, check=True, capture_output=True) # noqa: S603 branch_name = result.stdout.strip() + short_branch_name = re.sub(r"([^a-zA-Z0-9]*)", "", branch_name).lower()[:20] except subprocess.CalledProcessError as e: logger.debug("Error when running git describe: %s", e.stderr) return SCMInfo(tool=cls) - info = SCMInfo(tool=cls, branch_name=branch_name) + info = SCMInfo(tool=cls, branch_name=branch_name, short_branch_name=short_branch_name) if describe_out[-1].strip() == "dirty": info.dirty = True diff --git a/tests/fixtures/pep440.toml b/tests/fixtures/pep440.toml new file mode 100644 index 00000000..214faa64 --- /dev/null +++ b/tests/fixtures/pep440.toml @@ -0,0 +1,75 @@ +[tool.bumpversion] +allow_dirty = false +commit = false +message = "Bump version: {current_version} → {new_version}" +commit_args = "" +tag = false +sign_tags = false +tag_name = "v{new_version}" +tag_message = "Bump version: {current_version} → {new_version}" +current_version = "1.0.0" +parse = """(?x) +(?: + (?P + (?P[0-9]+) + (?: + \\.(?P[0-9]+) + (?: + \\.(?P[0-9]+) + )? + )? + ) + (?P + [-_\\.]? + (?Pa|b|rc) + [-_\\.]? + (?P[0-9]+)? + )? + (?P + (?: + [-_\\.]? + (?Ppost|rev|r) + [-_\\.]? + (?P[0-9]+)? + ) + )? + (?P + [-_\\.]? + (?Pdev) + [-_\\.]? + (?P[0-9]+)? + )? +) +(?:\\+(?P[a-z0-9]+(?:[-_\\.][a-z0-9]+)*))? +""" +serialize = [ + "{major}.{minor}.{patch}.{dev_label}{distance_to_latest_tag}+{short_branch_name}", +# "{major}.{minor}.{patch}{pre_label}{pre_n}", +# "{major}.{minor}.{patch}+{branch_name}", + "{major}.{minor}.{patch}", +] +search = "{current_version}" +replace = "{new_version}" + +[tool.bumpversion.parts.pre_label] +values = ["final", "a", "b", "rc"] + +[tool.bumpversion.parts.pre_n] +first_value = 1 + +[tool.bumpversion.parts.post_label] +values = ["final", "post"] + +[tool.bumpversion.parts.post_n] +first_value = 1 + + +[tool.bumpversion.parts.dev_label] +values = ["final", "dev"] +independent = true + +[tool.bumpversion.parts.dev_n] +first_value = 1 + +[tool.bumpversion.parts.local] +independent = true diff --git a/tests/test_config.py b/tests/test_config.py index 78bde117..950d0c78 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -5,6 +5,7 @@ from textwrap import dedent import pytest +from click.testing import CliRunner, Result from pytest import param from bumpversion import config @@ -194,3 +195,54 @@ def test_update_config_file(tmp_path: Path, cfg_file_name: str, expected_diff: s new_content = cfg_path.read_text().splitlines(keepends=True) difference = difflib.context_diff(original_content, new_content, n=0) assert "".join(difference) == expected_diff + + +def test_pep440_config(git_repo: Path, fixtures_path: Path): + """ + Check the PEP440 config file. + """ + from bumpversion.utils import get_context + from bumpversion.bump import get_next_version + from bumpversion import cli + import subprocess + + # Arrange + + cfg_path = git_repo / "pyproject.toml" + orig_path = fixtures_path / "pep440.toml" + cfg_path.write_text(orig_path.read_text()) + version_path = git_repo / "VERSION" + version_path.write_text("1.0.0") + readme_path = git_repo / "README.md" + runner: CliRunner = CliRunner() + + with inside_dir(git_repo): + subprocess.run(["git", "add", "VERSION"], check=True, capture_output=True) + subprocess.run(["git", "commit", "-m", "initial commit"], check=True, capture_output=True) + subprocess.run(["git", "tag", "v1.0.0"], check=True, capture_output=True) + + cfg = config.get_configuration(cfg_path) + ctx = get_context(cfg) + version = cfg.version_config.parse(cfg.current_version) + next_version = get_next_version(version, cfg, "patch", None) + next_version_str = cfg.version_config.serialize(next_version, ctx) + assert next_version_str == "1.0.1" + + subprocess.run(["git", "checkout", "-b", "my-really-LONG-branch_name"], check=True, capture_output=True) + readme_path.write_text("This is my branch!") + result: Result = runner.invoke(cli.cli, ["bump", "dev_label", "--no-tag"]) + assert result.exit_code == 0 + cfg = config.get_configuration(cfg_path) + assert cfg.current_version == "1.0.0.dev0+myreallylongbranchna" + + # try: + # subprocess.run(["git", "add", "README.md"], check=True, capture_output=True) + # subprocess.run(["git", "commit", "-am", "my branch commit"], check=True, capture_output=True) + # except subprocess.CalledProcessError as e: + # print(e.stdout) + # print(e.stderr) + # raise + # result: Result = runner.invoke(cli.cli, ["bump", "dev_label", "--no-tag"]) + # assert result.exit_code == 0 + # cfg = config.get_configuration(cfg_path) + # assert cfg.current_version == "1.0.0.dev1+myreallylongbranchna"