comparison mercurial/patch.py @ 7153:353141d74ca8

patch: pass linereader to binaryhunk.extract() instead of wrapped fp It unifies input patch file access methods
author Patrick Mezard <pmezard@gmail.com>
date Sat, 18 Oct 2008 23:45:46 +0200
parents f0055cec8446
children f77c8d8331ca
comparison
equal deleted inserted replaced
7152:f0055cec8446 7153:353141d74ca8
667 return self.text is not None 667 return self.text is not None
668 668
669 def new(self): 669 def new(self):
670 return [self.text] 670 return [self.text]
671 671
672 def extract(self, fp): 672 def extract(self, lr):
673 line = fp.readline() 673 line = lr.readline()
674 self.hunk.append(line) 674 self.hunk.append(line)
675 while line and not line.startswith('literal '): 675 while line and not line.startswith('literal '):
676 line = fp.readline() 676 line = lr.readline()
677 self.hunk.append(line) 677 self.hunk.append(line)
678 if not line: 678 if not line:
679 raise PatchError(_('could not extract binary patch')) 679 raise PatchError(_('could not extract binary patch'))
680 size = int(line[8:].rstrip()) 680 size = int(line[8:].rstrip())
681 dec = [] 681 dec = []
682 line = fp.readline() 682 line = lr.readline()
683 self.hunk.append(line) 683 self.hunk.append(line)
684 while len(line) > 1: 684 while len(line) > 1:
685 l = line[0] 685 l = line[0]
686 if l <= 'Z' and l >= 'A': 686 if l <= 'Z' and l >= 'A':
687 l = ord(l) - ord('A') + 1 687 l = ord(l) - ord('A') + 1
688 else: 688 else:
689 l = ord(l) - ord('a') + 27 689 l = ord(l) - ord('a') + 27
690 dec.append(base85.b85decode(line[1:-1])[:l]) 690 dec.append(base85.b85decode(line[1:-1])[:l])
691 line = fp.readline() 691 line = lr.readline()
692 self.hunk.append(line) 692 self.hunk.append(line)
693 text = zlib.decompress(''.join(dec)) 693 text = zlib.decompress(''.join(dec))
694 if len(text) != size: 694 if len(text) != size:
695 raise PatchError(_('binary patch is %d bytes, not %d') % 695 raise PatchError(_('binary patch is %d bytes, not %d') %
696 len(text), size) 696 len(text), size)
804 fp = cStringIO.StringIO(lr.fp.read()) 804 fp = cStringIO.StringIO(lr.fp.read())
805 gitlr = linereader(fp) 805 gitlr = linereader(fp)
806 gitlr.push(firstline) 806 gitlr.push(firstline)
807 (dopatch, gitpatches) = readgitpatch(gitlr) 807 (dopatch, gitpatches) = readgitpatch(gitlr)
808 fp.seek(pos) 808 fp.seek(pos)
809 return fp, dopatch, gitpatches 809 return dopatch, gitpatches
810 810
811 def iterhunks(ui, fp, sourcefile=None): 811 def iterhunks(ui, fp, sourcefile=None):
812 """Read a patch and yield the following events: 812 """Read a patch and yield the following events:
813 - ("file", afile, bfile, firsthunk): select a new target file. 813 - ("file", afile, bfile, firsthunk): select a new target file.
814 - ("hunk", hunk): a new hunk is ready to be applied, follows a 814 - ("hunk", hunk): a new hunk is ready to be applied, follows a
869 current_hunk = binhunk(changed[bfile[2:]]) 869 current_hunk = binhunk(changed[bfile[2:]])
870 hunknum += 1 870 hunknum += 1
871 if emitfile: 871 if emitfile:
872 emitfile = False 872 emitfile = False
873 yield 'file', (afile, bfile, current_hunk) 873 yield 'file', (afile, bfile, current_hunk)
874 current_hunk.extract(fp) 874 current_hunk.extract(lr)
875 elif x.startswith('diff --git'): 875 elif x.startswith('diff --git'):
876 # check for git diff, scanning the whole patch file if needed 876 # check for git diff, scanning the whole patch file if needed
877 m = gitre.match(x) 877 m = gitre.match(x)
878 if m: 878 if m:
879 afile, bfile = m.group(1, 2) 879 afile, bfile = m.group(1, 2)
880 if not git: 880 if not git:
881 git = True 881 git = True
882 fp, dopatch, gitpatches = scangitpatch(lr, x) 882 dopatch, gitpatches = scangitpatch(lr, x)
883 yield 'git', gitpatches 883 yield 'git', gitpatches
884 for gp in gitpatches: 884 for gp in gitpatches:
885 changed[gp.path] = gp 885 changed[gp.path] = gp
886 # else error? 886 # else error?
887 # copy/rename + modify should modify target, not source 887 # copy/rename + modify should modify target, not source