Skip to content

Commit

Permalink
Change: Improve performance of parsing conventional commits
Browse files Browse the repository at this point in the history
Only create the regular expressions once per commit type. This should
improve the performance when parsing a lot of log entries.
  • Loading branch information
bjoernricks committed Jul 6, 2023
1 parent 54f923a commit 451e229
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions pontos/changelog/conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import re
from collections import defaultdict
from datetime import date
from pathlib import Path
from typing import NamedTuple, Optional, TypedDict, Union
Expand Down Expand Up @@ -166,24 +167,25 @@ def _sort_commits(
Returns
The dict containing the commit messages
"""
commit_dict = {}
expressions = [
(
commit_type["group"],
re.compile(rf'{commit_type["message"]}\s?[:|-]', flags=re.I),
)
for commit_type in self.commit_types()
]
commit_dict = defaultdict(list)
if commits and len(commits) > 0:
for commit in commits:
commit_id, message = commit.split(" ", maxsplit=1)
for commit_type in self.commit_types():
reg = re.compile(
rf'{commit_type["message"]}\s?[:|-]', flags=re.I
)
for group, reg in expressions:
match = reg.match(message)
if match:
if commit_type["group"] not in commit_dict:
commit_dict[commit_type["group"]] = []

# remove the commit tag from commit message
cleaned_msg = message.replace(
match.group(0), ""
).strip()
commit_dict[commit_type["group"]].append(
commit_dict[group].append(
CommitLogEntry(
commit_id=commit_id, message=cleaned_msg
)
Expand Down

0 comments on commit 451e229

Please sign in to comment.