Skip to content

Commit a18a2e4

Browse files
authored
Merge pull request #681 from eduardocardoso/fix-changelog-rc-bump
fix: bump changelog for prerelease without commits
2 parents 073dd45 + ca725f2 commit a18a2e4

13 files changed

+347
-3
lines changed

commitizen/changelog.py

+4-2
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

+13-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, version_types
79
from commitizen.config import BaseConfig
810
from commitizen.exceptions import (
@@ -36,6 +38,13 @@ 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 = (
46+
parse(self.current_version) if self.current_version else None
47+
)
3948
self.unreleased_version = args["unreleased_version"]
4049
self.change_type_map = (
4150
self.config.settings.get("change_type_map") or self.cz.change_type_map
@@ -155,7 +164,10 @@ def __call__(self):
155164
)
156165

157166
commits = git.get_commits(start=start_rev, end=end_rev, args="--topo-order")
158-
if not commits:
167+
if not commits and (
168+
self.current_version_instance is None
169+
or not self.current_version_instance.is_prerelease
170+
):
159171
raise NoCommitsFoundError("No commits found")
160172

161173
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

@@ -660,6 +661,36 @@ def test_changelog_incremental_with_release_candidate_version(
660661
file_regression.check(out, extension=".md")
661662

662663

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