Patchwork mdiff: add a hunkinrange helper function

login
register
mail settings
Submitter Denis Laxalde
Date April 3, 2017, 8:32 a.m.
Message ID <e8e4889598b8e0d50f2c.1491208358@sh77.tls.logilab.fr>
Download mbox | patch
Permalink /patch/19925/
State Accepted
Headers show

Comments

Denis Laxalde - April 3, 2017, 8:32 a.m.
# HG changeset patch
# User Denis Laxalde <denis@laxalde.org>
# Date 1491042299 -7200
#      Sat Apr 01 12:24:59 2017 +0200
# Node ID e8e4889598b8e0d50f2cc14c87a83268187c4f5f
# Parent  04ec317b81280c189fcea33a05c8cbbac3c186b1
# Available At http://hg.logilab.org/users/dlaxalde/hg
#              hg pull http://hg.logilab.org/users/dlaxalde/hg -r e8e4889598b8
mdiff: add a hunkinrange helper function

This factors out hunk filtering logic by line range that is similar in
mdiff.blocksinrange() and hgweb.webutil.diffs().
Augie Fackler - April 3, 2017, 9:04 p.m.
On Mon, Apr 03, 2017 at 10:32:38AM +0200, Denis Laxalde wrote:
> # HG changeset patch
> # User Denis Laxalde <denis@laxalde.org>
> # Date 1491042299 -7200
> #      Sat Apr 01 12:24:59 2017 +0200
> # Node ID e8e4889598b8e0d50f2cc14c87a83268187c4f5f
> # Parent  04ec317b81280c189fcea33a05c8cbbac3c186b1
> # Available At http://hg.logilab.org/users/dlaxalde/hg

queued, thanks

