From 713f784c2faeb996563b725da5717330f18dfbd1 Mon Sep 17 00:00:00 2001 From: Alex Fabre Date: Wed, 15 Oct 2025 15:09:28 +0200 Subject: [PATCH 1/2] script: fix footer detection when footer is not required the last line must be part of the body and not the footer. This commit makes the footer detection a requirement for the split function. Signed-off-by: Alex Fabre --- commit_format/commit_format.py | 51 ++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/commit_format/commit_format.py b/commit_format/commit_format.py index e393df5..93df001 100644 --- a/commit_format/commit_format.py +++ b/commit_format/commit_format.py @@ -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 [] @@ -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'): @@ -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") From c5d77658cc8e59664f76fd8c58134c202bd21643 Mon Sep 17 00:00:00 2001 From: Alex Fabre Date: Wed, 15 Oct 2025 15:25:42 +0200 Subject: [PATCH 2/2] release: prepare version 0.2.3 This new version fixes a bad body detection on single line body where footer is not required. Signed-off-by: Alex Fabre --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 458ebb4..d731555 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "commit_format" -version = "0.2.2" +version = "0.2.3" authors = [ { name="Alex Fabre", email="m.alexfabre@gmail.com" }, ]