Bug 914269 - Strip the original header from imported patches, to avoid duplication; r=sfink
authorEd Morley <emorley@mozilla.com>
Wed, 11 Sep 2013 13:14:52 +0100
changeset 79 e1be488e64f4
parent 78 9c5006e8d531
child 80 414b18290a92
push id59
push useremorley@mozilla.com
push date2013-09-11 12:15 +0000
reviewerssfink
bugs914269
Bug 914269 - Strip the original header from imported patches, to avoid duplication; r=sfink
bz.py
--- a/bz.py
+++ b/bz.py
@@ -90,16 +90,26 @@ class Patch(Attachment):
         patch.extract(bug.settings.ui, StringIO.StringIO(rawtext))
     # for some reason, patch.extract writes a temporary file with the diff hunks
     if filename:
       fp = file(filename)
       try:
         # BugZilla is not explicit about patch encoding. We need to check it's utf-8.
         # utf-8: convert from 8-bit encoding to internal (16/32-bit) Unicode.
         self.data = fp.read().decode('utf-8')
+        # Attempt to detect the start of the diff. Borrowed from:
+        # http://selenic.com/hg/file/79e5de2bfa8c/mercurial/patch.py#l163
+        diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |'
+                            r'retrieving revision [0-9]+(\.[0-9]+)*$|'
+                            r'---[ \t].*?^\+\+\+[ \t]|'
+                            r'\*\*\*[ \t].*?^---[ \t])', re.MULTILINE|re.DOTALL)
+        m = diffre.search(self.data)
+        if m:
+          # Remove the patch header, since we'll be re-adding a cleaned up version later.
+          self.data = self.data[m.start(0):]
       except UnicodeDecodeError:
         # Ftr, this could be due to the (included) message part: see later message block.
         bug.settings.ui.warn("Patch id=%s desc=\"%s\" diff data were discarded:\n" % (self.id, self.desc))
         # Print the exception without its traceback.
         sys.excepthook(sys.exc_info()[0], sys.exc_info()[1], None)
         # Can't do better than discard data:
         # trying |.decode('utf-8', 'replace')| as a fallback would be too risky
         #   if user imports the result then forgets to fix it.