Skip to content

Commit

Permalink
Adds short_branch_name to version rendering context
Browse files Browse the repository at this point in the history
- `short_branch_name` is the branch name, lower case, containing only a-z and 0-9, and truncated to 20 characters.

Fixes #28
  • Loading branch information
coordt committed Jun 30, 2023
1 parent 9a6bbe2 commit 7f7e50c
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bumpversion/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import os
import re
import subprocess
from dataclasses import dataclass
from pathlib import Path
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
75 changes: 75 additions & 0 deletions tests/fixtures/pep440.toml
Original file line number Diff line number Diff line change
@@ -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<release>
(?P<major>[0-9]+)
(?:
\\.(?P<minor>[0-9]+)
(?:
\\.(?P<patch>[0-9]+)
)?
)?
)
(?P<prerelease>
[-_\\.]?
(?P<pre_label>a|b|rc)
[-_\\.]?
(?P<pre_n>[0-9]+)?
)?
(?P<postrelease>
(?:
[-_\\.]?
(?P<post_label>post|rev|r)
[-_\\.]?
(?P<post_n>[0-9]+)?
)
)?
(?P<dev>
[-_\\.]?
(?P<dev_label>dev)
[-_\\.]?
(?P<dev_n>[0-9]+)?
)?
)
(?:\\+(?P<local>[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
52 changes: 52 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from textwrap import dedent

import pytest
from click.testing import CliRunner, Result
from pytest import param

from bumpversion import config
Expand Down Expand Up @@ -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"

0 comments on commit 7f7e50c

Please sign in to comment.