Skip to content

Commit b328f6a

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 fb0d1eb commit b328f6a

13 files changed

+342
-3
lines changed

commitizen/changelog.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ def generate_tree_from_commits(
5555
pat = re.compile(changelog_pattern)
5656
map_pat = re.compile(commit_parser, re.MULTILINE)
5757
body_map_pat = re.compile(commit_parser, re.MULTILINE | re.DOTALL)
58+
current_tag: Optional[GitTag] = None
5859

5960
# Check if the latest commit is not tagged
60-
latest_commit = commits[0]
61-
current_tag: Optional[GitTag] = get_commit_tag(latest_commit, tags)
61+
if commits:
62+
latest_commit = commits[0]
63+
current_tag = get_commit_tag(latest_commit, tags)
6264

6365
current_tag_name: str = unreleased_version or "Unreleased"
6466
current_tag_date: str = ""

commitizen/commands/changelog.py

+8-1
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
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)
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
@@ -147,7 +154,7 @@ def __call__(self):
147154
commits = git.get_commits(
148155
start=start_rev, end=end_rev, args="--author-date-order"
149156
)
150-
if not commits:
157+
if not commits and not self.current_version_instance.is_prerelease:
151158
raise NoCommitsFoundError("No commits found")
152159

153160
tree = changelog.generate_tree_from_commits(

tests/commands/test_changelog_command.py

+31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools
12
import sys
23
from datetime import datetime
34

@@ -549,6 +550,36 @@ def test_changelog_incremental_with_release_candidate_version(
549550
file_regression.check(out, extension=".md")
550551

551552

553+
@pytest.mark.parametrize(
554+
"from_pre,to_pre", itertools.product(["alpha", "beta", "rc"], repeat=2)
555+
)
556+
@pytest.mark.usefixtures("tmp_commitizen_project")
557+
@pytest.mark.freeze_time("2021-06-11")
558+
def test_changelog_incremental_with_prerelease_version_to_prerelease_version(
559+
mocker: MockFixture, changelog_path, file_regression, from_pre, to_pre
560+
):
561+
with open(changelog_path, "w") as f:
562+
f.write(KEEP_A_CHANGELOG)
563+
create_file_and_commit("irrelevant commit")
564+
git.tag("1.0.0", annotated=True)
565+
566+
create_file_and_commit("feat: add new output")
567+
create_file_and_commit("fix: output glitch")
568+
569+
testargs = ["cz", "bump", "--changelog", "--prerelease", from_pre, "--yes"]
570+
mocker.patch.object(sys, "argv", testargs)
571+
cli.main()
572+
573+
testargs = ["cz", "bump", "--changelog", "--prerelease", to_pre, "--yes"]
574+
mocker.patch.object(sys, "argv", testargs)
575+
cli.main()
576+
577+
with open(changelog_path, "r") as f:
578+
out = f.read()
579+
580+
file_regression.check(out, extension=".md")
581+
582+
552583
@pytest.mark.usefixtures("tmp_commitizen_project")
553584
def test_changelog_with_filename_as_empty_string(
554585
mocker: MockFixture, changelog_path, config_path
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)