Skip to content

Commit

Permalink
fix: filter git diff from commit message
Browse files Browse the repository at this point in the history
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: #598
  • Loading branch information
albertocerrone authored and Lee-W committed Dec 31, 2022
1 parent 45f358e commit 004c86e
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions commitizen/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 004c86e

Please sign in to comment.