[PATCH 2 of 9] formatter: carry opts to file-based formatters by basefm

Yuya Nishihara yuya at tcha.org
Thu Apr 12 12:17:05 EDT 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1523543158 -32400
#      Thu Apr 12 23:25:58 2018 +0900
# Node ID aa00793f5b3567c6768e8f7be43c3043ea29f536
# Parent  e6609e390f284931c10f06f3c8258769fad9bbc5
formatter: carry opts to file-based formatters by basefm

This makes it slightly easier to port "hg export" to formatter.

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -2231,7 +2231,7 @@ def cat(ui, repo, ctx, matcher, basefm, 
                 os.makedirs(os.path.dirname(filename))
             except OSError:
                 pass
-        with formatter.maybereopen(basefm, filename, opts) as fm:
+        with formatter.maybereopen(basefm, filename) as fm:
             _updatecatformatter(fm, ctx, matcher, path, opts.get('decode'))
 
     # Automation often uses hg cat on single files, so special case it
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1318,7 +1318,7 @@ def cat(ui, repo, file1, *pats, **opts):
         fntemplate = ''
 
     if fntemplate:
-        fm = formatter.nullformatter(ui, 'cat')
+        fm = formatter.nullformatter(ui, 'cat', opts)
     else:
         ui.pager('cat')
         fm = ui.formatter('cat', opts)
diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -160,6 +160,7 @@ class baseformatter(object):
     def __init__(self, ui, topic, opts, converter):
         self._ui = ui
         self._topic = topic
+        self._opts = opts
         self._converter = converter
         self._item = None
         # function to convert node to string suitable for this output
@@ -222,9 +223,9 @@ class baseformatter(object):
         if self._item is not None:
             self._showitem()
 
-def nullformatter(ui, topic):
+def nullformatter(ui, topic, opts):
     '''formatter that prints nothing'''
-    return baseformatter(ui, topic, opts={}, converter=_nullconverter)
+    return baseformatter(ui, topic, opts, converter=_nullconverter)
 
 class _nestedformatter(baseformatter):
     '''build sub items and store them in the parent formatter'''
@@ -595,7 +596,7 @@ def openformatter(ui, filename, topic, o
 def _neverending(fm):
     yield fm
 
-def maybereopen(fm, filename, opts):
+def maybereopen(fm, filename):
     """Create a formatter backed by file if filename specified, else return
     the given formatter
 
@@ -603,6 +604,6 @@ def maybereopen(fm, filename, opts):
     of the given formatter.
     """
     if filename:
-        return openformatter(fm._ui, filename, fm._topic, opts)
+        return openformatter(fm._ui, filename, fm._topic, fm._opts)
     else:
         return _neverending(fm)


More information about the Mercurial-devel mailing list