Mercurial > public > mercurial-scm > hg
comparison mercurial/patch.py @ 3717:9e248cfd8b94
handle files with more than one git binary patch
author | Alexis S. L. Carvalho <alexis@cecm.usp.br> |
---|---|
date | Mon, 27 Nov 2006 22:03:36 -0200 |
parents | ab5600428b08 |
children | abaee83ce0a6 504dee0abeac |
comparison
equal
deleted
inserted
replaced
3716:ab5600428b08 | 3717:9e248cfd8b94 |
---|---|
188 return (dopatch, gitpatches) | 188 return (dopatch, gitpatches) |
189 | 189 |
190 def dogitpatch(patchname, gitpatches, cwd=None): | 190 def dogitpatch(patchname, gitpatches, cwd=None): |
191 """Preprocess git patch so that vanilla patch can handle it""" | 191 """Preprocess git patch so that vanilla patch can handle it""" |
192 def extractbin(fp): | 192 def extractbin(fp): |
193 line = fp.readline().rstrip() | 193 i = [0] # yuck |
194 def readline(): | |
195 i[0] += 1 | |
196 return fp.readline().rstrip() | |
197 line = readline() | |
194 while line and not line.startswith('literal '): | 198 while line and not line.startswith('literal '): |
195 line = fp.readline().rstrip() | 199 line = readline() |
196 if not line: | 200 if not line: |
197 return | 201 return None, i[0] |
198 size = int(line[8:]) | 202 size = int(line[8:]) |
199 dec = [] | 203 dec = [] |
200 line = fp.readline().rstrip() | 204 line = readline() |
201 while line: | 205 while line: |
202 l = line[0] | 206 l = line[0] |
203 if l <= 'Z' and l >= 'A': | 207 if l <= 'Z' and l >= 'A': |
204 l = ord(l) - ord('A') + 1 | 208 l = ord(l) - ord('A') + 1 |
205 else: | 209 else: |
206 l = ord(l) - ord('a') + 27 | 210 l = ord(l) - ord('a') + 27 |
207 dec.append(base85.b85decode(line[1:])[:l]) | 211 dec.append(base85.b85decode(line[1:])[:l]) |
208 line = fp.readline().rstrip() | 212 line = readline() |
209 text = zlib.decompress(''.join(dec)) | 213 text = zlib.decompress(''.join(dec)) |
210 if len(text) != size: | 214 if len(text) != size: |
211 raise util.Abort(_('binary patch is %d bytes, not %d') % | 215 raise util.Abort(_('binary patch is %d bytes, not %d') % |
212 (len(text), size)) | 216 (len(text), size)) |
213 return text | 217 return text, i[0] |
214 | 218 |
215 pf = file(patchname) | 219 pf = file(patchname) |
216 pfline = 1 | 220 pfline = 1 |
217 | 221 |
218 fd, patchname = tempfile.mkstemp(prefix='hg-patch-') | 222 fd, patchname = tempfile.mkstemp(prefix='hg-patch-') |
228 while pfline < p.lineno: | 232 while pfline < p.lineno: |
229 tmpfp.write(pf.readline()) | 233 tmpfp.write(pf.readline()) |
230 pfline += 1 | 234 pfline += 1 |
231 | 235 |
232 if p.binary: | 236 if p.binary: |
233 text = extractbin(pf) | 237 text, delta = extractbin(pf) |
234 if not text: | 238 if not text: |
235 raise util.Abort(_('binary patch extraction failed')) | 239 raise util.Abort(_('binary patch extraction failed')) |
240 pfline += delta | |
236 if not cwd: | 241 if not cwd: |
237 cwd = os.getcwd() | 242 cwd = os.getcwd() |
238 absdst = os.path.join(cwd, p.path) | 243 absdst = os.path.join(cwd, p.path) |
239 basedir = os.path.dirname(absdst) | 244 basedir = os.path.dirname(absdst) |
240 if not os.path.isdir(basedir): | 245 if not os.path.isdir(basedir): |