changeset 31808:ca3b4a2b7e54

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().
author Denis Laxalde <denis@laxalde.org>
date Sat, 01 Apr 2017 12:24:59 +0200
parents e6eb86b154c5
children 35b8bb1ef02b
files mercurial/hgweb/webutil.py mercurial/mdiff.py tests/test-doctest.py
diffstat 3 files changed, 29 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hgweb/webutil.py	Fri Apr 22 21:46:33 2016 +0900
+++ b/mercurial/hgweb/webutil.py	Sat Apr 01 12:24:59 2017 +0200
@@ -27,6 +27,7 @@
     context,
     error,
     match,
+    mdiff,
     patch,
     pathutil,
     templatefilters,
@@ -473,8 +474,7 @@
         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:
--- a/mercurial/mdiff.py	Fri Apr 22 21:46:33 2016 +0900
+++ b/mercurial/mdiff.py	Sat Apr 01 12:24:59 2017 +0200
@@ -117,6 +117,31 @@
         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 @@
                     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'))
--- a/tests/test-doctest.py	Fri Apr 22 21:46:33 2016 +0900
+++ b/tests/test-doctest.py	Sat Apr 01 12:24:59 2017 +0200
@@ -32,6 +32,7 @@
 testmod('mercurial.hg')
 testmod('mercurial.hgweb.hgwebdir_mod')
 testmod('mercurial.match')
+testmod('mercurial.mdiff')
 testmod('mercurial.minirst')
 testmod('mercurial.patch')
 testmod('mercurial.pathutil')