[FIX] runbot_merge: EmailMessage.get_content misbehaves

`get_content` round-trips the text part through `ascii` with
`error=replace`, so if the input is not ascii it screws up
tremendously, which leads to either failing to apply patches (the more
likely situation) or corrupting the patches.

`get_payload`, if called without `decode`, pretty much just returns
the payload unless it needs a decoding pass (e.g. because it contains
raw surrogates, but that should not be an issue for us). So this is
really what we want.

While at it, increase `patch`'s verbosity in case it can give us more
info.
This commit is contained in:
Xavier Morel 2024-11-19 10:15:25 +01:00
parent 4563fc5fc0
commit c4bdb75d9c
2 changed files with 5 additions and 5 deletions

View File

@ -99,7 +99,7 @@ def parse_format_patch(p: Patch) -> ParseResult:
name, email = parseaddr(m['from']) name, email = parseaddr(m['from'])
author = (name, email, m['date']) author = (name, email, m['date'])
msg = re.sub(r'^\[PATCH( \d+/\d+)?\] ', '', m['subject']) msg = re.sub(r'^\[PATCH( \d+/\d+)?\] ', '', m['subject'])
body, _, rest = m.get_content().partition('---\n') body, _, rest = m.get_payload().partition('---\n')
if body: if body:
msg += '\n\n' + body msg += '\n\n' + body
@ -300,14 +300,14 @@ class Patch(models.Model):
tempfile.TemporaryDirectory() as tmpdir: tempfile.TemporaryDirectory() as tmpdir:
tf.extractall(tmpdir) tf.extractall(tmpdir)
patch = subprocess.run( patch = subprocess.run(
['patch', f'-p{prefix}', '-d', tmpdir], ['patch', f'-p{prefix}', '--directory', tmpdir, '--verbose'],
input=p.patch, input=p.patch,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
encoding='utf-8', encoding='utf-8',
) )
if patch.returncode: if patch.returncode:
raise PatchFailure(patch.stdout) raise PatchFailure("\n---------\n".join(filter(None, [p.patch, patch.stdout.strip(), patch.stderr.strip()])))
new_tree = r.update_tree(self.target.name, files) new_tree = r.update_tree(self.target.name, files)
sha = r.stdout().with_config(encoding='utf-8')\ sha = r.stdout().with_config(encoding='utf-8')\

View File

@ -2,7 +2,7 @@ import xmlrpc.client
import pytest import pytest
from utils import Commit, read_tracking_value from utils import Commit, read_tracking_value, matches
# basic udiff / show style patch, updates `b` from `1` to `2` # basic udiff / show style patch, updates `b` from `1` to `2`
BASIC_UDIFF = """\ BASIC_UDIFF = """\
@ -255,7 +255,7 @@ def test_patch_conflict(env, project, repo, users):
[], [],
), ( ), (
"Unable to apply patch", "Unable to apply patch",
"<p>patching file b<br>Hunk #1 FAILED at 1.<br>1 out of 1 hunk FAILED -- saving rejects to file b.rej<br></p>", matches("$$"), # feedback from patch can vary
[], [],
), ( ), (
False, '', [('active', 1, 0)] False, '', [('active', 1, 0)]