diff --git a/commitizen/changelog.py b/commitizen/changelog.py index 1a6ca04cc1..74cee3260a 100644 --- a/commitizen/changelog.py +++ b/commitizen/changelog.py @@ -90,10 +90,12 @@ def generate_tree_from_commits( pat = re.compile(changelog_pattern) map_pat = re.compile(commit_parser, re.MULTILINE) body_map_pat = re.compile(commit_parser, re.MULTILINE | re.DOTALL) + current_tag: Optional[GitTag] = None # Check if the latest commit is not tagged - latest_commit = commits[0] - current_tag: Optional[GitTag] = get_commit_tag(latest_commit, tags) + if commits: + latest_commit = commits[0] + current_tag = get_commit_tag(latest_commit, tags) current_tag_name: str = unreleased_version or "Unreleased" current_tag_date: str = "" diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index 4c291d3ac2..69d223ed49 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -3,6 +3,8 @@ from operator import itemgetter from typing import Callable, Dict, List, Optional +from packaging.version import parse + from commitizen import bump, changelog, defaults, factory, git, out, version_types from commitizen.config import BaseConfig from commitizen.exceptions import ( @@ -36,6 +38,13 @@ def __init__(self, config: BaseConfig, args): "changelog_incremental" ) self.dry_run = args["dry_run"] + + self.current_version = ( + args.get("current_version") or self.config.settings.get("version") or "" + ) + self.current_version_instance = ( + parse(self.current_version) if self.current_version else None + ) self.unreleased_version = args["unreleased_version"] self.change_type_map = ( self.config.settings.get("change_type_map") or self.cz.change_type_map @@ -155,7 +164,10 @@ def __call__(self): ) commits = git.get_commits(start=start_rev, end=end_rev, args="--topo-order") - if not commits: + if not commits and ( + self.current_version_instance is None + or not self.current_version_instance.is_prerelease + ): raise NoCommitsFoundError("No commits found") tree = changelog.generate_tree_from_commits( diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index bdc384c3ca..4f25ebcad5 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1,3 +1,4 @@ +import itertools import sys from datetime import datetime @@ -660,6 +661,36 @@ def test_changelog_incremental_with_release_candidate_version( file_regression.check(out, extension=".md") +@pytest.mark.parametrize( + "from_pre,to_pre", itertools.product(["alpha", "beta", "rc"], repeat=2) +) +@pytest.mark.usefixtures("tmp_commitizen_project") +@pytest.mark.freeze_time("2021-06-11") +def test_changelog_incremental_with_prerelease_version_to_prerelease_version( + mocker: MockFixture, changelog_path, file_regression, from_pre, to_pre +): + with open(changelog_path, "w") as f: + f.write(KEEP_A_CHANGELOG) + create_file_and_commit("irrelevant commit") + git.tag("1.0.0", annotated=True) + + create_file_and_commit("feat: add new output") + create_file_and_commit("fix: output glitch") + + testargs = ["cz", "bump", "--changelog", "--prerelease", from_pre, "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + testargs = ["cz", "bump", "--changelog", "--prerelease", to_pre, "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + with open(changelog_path, "r") as f: + out = f.read() + + file_regression.check(out, extension=".md") + + @pytest.mark.parametrize("test_input", ["rc", "alpha", "beta"]) @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_release_candidate_version_with_merge_prerelease( diff --git a/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_alpha_alpha_.md b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_alpha_alpha_.md new file mode 100644 index 0000000000..cfaca36ab9 --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_alpha_alpha_.md @@ -0,0 +1,32 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## 0.2.0a1 (2021-06-11) + +## 0.2.0a0 (2021-06-11) + +### Feat + +- add new output + +### Fix + +- output glitch + +## [1.0.0] - 2017-06-20 +### Added +- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8). +- Version navigation. + +### Changed +- Start using "changelog" over "change log" since it's the common usage. + +### Removed +- Section about "changelog" vs "CHANGELOG". + +## [0.3.0] - 2015-12-03 +### Added +- RU translation from [@aishek](https://github.com/aishek). diff --git a/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_alpha_beta_.md b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_alpha_beta_.md new file mode 100644 index 0000000000..013e16fc22 --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_alpha_beta_.md @@ -0,0 +1,32 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## 0.2.0b0 (2021-06-11) + +## 0.2.0a0 (2021-06-11) + +### Feat + +- add new output + +### Fix + +- output glitch + +## [1.0.0] - 2017-06-20 +### Added +- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8). +- Version navigation. + +### Changed +- Start using "changelog" over "change log" since it's the common usage. + +### Removed +- Section about "changelog" vs "CHANGELOG". + +## [0.3.0] - 2015-12-03 +### Added +- RU translation from [@aishek](https://github.com/aishek). diff --git a/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_alpha_rc_.md b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_alpha_rc_.md new file mode 100644 index 0000000000..25cc27682b --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_alpha_rc_.md @@ -0,0 +1,32 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## 0.2.0rc0 (2021-06-11) + +## 0.2.0a0 (2021-06-11) + +### Feat + +- add new output + +### Fix + +- output glitch + +## [1.0.0] - 2017-06-20 +### Added +- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8). +- Version navigation. + +### Changed +- Start using "changelog" over "change log" since it's the common usage. + +### Removed +- Section about "changelog" vs "CHANGELOG". + +## [0.3.0] - 2015-12-03 +### Added +- RU translation from [@aishek](https://github.com/aishek). diff --git a/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_beta_alpha_.md b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_beta_alpha_.md new file mode 100644 index 0000000000..300ca95756 --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_beta_alpha_.md @@ -0,0 +1,32 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## 0.2.0a0 (2021-06-11) + +## 0.2.0b0 (2021-06-11) + +### Feat + +- add new output + +### Fix + +- output glitch + +## [1.0.0] - 2017-06-20 +### Added +- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8). +- Version navigation. + +### Changed +- Start using "changelog" over "change log" since it's the common usage. + +### Removed +- Section about "changelog" vs "CHANGELOG". + +## [0.3.0] - 2015-12-03 +### Added +- RU translation from [@aishek](https://github.com/aishek). diff --git a/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_beta_beta_.md b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_beta_beta_.md new file mode 100644 index 0000000000..ec9ab11d7b --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_beta_beta_.md @@ -0,0 +1,32 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## 0.2.0b1 (2021-06-11) + +## 0.2.0b0 (2021-06-11) + +### Feat + +- add new output + +### Fix + +- output glitch + +## [1.0.0] - 2017-06-20 +### Added +- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8). +- Version navigation. + +### Changed +- Start using "changelog" over "change log" since it's the common usage. + +### Removed +- Section about "changelog" vs "CHANGELOG". + +## [0.3.0] - 2015-12-03 +### Added +- RU translation from [@aishek](https://github.com/aishek). diff --git a/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_beta_rc_.md b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_beta_rc_.md new file mode 100644 index 0000000000..22b1e14dba --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_beta_rc_.md @@ -0,0 +1,32 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## 0.2.0rc0 (2021-06-11) + +## 0.2.0b0 (2021-06-11) + +### Feat + +- add new output + +### Fix + +- output glitch + +## [1.0.0] - 2017-06-20 +### Added +- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8). +- Version navigation. + +### Changed +- Start using "changelog" over "change log" since it's the common usage. + +### Removed +- Section about "changelog" vs "CHANGELOG". + +## [0.3.0] - 2015-12-03 +### Added +- RU translation from [@aishek](https://github.com/aishek). diff --git a/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_rc_alpha_.md b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_rc_alpha_.md new file mode 100644 index 0000000000..9a7e9f161f --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_rc_alpha_.md @@ -0,0 +1,32 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## 0.2.0a0 (2021-06-11) + +## 0.2.0rc0 (2021-06-11) + +### Feat + +- add new output + +### Fix + +- output glitch + +## [1.0.0] - 2017-06-20 +### Added +- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8). +- Version navigation. + +### Changed +- Start using "changelog" over "change log" since it's the common usage. + +### Removed +- Section about "changelog" vs "CHANGELOG". + +## [0.3.0] - 2015-12-03 +### Added +- RU translation from [@aishek](https://github.com/aishek). diff --git a/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_rc_beta_.md b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_rc_beta_.md new file mode 100644 index 0000000000..b3be6f3c1d --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_rc_beta_.md @@ -0,0 +1,32 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## 0.2.0b0 (2021-06-11) + +## 0.2.0rc0 (2021-06-11) + +### Feat + +- add new output + +### Fix + +- output glitch + +## [1.0.0] - 2017-06-20 +### Added +- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8). +- Version navigation. + +### Changed +- Start using "changelog" over "change log" since it's the common usage. + +### Removed +- Section about "changelog" vs "CHANGELOG". + +## [0.3.0] - 2015-12-03 +### Added +- RU translation from [@aishek](https://github.com/aishek). diff --git a/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_rc_rc_.md b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_rc_rc_.md new file mode 100644 index 0000000000..e1c20b7e0d --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_rc_rc_.md @@ -0,0 +1,32 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## 0.2.0rc1 (2021-06-11) + +## 0.2.0rc0 (2021-06-11) + +### Feat + +- add new output + +### Fix + +- output glitch + +## [1.0.0] - 2017-06-20 +### Added +- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8). +- Version navigation. + +### Changed +- Start using "changelog" over "change log" since it's the common usage. + +### Removed +- Section about "changelog" vs "CHANGELOG". + +## [0.3.0] - 2015-12-03 +### Added +- RU translation from [@aishek](https://github.com/aishek). diff --git a/tests/test_changelog.py b/tests/test_changelog.py index 0aa9fdfbff..78a2697c36 100644 --- a/tests/test_changelog.py +++ b/tests/test_changelog.py @@ -1083,6 +1083,17 @@ def test_generate_tree_from_commits(gitcommits, tags, merge_prereleases): assert tuple(tree) == COMMITS_TREE +def test_generate_tree_from_commits_with_no_commits(tags): + gitcommits = [] + parser = defaults.commit_parser + changelog_pattern = defaults.bump_pattern + tree = changelog.generate_tree_from_commits( + gitcommits, tags, parser, changelog_pattern + ) + + assert tuple(tree) == ({"changes": {}, "date": "", "version": "Unreleased"},) + + @pytest.mark.parametrize( "change_type_order, expected_reordering", (