diff mercurial/cmdutil.py @ 37604:daafaff4e5be

export: enable formatter support (API) This change is basically the same as "hg cat". A formatter object is created by caller. .. api:: ``cmdutil.export()`` takes a formatter as an argument.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 12 Apr 2018 23:13:55 +0900
parents 678d760c71ff
children fd1dd79cff20
line wrap: on
line diff
--- a/mercurial/cmdutil.py	Thu Apr 12 22:59:49 2018 +0900
+++ b/mercurial/cmdutil.py	Thu Apr 12 23:13:55 2018 +0900
@@ -1565,24 +1565,17 @@
         # TODO: make it structured?
         fm.data(diff=b''.join(chunkiter))
 
-def _exportfile(repo, revs, fp, switch_parent, diffopts, match):
+def _exportfile(repo, revs, fm, dest, switch_parent, diffopts, match):
     """Export changesets to stdout or a single file"""
-    dest = '<unnamed>'
-    if fp:
-        dest = getattr(fp, 'name', dest)
-        fm = formatter.formatter(repo.ui, fp, 'export', {})
-    else:
-        fm = repo.ui.formatter('export', {})
-
     for seqno, rev in enumerate(revs, 1):
         ctx = repo[rev]
         if not dest.startswith('<'):
             repo.ui.note("%s\n" % dest)
         fm.startitem()
         _exportsingle(repo, ctx, fm, match, switch_parent, seqno, diffopts)
-    fm.end()
-
-def _exportfntemplate(repo, revs, fntemplate, switch_parent, diffopts, match):
+
+def _exportfntemplate(repo, revs, basefm, fntemplate, switch_parent, diffopts,
+                      match):
     """Export changesets to possibly multiple files"""
     total = len(revs)
     revwidth = max(len(str(rev)) for rev in revs)
@@ -1595,7 +1588,7 @@
         filemap.setdefault(dest, []).append((seqno, rev))
 
     for dest in filemap:
-        with formatter.openformatter(repo.ui, dest, 'export', {}) as fm:
+        with formatter.maybereopen(basefm, dest) as fm:
             repo.ui.note("%s\n" % dest)
             for seqno, rev in filemap[dest]:
                 fm.startitem()
@@ -1603,13 +1596,14 @@
                 _exportsingle(repo, ctx, fm, match, switch_parent, seqno,
                               diffopts)
 
-def export(repo, revs, fntemplate='hg-%h.patch', switch_parent=False,
+def export(repo, revs, basefm, fntemplate='hg-%h.patch', switch_parent=False,
            opts=None, match=None):
     '''export changesets as hg patches
 
     Args:
       repo: The repository from which we're exporting revisions.
       revs: A list of revisions to export as revision numbers.
+      basefm: A formatter to which patches should be written.
       fntemplate: An optional string to use for generating patch file names.
       switch_parent: If True, show diffs against second parent when not nullid.
                      Default is false, which always shows diff against p1.
@@ -1624,16 +1618,19 @@
       destinations:
         fntemplate specified: Each rev is written to a unique file named using
                             the given template.
-        Otherwise: All revs written to repo.ui.write()
+        Otherwise: All revs will be written to basefm.
     '''
     if not fntemplate:
-        _exportfile(repo, revs, None, switch_parent, opts, match)
+        _exportfile(repo, revs, basefm, '<unnamed>', switch_parent, opts, match)
     else:
-        _exportfntemplate(repo, revs, fntemplate, switch_parent, opts, match)
+        _exportfntemplate(repo, revs, basefm, 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)
+    dest = getattr(fp, 'name', '<unnamed>')
+    with formatter.formatter(repo.ui, fp, 'export', {}) as fm:
+        _exportfile(repo, revs, fm, dest, switch_parent, opts, match)
 
 def showmarker(fm, marker, index=None):
     """utility function to display obsolescence marker in a readable way