[issue1941] built-in patch mishandles offset hunks

Greg Onufer bugs at mercurial.selenic.com
Tue Dec 8 16:02:21 CST 2009

New submission from Greg Onufer <gonufer at jazzhaiku.com>:

I have a long file with a new 41 line copyright header at the beginning, the 
"hg export -g" patch is against a file without the 41 line copyright header.  
Two of the chunks could apply to several places in the file.  hg import 
using the built-in patch implementation doesn't adjust self.offset like GNU 
patch does, it applies hunks to the wrong locations in the file.

This tweak makes it produce the same output file as GNU patch 2.5.9 (note: 
patch 2.5.4 had broken offset tracking and did it wrong, too).

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -450,7 +450,7 @@
                     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.offset += len(newlines) - len(old) + l - 
                         self.dirty = 1
                         if fuzzlen:
                             fuzzstr = "with fuzz %d " % fuzzlen

messages: 11181
nosy: gco
priority: bug
status: unread
title: built-in patch mishandles offset hunks

Mercurial issue tracker <bugs at mercurial.selenic.com>

More information about the Mercurial-devel mailing list