Skip to content

Commit dd4364e

Browse files
authored
Merge pull request #511 from Kurt-von-Laven/changelog
Ensure At Least One Blank Line Between Old and New Changelog Content
2 parents 354f9ea + 7b69599 commit dd4364e

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

.github/pull_request_template.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Please fill in the following content to let us know better about this change.
1010
## Checklist
1111

1212
- [ ] Add test cases to all the changes you introduce
13-
- [ ] Run `./script/format` and `./script/test` locally to ensure this change passes linter check and test
13+
- [ ] Run `./scripts/format` and `./scripts/test` locally to ensure this change passes linter check and test
1414
- [ ] Test the changes on the local machine manually
1515
- [ ] Update the documentation for the changes
1616

.pre-commit-config.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ repos:
55
hooks:
66
- id: check-vcs-permalinks
77
- id: end-of-file-fixer
8-
exclude: "tests/[test_*|data|commands/tests_*]/*"
8+
exclude: "tests/((commands|data)/|test_).+"
99
- id: trailing-whitespace
1010
args: [--markdown-linebreak-ext=md]
1111
- id: debug-statements
1212
- id: no-commit-to-branch
1313

14-
- repo: https://github.com/Woile/commitizen
15-
rev: v1.23.0
14+
- repo: https://github.com/commitizen-tools/commitizen
15+
rev: v2.24.0 # automatically updated by Commitizen
1616
hooks:
1717
- id: commitizen
1818
stages: [commit-msg]

commitizen/changelog.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,11 @@ def get_metadata(filepath: str) -> Dict:
235235
}
236236

237237

238-
def incremental_build(new_content: str, lines: List, metadata: Dict) -> List:
238+
def incremental_build(new_content: str, lines: List[str], metadata: Dict) -> List[str]:
239239
"""Takes the original lines and updates with new_content.
240240
241-
The metadata holds information enough to remove the old unreleased and
242-
where to place the new content
241+
The metadata governs how to remove the old unreleased section and where to place the
242+
new content.
243243
244244
Args:
245245
lines: The lines from the changelog
@@ -253,7 +253,7 @@ def incremental_build(new_content: str, lines: List, metadata: Dict) -> List:
253253
unreleased_end = metadata.get("unreleased_end")
254254
latest_version_position = metadata.get("latest_version_position")
255255
skip = False
256-
output_lines: List = []
256+
output_lines: List[str] = []
257257
for index, line in enumerate(lines):
258258
if index == unreleased_start:
259259
skip = True
@@ -270,16 +270,14 @@ def incremental_build(new_content: str, lines: List, metadata: Dict) -> List:
270270
if skip:
271271
continue
272272

273-
if (
274-
isinstance(latest_version_position, int)
275-
and index == latest_version_position
276-
):
277-
278-
output_lines.append(new_content)
279-
output_lines.append("\n")
273+
if index == latest_version_position:
274+
output_lines.extend([new_content, "\n"])
280275

281276
output_lines.append(line)
282277
if not isinstance(latest_version_position, int):
278+
if output_lines and output_lines[-1].strip():
279+
# Ensure at least one blank line between existing and new content.
280+
output_lines.append("\n")
283281
output_lines.append(new_content)
284282
return output_lines
285283

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ version = "2.24.0"
33
tag_format = "v$version"
44
version_files = [
55
"pyproject.toml:version",
6-
"commitizen/__version__.py"
6+
"commitizen/__version__.py",
7+
".pre-commit-config.yaml:rev.\\s+(?=[^\\n]+Commitizen)"
78
]
89

910
[tool.black]

tests/commands/test_changelog_command.py

+24
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,30 @@ def test_changelog_multiple_incremental_do_not_add_new_lines(
320320
assert out.startswith("#")
321321

322322

323+
@pytest.mark.usefixtures("tmp_commitizen_project")
324+
def test_changelog_incremental_newline_separates_new_content_from_old(
325+
mocker, changelog_path
326+
):
327+
"""Test for https://github.com/commitizen-tools/commitizen/issues/509"""
328+
with open(changelog_path, "w") as f:
329+
f.write("Pre-existing content that should be kept\n")
330+
331+
create_file_and_commit("feat: add more cat videos")
332+
333+
testargs = ["cz", "changelog", "--incremental"]
334+
335+
mocker.patch.object(sys, "argv", testargs)
336+
cli.main()
337+
338+
with open(changelog_path, "r") as f:
339+
out = f.read()
340+
341+
assert (
342+
out
343+
== "Pre-existing content that should be kept\n\n## Unreleased\n\n### Feat\n\n- add more cat videos\n"
344+
)
345+
346+
323347
def test_changelog_without_revision(mocker, tmp_commitizen_project):
324348
changelog_file = tmp_commitizen_project.join("CHANGELOG.md")
325349
changelog_file.write(

0 commit comments

Comments
 (0)