Patchwork [2,of,2] commands: add template support for config

login
register
mail settings
Submitter Mathias De Maré
Date Sept. 19, 2016, 2:14 p.m.
Message ID <c037f0fbd6374b585537.1474294482@waste.org>
Download mbox | patch
Permalink /patch/16666/
State Accepted
Headers show

Comments

Mathias De Maré - Sept. 19, 2016, 2:14 p.m.
# HG changeset patch
# User Mathias De Maré <mathias.demare@gmail.com>
# Date 1472447235 -7200
#      Mon Aug 29 07:07:15 2016 +0200
# Node ID c037f0fbd6374b5855373a82056015289910fe69
# Parent  084ca55ce77a5fe77bc93db26346cc71211a0070
commands: add template support for config

V2:
- Limit escaping to plain formatting only
- Use the formatter consistently (no more ui.debug)
- Always include 'name' and 'value'

V3:
- Always convert 'value' to string (this also makes sure we handle functions)
- Keep real debug message as ui.debug for now
- Add additional tests.
  Note: I'm not quite sure about the best approach to handling
  the 'print the full config' case.
  For me, it printed the 'ui.promptecho' key at the end.
  I went with globs there as that at least tests the json display reliably.

Example output:
[
 {
  "name": "ui.username",
  "source": "/home/mathias/.hgrc:2",
  "value": "Mathias De Maré <mathias.demare@gmail.com>"
 }
]
Yuya Nishihara - Sept. 20, 2016, 1:32 p.m.
On Mon, 19 Sep 2016 09:14:42 -0500, Mathias De Maré wrote:
> # HG changeset patch
> # User Mathias De Maré <mathias.demare@gmail.com>
> # Date 1472447235 -7200
> #      Mon Aug 29 07:07:15 2016 +0200
> # Node ID c037f0fbd6374b5855373a82056015289910fe69
> # Parent  084ca55ce77a5fe77bc93db26346cc71211a0070
> commands: add template support for config

Queued this, thanks.

>   Note: I'm not quite sure about the best approach to handling
>   the 'print the full config' case.
>   For me, it printed the 'ui.promptecho' key at the end.
>   I went with globs there as that at least tests the json display reliably.

Maybe we can switch HGRCPATH to get cleaner result? IIRC, the order of
ui.walkconfig() is deterministic.

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1788,7 +1788,7 @@ 
     [('u', 'untrusted', None, _('show untrusted configuration options')),
      ('e', 'edit', None, _('edit user config')),
      ('l', 'local', None, _('edit repository config')),
-     ('g', 'global', None, _('edit global config'))],
+     ('g', 'global', None, _('edit global config'))] + formatteropts,
     _('[-u] [NAME]...'),
     optionalrepo=True)
 def config(ui, repo, *values, **opts):
@@ -1849,6 +1849,7 @@ 
                   onerr=error.Abort, errprefix=_("edit failed"))
         return
 
+    fm = ui.formatter('config', opts)
     for f in scmutil.rcpath():
         ui.debug('read config from: %s\n' % f)
     untrusted = bool(opts.get('untrusted'))
@@ -1859,25 +1860,32 @@ 
             raise error.Abort(_('only one config item permitted'))
     matched = False
     for section, name, value in ui.walkconfig(untrusted=untrusted):
-        value = str(value).replace('\n', '\\n')
-        sectname = section + '.' + name
+        value = str(value)
+        if fm.isplain():
+            value = value.replace('\n', '\\n')
+        entryname = section + '.' + name
         if values:
             for v in values:
                 if v == section:
-                    ui.debug('%s: ' %
+                    fm.startitem()
+                    fm.condwrite(ui.debugflag, 'source', '%s: ',
                              ui.configsource(section, name, untrusted))
-                    ui.write('%s=%s\n' % (sectname, value))
+                    fm.write('name value', '%s=%s\n', entryname, value)
                     matched = True
-                elif v == sectname:
-                    ui.debug('%s: ' %
+                elif v == entryname:
+                    fm.startitem()
+                    fm.condwrite(ui.debugflag, 'source', '%s: ',
                              ui.configsource(section, name, untrusted))
-                    ui.write(value, '\n')
+                    fm.write('value', '%s\n', value)
+                    fm.data(name=entryname)
                     matched = True
         else:
-            ui.debug('%s: ' %
+            fm.startitem()
+            fm.condwrite(ui.debugflag, 'source', '%s: ',
                      ui.configsource(section, name, untrusted))
-            ui.write('%s=%s\n' % (sectname, value))
+            fm.write('name value', '%s=%s\n', entryname, value)
             matched = True
+    fm.end()
     if matched:
         return 0
     return 1
diff --git a/tests/test-completion.t b/tests/test-completion.t
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -232,7 +232,7 @@ 
   branches: active, closed, template
   bundle: force, rev, branch, base, all, type, ssh, remotecmd, insecure
   cat: output, rev, decode, include, exclude
-  config: untrusted, edit, local, global
+  config: untrusted, edit, local, global, template
   copy: after, force, include, exclude, dry-run
   debugancestor: 
   debugapplystreamclonebundle: 
diff --git a/tests/test-config.t b/tests/test-config.t
--- a/tests/test-config.t
+++ b/tests/test-config.t
@@ -54,6 +54,36 @@ 
   Section.KeY=Case Sensitive
   Section.key=lower case
 
+  $ hg showconfig Section -Tjson
+  [
+   {
+    "name": "Section.KeY",
+    "source": "*.hgrc:16", (glob)
+    "value": "Case Sensitive"
+   },
+   {
+    "name": "Section.key",
+    "source": "*.hgrc:17", (glob)
+    "value": "lower case"
+   }
+  ]
+  $ hg showconfig Section.KeY -Tjson
+  [
+   {
+    "name": "Section.KeY",
+    "source": "*.hgrc:16", (glob)
+    "value": "Case Sensitive"
+   }
+  ]
+  $ hg showconfig -Tjson | tail -7
+   },
+   {
+    "name": "*", (glob)
+    "source": "*", (glob)
+    "value": "*" (glob)
+   }
+  ]
+
 Test "%unset"
 
   $ cat >> $HGRCPATH <<EOF