export: extract function to write patch to file object (API)
authorYuya Nishihara <yuya@tcha.org>
Thu, 12 Apr 2018 22:59:49 +0900
changeset 37603 678d760c71ff
parent 37602 52670eaa14b4
child 37604 daafaff4e5be
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.
hgext/mq.py
hgext/patchbomb.py
hgext/shelve.py
mercurial/cmdutil.py
--- a/hgext/mq.py	Thu Apr 12 22:39:43 2018 +0900
+++ b/hgext/mq.py	Thu Apr 12 22:59:49 2018 +0900
@@ -2182,9 +2182,8 @@
                     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)
--- a/hgext/patchbomb.py	Thu Apr 12 22:39:43 2018 +0900
+++ b/hgext/patchbomb.py	Thu Apr 12 22:59:49 2018 +0900
@@ -306,8 +306,8 @@
             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"
--- a/hgext/shelve.py	Thu Apr 12 22:39:43 2018 +0900
+++ b/hgext/shelve.py	Thu Apr 12 22:59:49 2018 +0900
@@ -413,9 +413,8 @@
 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),
--- a/mercurial/cmdutil.py	Thu Apr 12 22:39:43 2018 +0900
+++ b/mercurial/cmdutil.py	Thu Apr 12 22:59:49 2018 +0900
@@ -1603,7 +1603,7 @@
                 _exportsingle(repo, ctx, fm, match, switch_parent, seqno,
                               diffopts)
 
-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 @@
       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 @@
     Side Effect:
       "HG Changeset Patch" data is emitted to one of the following
       destinations:
-        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)
     else:
         _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