Patchwork [5,of,9] export: split cmdutil.export() to single-file and maybe-multiple-files cases

login
register
mail settings
Submitter Yuya Nishihara
Date April 12, 2018, 4:17 p.m.
Message ID <97ed426f12977158a72a.1523549828@mimosa>
Download mbox | patch
Permalink /patch/30810/
State Accepted
Headers show

Comments

Yuya Nishihara - April 12, 2018, 4:17 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1523537762 -32400
#      Thu Apr 12 21:56:02 2018 +0900
# Node ID 97ed426f12977158a72a81e64514aa69a735766b
# Parent  905b4afbfed88a336287cc79e7f3258fd05380cd
export: split cmdutil.export() to single-file and maybe-multiple-files cases

Porting "hg export" to formatter is a bit hard because cmdutil.export() may
append to files if the fntemplate is specified. This patch splits the hard
part from the trivial case.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1559,6 +1559,39 @@  def _exportsingle(repo, ctx, match, swit
     for chunk, label in patch.diffui(repo, prev, node, match, opts=diffopts):
         write(chunk, label=label)
 
+def _exportfile(repo, revs, fp, switch_parent, diffopts, match):
+    """Export changesets to stdout or a single file"""
+    dest = '<unnamed>'
+    if fp:
+        dest = getattr(fp, 'name', dest)
+        def write(s, **kw):
+            fp.write(s)
+    else:
+        write = repo.ui.write
+
+    for seqno, rev in enumerate(revs, 1):
+        ctx = repo[rev]
+        if not dest.startswith('<'):
+            repo.ui.note("%s\n" % dest)
+        _exportsingle(repo, ctx, match, switch_parent, seqno, write, diffopts)
+
+def _exportfntemplate(repo, revs, fntemplate, switch_parent, diffopts, match):
+    """Export changesets to possibly multiple files"""
+    total = len(revs)
+    revwidth = max(len(str(rev)) for rev in revs)
+    filemode = {}
+
+    for seqno, rev in enumerate(revs, 1):
+        ctx = repo[rev]
+        fo = makefileobj(ctx, fntemplate, mode='wb', modemap=filemode,
+                         total=total, seqno=seqno, revwidth=revwidth)
+        dest = fo.name
+        def write(s, **kw):
+            fo.write(s)
+        repo.ui.note("%s\n" % dest)
+        _exportsingle(repo, ctx, match, switch_parent, seqno, write, diffopts)
+        fo.close()
+
 def export(repo, revs, fntemplate='hg-%h.patch', fp=None, switch_parent=False,
            opts=None, match=None):
     '''export changesets as hg patches
@@ -1585,35 +1618,10 @@  def export(repo, revs, fntemplate='hg-%h
                             the given template.
         Neither fp nor template specified: All revs written to repo.ui.write()
     '''
-
-    total = len(revs)
-    revwidth = max(len(str(rev)) for rev in revs)
-    filemode = {}
-
-    write = None
-    dest = '<unnamed>'
-    if fp:
-        dest = getattr(fp, 'name', dest)
-        def write(s, **kw):
-            fp.write(s)
-    elif not fntemplate:
-        write = repo.ui.write
-
-    for seqno, rev in enumerate(revs, 1):
-        ctx = repo[rev]
-        fo = None
-        if not fp and fntemplate:
-            fo = makefileobj(ctx, fntemplate, mode='wb', modemap=filemode,
-                             total=total, seqno=seqno, revwidth=revwidth)
-            dest = fo.name
-            def write(s, **kw):
-                fo.write(s)
-        if not dest.startswith('<'):
-            repo.ui.note("%s\n" % dest)
-        _exportsingle(
-            repo, ctx, match, switch_parent, seqno, write, opts)
-        if fo is not None:
-            fo.close()
+    if fp or not fntemplate:
+        _exportfile(repo, revs, fp, switch_parent, opts, match)
+    else:
+        _exportfntemplate(repo, revs, fntemplate, switch_parent, opts, match)
 
 def showmarker(fm, marker, index=None):
     """utility function to display obsolescence marker in a readable way