[PATCH 1 of 3 STABLE] patch: fuzz old and new lines at the same time

Patrick Mezard patrick at mezard.eu
Mon Feb 13 16:02:07 UTC 2012


# HG changeset patch
# User Patrick Mezard <patrick at mezard.eu>
# Date 1329135660 -3600
# Branch stable
# Node ID 400a921ae0029bb08ce36757bf78a4fff1afeee9
# Parent  f7e0d95d0a0bc5545f3dad40b9ceaee362d7c553
patch: fuzz old and new lines at the same time

In theory, the fuzzed offsets for old and new lines should be exactly the same
as they are based on hunk parsing. Make it true in practice.

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -722,7 +722,7 @@
             h = h.getnormalized()
 
         # fast case first, no offsets, no fuzz
-        old = h.old()
+        old, new = h.fuzzit(0, False)
         start = h.starta + self.offset
         # zero length hunk ranges already have their start decremented
         if h.lena:
@@ -735,7 +735,7 @@
             if self.remove:
                 self.backend.unlink(self.fname)
             else:
-                self.lines[start : start + h.lena] = h.new()
+                self.lines[start : start + h.lena] = new
                 self.offset += h.lenb - h.lena
                 self.dirty = True
             return 0
@@ -753,14 +753,13 @@
 
         for fuzzlen in xrange(3):
             for toponly in [True, False]:
-                old = h.old(fuzzlen, toponly)
+                old, new = h.fuzzit(fuzzlen, toponly)
 
                 cand = self.findlines(old[0][1:], search_start)
                 for l in cand:
                     if diffhelpers.testhunk(old, self.lines, l) == 0:
-                        newlines = h.new(fuzzlen, toponly)
-                        self.lines[l : l + len(old)] = newlines
-                        self.offset += len(newlines) - len(old)
+                        self.lines[l : l + len(old)] = new
+                        self.offset += len(new) - len(old)
                         self.skew = l - orig_start
                         self.dirty = True
                         offset = l - orig_start - fuzzlen
@@ -965,11 +964,11 @@
     def complete(self):
         return len(self.a) == self.lena and len(self.b) == self.lenb
 
-    def fuzzit(self, l, fuzz, toponly):
+    def _fuzzit(self, old, new, fuzz, toponly):
         # this removes context lines from the top and bottom of list 'l'.  It
         # checks the hunk to make sure only context lines are removed, and then
         # returns a new shortened list of lines.
-        fuzz = min(fuzz, len(l)-1)
+        fuzz = min(fuzz, len(old)-1)
         if fuzz:
             top = 0
             bot = 0
@@ -999,14 +998,11 @@
             else:
                 top = min(fuzz, top)
 
-            return l[top:len(l)-bot]
-        return l
+            return old[top:len(old)-bot], new[top:len(new)-bot]
+        return old, new
 
-    def old(self, fuzz=0, toponly=False):
-        return self.fuzzit(self.a, fuzz, toponly)
-
-    def new(self, fuzz=0, toponly=False):
-        return self.fuzzit(self.b, fuzz, toponly)
+    def fuzzit(self, fuzz, toponly):
+        return self._fuzzit(self.a, self.b, fuzz, toponly)
 
 class binhunk(object):
     'A binary patch file. Only understands literals so far.'



More information about the Mercurial-devel mailing list