diff --git a/mercurial/patch.py b/mercurial/patch.py --- a/mercurial/patch.py +++ b/mercurial/patch.py @@ -1035,18 +1035,20 @@ class binhunk(object): 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 @@ class binhunk(object): 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 @@ def iterhunks(fp): 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 @@ Invalid base85 content 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)