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

login
register
mail settings
Submitter Mathias De Maré
Date Aug. 8, 2016, 3:54 p.m.
Message ID <c497d3c35883e43599d0.1470671686@waste.org>
Download mbox | patch
Permalink /patch/16206/
State Changes Requested
Headers show

Comments

Mathias De Maré - Aug. 8, 2016, 3:54 p.m.
# HG changeset patch
# User Mathias De Maré <mathias.demare@gmail.com>
# Date 1470324689 -7200
#      Thu Aug 04 17:31:29 2016 +0200
# Node ID c497d3c35883e43599d0a9f07f0894a582c4aede
# Parent  74056050d1bd9069f0dfaed861162ee65a77032d
commands: add template support for config

Example output:
hg config -Tjson ui.username
[
 {
  "value": "Mathias De Maré <mathias.demare@gmail.com>"
 }
]
Yuya Nishihara - Aug. 9, 2016, 1:55 p.m.
On Mon, 08 Aug 2016 10:54:46 -0500, Mathias De Maré wrote:
> # HG changeset patch
> # User Mathias De Maré <mathias.demare@gmail.com>
> # Date 1470324689 -7200
> #      Thu Aug 04 17:31:29 2016 +0200
> # Node ID c497d3c35883e43599d0a9f07f0894a582c4aede
> # Parent  74056050d1bd9069f0dfaed861162ee65a77032d
> commands: add template support for config
> 
> Example output:
> hg config -Tjson ui.username
> [
>  {
>   "value": "Mathias De Maré <mathias.demare@gmail.com>"
>  }
> ]
> 
> diff --git a/mercurial/commands.py b/mercurial/commands.py
> --- a/mercurial/commands.py
> +++ b/mercurial/commands.py
> @@ -1787,7 +1787,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):
> @@ -1848,6 +1848,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'))
> @@ -1865,18 +1866,22 @@

       value = str(value).replace('\n', '\\n')

'\n' should be escaped only in plain format.

>                  if v == section:
>                      ui.debug('%s: ' %
>                               ui.configsource(section, name, untrusted))

fm.condwrite(ui.debug, 'source', ...) ?

> -                    ui.write('%s=%s\n' % (sectname, value))
> +                    fm.startitem()
> +                    fm.write('section value', '%s=%s\n', sectname, value)

sectname isn't a section. I don't know the right word, but "name" would be
better.

>                  elif v == sectname:
>                      ui.debug('%s: ' %
>                               ui.configsource(section, name, untrusted))
> -                    ui.write(value, '\n')
> +                    fm.startitem()
> +                    fm.write('value', '%s\n', value)

fm.data(...=sectname) to include both name and value.

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1787,7 +1787,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):
@@ -1848,6 +1848,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'))
@@ -1865,18 +1866,22 @@ 
                 if v == section:
                     ui.debug('%s: ' %
                              ui.configsource(section, name, untrusted))
-                    ui.write('%s=%s\n' % (sectname, value))
+                    fm.startitem()
+                    fm.write('section value', '%s=%s\n', sectname, value)
                     matched = True
                 elif v == sectname:
                     ui.debug('%s: ' %
                              ui.configsource(section, name, untrusted))
-                    ui.write(value, '\n')
+                    fm.startitem()
+                    fm.write('value', '%s\n', value)
                     matched = True
         else:
             ui.debug('%s: ' %
                      ui.configsource(section, name, untrusted))
-            ui.write('%s=%s\n' % (sectname, value))
+            fm.startitem()
+            fm.write('section value', '%s=%s\n', sectname, 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,18 @@ 
   Section.KeY=Case Sensitive
   Section.key=lower case
 
+  $ hg showconfig Section -Tjson
+  [
+   {
+    "section": "Section.KeY",
+    "value": "Case Sensitive"
+   },
+   {
+    "section": "Section.key",
+    "value": "lower case"
+   }
+  ]
+
 Test "%unset"
 
   $ cat >> $HGRCPATH <<EOF