comparison mercurial/patch.py @ 15462:2b1ec74c961f stable

mdiff/patch: fix bad hunk handling for unified diffs with zero context Prior to this patch "hg diff -U0", i.e., zero lines of context, would output hunk headers with a start line one greater than what GNU patch and git output. Guido van Rossum documents the unified diff format[1] as having a start line value "one lower than one would expect" for zero length hunks. Comparing the behaviour of the three systems prior to this patch in transforming c1 c3 to c1 c2 c3 - GNU "diff -U0" reports the hunk as "@@ -1,0 +2 @@" - "git diff -U0" reports the hunk as "@@ -1,0 +2 @@" - "hg diff -U0" reports the hunk as "@@ -2,0 +2,1 @@" After this patch, "hg diff -U0" reports "@@ -1,0 +2,1 @@". Since "hg export --config diff.unified=0" outputs zero-context unified diffs, "hg import" has also been updated to account for start lines one less than expected for zero length hunk ranges. [1]: http://www.artima.com/weblogs/viewpost.jsp?thread=164293
author Nicolas Venegas <nvenegas@atlassian.com>
date Wed, 09 Nov 2011 16:55:59 -0800
parents 628a4a9e411d
children 5414b56cfad6 3da1f60fc80d
comparison
equal deleted inserted replaced
15461:6ba2fc0a87ab 15462:2b1ec74c961f
721 # line-endings. 721 # line-endings.
722 h = h.getnormalized() 722 h = h.getnormalized()
723 723
724 # fast case first, no offsets, no fuzz 724 # fast case first, no offsets, no fuzz
725 old = h.old() 725 old = h.old()
726 # patch starts counting at 1 unless we are adding the file 726 start = h.starta + self.offset
727 if h.starta == 0: 727 # zero length hunk ranges already have their start decremented
728 start = 0 728 if h.lena:
729 else: 729 start -= 1
730 start = h.starta + self.offset - 1
731 orig_start = start 730 orig_start = start
732 # if there's skew we want to emit the "(offset %d lines)" even 731 # if there's skew we want to emit the "(offset %d lines)" even
733 # when the hunk cleanly applies at start + skew, so skip the 732 # when the hunk cleanly applies at start + skew, so skip the
734 # fast case code 733 # fast case code
735 if self.skew == 0 and diffhelpers.testhunk(old, self.lines, start) == 0: 734 if self.skew == 0 and diffhelpers.testhunk(old, self.lines, start) == 0: