From 004c86e02f415c126707acc1ef27d4f437ba2a6d Mon Sep 17 00:00:00 2001 From: Alberto Cerrone Date: Sat, 15 Oct 2022 15:26:25 +0100 Subject: [PATCH] fix: filter git diff from commit message When running `git commit --verbose` when using commitizen as a pre-commit we have a bug because of the diff that is incorrectly included. I have filtered out everything that is auto generated by git and that is normally excluded. See issue: https://github.com/commitizen-tools/commitizen/issues/598 --- commitizen/commands/check.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/commitizen/commands/check.py b/commitizen/commands/check.py index f332b6f318..e42dbf7f49 100644 --- a/commitizen/commands/check.py +++ b/commitizen/commands/check.py @@ -98,8 +98,35 @@ def _get_commits(self): # Get commit messages from git log (--rev-range) return git.get_commits(end=self.rev_range) - def _filter_comments(self, msg: str) -> str: - lines = [line for line in msg.split("\n") if not line.startswith("#")] + @staticmethod + def _filter_comments(msg: str) -> str: + """Filter the commit message by removing comments. + + When using `git commit --verbose`, we exclude the diff that is going to + generated, like the following example: + + ```bash + ... + # ------------------------ >8 ------------------------ + # Do not modify or remove the line above. + # Everything below it will be ignored. + diff --git a/... b/... + ... + ``` + + Args: + msg: The commit message to filter. + + Returns: + The filtered commit message without comments. + """ + + lines = [] + for line in msg.split("\n"): + if "# ------------------------ >8 ------------------------" in line: + break + if not line.startswith("#"): + lines.append(line) return "\n".join(lines) def validate_commit_message(self, commit_msg: str, pattern: str) -> bool: