Skip to content

Commit

Permalink
feat: Allow choosing whether to guess new version by bumping latest
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Feb 2, 2023
1 parent d3dca05 commit 85c04fd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
57 changes: 32 additions & 25 deletions src/git_changelog/build.py
Expand Up @@ -161,6 +161,7 @@ def __init__( # noqa: WPS231
parse_provider_refs: bool = True,
parse_trailers: bool = False,
sections: list[str] | None = None,
bump_latest: bool = True,
):
"""
Initialization method.
Expand All @@ -172,6 +173,7 @@ def __init__( # noqa: WPS231
parse_provider_refs: Whether to parse provider-specific references in the commit messages.
parse_trailers: Whether to parse Git trailers in the commit messages.
sections: The sections to render (features, bug fixes, etc.).
bump_latest: Whether to try and bump latest version to guess new one.
"""
self.repository: str = repository
self.parse_provider_refs: bool = parse_provider_refs
Expand Down Expand Up @@ -220,31 +222,9 @@ def __init__( # noqa: WPS231
self.versions_list = v_list
self.versions_dict = v_dict

# guess the next version number based on last version and recent commits
last_version = self.versions_list[0]
if not last_version.tag and last_version.previous_version:
last_tag = last_version.previous_version.tag
major = minor = False # noqa: WPS429
for commit in last_version.commits:
if commit.style["is_major"]:
major = True
break
elif commit.style["is_minor"]:
minor = True
# never fail on non-semver versions
with suppress(ValueError):
if major:
planned_tag = bump(last_tag, "major")
elif minor:
planned_tag = bump(last_tag, "minor")
else:
planned_tag = bump(last_tag, "patch")
last_version.planned_tag = planned_tag
if self.provider:
last_version.url = self.provider.get_tag_url(tag=planned_tag)
last_version.compare_url = self.provider.get_compare_url(
base=last_version.previous_version.tag, target=last_version.planned_tag
)
# try to guess the new version by bumping the latest one
if bump_latest:
self._bump_latest()

def run_git(self, *args: str) -> str:
"""Run a git command in the chosen repository.
Expand Down Expand Up @@ -387,3 +367,30 @@ def group_commits_by_version( # noqa: WPS231
base=versions_list[-1].commits[-1].hash, target=next_version.tag or "HEAD"
)
return versions_list, versions_dict

def _bump_latest(self) -> None: # noqa: WPS231
# guess the next version number based on last version and recent commits
last_version = self.versions_list[0]
if not last_version.tag and last_version.previous_version:
last_tag = last_version.previous_version.tag
major = minor = False # noqa: WPS429
for commit in last_version.commits:
if commit.style["is_major"]:
major = True
break
elif commit.style["is_minor"]:
minor = True
# never fail on non-semver versions
with suppress(ValueError):
if major:
planned_tag = bump(last_tag, "major")
elif minor:
planned_tag = bump(last_tag, "minor")
else:
planned_tag = bump(last_tag, "patch")
last_version.planned_tag = planned_tag
if self.provider:
last_version.url = self.provider.get_tag_url(tag=planned_tag)
last_version.compare_url = self.provider.get_compare_url(
base=last_version.previous_version.tag, target=last_version.planned_tag
)
10 changes: 10 additions & 0 deletions src/git_changelog/cli.py
Expand Up @@ -86,6 +86,16 @@ def get_parser() -> argparse.ArgumentParser:

parser.add_argument("repository", metavar="REPOSITORY", help="The repository path, relative or absolute.")

parser.add_argument(
"-b",
"--bump",
action="store_true",
dest="bump_latest",
default=True,
help="Guess the new latest version by bumping the previous one based on the set of unreleased commits. "
"For example, if a commit contains breaking changes, bump the major number (or the minor number for 0.x versions). "
"Else if there are new features, bump the minor number. Else just bump the patch number.",
)
parser.add_argument(
"-h", "--help", action="help", default=argparse.SUPPRESS, help="Show this help message and exit."
)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_end_to_end.py
Expand Up @@ -57,6 +57,32 @@ def git_repo(tmp_path_factory) -> Iterator[Path]:
yield tmp_path
shutil.rmtree(tmp_path)


def test_bumping_latest(repo):
"""Bump latest version.
Parameters:
repo: Path to a temporary repository.
"""
changelog = Changelog(repo, style=AngularStyle, bump_latest=True)
# features, no breaking changes: minor bumped
assert changelog.versions_list[0].planned_tag == bump(VERSIONS[-2], "minor")
rendered = KEEP_A_CHANGELOG.render(changelog=changelog)
assert "Unreleased" not in rendered


def test_not_bumping_latest(repo):
"""Don't bump latest version.
Parameters:
repo: Path to a temporary repository.
"""
changelog = Changelog(repo, style=AngularStyle, bump_latest=False)
assert changelog.versions_list[0].planned_tag is None
rendered = KEEP_A_CHANGELOG.render(changelog=changelog)
assert "Unreleased" in rendered


def test_rendering_custom_sections(repo):
"""Render custom sections.
Expand Down

0 comments on commit 85c04fd

Please sign in to comment.