Patchwork [08,of,10,RESEND] patch: add a diffhunks function yielding (diffheaders, hunks)

login
register
mail settings
Submitter Denis Laxalde
Date March 9, 2017, 8:08 a.m.
Message ID <c907c7574d72f9d82eb5.1489046885@sh77.tls.logilab.fr>
Download mbox | patch
Permalink /patch/19059/
State Accepted
Headers show

Comments

Denis Laxalde - March 9, 2017, 8:08 a.m.
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1488558011 -3600
#      Fri Mar 03 17:20:11 2017 +0100
# Node ID c907c7574d72f9d82eb590057ecc19133920ad00
# Parent  71710fc049991061b63818ea10aa24f24603c514
# Available At http://hg.logilab.org/users/dlaxalde/hg
#              hg pull http://hg.logilab.org/users/dlaxalde/hg -r c907c7574d72
# EXP-Topic diffhunks
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().

Patch

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -2213,8 +2213,8 @@  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):
+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.
 
@@ -2237,6 +2237,24 @@  def diff(repo, node1=None, node2=None, m
 
     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
@@ -2537,6 +2555,7 @@  def trydiff(repo, revs, ctx1, ctx2, modi
             if text:
                 header.append('index %s..%s' %
                               (gitindex(content1), gitindex(content2)))
+            hunks = (None, [text]),
         else:
             if opts.git and opts.index > 0:
                 flag = flag1
@@ -2551,11 +2570,7 @@  def trydiff(repo, revs, ctx1, ctx2, modi
                                             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