comparison mercurial/mdiff.py @ 17946:1e13b1184292

diff: move index header generation to patch In an upcoming patch, we will add index information to all git diffs, not only binary diffs, so this code needs to be moved to a more appropriate place. Also, since this information is used for patch headers, it makes more sense to be in the patch module, along with other patch-related metadata.
author Guillermo P?rez <bisho@fb.com>
date Thu, 15 Nov 2012 15:16:41 -0800
parents 9a6e4d5d7ea8
children 1e5b38a919dd
comparison
equal deleted inserted replaced
17945:45766e2a7384 17946:1e13b1184292
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 7
8 from i18n import _ 8 from i18n import _
9 import bdiff, mpatch, util 9 import bdiff, mpatch, util
10 import re, struct, base85, zlib 10 import re, struct, base85, zlib
11 from node import hex, nullid
12 11
13 def splitnewlines(text): 12 def splitnewlines(text):
14 '''like str.splitlines, but only split on newlines.''' 13 '''like str.splitlines, but only split on newlines.'''
15 lines = [l + '\n' for l in text.split('\n')] 14 lines = [l + '\n' for l in text.split('\n')]
16 if lines: 15 if lines:
299 for x in yieldhunk(hunk): 298 for x in yieldhunk(hunk):
300 yield x 299 yield x
301 300
302 def b85diff(to, tn): 301 def b85diff(to, tn):
303 '''print base85-encoded binary diff''' 302 '''print base85-encoded binary diff'''
304 def gitindex(text):
305 if not text:
306 return hex(nullid)
307 l = len(text)
308 s = util.sha1('blob %d\0' % l)
309 s.update(text)
310 return s.hexdigest()
311
312 def fmtline(line): 303 def fmtline(line):
313 l = len(line) 304 l = len(line)
314 if l <= 26: 305 if l <= 26:
315 l = chr(ord('A') + l - 1) 306 l = chr(ord('A') + l - 1)
316 else: 307 else:
322 i = 0 313 i = 0
323 while i < l: 314 while i < l:
324 yield text[i:i + csize] 315 yield text[i:i + csize]
325 i += csize 316 i += csize
326 317
327 tohash = gitindex(to) 318 if to is None:
328 tnhash = gitindex(tn) 319 to = ''
329 if tohash == tnhash: 320 if tn is None:
330 return "" 321 tn = ''
322
323 if to == tn:
324 return ''
331 325
332 # TODO: deltas 326 # TODO: deltas
333 ret = ['index %s..%s\nGIT binary patch\nliteral %s\n' % 327 ret = []
334 (tohash, tnhash, len(tn))] 328 ret.append('GIT binary patch\n')
329 ret.append('literal %s\n' % len(tn))
335 for l in chunk(zlib.compress(tn)): 330 for l in chunk(zlib.compress(tn)):
336 ret.append(fmtline(l)) 331 ret.append(fmtline(l))
337 ret.append('\n') 332 ret.append('\n')
333
338 return ''.join(ret) 334 return ''.join(ret)
339 335
340 def patchtext(bin): 336 def patchtext(bin):
341 pos = 0 337 pos = 0
342 t = [] 338 t = []