comparison mercurial/patch.py @ 16122:9ef3a4a2c6c0 stable

patch: make hunk.fuzzit() compute the fuzzed start locations - It moves hunks processing weirdness where it belongs - It helps reusing said weirdness whenever fuzzit() is called, like during the actual hunk fuzzing.
author Patrick Mezard <patrick@mezard.eu>
date Mon, 13 Feb 2012 13:51:38 +0100
parents ccba74472af2
children b0c7525f826d
comparison
equal deleted inserted replaced
16121:ccba74472af2 16122:9ef3a4a2c6c0
726 # hunk data before patching. Otherwise, preserve input 726 # hunk data before patching. Otherwise, preserve input
727 # line-endings. 727 # line-endings.
728 h = h.getnormalized() 728 h = h.getnormalized()
729 729
730 # fast case first, no offsets, no fuzz 730 # fast case first, no offsets, no fuzz
731 old, new = h.fuzzit(0, False) 731 old, oldstart, new, newstart = h.fuzzit(0, False)
732 start = h.starta + self.offset 732 oldstart += self.offset
733 # zero length hunk ranges already have their start decremented 733 orig_start = oldstart
734 if h.lena:
735 start -= 1
736 orig_start = start
737 # if there's skew we want to emit the "(offset %d lines)" even 734 # if there's skew we want to emit the "(offset %d lines)" even
738 # when the hunk cleanly applies at start + skew, so skip the 735 # when the hunk cleanly applies at start + skew, so skip the
739 # fast case code 736 # fast case code
740 if self.skew == 0 and diffhelpers.testhunk(old, self.lines, start) == 0: 737 if (self.skew == 0 and
738 diffhelpers.testhunk(old, self.lines, oldstart) == 0):
741 if self.remove: 739 if self.remove:
742 self.backend.unlink(self.fname) 740 self.backend.unlink(self.fname)
743 else: 741 else:
744 self.lines[start : start + h.lena] = new 742 self.lines[oldstart:oldstart + len(old)] = new
745 self.offset += h.lenb - h.lena 743 self.offset += len(new) - len(old)
746 self.dirty = True 744 self.dirty = True
747 return 0 745 return 0
748 746
749 # ok, we couldn't match the hunk. Lets look for offsets and fuzz it 747 # ok, we couldn't match the hunk. Lets look for offsets and fuzz it
750 self.hash = {} 748 self.hash = {}
757 else: 755 else:
758 search_start = orig_start + self.skew 756 search_start = orig_start + self.skew
759 757
760 for fuzzlen in xrange(3): 758 for fuzzlen in xrange(3):
761 for toponly in [True, False]: 759 for toponly in [True, False]:
762 old, new = h.fuzzit(fuzzlen, toponly) 760 old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly)
763 761
764 cand = self.findlines(old[0][1:], search_start) 762 cand = self.findlines(old[0][1:], search_start)
765 for l in cand: 763 for l in cand:
766 if diffhelpers.testhunk(old, self.lines, l) == 0: 764 if diffhelpers.testhunk(old, self.lines, l) == 0:
767 self.lines[l : l + len(old)] = new 765 self.lines[l : l + len(old)] = new
1002 if top < context: 1000 if top < context:
1003 top = max(0, fuzz - (context - top)) 1001 top = max(0, fuzz - (context - top))
1004 else: 1002 else:
1005 top = min(fuzz, top) 1003 top = min(fuzz, top)
1006 1004
1007 return old[top:len(old)-bot], new[top:len(new)-bot] 1005 return old[top:len(old)-bot], new[top:len(new)-bot], top
1008 return old, new 1006 return old, new, 0
1009 1007
1010 def fuzzit(self, fuzz, toponly): 1008 def fuzzit(self, fuzz, toponly):
1011 return self._fuzzit(self.a, self.b, fuzz, toponly) 1009 old, new, top = self._fuzzit(self.a, self.b, fuzz, toponly)
1010 oldstart = self.starta + top
1011 newstart = self.startb + top
1012 # zero length hunk ranges already have their start decremented
1013 if self.lena:
1014 oldstart -= 1
1015 if self.lenb:
1016 newstart -= 1
1017 return old, oldstart, new, newstart
1012 1018
1013 class binhunk(object): 1019 class binhunk(object):
1014 'A binary patch file. Only understands literals so far.' 1020 'A binary patch file. Only understands literals so far.'
1015 def __init__(self, lr): 1021 def __init__(self, lr):
1016 self.text = None 1022 self.text = None