# HG changeset patch
# User Yuya Nishihara <y...@tcha.org>
# Date 1523541589 -32400
#      Thu Apr 12 22:59:49 2018 +0900
# Node ID 499cfc83b0cba948eec6b1e30998862b7e7fc051
# Parent  6041c9cdcee8f25328016f1727055d1bc1677054
export: extract function to write patch to file object (API)

This is common use case of cmdutil.export(), and we wouldn't want to handle
formatter thingy everywhere.

.. api::

   The ``fp`` argument is removed from ``cmdutil.export()``. Use
   ``cmdutil.exportfile()`` instead.

diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -2182,9 +2182,8 @@ class queue(object):
                     self.checkpatchname(patchname, force)
                     self.fullseries.insert(0, patchname)
-                    patchf = self.opener(patchname, "w")
-                    cmdutil.export(repo, [n], fp=patchf, opts=diffopts)
-                    patchf.close()
+                    with self.opener(patchname, "w") as fp:
+                        cmdutil.exportfile(repo, [n], fp, opts=diffopts)
                     se = statusentry(n, patchname)
                     self.applied.insert(0, se)
diff --git a/hgext/patchbomb.py b/hgext/patchbomb.py
--- a/hgext/patchbomb.py
+++ b/hgext/patchbomb.py
@@ -306,8 +306,8 @@ def _getpatches(repo, revs, **opts):
             ui.warn(_('warning: working directory has '
                       'uncommitted changes\n'))
         output = stringio()
-        cmdutil.export(repo, [r], fp=output,
-                     opts=patch.difffeatureopts(ui, opts, git=True))
+        cmdutil.exportfile(repo, [r], output,
+                           opts=patch.difffeatureopts(ui, opts, git=True))
         yield output.getvalue().split('\n')
 def _getbundle(repo, dest, **opts):
     """return a bundle containing changesets missing in "dest"
diff --git a/hgext/shelve.py b/hgext/shelve.py
--- a/hgext/shelve.py
+++ b/hgext/shelve.py
@@ -413,9 +413,8 @@ def _nothingtoshelvemessaging(ui, repo, 
 def _shelvecreatedcommit(repo, node, name):
     bases = list(mutableancestors(repo[node]))
     shelvedfile(repo, name, 'hg').writebundle(bases, node)
-    cmdutil.export(repo, [node],
-                   fp=shelvedfile(repo, name, patchextension).opener('wb'),
-                   opts=mdiff.diffopts(git=True))
+    with shelvedfile(repo, name, patchextension).opener('wb') as fp:
+        cmdutil.exportfile(repo, [node], fp, opts=mdiff.diffopts(git=True))
 def _includeunknownfiles(repo, pats, opts, extra):
     s = repo.status(match=scmutil.match(repo[None], pats, opts),
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1603,7 +1603,7 @@ def _exportfntemplate(repo, revs, fntemp
                 _exportsingle(repo, ctx, fm, match, switch_parent, seqno,
-def export(repo, revs, fntemplate='hg-%h.patch', fp=None, switch_parent=False,
+def export(repo, revs, fntemplate='hg-%h.patch', switch_parent=False,
            opts=None, match=None):
     '''export changesets as hg patches
@@ -1611,7 +1611,6 @@ def export(repo, revs, fntemplate='hg-%h
       repo: The repository from which we're exporting revisions.
       revs: A list of revisions to export as revision numbers.
       fntemplate: An optional string to use for generating patch file names.
-      fp: An optional file-like object to which patches should be written.
       switch_parent: If True, show diffs against second parent when not nullid.
                      Default is false, which always shows diff against p1.
       opts: diff options to use for generating the patch.
@@ -1623,17 +1622,19 @@ def export(repo, revs, fntemplate='hg-%h
     Side Effect:
       "HG Changeset Patch" data is emitted to one of the following
-        fp is specified: All revs are written to the specified
-                         file-like object.
         fntemplate specified: Each rev is written to a unique file named using
                             the given template.
-        Neither fp nor template specified: All revs written to repo.ui.write()
+        Otherwise: All revs written to repo.ui.write()
-    if fp or not fntemplate:
-        _exportfile(repo, revs, fp, switch_parent, opts, match)
+    if not fntemplate:
+        _exportfile(repo, revs, None, switch_parent, opts, match)
         _exportfntemplate(repo, revs, fntemplate, switch_parent, opts, match)
+def exportfile(repo, revs, fp, switch_parent=False, opts=None, match=None):
+    """Export changesets to the given file stream"""
+    _exportfile(repo, revs, fp, switch_parent, opts, match)
 def showmarker(fm, marker, index=None):
     """utility function to display obsolescence marker in a readable way
Mercurial-devel mailing list

Reply via email to