[PATCH] patch.py: correctly handle non-tabulator Subject: line continuation

Steffen Daode Nurpmeso sdaoden at googlemail.com
Thu Sep 22 05:55:28 CDT 2011

Heya Mercurial,

i've posted rather the same post almost one month ago, but since
yesterdays tip still requires my patch, it maybe simply has been
lost in holiday noise.  Thus i'll repost it today, with a full
walk this time:

If one uses 'hg import' to apply the valid (MBOX) patch

> Message-Id: <a14f705d33ab6d1458ab8a45f60361cd83d36278.1316684145.potato.sdaoden at gmail.com>
> From: Steffen Daode Nurpmeso <sdaoden at gmail.com>
> Date: Wed, 21 Sep 2011 14:00:55 +0200
> Subject: [PATCH] SaSo._wrap_tls_ssl():
>     fix buggy return overlay..
> Bla bla
> diff --git a/s-postman.py b/s-postman.py
> index b141ead..5f3c025 100755
> --- a/s-postman.py
> +++ b/s-postman.py
> @@ -1353,11 +1353,11 @@ Blablalba
> Hey
> Pippi
> Langstrumpf
> - Falleri, Fallera, Aua
> + Falleri, Fallera, Faller-Hopp-Sa-Sa
> Die
> macht
> was ihr gefällt

then this ends up like the following in 'hg log':

> summary:     SaSo._wrap_tls_ssl():

This is because in mercurial/patch.py line continuation is
supposed to work for tabulators only (well, not always but at that
very place it does):

> subject = subject.replace('\n\t', ' ')

instead of checking also for U+0020 SPACE.
See RFC 5322, section 2.2 for the definition of *WSP*, and, even
more, RFC 5322 section 2.2.3. "Long Header Fields" for the
definition of folding:

    The general rule is that wherever this specification allows
    for folding white space (not simply WSP characters), a CRLF
    may be inserted before any WSP.

The attached patch does that (i.e., changes meaning of *WSP*).
I personally extend this patch for my personal use, because with
only that patch we end up like this:

> Rev:  || 374:70b93c09d2e0 tip  
> Auth: Steffen Daode Nurpmeso <sdaoden at gmail.com>
> Date: Thu, 22 Sep 2011 11:48:23 +0200
>         SaSo._wrap_tls_ssl(): fix buggy return overlay.. Bla bla

i.e., "Bla bla" immediately follows "Subject:".
The solution would be to start the body of the mail (the MBOX
patch) with an empty line, which ends up as the desired

>       SaSo._wrap_tls_ssl(): fix buggy return overlay..
>       Bla bla

But there is no easy way to automate this.
Not that i fail to see the charm of the current code, because with
the second hunk of the appended patch there is no way to actually
continue the Subject: with the first paragraph of text.  However,
if i would like to do that, i would simply use a longer subject,
the RFC allows for almost 1000 characters?!

Ciao, sdaoden(*)(gmail.com)
ASCII ribbon campaign           ( ) More nuclear fission plants
  against HTML e-mail            X    can serve more coloured
    and proprietary attachments / \     and sounding animations
-- >8 --
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -188,7 +188,7 @@
                 pend = subject.find(']')
                 if pend >= 0:
                     subject = subject[pend + 1:].lstrip()
-            subject = subject.replace('\n\t', ' ')
+            subject = re.sub(r'\n[ \t]+', ' ', subject)
             ui.debug('Subject: %s\n' % subject)
         if user:
             ui.debug('From: %s\n' % user)
@@ -251,7 +251,7 @@
     if subject and not message.startswith(subject):
-        message = '%s\n%s' % (subject, message)
+        message = '%s\n\n%s' % (subject, message)
     if not diffs_seen:

More information about the Mercurial-devel mailing list