diff mercurial/patch.py @ 7308:b6f5490effbf

patch: turn patch.diff() into a generator This should even be a little faster than passing in an fp argument.
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Mon, 03 Nov 2008 16:48:23 +0100
parents 6a51ca1e05c3
children eae1767cc6a8
line wrap: on
line diff
--- a/mercurial/patch.py	Mon Nov 03 16:31:47 2008 +0100
+++ b/mercurial/patch.py	Mon Nov 03 16:48:23 2008 +0100
@@ -1169,9 +1169,8 @@
         header.append('old mode %s\n' % omode)
         header.append('new mode %s\n' % nmode)
 
-def diff(repo, node1=None, node2=None, match=None,
-         fp=None, changes=None, opts=None):
-    '''print diff of changes to files between two nodes, or node and
+def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None):
+    '''yields diff of changes to files between two nodes, or node and
     working directory.
 
     if node1 is None, use first dirstate parent instead.
@@ -1182,8 +1181,6 @@
 
     if opts is None:
         opts = mdiff.defaultopts
-    if fp is None:
-        fp = repo.ui
 
     if not node1:
         node1 = repo.dirstate.parents()[0]
@@ -1274,9 +1271,10 @@
                                     # ctx2 date may be dynamic
                                     tn, util.datestr(ctx2.date()),
                                     a, b, r, opts=opts)
-            if text or len(header) > 1:
-                fp.write(''.join(header))
-            fp.write(text)
+            if header and (text or len(header) > 1):
+                yield ''.join(header)
+            if text:
+                yield text
 
 def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False,
            opts=None):
@@ -1312,7 +1310,8 @@
         fp.write(ctx.description().rstrip())
         fp.write("\n\n")
 
-        diff(repo, prev, node, fp=fp, opts=opts)
+        for chunk in diff(repo, prev, node, opts=opts):
+            fp.write(chunk)
         if fp not in (sys.stdout, repo.ui):
             fp.close()