[PATCH 4 of 8 V2] patch: add filtering by line ranges in diff()

Denis Laxalde denis at laxalde.org
Sat Feb 25 04:06:01 EST 2017


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1484921270 -3600
#      Fri Jan 20 15:07:50 2017 +0100
# Node ID 6625dfbecf264f51748a96aa7203fc09bee597e9
# Parent  fcc621e177b1034101f7acc0ba6828e9db6be1a7
# Available At https://hg.logilab.org/users/dlaxalde/hg
#              hg pull https://hg.logilab.org/users/dlaxalde/hg -r 6625dfbecf26
# EXP-Topic linerange-log/hgweb-filelog
patch: add filtering by line ranges in diff()

We add a "lineranges" parameter to patch.diff() that is mapping from filectx
to line range tuple (fromline, toline). This is used in getfilectx inner
function (used in patch.trydiff()) so that the latter returns the line range
associated with the filectx, if any. Then, in patch.trydiff() we pass this
information to mdiff.unidiff() where hunks will be filtered accordingly.

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -2214,7 +2214,7 @@ def difffeatureopts(ui, opts=None, untru
     return mdiff.diffopts(**buildopts)
 
 def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None,
-         losedatafn=None, prefix='', relroot='', copy=None):
+         losedatafn=None, prefix='', relroot='', copy=None, lineranges=None):
     '''yields diff of changes to files between two nodes, or node and
     working directory.
 
@@ -2236,7 +2236,12 @@ def diff(repo, node1=None, node2=None, m
     patterns that fall outside it will be ignored.
 
     copy, if not empty, should contain mappings {dst at y: src at x} of copy
-    information.'''
+    information.
+
+    lineranges, if not None, should be a mapping {fctx: (fromline, toline)}
+    where fctx is a filectx at specified nodes and (fromline, toline) is a
+    range of lines outside which diff hunks should be filtered out.
+    '''
 
     if opts is None:
         opts = mdiff.defaultopts
@@ -2256,7 +2261,10 @@ def diff(repo, node1=None, node2=None, m
             else:
                 order.remove(f)
             order.append(f)
-            return fctx
+            linerange = None
+            if lineranges is not None:
+                linerange = lineranges.get(fctx, None)
+            return fctx, linerange
         return getfilectx
     getfilectx = lrugetfilectx()
 
@@ -2480,12 +2488,16 @@ def trydiff(repo, revs, ctx1, ctx2, modi
         content2 = None
         flag1 = None
         flag2 = None
+        linerange1 = None
+        linerange2 = None
         if f1:
-            content1 = getfilectx(f1, ctx1).data()
+            fctx1, linerange1 = getfilectx(f1, ctx1)
+            content1 = fctx1.data()
             if opts.git or losedatafn:
                 flag1 = ctx1.flags(f1)
         if f2:
-            content2 = getfilectx(f2, ctx2).data()
+            fctx2, linerange2 = getfilectx(f2, ctx2)
+            content2 = fctx2.data()
             if opts.git or losedatafn:
                 flag2 = ctx2.flags(f2)
         binary = False
@@ -2549,7 +2561,8 @@ def trydiff(repo, revs, ctx1, ctx2, modi
 
             text = mdiff.unidiff(content1, date1,
                                  content2, date2,
-                                 path1, path2, opts=opts)
+                                 path1, path2, opts=opts,
+                                 rangea=linerange1, rangeb=linerange2)
         if header and (text or len(header) > 1):
             yield '\n'.join(header) + '\n'
         if text:


More information about the Mercurial-devel mailing list