changeset 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 6ba2fc0a87ab
children e1005da0ae04
files mercurial/mdiff.py mercurial/patch.py tests/test-diff-unified.t tests/test-import.t
diffstat 4 files changed, 108 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/mdiff.py	Tue Nov 08 17:08:58 2011 +0100
+++ b/mercurial/mdiff.py	Wed Nov 09 16:55:59 2011 -0800
@@ -180,8 +180,14 @@
             # the file more than once.
             lastfunc[0] = astart
 
-        yield "@@ -%d,%d +%d,%d @@%s\n" % (astart + 1, alen,
-                                           bstart + 1, blen, func)
+        # zero-length hunk ranges report their start line as one less
+        if alen:
+            astart += 1
+        if blen:
+            bstart += 1
+
+        yield "@@ -%d,%d +%d,%d @@%s\n" % (astart, alen,
+                                           bstart, blen, func)
         for x in delta:
             yield x
         for x in xrange(a2, aend):
--- a/mercurial/patch.py	Tue Nov 08 17:08:58 2011 +0100
+++ b/mercurial/patch.py	Wed Nov 09 16:55:59 2011 -0800
@@ -723,11 +723,10 @@
 
         # fast case first, no offsets, no fuzz
         old = h.old()
-        # patch starts counting at 1 unless we are adding the file
-        if h.starta == 0:
-            start = 0
-        else:
-            start = h.starta + self.offset - 1
+        start = h.starta + self.offset
+        # zero length hunk ranges already have their start decremented
+        if h.lena:
+            start -= 1
         orig_start = start
         # if there's skew we want to emit the "(offset %d lines)" even
         # when the hunk cleanly applies at start + skew, so skip the
--- a/tests/test-diff-unified.t	Tue Nov 08 17:08:58 2011 +0100
+++ b/tests/test-diff-unified.t	Wed Nov 09 16:55:59 2011 -0800
@@ -89,23 +89,65 @@
   abort: diff context lines count must be an integer, not 'foo'
   [255]
 
-test off-by-one error with diff -p
+0 lines of context hunk header matches gnu diff hunk header
+
+  $ hg init diffzero
+  $ cd diffzero
+  $ cat > f1 << EOF
+  > c2
+  > c4
+  > c5
+  > EOF
+  $ hg commit -Am0
+  adding f1
+
+  $ cat > f2 << EOF
+  > c1
+  > c2
+  > c3
+  > c4
+  > EOF
+  $ diff -U0 f1 f2
+  --- f1	* (glob)
+  +++ f2	* (glob)
+  @@ -0,0 +1 @@
+  +c1
+  @@ -1,0 +3 @@
+  +c3
+  @@ -3 +4,0 @@
+  -c5
+  [1]
 
-  $ hg init diffp
-  $ cd diffp
-  $ echo a > a
-  $ hg ci -Ama
-  adding a
-  $ rm a
-  $ echo b > a
-  $ echo a >> a
-  $ echo c >> a
-  $ hg diff -U0 -p --nodates
-  diff -r cb9a9f314b8b a
-  --- a/a
-  +++ b/a
-  @@ -1,0 +1,1 @@
-  +b
-  @@ -2,0 +3,1 @@ a
-  +c
+  $ mv f2 f1
+  $ hg diff -U0 --nodates
+  diff -r 55d8ff78db23 f1
+  --- a/f1
+  +++ b/f1
+  @@ -0,0 +1,1 @@
+  +c1
+  @@ -1,0 +3,1 @@
+  +c3
+  @@ -3,1 +4,0 @@
+  -c5
 
+  $ hg diff -U0 --nodates --git
+  diff --git a/f1 b/f1
+  --- a/f1
+  +++ b/f1
+  @@ -0,0 +1,1 @@
+  +c1
+  @@ -1,0 +3,1 @@
+  +c3
+  @@ -3,1 +4,0 @@
+  -c5
+
+  $ hg diff -U0 --nodates -p
+  diff -r 55d8ff78db23 f1
+  --- a/f1
+  +++ b/f1
+  @@ -0,0 +1,1 @@
+  +c1
+  @@ -1,0 +3,1 @@ c2
+  +c3
+  @@ -3,1 +4,0 @@ c4
+  -c5
--- a/tests/test-import.t	Tue Nov 08 17:08:58 2011 +0100
+++ b/tests/test-import.t	Wed Nov 09 16:55:59 2011 -0800
@@ -958,3 +958,39 @@
   $ diff want have
   $ cd ..
 
+import a unified diff with no lines of context (diff -U0)
+
+  $ hg init diffzero
+  $ cd diffzero
+  $ cat > f << EOF
+  > c2
+  > c4
+  > c5
+  > EOF
+  $ hg commit -Am0
+  adding f
+
+  $ hg import --no-commit - << EOF
+  > # HG changeset patch
+  > # User test
+  > # Date 0 0
+  > # Node ID f4974ab632f3dee767567b0576c0ec9a4508575c
+  > # Parent  8679a12a975b819fae5f7ad3853a2886d143d794
+  > 1
+  > diff -r 8679a12a975b -r f4974ab632f3 f
+  > --- a/f	Thu Jan 01 00:00:00 1970 +0000
+  > +++ b/f	Thu Jan 01 00:00:00 1970 +0000
+  > @@ -0,0 +1,1 @@
+  > +c1
+  > @@ -1,0 +3,1 @@
+  > +c3
+  > @@ -3,1 +4,0 @@
+  > -c5
+  > EOF
+  applying patch from stdin
+
+  $ cat f
+  c1
+  c2
+  c3
+  c4