Skip to content

Commit 713f784

Browse files
committed
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 <[email protected]>
1 parent 06cb5db commit 713f784

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

commit_format/commit_format.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,40 @@ def load_template(self, template_path: str):
201201
sys.exit(2)
202202
self.commit_template = cfg
203203

204-
def _split_message(self, message: str):
204+
def _split_message(self, message: str, has_footer: bool):
205+
"""
206+
Splits a message into its header, body, and footer components.
207+
208+
This function processes a multi-line message string and separates it into
209+
three parts: the header, the body, and the footer. The header is always the
210+
first line of the message. If `has_footer` is True, the function attempts to
211+
identify the last non-empty line as the footer. The body consists of all lines
212+
between the header and the footer.
213+
214+
Args:
215+
message (str): The multi-line message to be split.
216+
has_footer (bool): A flag indicating whether the message contains a footer.
217+
218+
Returns:
219+
tuple: A tuple containing four elements:
220+
- header (str): The first line of the message.
221+
- body (list of str): A list of lines representing the body of the message.
222+
- footers (list of str): A list containing the footer line if present.
223+
- lines (list of str): A list of all lines in the original message.
224+
225+
"""
205226
lines = message.splitlines()
206227
line_cnt = len(lines)
228+
footer_start = line_cnt
207229
header = lines[0] if lines else ""
208230

209-
# Identify the last non-empty line as the potential footer
210-
i = line_cnt - 1
211-
while i > 0 and lines[i].strip() == "":
212-
i -= 1
231+
if has_footer:
232+
# Identify the last non-empty line as the potential footer
233+
i = line_cnt - 1
234+
while i > 0 and lines[i].strip() == "":
235+
i -= 1
213236

214-
footer_start = i if i > 0 else line_cnt
237+
footer_start = i if i > 0 else line_cnt
215238

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

231-
header, body, footers, all_lines = self._split_message(commit_message)
254+
footer_required = False
255+
if cfg.has_section('footer') and cfg.has_option('footer', 'required'):
256+
try:
257+
footer_required = cfg.getboolean('footer', 'required')
258+
except ValueError:
259+
footer_required = False
260+
261+
header, body, footers, all_lines = self._split_message(commit_message, footer_required)
232262

233263
# Header checks
234264
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:
268298
self.warning(f"Commit {commit}: commit body is empty")
269299

270300
# Footer checks
271-
footer_required = False
272-
if cfg.has_section('footer') and cfg.has_option('footer', 'required'):
273-
try:
274-
footer_required = cfg.getboolean('footer', 'required')
275-
except ValueError:
276-
footer_required = False
277-
278301
if footer_required and len(footers) == 0:
279302
errors += 1
280303
self.warning(f"Commit {commit}: missing required footer section")

0 commit comments

Comments
 (0)