patch: add a diffhunks function yielding (diffheaders, hunks)
authorDenis Laxalde <denis.laxalde@logilab.fr>
Fri, 03 Mar 2017 17:20:11 +0100
changeset 31274 a8023a64c40d
parent 31273 92714858dd3e
child 31275 e2f141045634
patch: add a diffhunks function yielding (diffheaders, hunks) trydiff function now yield (header, hunks) tuple that are processed by diffhunks(). Then diff() is a wrapper around diffhunks().
mercurial/patch.py
--- a/mercurial/patch.py	Fri Mar 03 17:46:40 2017 +0100
+++ b/mercurial/patch.py	Fri Mar 03 17:20:11 2017 +0100
@@ -2215,8 +2215,8 @@
 
     return mdiff.diffopts(**buildopts)
 
-def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None,
-         losedatafn=None, prefix='', relroot='', copy=None):
+def diff(repo, node1=None, node2=None, match=None, changes=None,
+         opts=None, losedatafn=None, prefix='', relroot='', copy=None):
     '''yields diff of changes to files between two nodes, or node and
     working directory.
 
@@ -2239,6 +2239,24 @@
 
     copy, if not empty, should contain mappings {dst@y: src@x} of copy
     information.'''
+    for header, hunks in diffhunks(repo, node1=node1, node2=node2, match=match,
+                                   changes=changes, opts=opts,
+                                   losedatafn=losedatafn, prefix=prefix,
+                                   relroot=relroot, copy=copy):
+        text = ''.join(sum((list(hlines) for hrange, hlines in hunks), []))
+        if header and (text or len(header) > 1):
+            yield '\n'.join(header) + '\n'
+        if text:
+            yield text
+
+def diffhunks(repo, node1=None, node2=None, match=None, changes=None,
+              opts=None, losedatafn=None, prefix='', relroot='', copy=None):
+    """Yield diff of changes to files in the form of (`header`, `hunks`) tuples
+    where `header` is a list of diff headers and `hunks` is an iterable of
+    (`hunkrange`, `hunklines`) tuples.
+
+    See diff() for the meaning of parameters.
+    """
 
     if opts is None:
         opts = mdiff.defaultopts
@@ -2539,6 +2557,7 @@
             if text:
                 header.append('index %s..%s' %
                               (gitindex(content1), gitindex(content2)))
+            hunks = (None, [text]),
         else:
             if opts.git and opts.index > 0:
                 flag = flag1
@@ -2553,11 +2572,7 @@
                                             content2, date2,
                                             path1, path2, opts=opts)
             header.extend(uheaders)
-            text = ''.join(sum((list(hlines) for hrange, hlines in hunks), []))
-        if header and (text or len(header) > 1):
-            yield '\n'.join(header) + '\n'
-        if text:
-            yield text
+        yield header, hunks
 
 def diffstatsum(stats):
     maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False