Mercurial > public > mercurial-scm > hg
comparison mercurial/patch.py @ 16123:b0c7525f826d stable
patch: fix fuzzing of hunks without previous lines (issue3264)
When applying hunks such as:
@@ -2,1 +2,2 @@
context
+change
fuzzing would empty the "old" block and make patchfile.apply() traceback.
Instead, we apply the new block at specified location without testing.
The "bottom hunk" test was removed as patch(1) has no problem applying hunk
with no context in the middle of a file.
author | Patrick Mezard <patrick@mezard.eu> |
---|---|
date | Mon, 13 Feb 2012 16:47:31 +0100 |
parents | 9ef3a4a2c6c0 |
children | 0e0060bf2f44 |
comparison
equal
deleted
inserted
replaced
16122:9ef3a4a2c6c0 | 16123:b0c7525f826d |
---|---|
746 | 746 |
747 # 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 |
748 self.hash = {} | 748 self.hash = {} |
749 for x, s in enumerate(self.lines): | 749 for x, s in enumerate(self.lines): |
750 self.hash.setdefault(s, []).append(x) | 750 self.hash.setdefault(s, []).append(x) |
751 if h.hunk[-1][0] != ' ': | |
752 # if the hunk tried to put something at the bottom of the file | |
753 # override the start line and use eof here | |
754 search_start = len(self.lines) | |
755 else: | |
756 search_start = orig_start + self.skew | |
757 | 751 |
758 for fuzzlen in xrange(3): | 752 for fuzzlen in xrange(3): |
759 for toponly in [True, False]: | 753 for toponly in [True, False]: |
760 old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly) | 754 old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly) |
761 | 755 oldstart = oldstart + self.offset + self.skew |
762 cand = self.findlines(old[0][1:], search_start) | 756 oldstart = min(oldstart, len(self.lines)) |
757 if old: | |
758 cand = self.findlines(old[0][1:], oldstart) | |
759 else: | |
760 # Only adding lines with no or fuzzed context, just | |
761 # take the skew in account | |
762 cand = [oldstart] | |
763 | |
763 for l in cand: | 764 for l in cand: |
764 if diffhelpers.testhunk(old, self.lines, l) == 0: | 765 if not old or diffhelpers.testhunk(old, self.lines, l) == 0: |
765 self.lines[l : l + len(old)] = new | 766 self.lines[l : l + len(old)] = new |
766 self.offset += len(new) - len(old) | 767 self.offset += len(new) - len(old) |
767 self.skew = l - orig_start | 768 self.skew = l - orig_start |
768 self.dirty = True | 769 self.dirty = True |
769 offset = l - orig_start - fuzzlen | 770 offset = l - orig_start - fuzzlen |