Make import command reject patches that resemble email messages.
authorbos@serpentine.internal.keyresearch.com
Thu, 01 Sep 2005 09:04:18 -0700
changeset 1190 737f9b90c571
parent 1189 4cbcc54695b2
child 1191 77a0c7528c2f
Make import command reject patches that resemble email messages. See changeset 120aa5fc7ced1bf765b4f025f5a3a138cd87f49e for an example of why this is a good idea.
doc/hg.1.txt
mercurial/commands.py
--- a/doc/hg.1.txt	Thu Sep 01 08:01:10 2005 -0700
+++ b/doc/hg.1.txt	Thu Sep 01 09:04:18 2005 -0700
@@ -251,11 +251,18 @@
 import [-p <n> -b <base> -f] <patches>::
     Import a list of patches and commit them individually.
 
+    If a patch looks like a mail message (its first line starts with
+    "From " or looks like an RFC822 header), it will not be applied
+    unless the -m option is used.  The importer neither parses nor
+    discards mail headers, so use -m only to override the "mailness"
+    safety check, not to import a real mail message.
+
     options:
     -p, --strip <n>   directory strip option for patch. This has the same
                       meaning as the corresponding patch option
     -b <path>         base directory to read patches from
     -f, --force	      skip check for outstanding uncommitted changes
+    -m, --mail-like   apply a patch that appears to be a mail message
 
     aliases: patch
 
--- a/mercurial/commands.py	Thu Sep 01 08:01:10 2005 -0700
+++ b/mercurial/commands.py	Thu Sep 01 09:04:18 2005 -0700
@@ -1009,6 +1009,8 @@
     d = opts["base"]
     strip = opts["strip"]
 
+    mailre = re.compile(r'(From |[\w-]+:)')
+
     for patch in patches:
         ui.status("applying %s\n" % patch)
         pf = os.path.join(d, patch)
@@ -1018,6 +1020,10 @@
         hgpatch = False
         for line in file(pf):
             line = line.rstrip()
+            if not message and mailre.match(line) and not opts['mail_like']:
+                if len(line) > 35: line = line[:32] + '...'
+                raise util.Abort('first line looks like a '
+                                 'mail header: ' + line)
             if line.startswith("--- ") or line.startswith("diff -r"):
                 break
             elif hgpatch:
@@ -1662,7 +1668,8 @@
         (import_,
          [('p', 'strip', 1, 'path strip'),
           ('f', 'force', None, 'skip check for outstanding changes'),
-          ('b', 'base', "", 'base path')],
+          ('b', 'base', "", 'base path'),
+          ('m', 'mail-like', None, 'apply a patch that looks like email')],
          "hg import [-f] [-p NUM] [-b BASE] PATCH..."),
     "incoming|in": (incoming, 
          [('p', 'patch', None, 'show patch')],