Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions commit_format/commit_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,40 @@ def load_template(self, template_path: str):
sys.exit(2)
self.commit_template = cfg

def _split_message(self, message: str):
def _split_message(self, message: str, has_footer: bool):
"""
Splits a message into its header, body, and footer components.

This function processes a multi-line message string and separates it into
three parts: the header, the body, and the footer. The header is always the
first line of the message. If `has_footer` is True, the function attempts to
identify the last non-empty line as the footer. The body consists of all lines
between the header and the footer.

Args:
message (str): The multi-line message to be split.
has_footer (bool): A flag indicating whether the message contains a footer.

Returns:
tuple: A tuple containing four elements:
- header (str): The first line of the message.
- body (list of str): A list of lines representing the body of the message.
- footers (list of str): A list containing the footer line if present.
- lines (list of str): A list of all lines in the original message.

"""
lines = message.splitlines()
line_cnt = len(lines)
footer_start = line_cnt
header = lines[0] if lines else ""

# Identify the last non-empty line as the potential footer
i = line_cnt - 1
while i > 0 and lines[i].strip() == "":
i -= 1
if has_footer:
# Identify the last non-empty line as the potential footer
i = line_cnt - 1
while i > 0 and lines[i].strip() == "":
i -= 1

footer_start = i if i > 0 else line_cnt
footer_start = i if i > 0 else line_cnt

# Determine the body by excluding the header and footer
body = lines[1:footer_start] if line_cnt > 1 else []
Expand All @@ -228,7 +251,14 @@ def template_check(self, commit: str, commit_message: str) -> int:
errors = 0
cfg = self.commit_template

header, body, footers, all_lines = self._split_message(commit_message)
footer_required = False
if cfg.has_section('footer') and cfg.has_option('footer', 'required'):
try:
footer_required = cfg.getboolean('footer', 'required')
except ValueError:
footer_required = False

header, body, footers, all_lines = self._split_message(commit_message, footer_required)

# Header checks
if cfg.has_section('header') and cfg.has_option('header', 'pattern'):
Expand Down Expand Up @@ -268,13 +298,6 @@ def template_check(self, commit: str, commit_message: str) -> int:
self.warning(f"Commit {commit}: commit body is empty")

# Footer checks
footer_required = False
if cfg.has_section('footer') and cfg.has_option('footer', 'required'):
try:
footer_required = cfg.getboolean('footer', 'required')
except ValueError:
footer_required = False

if footer_required and len(footers) == 0:
errors += 1
self.warning(f"Commit {commit}: missing required footer section")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "commit_format"
version = "0.2.2"
version = "0.2.3"
authors = [
{ name="Alex Fabre", email="[email protected]" },
]
Expand Down