changeset 10135:9a4034b630c4 stable

patch: better handling of sequence of offset patch hunks (issue1941) The built-in patch implementation applied the hunks to the wrong lines of the file if the file in the repo has been modified to skew the patch line numbers and the file contains repetitive sequences of lines.
author Greg Onufer <gonufer@jazzhaiku.com>
date Wed, 09 Dec 2009 16:56:00 -0800
parents ebf69364e80f
children 270367ec4d30 daec0f64656e
files mercurial/patch.py tests/test-patch-offset tests/test-patch-offset.out
diffstat 3 files changed, 84 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/patch.py	Wed Dec 23 20:31:35 2009 +0100
+++ b/mercurial/patch.py	Wed Dec 09 16:56:00 2009 -0800
@@ -286,6 +286,7 @@
         self.hash = {}
         self.dirty = 0
         self.offset = 0
+        self.skew = 0
         self.rej = []
         self.fileprinted = False
         self.printfile(False)
@@ -423,7 +424,10 @@
         else:
             start = h.starta + self.offset - 1
         orig_start = start
-        if diffhelpers.testhunk(old, self.lines, start) == 0:
+        # if there's skew we want to emit the "(offset %d lines)" even
+        # when the hunk cleanly applies at start + skew, so skip the
+        # fast case code
+        if self.skew == 0 and diffhelpers.testhunk(old, self.lines, start) == 0:
             if h.rmfile():
                 self.unlink(self.fname)
             else:
@@ -439,7 +443,7 @@
             # override the start line and use eof here
             search_start = len(self.lines)
         else:
-            search_start = orig_start
+            search_start = orig_start + self.skew
 
         for fuzzlen in xrange(3):
             for toponly in [ True, False ]:
@@ -451,6 +455,7 @@
                         newlines = h.new(fuzzlen, toponly)
                         self.lines[l : l + len(old)] = newlines
                         self.offset += len(newlines) - len(old)
+                        self.skew = l - orig_start
                         self.dirty = 1
                         if fuzzlen:
                             fuzzstr = "with fuzz %d " % fuzzlen
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-patch-offset	Wed Dec 09 16:56:00 2009 -0800
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+cat > writepatterns.py <<EOF
+import sys
+
+path = sys.argv[1]
+patterns = sys.argv[2:]
+
+fp = file(path, 'wb')
+for pattern in patterns:
+    count = int(pattern[0:-1])
+    char = pattern[-1] + '\n'
+    fp.write(char*count)
+fp.close()
+EOF
+
+echo % prepare repo
+hg init a
+cd a
+
+# These initial lines of Xs were not in the original file used to generate
+# the patch.  So all the patch hunks need to be applied to a constant offset
+# within this file.  If the offset isn't tracked then the hunks can be
+# applied to the wrong lines of this file.
+python ../writepatterns.py a 34X 10A 1B 10A 1C 10A 1B 10A 1D 10A 1B 10A 1E 10A 1B 10A
+hg commit -Am adda
+
+# This is a cleaner patch generated via diff
+# In this case it reproduces the problem when
+# the output of hg export does not
+echo % import patch
+hg import -v -m 'b' -d '2 0' - <<EOF
+--- a/a	2009-12-08 19:26:17.000000000 -0800
++++ b/a	2009-12-08 19:26:17.000000000 -0800
+@@ -9,7 +9,7 @@
+ A
+ A
+ B
+-A
++a
+ A
+ A
+ A
+@@ -53,7 +53,7 @@
+ A
+ A
+ B
+-A
++a
+ A
+ A
+ A
+@@ -75,7 +75,7 @@
+ A
+ A
+ B
+-A
++a
+ A
+ A
+ A
+EOF
+
+echo % compare imported changes against reference file
+python ../writepatterns.py aref 34X 10A 1B 1a 9A 1C 10A 1B 10A 1D 10A 1B 1a 9A 1E 10A 1B 1a 9A
+diff -u aref a
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-patch-offset.out	Wed Dec 09 16:56:00 2009 -0800
@@ -0,0 +1,10 @@
+% prepare repo
+adding a
+% import patch
+applying patch from stdin
+patching file a
+Hunk #1 succeeded at 43 (offset 34 lines).
+Hunk #2 succeeded at 87 (offset 34 lines).
+Hunk #3 succeeded at 109 (offset 34 lines).
+a
+% compare imported changes against reference file