From 34436a6f76446ad04afefb090748159938271605 Mon Sep 17 00:00:00 2001 From: Kurt von Laven Date: Tue, 3 May 2022 19:53:03 -0700 Subject: [PATCH 1/6] ci(pre-commit): Upgrade commitizen hook Upgrade self-test hook from v1.23.0 to v2.24.0. The former version crashes with the following error: ImportError: cannot import name 'soft_unicode' from 'markupsafe' Configure Commitizen to automatically bump the version of its own hook that it uses. Use the new location of the repository in the commitizen-tools org. --- .pre-commit-config.yaml | 4 ++-- pyproject.toml | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c7b0c4a38..0107c30c3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,8 +11,8 @@ repos: - id: debug-statements - id: no-commit-to-branch - - repo: https://github.com/Woile/commitizen - rev: v1.23.0 + - repo: https://github.com/commitizen-tools/commitizen + rev: v2.24.0 # automatically updated by Commitizen hooks: - id: commitizen stages: [commit-msg] diff --git a/pyproject.toml b/pyproject.toml index 070b950cd..0bb7d8eb5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,8 @@ version = "2.24.0" tag_format = "v$version" version_files = [ "pyproject.toml:version", - "commitizen/__version__.py" + "commitizen/__version__.py", + ".pre-commit-config.yaml:rev.\\s+(?=[^\\n]+Commitizen)" ] [tool.black] From a9e5a4cbc321b6f40885b887b175690072448b3b Mon Sep 17 00:00:00 2001 From: Kurt von Laven Date: Tue, 3 May 2022 19:49:21 -0700 Subject: [PATCH 2/6] ci(pre-commit): Correct end-of-file-fixer exclude The existing regex had numerous bugs and boiled down to a very complex way of matching any file whatsoever in the tests directory. Correct the syntax issues, and make a best guess at the original intent. --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0107c30c3..71e15d287 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: hooks: - id: check-vcs-permalinks - id: end-of-file-fixer - exclude: "tests/[test_*|data|commands/tests_*]/*" + exclude: "tests/((commands|data)/|test_).+" - id: trailing-whitespace args: [--markdown-linebreak-ext=md] - id: debug-statements From df12f528b390f5e2097dda4005cbeaf6b3fe33eb Mon Sep 17 00:00:00 2001 From: Kurt von Laven Date: Tue, 3 May 2022 19:54:51 -0700 Subject: [PATCH 3/6] docs(pull_request_template): Correct typos Replace "script" with the current name of the directory, "scripts." --- .github/pull_request_template.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index d0d5eba17..14bee6b43 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -10,7 +10,7 @@ Please fill in the following content to let us know better about this change. ## Checklist - [ ] Add test cases to all the changes you introduce -- [ ] Run `./script/format` and `./script/test` locally to ensure this change passes linter check and test +- [ ] Run `./scripts/format` and `./scripts/test` locally to ensure this change passes linter check and test - [ ] Test the changes on the local machine manually - [ ] Update the documentation for the changes From 186de51919a4bdff44e6c069b16d38806fc57006 Mon Sep 17 00:00:00 2001 From: Kurt von Laven Date: Tue, 3 May 2022 16:04:58 -0700 Subject: [PATCH 4/6] docs(changelog): Clarify incremental_build comment The original explanation has some minor grammatical errors. --- commitizen/changelog.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commitizen/changelog.py b/commitizen/changelog.py index 0738c2e28..647f149db 100644 --- a/commitizen/changelog.py +++ b/commitizen/changelog.py @@ -238,8 +238,8 @@ def get_metadata(filepath: str) -> Dict: def incremental_build(new_content: str, lines: List, metadata: Dict) -> List: """Takes the original lines and updates with new_content. - The metadata holds information enough to remove the old unreleased and - where to place the new content + The metadata governs how to remove the old unreleased section and where to place the + new content. Args: lines: The lines from the changelog From f8c617eaa8418be61ec10928a70c26e63ff31c8f Mon Sep 17 00:00:00 2001 From: Kurt von Laven Date: Tue, 3 May 2022 16:06:44 -0700 Subject: [PATCH 5/6] refactor(changelog): Simplify incremental_build - Specify what output_lines and lines are lists of, namely strings. - Remove unnecessary test of whether latest_version_position is an int since x != None for any integer x. - Replace two consecutive list.append calls with a single list.extend call. --- commitizen/changelog.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/commitizen/changelog.py b/commitizen/changelog.py index 647f149db..a35b62dca 100644 --- a/commitizen/changelog.py +++ b/commitizen/changelog.py @@ -235,7 +235,7 @@ def get_metadata(filepath: str) -> Dict: } -def incremental_build(new_content: str, lines: List, metadata: Dict) -> List: +def incremental_build(new_content: str, lines: List[str], metadata: Dict) -> List[str]: """Takes the original lines and updates with new_content. The metadata governs how to remove the old unreleased section and where to place the @@ -253,7 +253,7 @@ def incremental_build(new_content: str, lines: List, metadata: Dict) -> List: unreleased_end = metadata.get("unreleased_end") latest_version_position = metadata.get("latest_version_position") skip = False - output_lines: List = [] + output_lines: List[str] = [] for index, line in enumerate(lines): if index == unreleased_start: skip = True @@ -270,13 +270,8 @@ def incremental_build(new_content: str, lines: List, metadata: Dict) -> List: if skip: continue - if ( - isinstance(latest_version_position, int) - and index == latest_version_position - ): - - output_lines.append(new_content) - output_lines.append("\n") + if index == latest_version_position: + output_lines.extend([new_content, "\n"]) output_lines.append(line) if not isinstance(latest_version_position, int): From 7b69599ce72807a0535e52413bd4d5a7abf71b55 Mon Sep 17 00:00:00 2001 From: Kurt von Laven Date: Tue, 3 May 2022 18:07:43 -0700 Subject: [PATCH 6/6] feat(changelog): Improve whitespace in changelog Ensure there is at least one blank line separating old and new content. We already separate the log entries for each release with blank lines, so separate pre-existing content from the first release with a blank line as well. If the pre-existing content ends in a blank line, then don't add an extra one. --- commitizen/changelog.py | 3 +++ tests/commands/test_changelog_command.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/commitizen/changelog.py b/commitizen/changelog.py index a35b62dca..5e43e2fef 100644 --- a/commitizen/changelog.py +++ b/commitizen/changelog.py @@ -275,6 +275,9 @@ def incremental_build(new_content: str, lines: List[str], metadata: Dict) -> Lis output_lines.append(line) if not isinstance(latest_version_position, int): + if output_lines and output_lines[-1].strip(): + # Ensure at least one blank line between existing and new content. + output_lines.append("\n") output_lines.append(new_content) return output_lines diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 222bf8dd6..3f90800e7 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -320,6 +320,30 @@ def test_changelog_multiple_incremental_do_not_add_new_lines( assert out.startswith("#") +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_changelog_incremental_newline_separates_new_content_from_old( + mocker, changelog_path +): + """Test for https://github.com/commitizen-tools/commitizen/issues/509""" + with open(changelog_path, "w") as f: + f.write("Pre-existing content that should be kept\n") + + create_file_and_commit("feat: add more cat videos") + + testargs = ["cz", "changelog", "--incremental"] + + mocker.patch.object(sys, "argv", testargs) + cli.main() + + with open(changelog_path, "r") as f: + out = f.read() + + assert ( + out + == "Pre-existing content that should be kept\n\n## Unreleased\n\n### Feat\n\n- add more cat videos\n" + ) + + def test_changelog_without_revision(mocker, tmp_commitizen_project): changelog_file = tmp_commitizen_project.join("CHANGELOG.md") changelog_file.write(