> #              hg pull http://hg.logilab.org/users/dlaxalde/hg -r e8e4889598b8
> mdiff: add a hunkinrange helper function
>
> This factors out hunk filtering logic by line range that is similar in
> mdiff.blocksinrange() and hgweb.webutil.diffs().
>
> diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
> --- a/mercurial/hgweb/webutil.py
> +++ b/mercurial/hgweb/webutil.py
> @@ -27,6 +27,7 @@ from .. import (
>      context,
>      error,
>      match,
> +    mdiff,
>      patch,
>      pathutil,
>      templatefilters,
> @@ -473,8 +474,7 @@ def diffs(web, tmpl, ctx, basectx, files
>          for hunkrange, hunklines in hunks:
>              if linerange is not None and hunkrange is not None:
>                  s1, l1, s2, l2 = hunkrange
> -                lb, ub = linerange
> -                if not (lb < s2 + l2 and ub > s2):
> +                if not mdiff.hunkinrange((s2, l2), linerange):
>                      continue
>              lines.extend(hunklines)
>          if lines:
> diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py
> --- a/mercurial/mdiff.py
> +++ b/mercurial/mdiff.py
> @@ -117,6 +117,31 @@ def splitblock(base1, lines1, base2, lin
>          s1 = i1
>          s2 = i2
>
> +def hunkinrange(hunk, linerange):
> +    """Return True if `hunk` defined as (start, length) is in `linerange`
> +    defined as (lowerbound, upperbound).
> +
> +    >>> hunkinrange((5, 10), (2, 7))
> +    True
> +    >>> hunkinrange((5, 10), (6, 12))
> +    True
> +    >>> hunkinrange((5, 10), (13, 17))
> +    True
> +    >>> hunkinrange((5, 10), (3, 17))
> +    True
> +    >>> hunkinrange((5, 10), (1, 3))
> +    False
> +    >>> hunkinrange((5, 10), (18, 20))
> +    False
> +    >>> hunkinrange((5, 10), (1, 5))
> +    False
> +    >>> hunkinrange((5, 10), (15, 27))
> +    False
> +    """
> +    start, length = hunk
> +    lowerbound, upperbound = linerange
> +    return lowerbound < start + length and start < upperbound
> +
>  def blocksinrange(blocks, rangeb):
>      """filter `blocks` like (a1, a2, b1, b2) from items outside line range
>      `rangeb` from ``(b1, b2)`` point of view.
> @@ -150,7 +175,7 @@ def blocksinrange(blocks, rangeb):
>                      uba = a1 + (ubb - b1)
>                  else:
>                      uba = a2
> -        if lbb < b2 and b1 < ubb:
> +        if hunkinrange((b1, (b2 - b1)), rangeb):
>              filteredblocks.append(block)
>      if lba is None or uba is None or uba < lba:
>          raise error.Abort(_('line range exceeds file size'))
> diff --git a/tests/test-doctest.py b/tests/test-doctest.py
> --- a/tests/test-doctest.py
> +++ b/tests/test-doctest.py
> @@ -32,6 +32,7 @@ testmod('mercurial.formatter')
>  testmod('mercurial.hg')
>  testmod('mercurial.hgweb.hgwebdir_mod')
>  testmod('mercurial.match')
> +testmod('mercurial.mdiff')
>  testmod('mercurial.minirst')
>  testmod('mercurial.patch')
>  testmod('mercurial.pathutil')
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -27,6 +27,7 @@  from .. import (
     context,
     error,
     match,
+    mdiff,
     patch,
     pathutil,
     templatefilters,
@@ -473,8 +474,7 @@  def diffs(web, tmpl, ctx, basectx, files
         for hunkrange, hunklines in hunks:
             if linerange is not None and hunkrange is not None:
                 s1, l1, s2, l2 = hunkrange
-                lb, ub = linerange
-                if not (lb < s2 + l2 and ub > s2):
+                if not mdiff.hunkinrange((s2, l2), linerange):
                     continue
             lines.extend(hunklines)
         if lines:
diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py
--- a/mercurial/mdiff.py
+++ b/mercurial/mdiff.py
@@ -117,6 +117,31 @@  def splitblock(base1, lines1, base2, lin
         s1 = i1
         s2 = i2
 
+def hunkinrange(hunk, linerange):
+    """Return True if `hunk` defined as (start, length) is in `linerange`
+    defined as (lowerbound, upperbound).
+
+    >>> hunkinrange((5, 10), (2, 7))
+    True
+    >>> hunkinrange((5, 10), (6, 12))
+    True
+    >>> hunkinrange((5, 10), (13, 17))
+    True
+    >>> hunkinrange((5, 10), (3, 17))
+    True
+    >>> hunkinrange((5, 10), (1, 3))
+    False
+    >>> hunkinrange((5, 10), (18, 20))
+    False
+    >>> hunkinrange((5, 10), (1, 5))
+    False
+    >>> hunkinrange((5, 10), (15, 27))
+    False
+    """
+    start, length = hunk
+    lowerbound, upperbound = linerange
+    return lowerbound < start + length and start < upperbound
+
 def blocksinrange(blocks, rangeb):
     """filter `blocks` like (a1, a2, b1, b2) from items outside line range
     `rangeb` from ``(b1, b2)`` point of view.
@@ -150,7 +175,7 @@  def blocksinrange(blocks, rangeb):
                     uba = a1 + (ubb - b1)
                 else:
                     uba = a2
-        if lbb < b2 and b1 < ubb:
+        if hunkinrange((b1, (b2 - b1)), rangeb):
             filteredblocks.append(block)
     if lba is None or uba is None or uba < lba:
         raise error.Abort(_('line range exceeds file size'))
diff --git a/tests/test-doctest.py b/tests/test-doctest.py
--- a/tests/test-doctest.py
+++ b/tests/test-doctest.py
@@ -32,6 +32,7 @@  testmod('mercurial.formatter')
 testmod('mercurial.hg')
 testmod('mercurial.hgweb.hgwebdir_mod')
 testmod('mercurial.match')
+testmod('mercurial.mdiff')
 testmod('mercurial.minirst')
 testmod('mercurial.patch')
 testmod('mercurial.pathutil')