Skip to content

Commit 37a3b62

Browse files
committed
fix: Fix bumping initial version
This change also changes the behavior back to having the latest version be "Unreleased" if the bump option wasn't used. Issue-82: #82
1 parent 38ef35b commit 37a3b62

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

src/git_changelog/build.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,6 @@ def __init__(
295295
if bump:
296296
self._bump(bump)
297297

298-
# fix a single, initial version to the user specified version or 0.1.0 if none is specified
299-
self._fix_single_version(bump)
300-
301298
def run_git(self, *args: str) -> str:
302299
"""Run a git command in the chosen repository.
303300
@@ -480,8 +477,11 @@ def _assign_previous_versions(self, versions_dict: dict[str, Version], previous_
480477

481478
def _bump(self, version: str) -> None:
482479
last_version = self.versions_list[0]
483-
if not last_version.tag and last_version.previous_version:
484-
last_tag = last_version.previous_version.tag
480+
if not last_version.tag:
481+
if last_version.previous_version:
482+
last_tag = last_version.previous_version.tag
483+
else:
484+
last_tag = self.version_bumper.initial
485485
version, *plus = version.split("+")
486486
if version == "auto":
487487
# guess the next version number based on last version and recent commits
@@ -507,14 +507,8 @@ def _bump(self, version: str) -> None:
507507
if self.provider:
508508
last_version.url = self.provider.get_tag_url(tag=last_version.planned_tag)
509509
last_version.compare_url = self.provider.get_compare_url(
510-
base=last_version.previous_version.tag,
510+
base=last_version.previous_version.tag
511+
if last_version.previous_version
512+
else last_version.commits[-1].hash,
511513
target=last_version.planned_tag,
512514
)
513-
514-
def _fix_single_version(self, version: str | None) -> None:
515-
last_version = self.versions_list[0]
516-
if len(self.versions_list) == 1 and last_version.planned_tag is None and not last_version.tag:
517-
planned_tag = version if version and version not in {"auto", "major", "minor", "patch"} else "0.1.0"
518-
last_version.tag = planned_tag
519-
last_version.url += planned_tag
520-
last_version.compare_url = last_version.compare_url.replace("HEAD", planned_tag)

src/git_changelog/versioning.py

+6
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,8 @@ def parse_pep440(version: str) -> tuple[PEP440Version, str]:
645645
class VersionBumper:
646646
"""Base class for version bumpers."""
647647

648+
initial: str
649+
648650
def __init__(self, strategies: tuple[str, ...]) -> None:
649651
"""Initialize the bumper.
650652
@@ -670,6 +672,8 @@ def __call__(self, version: str, strategy: str = ..., **kwargs: Any) -> str:
670672
class PEP440Bumper(VersionBumper):
671673
"""PEP 440 version bumper."""
672674

675+
initial: str = "0.0.0"
676+
673677
def __call__( # type: ignore[override]
674678
self,
675679
version: str,
@@ -744,6 +748,8 @@ def __call__( # type: ignore[override]
744748
class SemVerBumper(VersionBumper):
745749
"""SemVer version bumper."""
746750

751+
initial: str = "0.0.0"
752+
747753
def __call__( # type: ignore[override]
748754
self,
749755
version: str,

tests/test_build.py

+25-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from time import sleep
6-
from typing import TYPE_CHECKING
6+
from typing import TYPE_CHECKING, Literal
77

88
import pytest
99

@@ -16,20 +16,36 @@
1616

1717

1818
@pytest.mark.parametrize(
19-
("bump", "expected"),
20-
[("auto", "0.1.0"), ("major", "0.1.0"), ("minor", "0.1.0"), ("1.1.1", "1.1.1")],
19+
("versioning", "bump", "expected"),
20+
[
21+
("semver", "auto", "0.0.1"), # chore commit
22+
("semver", "major", "1.0.0"),
23+
("semver", "minor", "0.1.0"),
24+
("semver", "patch", "0.0.1"),
25+
("semver", "1.1.1", "1.1.1"),
26+
("pep440", "auto", "0.0.1"), # chore commit
27+
("pep440", "major", "1.0.0"),
28+
("pep440", "minor+dev", "0.1.0.dev0"),
29+
("pep440", "micro+alpha+dev", "0.0.1a0.dev0"),
30+
("pep440", "1.1.1", "1.1.1"),
31+
],
2132
)
22-
def test_bump_with_semver_on_new_repo(repo: GitRepo, bump: str, expected: str) -> None:
23-
"""Bump to user specified version (SemVer) on new git repo.
33+
def test_bump_with_semver_on_new_repo(
34+
repo: GitRepo,
35+
versioning: Literal["pep440", "semver"],
36+
bump: str,
37+
expected: str,
38+
) -> None:
39+
"""Bump to user specified version on new Git repo.
2440
2541
Parameters:
2642
repo: GitRepo to a temporary repository.
2743
bump: The bump parameter value.
2844
expected: Expected version for the new changelog entry.
2945
"""
30-
changelog = Changelog(repo.path, convention=AngularConvention, bump=bump)
46+
changelog = Changelog(repo.path, convention=AngularConvention, bump=bump, versioning=versioning, zerover=False)
3147
assert len(changelog.versions_list) == 1
32-
assert changelog.versions_list[0].tag == expected
48+
assert changelog.versions_list[0].planned_tag == expected
3349

3450

3551
@pytest.mark.parametrize("bump", ["auto", "major", "minor", "2.0.0"])
@@ -189,7 +205,7 @@ def _assert_version(
189205
expected_prev_tag: str | None,
190206
expected_commits: list[str],
191207
) -> None:
192-
assert version.tag == expected_tag
208+
assert expected_tag in (version.tag, version.planned_tag)
193209
if expected_prev_tag:
194210
assert version.previous_version is not None, f"Expected previous version '{expected_prev_tag}', but was None"
195211
assert version.previous_version.tag == expected_prev_tag
@@ -252,7 +268,7 @@ def test_merge_into_unreleased(repo: GitRepo) -> None:
252268
version = changelog.versions_list[0]
253269
_assert_version(
254270
version,
255-
expected_tag="0.1.0",
271+
expected_tag="",
256272
expected_prev_tag=None,
257273
expected_commits=[commit_e, commit_c, commit_d, commit_a, commit_b],
258274
)

0 commit comments

Comments
 (0)