[PATCH] notify extension: truncate lines to 998 characters to conform to RFC 5322

William Gallafent william at gallaf.net
Thu Oct 27 08:51:55 CDT 2011

According to RFC 5322 ( http://www.rfc-editor.org/rfc/rfc5322.txt ) section 2.1.1, lines in internet messages must not exceed 998 characters in length. The Notify extension does not enforce this when sending emails containing the result of a diff. The following patch fixes this by truncating any such lines found in the diff output, adding the conventional "..." to indicate that truncation has taken place on that line, and preceding the diff output with a warning that the truncation has taken place.

I use the approach given at http://www.guyrutenberg.com/2007/10/12/conditional-expressions-in-python-24/ to retain something similar to conditional expression syntax within the list comprehension, allowing it to work with Python 2.4.

Apologies for unwrapped paragraphs above, Thunderbird's composition window not letting me wrap the text but not wrap the diff! This diff just comes from my live installation, I don't have a development tree installed here. I hope that its simplicity means it is still acceptable.

--- /usr/share/pyshared/hgext/notify.py~	2011-08-01 23:08:59.000000000 +0000
+++ /usr/share/pyshared/hgext/notify.py	2011-10-27 13:06:12.000000000 +0000
@@ -257,6 +257,11 @@
         chunks = patch.diff(self.repo, prev, ref, opts=patch.diffopts(self.ui))
         difflines = ''.join(chunks).splitlines()
+        if any(len(l)>998 for l in difflines):
+            msg = _('\nwarning: long diff lines truncated to 998 characters to conform to RFC5322.\n')
+            self.ui.write(msg)
+            difflines = [(l[:995]+"...",l)[len(l)<999] for l in difflines]
         if self.ui.configbool('notify', 'diffstat', True):
             s = patch.diffstat(difflines)
             # s may be nil, don't include the header if it is

Bill Gallafent

More information about the Mercurial-devel mailing list