[PATCH 3 of 3] patch: be more tolerant with EOLs in binary diffs (issue2870)

Patrick Mezard patrick at mezard.eu
Thu Apr 26 14:48:15 CDT 2012


# HG changeset patch
# User Patrick Mezard <patrick at mezard.eu>
# Date 1335469442 -7200
# Branch stable
# Node ID 334c67f35c2a1dcd8e1b98344101998f6d0b33ae
# Parent  e4dd03a86875c5e9b01b6c2a69b6b9c99d90cef6
patch: be more tolerant with EOLs in binary diffs (issue2870)

The only place where an trailing CR could be meaningful is in the "diff --git"
line as part of a filename, and existing code already rule out this
possibility. Extend the CR/LF filtering to the whole binary hunk.

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1035,18 +1035,20 @@
         return [self.text]
 
     def _read(self, lr):
-        line = lr.readline()
-        self.hunk.append(line)
+        def getline(lr, hunk):
+            l = lr.readline()
+            hunk.append(l)
+            return l.rstrip('\r\n')
+
+        line = getline(lr, self.hunk)
         while line and not line.startswith('literal '):
-            line = lr.readline()
-            self.hunk.append(line)
+            line = getline(lr, self.hunk)
         if not line:
             raise PatchError(_('could not extract "%s" binary data')
                              % self._fname)
         size = int(line[8:].rstrip())
         dec = []
-        line = lr.readline()
-        self.hunk.append(line)
+        line = getline(lr, self.hunk)
         while len(line) > 1:
             l = line[0]
             if l <= 'Z' and l >= 'A':
@@ -1054,12 +1056,11 @@
             else:
                 l = ord(l) - ord('a') + 27
             try:
-                dec.append(base85.b85decode(line[1:-1])[:l])
+                dec.append(base85.b85decode(line[1:])[:l])
             except ValueError, e:
                 raise PatchError(_('could not decode "%s" binary patch: %s')
                                  % (self._fname, str(e)))
-            line = lr.readline()
-            self.hunk.append(line)
+            line = getline(lr, self.hunk)
         text = zlib.decompress(''.join(dec))
         if len(text) != size:
             raise PatchError(_('"%s" length is %d bytes, should be %d')
@@ -1213,7 +1214,7 @@
                 yield 'file', (afile, bfile, h, gp and gp.copy() or None)
             yield 'hunk', h
         elif x.startswith('diff --git'):
-            m = gitre.match(x)
+            m = gitre.match(x.rstrip(' \r\n'))
             if not m:
                 continue
             if gitpatches is None:
diff --git a/tests/test-import-git.t b/tests/test-import-git.t
--- a/tests/test-import-git.t
+++ b/tests/test-import-git.t
@@ -412,6 +412,27 @@
   abort: could not extract "binary2" binary data
   [255]
 
+Simulate a copy/paste turning LF into CRLF (issue2870)
+
+  $ hg revert -aq
+  $ cat > binary.diff <<"EOF"
+  > diff --git a/text2 b/binary2
+  > rename from text2
+  > rename to binary2
+  > index 78981922613b2afb6025042ff6bd878ac1994e85..10efcb362e9f3b3420fcfbfc0e37f3dc16e29757
+  > GIT binary patch
+  > literal 5
+  > Mc$`b*O5$Pw00T?_*Z=?k
+  > 
+  > EOF
+  >>> fp = file('binary.diff', 'rb')
+  >>> data = fp.read()
+  >>> fp.close()
+  >>> file('binary.diff', 'wb').write(data.replace('\n', '\r\n'))
+  $ rm binary2
+  $ hg import --no-commit binary.diff
+  applying binary.diff
+
   $ cd ..
 
 Consecutive import with renames (issue2459)


More information about the Mercurial-devel mailing list