Skip to content

Commit 943cbd2

Browse files
fix(changelog): add incremental parameter to changelog generation (#1808)
Relate #1620
1 parent c9d8ece commit 943cbd2

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

commitizen/commands/changelog.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ def __call__(self) -> None:
266266
self.cz.template_loader,
267267
self.template,
268268
**{
269+
"incremental": self.incremental, # extra variable for the template
269270
**self.cz.template_extras,
270271
**self.config.settings["extras"],
271272
**self.extras,

tests/commands/test_changelog_command.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,3 +1672,57 @@ class FakeTemplate:
16721672

16731673
assert not changelog_jinja_file.exists()
16741674
assert "Template filename is not set" in str(exc_info.value)
1675+
1676+
1677+
def test_changelog_template_incremental_variable(
1678+
tmp_commitizen_project: Path,
1679+
any_changelog_format: ChangelogFormat,
1680+
util: UtilFixture,
1681+
file_regression: FileRegressionFixture,
1682+
):
1683+
"""
1684+
Test that the changelog template is not rendered when the incremental flag is not set.
1685+
Reference: https://github.com/commitizen-tools/commitizen/discussions/1620
1686+
"""
1687+
project_root = Path(tmp_commitizen_project)
1688+
changelog_tpl = project_root / any_changelog_format.template
1689+
changelog_tpl.write_text(
1690+
dedent("""
1691+
{% if not incremental %}
1692+
# CHANGELOG
1693+
{% endif %}
1694+
1695+
{% for entry in tree %}
1696+
1697+
## {{ entry.version }}{% if entry.date %} ({{ entry.date }}){% endif %}
1698+
1699+
{% for change_key, changes in entry.changes.items() %}
1700+
1701+
{% if change_key %}
1702+
### {{ change_key }}
1703+
{% endif %}
1704+
1705+
{% for change in changes %}
1706+
{% if change.scope %}
1707+
- **{{ change.scope }}**: {{ change.message }}
1708+
{% elif change.message %}
1709+
- {{ change.message }}
1710+
{% endif %}
1711+
{% endfor %}
1712+
{% endfor %}
1713+
{% endfor %}
1714+
""")
1715+
)
1716+
target = "CHANGELOG.md"
1717+
1718+
util.create_file_and_commit("feat(foo): new file")
1719+
util.run_cli("changelog", "--file-name", target)
1720+
with open(target, encoding="utf-8") as f:
1721+
out = f.read()
1722+
file_regression.check(out, extension=".md")
1723+
1724+
util.create_file_and_commit("refactor(bar): another new file")
1725+
util.run_cli("changelog", "--file-name", target, "--incremental")
1726+
with open(target, encoding="utf-8") as f:
1727+
out = f.read()
1728+
file_regression.check(out, extension=".incremental.md")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# CHANGELOG
2+
3+
4+
## Unreleased
5+
6+
### Feat
7+
8+
- **foo**: new file
9+
10+
### Refactor
11+
12+
- **bar**: another new file
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# CHANGELOG
2+
3+
4+
## Unreleased
5+
6+
### Feat
7+
8+
- **foo**: new file

0 commit comments

Comments
 (0)