Skip to content

Commit 89122d0

Browse files
fix: bump changelog for prerelease without commits
generate changelog for a new version without new commits when the current version is a prerelease. This matches current behaviour for bump command
1 parent 996bff8 commit 89122d0

13 files changed

+341
-3
lines changed

commitizen/changelog.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ def generate_tree_from_commits(
9090
pat = re.compile(changelog_pattern)
9191
map_pat = re.compile(commit_parser, re.MULTILINE)
9292
body_map_pat = re.compile(commit_parser, re.MULTILINE | re.DOTALL)
93+
current_tag: Optional[GitTag] = None
9394

9495
# Check if the latest commit is not tagged
95-
latest_commit = commits[0]
96-
current_tag: Optional[GitTag] = get_commit_tag(latest_commit, tags)
96+
if commits:
97+
latest_commit = commits[0]
98+
current_tag = get_commit_tag(latest_commit, tags)
9799

98100
current_tag_name: str = unreleased_version or "Unreleased"
99101
current_tag_date: str = ""

commitizen/commands/changelog.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from operator import itemgetter
44
from typing import Callable, Dict, List, Optional
55

6+
from packaging.version import parse
7+
68
from commitizen import bump, changelog, defaults, factory, git, out, version_types
79
from commitizen.config import BaseConfig
810
from commitizen.exceptions import (
@@ -36,6 +38,11 @@ def __init__(self, config: BaseConfig, args):
3638
"changelog_incremental"
3739
)
3840
self.dry_run = args["dry_run"]
41+
42+
self.current_version = (
43+
args.get("current_version") or self.config.settings.get("version") or ""
44+
)
45+
self.current_version_instance = parse(self.current_version) if self.current_version else None
3946
self.unreleased_version = args["unreleased_version"]
4047
self.change_type_map = (
4148
self.config.settings.get("change_type_map") or self.cz.change_type_map
@@ -155,7 +162,7 @@ def __call__(self):
155162
)
156163

157164
commits = git.get_commits(start=start_rev, end=end_rev, args="--topo-order")
158-
if not commits:
165+
if not commits and (self.current_version_instance is None or not self.current_version_instance.is_prerelease):
159166
raise NoCommitsFoundError("No commits found")
160167

161168
tree = changelog.generate_tree_from_commits(

tests/commands/test_changelog_command.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools
12
import sys
23
from datetime import datetime
34

@@ -659,6 +660,35 @@ def test_changelog_incremental_with_release_candidate_version(
659660

660661
file_regression.check(out, extension=".md")
661662

663+
@pytest.mark.parametrize(
664+
"from_pre,to_pre", itertools.product(["alpha", "beta", "rc"], repeat=2)
665+
)
666+
@pytest.mark.usefixtures("tmp_commitizen_project")
667+
@pytest.mark.freeze_time("2021-06-11")
668+
def test_changelog_incremental_with_prerelease_version_to_prerelease_version(
669+
mocker: MockFixture, changelog_path, file_regression, from_pre, to_pre
670+
):
671+
with open(changelog_path, "w") as f:
672+
f.write(KEEP_A_CHANGELOG)
673+
create_file_and_commit("irrelevant commit")
674+
git.tag("1.0.0", annotated=True)
675+
676+
create_file_and_commit("feat: add new output")
677+
create_file_and_commit("fix: output glitch")
678+
679+
testargs = ["cz", "bump", "--changelog", "--prerelease", from_pre, "--yes"]
680+
mocker.patch.object(sys, "argv", testargs)
681+
cli.main()
682+
683+
testargs = ["cz", "bump", "--changelog", "--prerelease", to_pre, "--yes"]
684+
mocker.patch.object(sys, "argv", testargs)
685+
cli.main()
686+
687+
with open(changelog_path, "r") as f:
688+
out = f.read()
689+
690+
file_regression.check(out, extension=".md")
691+
662692

663693
@pytest.mark.parametrize("test_input", ["rc", "alpha", "beta"])
664694
@pytest.mark.usefixtures("tmp_commitizen_project")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## 0.2.0a1 (2021-06-11)
8+
9+
## 0.2.0a0 (2021-06-11)
10+
11+
### Feat
12+
13+
- add new output
14+
15+
### Fix
16+
17+
- output glitch
18+
19+
## [1.0.0] - 2017-06-20
20+
### Added
21+
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
22+
- Version navigation.
23+
24+
### Changed
25+
- Start using "changelog" over "change log" since it's the common usage.
26+
27+
### Removed
28+
- Section about "changelog" vs "CHANGELOG".
29+
30+
## [0.3.0] - 2015-12-03
31+
### Added
32+
- RU translation from [@aishek](https://github.com/aishek).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## 0.2.0b0 (2021-06-11)
8+
9+
## 0.2.0a0 (2021-06-11)
10+
11+
### Feat
12+
13+
- add new output
14+
15+
### Fix
16+
17+
- output glitch
18+
19+
## [1.0.0] - 2017-06-20
20+
### Added
21+
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
22+
- Version navigation.
23+
24+
### Changed
25+
- Start using "changelog" over "change log" since it's the common usage.
26+
27+
### Removed
28+
- Section about "changelog" vs "CHANGELOG".
29+
30+
## [0.3.0] - 2015-12-03
31+
### Added
32+
- RU translation from [@aishek](https://github.com/aishek).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## 0.2.0rc0 (2021-06-11)
8+
9+
## 0.2.0a0 (2021-06-11)
10+
11+
### Feat
12+
13+
- add new output
14+
15+
### Fix
16+
17+
- output glitch
18+
19+
## [1.0.0] - 2017-06-20
20+
### Added
21+
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
22+
- Version navigation.
23+
24+
### Changed
25+
- Start using "changelog" over "change log" since it's the common usage.
26+
27+
### Removed
28+
- Section about "changelog" vs "CHANGELOG".
29+
30+
## [0.3.0] - 2015-12-03
31+
### Added
32+
- RU translation from [@aishek](https://github.com/aishek).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## 0.2.0a0 (2021-06-11)
8+
9+
## 0.2.0b0 (2021-06-11)
10+
11+
### Feat
12+
13+
- add new output
14+
15+
### Fix
16+
17+
- output glitch
18+
19+
## [1.0.0] - 2017-06-20
20+
### Added
21+
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
22+
- Version navigation.
23+
24+
### Changed
25+
- Start using "changelog" over "change log" since it's the common usage.
26+
27+
### Removed
28+
- Section about "changelog" vs "CHANGELOG".
29+
30+
## [0.3.0] - 2015-12-03
31+
### Added
32+
- RU translation from [@aishek](https://github.com/aishek).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## 0.2.0b1 (2021-06-11)
8+
9+
## 0.2.0b0 (2021-06-11)
10+
11+
### Feat
12+
13+
- add new output
14+
15+
### Fix
16+
17+
- output glitch
18+
19+
## [1.0.0] - 2017-06-20
20+
### Added
21+
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
22+
- Version navigation.
23+
24+
### Changed
25+
- Start using "changelog" over "change log" since it's the common usage.
26+
27+
### Removed
28+
- Section about "changelog" vs "CHANGELOG".
29+
30+
## [0.3.0] - 2015-12-03
31+
### Added
32+
- RU translation from [@aishek](https://github.com/aishek).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## 0.2.0rc0 (2021-06-11)
8+
9+
## 0.2.0b0 (2021-06-11)
10+
11+
### Feat
12+
13+
- add new output
14+
15+
### Fix
16+
17+
- output glitch
18+
19+
## [1.0.0] - 2017-06-20
20+
### Added
21+
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
22+
- Version navigation.
23+
24+
### Changed
25+
- Start using "changelog" over "change log" since it's the common usage.
26+
27+
### Removed
28+
- Section about "changelog" vs "CHANGELOG".
29+
30+
## [0.3.0] - 2015-12-03
31+
### Added
32+
- RU translation from [@aishek](https://github.com/aishek).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## 0.2.0a0 (2021-06-11)
8+
9+
## 0.2.0rc0 (2021-06-11)
10+
11+
### Feat
12+
13+
- add new output
14+
15+
### Fix
16+
17+
- output glitch
18+
19+
## [1.0.0] - 2017-06-20
20+
### Added
21+
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
22+
- Version navigation.
23+
24+
### Changed
25+
- Start using "changelog" over "change log" since it's the common usage.
26+
27+
### Removed
28+
- Section about "changelog" vs "CHANGELOG".
29+
30+
## [0.3.0] - 2015-12-03
31+
### Added
32+
- RU translation from [@aishek](https://github.com/aishek).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## 0.2.0b0 (2021-06-11)
8+
9+
## 0.2.0rc0 (2021-06-11)
10+
11+
### Feat
12+
13+
- add new output
14+
15+
### Fix
16+
17+
- output glitch
18+
19+
## [1.0.0] - 2017-06-20
20+
### Added
21+
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
22+
- Version navigation.
23+
24+
### Changed
25+
- Start using "changelog" over "change log" since it's the common usage.
26+
27+
### Removed
28+
- Section about "changelog" vs "CHANGELOG".
29+
30+
## [0.3.0] - 2015-12-03
31+
### Added
32+
- RU translation from [@aishek](https://github.com/aishek).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## 0.2.0rc1 (2021-06-11)
8+
9+
## 0.2.0rc0 (2021-06-11)
10+
11+
### Feat
12+
13+
- add new output
14+
15+
### Fix
16+
17+
- output glitch
18+
19+
## [1.0.0] - 2017-06-20
20+
### Added
21+
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
22+
- Version navigation.
23+
24+
### Changed
25+
- Start using "changelog" over "change log" since it's the common usage.
26+
27+
### Removed
28+
- Section about "changelog" vs "CHANGELOG".
29+
30+
## [0.3.0] - 2015-12-03
31+
### Added
32+
- RU translation from [@aishek](https://github.com/aishek).

0 commit comments

Comments
 (0)