comparison mercurial/patch.py @ 16523:727068417b95 stable

patch: include file name in binary patch error messages $ hg import --no-commit ../mercurial_1915035238540490516.patch applying ../mercurial_1915035238540490516.patch abort: could not extract binary data Becomes: abort: could not extract "binary2" binary data
author Patrick Mezard <patrick@mezard.eu>
date Thu, 26 Apr 2012 21:44:00 +0200
parents a8065323c003
children ed6a74312176
comparison
equal deleted inserted replaced
16522:a8065323c003 16523:727068417b95
1020 newstart -= 1 1020 newstart -= 1
1021 return old, oldstart, new, newstart 1021 return old, oldstart, new, newstart
1022 1022
1023 class binhunk(object): 1023 class binhunk(object):
1024 'A binary patch file. Only understands literals so far.' 1024 'A binary patch file. Only understands literals so far.'
1025 def __init__(self, lr): 1025 def __init__(self, lr, fname):
1026 self.text = None 1026 self.text = None
1027 self.hunk = ['GIT binary patch\n'] 1027 self.hunk = ['GIT binary patch\n']
1028 self._fname = fname
1028 self._read(lr) 1029 self._read(lr)
1029 1030
1030 def complete(self): 1031 def complete(self):
1031 return self.text is not None 1032 return self.text is not None
1032 1033
1038 self.hunk.append(line) 1039 self.hunk.append(line)
1039 while line and not line.startswith('literal '): 1040 while line and not line.startswith('literal '):
1040 line = lr.readline() 1041 line = lr.readline()
1041 self.hunk.append(line) 1042 self.hunk.append(line)
1042 if not line: 1043 if not line:
1043 raise PatchError(_('could not extract binary patch')) 1044 raise PatchError(_('could not extract "%s" binary data')
1045 % self._fname)
1044 size = int(line[8:].rstrip()) 1046 size = int(line[8:].rstrip())
1045 dec = [] 1047 dec = []
1046 line = lr.readline() 1048 line = lr.readline()
1047 self.hunk.append(line) 1049 self.hunk.append(line)
1048 while len(line) > 1: 1050 while len(line) > 1:
1052 else: 1054 else:
1053 l = ord(l) - ord('a') + 27 1055 l = ord(l) - ord('a') + 27
1054 try: 1056 try:
1055 dec.append(base85.b85decode(line[1:-1])[:l]) 1057 dec.append(base85.b85decode(line[1:-1])[:l])
1056 except ValueError, e: 1058 except ValueError, e:
1057 raise PatchError(_('could not decode binary patch: %s') 1059 raise PatchError(_('could not decode "%s" binary patch: %s')
1058 % str(e)) 1060 % (self._fname, str(e)))
1059 line = lr.readline() 1061 line = lr.readline()
1060 self.hunk.append(line) 1062 self.hunk.append(line)
1061 text = zlib.decompress(''.join(dec)) 1063 text = zlib.decompress(''.join(dec))
1062 if len(text) != size: 1064 if len(text) != size:
1063 raise PatchError(_('binary patch is %d bytes, not %d') % 1065 raise PatchError(_('"%s" length is %d bytes, should be %d')
1064 len(text), size) 1066 % (self._fname, len(text), size))
1065 self.text = text 1067 self.text = text
1066 1068
1067 def parsefilename(str): 1069 def parsefilename(str):
1068 # --- filename \t|space stuff 1070 # --- filename \t|space stuff
1069 s = str[4:].rstrip('\r\n') 1071 s = str[4:].rstrip('\r\n')
1198 gp = None 1200 gp = None
1199 if (gitpatches and 1201 if (gitpatches and
1200 gitpatches[-1].ispatching(afile, bfile)): 1202 gitpatches[-1].ispatching(afile, bfile)):
1201 gp = gitpatches.pop() 1203 gp = gitpatches.pop()
1202 if x.startswith('GIT binary patch'): 1204 if x.startswith('GIT binary patch'):
1203 h = binhunk(lr) 1205 h = binhunk(lr, gp.path)
1204 else: 1206 else:
1205 if context is None and x.startswith('***************'): 1207 if context is None and x.startswith('***************'):
1206 context = True 1208 context = True
1207 h = hunk(x, hunknum + 1, lr, context) 1209 h = hunk(x, hunknum + 1, lr, context)
1208 hunknum += 1 1210 hunknum += 1