Skip to content

Commit 7f7e50c

Browse files
committed
Adds short_branch_name to version rendering context
- `short_branch_name` is the branch name, lower case, containing only a-z and 0-9, and truncated to 20 characters. Fixes #28
1 parent 9a6bbe2 commit 7f7e50c

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

bumpversion/scm.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
import os
5+
import re
56
import subprocess
67
from dataclasses import dataclass
78
from pathlib import Path
@@ -25,6 +26,7 @@ class SCMInfo:
2526
distance_to_latest_tag: Optional[int] = None
2627
current_version: Optional[str] = None
2728
branch_name: Optional[str] = None
29+
short_branch_name: Optional[str] = None
2830
dirty: Optional[bool] = None
2931

3032
def __str__(self):
@@ -245,11 +247,12 @@ def latest_tag_info(cls, tag_pattern: str) -> SCMInfo:
245247
git_cmd = ["git", "rev-parse", "--abbrev-ref", "HEAD"]
246248
result = subprocess.run(git_cmd, text=True, check=True, capture_output=True) # noqa: S603
247249
branch_name = result.stdout.strip()
250+
short_branch_name = re.sub(r"([^a-zA-Z0-9]*)", "", branch_name).lower()[:20]
248251
except subprocess.CalledProcessError as e:
249252
logger.debug("Error when running git describe: %s", e.stderr)
250253
return SCMInfo(tool=cls)
251254

252-
info = SCMInfo(tool=cls, branch_name=branch_name)
255+
info = SCMInfo(tool=cls, branch_name=branch_name, short_branch_name=short_branch_name)
253256

254257
if describe_out[-1].strip() == "dirty":
255258
info.dirty = True

tests/fixtures/pep440.toml

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[tool.bumpversion]
2+
allow_dirty = false
3+
commit = false
4+
message = "Bump version: {current_version} → {new_version}"
5+
commit_args = ""
6+
tag = false
7+
sign_tags = false
8+
tag_name = "v{new_version}"
9+
tag_message = "Bump version: {current_version} → {new_version}"
10+
current_version = "1.0.0"
11+
parse = """(?x)
12+
(?:
13+
(?P<release>
14+
(?P<major>[0-9]+)
15+
(?:
16+
\\.(?P<minor>[0-9]+)
17+
(?:
18+
\\.(?P<patch>[0-9]+)
19+
)?
20+
)?
21+
)
22+
(?P<prerelease>
23+
[-_\\.]?
24+
(?P<pre_label>a|b|rc)
25+
[-_\\.]?
26+
(?P<pre_n>[0-9]+)?
27+
)?
28+
(?P<postrelease>
29+
(?:
30+
[-_\\.]?
31+
(?P<post_label>post|rev|r)
32+
[-_\\.]?
33+
(?P<post_n>[0-9]+)?
34+
)
35+
)?
36+
(?P<dev>
37+
[-_\\.]?
38+
(?P<dev_label>dev)
39+
[-_\\.]?
40+
(?P<dev_n>[0-9]+)?
41+
)?
42+
)
43+
(?:\\+(?P<local>[a-z0-9]+(?:[-_\\.][a-z0-9]+)*))?
44+
"""
45+
serialize = [
46+
"{major}.{minor}.{patch}.{dev_label}{distance_to_latest_tag}+{short_branch_name}",
47+
# "{major}.{minor}.{patch}{pre_label}{pre_n}",
48+
# "{major}.{minor}.{patch}+{branch_name}",
49+
"{major}.{minor}.{patch}",
50+
]
51+
search = "{current_version}"
52+
replace = "{new_version}"
53+
54+
[tool.bumpversion.parts.pre_label]
55+
values = ["final", "a", "b", "rc"]
56+
57+
[tool.bumpversion.parts.pre_n]
58+
first_value = 1
59+
60+
[tool.bumpversion.parts.post_label]
61+
values = ["final", "post"]
62+
63+
[tool.bumpversion.parts.post_n]
64+
first_value = 1
65+
66+
67+
[tool.bumpversion.parts.dev_label]
68+
values = ["final", "dev"]
69+
independent = true
70+
71+
[tool.bumpversion.parts.dev_n]
72+
first_value = 1
73+
74+
[tool.bumpversion.parts.local]
75+
independent = true

tests/test_config.py

+52
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from textwrap import dedent
66

77
import pytest
8+
from click.testing import CliRunner, Result
89
from pytest import param
910

1011
from bumpversion import config
@@ -194,3 +195,54 @@ def test_update_config_file(tmp_path: Path, cfg_file_name: str, expected_diff: s
194195
new_content = cfg_path.read_text().splitlines(keepends=True)
195196
difference = difflib.context_diff(original_content, new_content, n=0)
196197
assert "".join(difference) == expected_diff
198+
199+
200+
def test_pep440_config(git_repo: Path, fixtures_path: Path):
201+
"""
202+
Check the PEP440 config file.
203+
"""
204+
from bumpversion.utils import get_context
205+
from bumpversion.bump import get_next_version
206+
from bumpversion import cli
207+
import subprocess
208+
209+
# Arrange
210+
211+
cfg_path = git_repo / "pyproject.toml"
212+
orig_path = fixtures_path / "pep440.toml"
213+
cfg_path.write_text(orig_path.read_text())
214+
version_path = git_repo / "VERSION"
215+
version_path.write_text("1.0.0")
216+
readme_path = git_repo / "README.md"
217+
runner: CliRunner = CliRunner()
218+
219+
with inside_dir(git_repo):
220+
subprocess.run(["git", "add", "VERSION"], check=True, capture_output=True)
221+
subprocess.run(["git", "commit", "-m", "initial commit"], check=True, capture_output=True)
222+
subprocess.run(["git", "tag", "v1.0.0"], check=True, capture_output=True)
223+
224+
cfg = config.get_configuration(cfg_path)
225+
ctx = get_context(cfg)
226+
version = cfg.version_config.parse(cfg.current_version)
227+
next_version = get_next_version(version, cfg, "patch", None)
228+
next_version_str = cfg.version_config.serialize(next_version, ctx)
229+
assert next_version_str == "1.0.1"
230+
231+
subprocess.run(["git", "checkout", "-b", "my-really-LONG-branch_name"], check=True, capture_output=True)
232+
readme_path.write_text("This is my branch!")
233+
result: Result = runner.invoke(cli.cli, ["bump", "dev_label", "--no-tag"])
234+
assert result.exit_code == 0
235+
cfg = config.get_configuration(cfg_path)
236+
assert cfg.current_version == "1.0.0.dev0+myreallylongbranchna"
237+
238+
# try:
239+
# subprocess.run(["git", "add", "README.md"], check=True, capture_output=True)
240+
# subprocess.run(["git", "commit", "-am", "my branch commit"], check=True, capture_output=True)
241+
# except subprocess.CalledProcessError as e:
242+
# print(e.stdout)
243+
# print(e.stderr)
244+
# raise
245+
# result: Result = runner.invoke(cli.cli, ["bump", "dev_label", "--no-tag"])
246+
# assert result.exit_code == 0
247+
# cfg = config.get_configuration(cfg_path)
248+
# assert cfg.current_version == "1.0.0.dev1+myreallylongbranchna"

0 commit comments

Comments
 (0)