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')