Re: [PATCH 3 of 3] commands: add template support for config

2016-08-30 Thread Yuya Nishihara
On Mon, 29 Aug 2016 12:52:43 -0500, Mathias De Maré wrote:
> # HG changeset patch
> # User Mathias De Maré 
> # Date 1472447235 -7200
> #  Mon Aug 29 07:07:15 2016 +0200
> # Node ID dd7b8ff51af615e1a44d32b103d23e8cd34d5dd9
> # Parent  e348c87b33d73a590bee53b74def915b1ce8c6fe
> commands: add template support for config

> -for f in scmutil.rcpath():
> -ui.debug('read config from: %s\n' % f)
> +fm = ui.formatter('config', opts)
> +if ui.debugflag:
> +for f in scmutil.rcpath():
> +fm.plain('read config from: %s\n' % f)

This is a real debug message. We'll need another workaround, so please keep
it as ui.debug().

https://www.mercurial-scm.org/wiki/GenericTemplatingPlan#Sanity_check_output

> @@ -1859,25 +1861,31 @@
>  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
> +if fm.isplain():
> +value = str(value).replace('\n', '\\n')
> +entryname = section + '.' + name

I think we can always convert value to str because config values should be
strings in the public (i.e. command) API.

> +++ b/tests/test-config.t
> @@ -54,6 +54,20 @@
>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"
> +   }
> +  ]

Can you add more tests to cover all "if"s ?
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


[PATCH 3 of 3] commands: add template support for config

2016-08-29 Thread Mathias De Maré
# HG changeset patch
# User Mathias De Maré 
# Date 1472447235 -7200
#  Mon Aug 29 07:07:15 2016 +0200
# Node ID dd7b8ff51af615e1a44d32b103d23e8cd34d5dd9
# Parent  e348c87b33d73a590bee53b74def915b1ce8c6fe
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'

Example output:
[
 {
  "name": "ui.username",
  "source": "/home/mathias/.hgrc:2",
  "value": "Mathias De Maré "
 }
]

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,8 +1849,10 @@
   onerr=error.Abort, errprefix=_("edit failed"))
 return
 
-for f in scmutil.rcpath():
-ui.debug('read config from: %s\n' % f)
+fm = ui.formatter('config', opts)
+if ui.debugflag:
+for f in scmutil.rcpath():
+fm.plain('read config from: %s\n' % f)
 untrusted = bool(opts.get('untrusted'))
 if values:
 sections = [v for v in values if '.' not in v]
@@ -1859,25 +1861,31 @@
 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
+if fm.isplain():
+value = str(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,20 @@
   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"
+   }
+  ]
+
 Test "%unset"
 
   $ cat >> $HGRCPATH