-
-
Notifications
You must be signed in to change notification settings - Fork 329
Feature/ add a tag(--body-length-limit) for command commit #1849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
aa15fda
1f74bb2
faacc87
071e450
005b422
f514bdd
3dc5471
f17a654
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |||||||||||||||||||||||||||
| import shutil | ||||||||||||||||||||||||||||
| import subprocess | ||||||||||||||||||||||||||||
| import tempfile | ||||||||||||||||||||||||||||
| import textwrap | ||||||||||||||||||||||||||||
| from typing import TYPE_CHECKING, TypedDict | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import questionary | ||||||||||||||||||||||||||||
|
|
@@ -37,6 +38,7 @@ class CommitArgs(TypedDict, total=False): | |||||||||||||||||||||||||||
| edit: bool | ||||||||||||||||||||||||||||
| extra_cli_args: str | ||||||||||||||||||||||||||||
| message_length_limit: int | ||||||||||||||||||||||||||||
| body_length_limit: int | ||||||||||||||||||||||||||||
| no_retry: bool | ||||||||||||||||||||||||||||
| signoff: bool | ||||||||||||||||||||||||||||
| write_message_to_file: Path | None | ||||||||||||||||||||||||||||
|
|
@@ -84,6 +86,7 @@ def _get_message_by_prompt_commit_questions(self) -> str: | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| message = self.cz.message(answers) | ||||||||||||||||||||||||||||
| self._validate_subject_length(message) | ||||||||||||||||||||||||||||
| message = self._rewrap_body(message) | ||||||||||||||||||||||||||||
| return message | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _validate_subject_length(self, message: str) -> None: | ||||||||||||||||||||||||||||
|
|
@@ -102,6 +105,31 @@ def _validate_subject_length(self, message: str) -> None: | |||||||||||||||||||||||||||
| f"Length of commit message exceeds limit ({len(subject)}/{message_length_limit}), subject: '{subject}'" | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _rewrap_body(self, message: str) -> str: | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
| body_length_limit = self.arguments.get( | ||||||||||||||||||||||||||||
| "body_length_limit", self.config.settings.get("body_length_limit", 0) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| # By the contract, body_length_limit is set to 0 for no limit | ||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||
| body_length_limit is None or body_length_limit <= 0 | ||||||||||||||||||||||||||||
| ): # do nothing for no limit | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
| if ( | |
| body_length_limit is None or body_length_limit <= 0 | |
| ): # do nothing for no limit | |
| if ( | |
| body_length_limit <= 0 | |
| ): # do nothing for no limit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question: I understand that the value is guaranteed, but according to defensive programming, isn’t it better to handle the null value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure whether body_length_limit is guaranteed to be an int. If it is None under some code paths, then something might be wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I probably would try not body_length_limit or body_length_limit < 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I found that if I don’t handle None, it can’t commit due to a Null error.
yjaw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should use wrap instead of fill
Reference: https://docs.python.org/3/library/textwrap.html#textwrap.wrap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also we should prefer using list comphrehension
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is wrap used instead of fill in this context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because we want to wrap instead of fill?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # First line is subject, second is blank line, rest is body | |
| subject = message_parts[0] | |
| blank_line = message_parts[1] | |
| body = message_parts[2].strip() | |
| body_lines = body.split("\n") | |
| wrapped_body_lines = [] | |
| for line in body_lines: | |
| wrapped_body_lines.append(textwrap.fill(line, width=body_length_limit)) | |
| wrapped_body = "\n".join(wrapped_body_lines) | |
| return f"{subject}\n{blank_line}\n{wrapped_body}" | |
| # First line is subject, second is blank line, rest is body | |
| wrapped_body_lines = [textwrap.fill(line, width=body_length_limit) for line in lines[2:]] | |
| return "\n".join(chain(lines[:2], wrapped_body_lines)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, this code is so clean! I Learned a lot.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -365,3 +365,230 @@ def test_commit_command_with_config_message_length_limit( | |||||
| success_mock.reset_mock() | ||||||
| commands.Commit(config, {"message_length_limit": 0})() | ||||||
| success_mock.assert_called_once() | ||||||
|
|
||||||
|
|
||||||
| @pytest.mark.usefixtures("staging_is_clean") | ||||||
| def test_commit_command_with_body_length_limit_wrapping( | ||||||
|
||||||
| config, success_mock: MockType, mocker: MockFixture | ||||||
| ): | ||||||
| """Test that long body lines are automatically wrapped to the specified limit.""" | ||||||
| mocker.patch( | ||||||
| "questionary.prompt", | ||||||
| return_value={ | ||||||
| "prefix": "feat", | ||||||
| "subject": "add feature", | ||||||
| "scope": "", | ||||||
| "is_breaking_change": False, | ||||||
| "body": "This is a very long line that exceeds 72 characters and should be automatically wrapped by the system to fit within the limit", | ||||||
| "footer": "", | ||||||
| }, | ||||||
|
Comment on lines
+411
to
+418
|
||||||
| ) | ||||||
|
|
||||||
| commit_mock = mocker.patch( | ||||||
| "commitizen.git.commit", return_value=cmd.Command("success", "", b"", b"", 0) | ||||||
| ) | ||||||
|
||||||
|
|
||||||
| # Execute with body_length_limit | ||||||
| commands.Commit(config, {"body_length_limit": 72})() | ||||||
|
||||||
| success_mock.assert_called_once() | ||||||
|
|
||||||
| # Verify wrapping occurred | ||||||
| committed_message = commit_mock.call_args[0][0] | ||||||
| lines = committed_message.split("\n") | ||||||
| assert lines[0] == "feat: add feature" | ||||||
| assert lines[1] == "" | ||||||
| body_lines = lines[2:] | ||||||
| for line in body_lines: | ||||||
| if line.strip(): | ||||||
|
||||||
| assert len(line) <= 72, ( | ||||||
| f"Line exceeds 72 chars: '{line}' ({len(line)} chars)" | ||||||
| ) | ||||||
|
|
||||||
|
||||||
|
|
||||||
| @pytest.mark.usefixtures("staging_is_clean") | ||||||
| def test_commit_command_with_body_length_limit_preserves_line_breaks( | ||||||
| config, success_mock: MockType, mocker: MockFixture | ||||||
| ): | ||||||
| """Test that intentional line breaks (from | character) are preserved.""" | ||||||
| # Simulate what happens after multiple_line_breaker processes "line1 | line2 | line3" | ||||||
| mocker.patch( | ||||||
| "questionary.prompt", | ||||||
| return_value={ | ||||||
| "prefix": "feat", | ||||||
| "subject": "add feature", | ||||||
| "scope": "", | ||||||
| "is_breaking_change": False, | ||||||
| "body": "Line1 that is very long and exceeds the limit\nLine2 that is very long and exceeds the limit\nLine3 that is very long and exceeds the limit", | ||||||
| "footer": "", | ||||||
| }, | ||||||
| ) | ||||||
|
|
||||||
| commit_mock = mocker.patch( | ||||||
| "commitizen.git.commit", return_value=cmd.Command("success", "", b"", b"", 0) | ||||||
| ) | ||||||
|
|
||||||
| commands.Commit(config, {"body_length_limit": 45})() | ||||||
| success_mock.assert_called_once() | ||||||
|
|
||||||
| committed_message = commit_mock.call_args[0][0] | ||||||
| lines = committed_message.split("\n") | ||||||
|
|
||||||
| # Should have a subject, a blank line | ||||||
| assert lines[0] == "feat: add feature" | ||||||
| assert lines[1] == "" | ||||||
| # Each original line should be wrapped separately, preserving the line breaks | ||||||
| body_lines = lines[2:] | ||||||
| # All lines should be <= 45 chars | ||||||
| for line in body_lines: | ||||||
| if line.strip(): | ||||||
| assert len(line) == 45, ( | ||||||
|
||||||
| assert len(line) == 45, ( | |
| assert len(line) <= 45, ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can set a default value here?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree. What’s the difference between setting a default value here and using default_setting in defaults.py?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I think
DEFAULT_SETTINGSis not a good design. I am not sure the mechanism of default value inargparse, not sure the exact difference here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally the default should be in DEFAULT_SETTINGS, which can be updated from the .cz.toml file. A default in argparse may override the setting in the file. You can add a test, to check that this is the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that multiple default setting is undesirable. Should I remove the default setting for this tag from
cli.py?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, after a second look, there should not be default values in cli.py because we want cz to use the settings from the configuration file if the value is None in arguments