Skip to content

Commit fb22533

Browse files
authored
git: scan end of patch for -- instead of assuming last two lines (Bug 1856590) (#373)
Some patches sent with Git will include whitespace at the end of the patch, meaning the Git version info is the last 3 lines instead of 2. Add a function which scans the lines in reverse and looks for the first line beginning with `--` instead of using a naive slice that removes the last two lines of output. Update the Git parsing test to include extra lines of whitespace to account for this change in the main tests for `GitPatchParser`. Also add a simple test for the new parsing function.
1 parent 012180a commit fb22533

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

landoapi/hgexports.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,23 @@ def get_header(self, name: bytes | str) -> Optional[str]:
290290
# `EmailMessage` will return `None` if the header isn't found.
291291
return self.message[name]
292292

293+
@classmethod
294+
def strip_git_version_info_lines(cls, patch_lines: list[str]) -> list[str]:
295+
"""Strip the Git version info lines from the end of the given patch lines.
296+
297+
Assumes the `patch_lines` is the remaining content of a `git format-patch`
298+
style patch with Git version info at the base of the patch. Moves backward
299+
through the patch to find the `--` barrier between the patch and the version
300+
info and strips the version info.
301+
"""
302+
# Collect the lines with the enumerated line numbers in a list, then
303+
# iterate through them in reverse order.
304+
for i, line in reversed(list(enumerate(patch_lines))):
305+
if line.startswith("--"):
306+
return patch_lines[:i]
307+
308+
raise ValueError("Malformed patch: could not find Git version info.")
309+
293310
def parse_email_body(self, content: str) -> tuple[str, str]:
294311
"""Parse the patch email's body, returning the commit message and diff.
295312
@@ -340,8 +357,10 @@ def parse_email_body(self, content: str) -> tuple[str, str]:
340357
raise ValueError("Patch is malformed, could not find start of patch diff.")
341358

342359
# The diff is the remainder of the patch, except the last two lines of Git version info.
343-
remaining_lines = list(line_iterator)
344-
diff_lines += list(remaining_lines[:-2])
360+
remaining_lines = GitPatchHelper.strip_git_version_info_lines(
361+
list(line_iterator)
362+
)
363+
diff_lines += remaining_lines
345364
diff = "\n".join(diff_lines)
346365

347366
return commit_message, diff

tests/test_hgexports.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@
9696
500,
9797
--
9898
2.31.1
99-
""".strip()
99+
100+
101+
""".lstrip()
100102

101103
GIT_PATCH_ONLY_DIFF = """diff --git a/landoui/errorhandlers.py b/landoui/errorhandlers.py
102104
index f56ba1c..33391ea 100644
@@ -413,3 +415,19 @@ def test_git_formatpatch_helper_empty_commit():
413415
"unavailable at the moment and is not broken."
414416
), "`commit_description()` should return full commit message."
415417
assert patch.get_diff() == "", "`get_diff()` should return an empty string."
418+
419+
420+
def test_strip_git_version_info_lines():
421+
lines = [
422+
"blah",
423+
"blah",
424+
"--",
425+
"git version info",
426+
"",
427+
"",
428+
]
429+
430+
assert GitPatchHelper.strip_git_version_info_lines(lines) == [
431+
"blah",
432+
"blah",
433+
]

0 commit comments

Comments
 (0)