showconfig: use set to filter sections and entry names
authorYuya Nishihara <yuya@tcha.org>
Wed, 21 Feb 2018 23:02:39 +0900
changeset 36416 07e207e88b42
parent 36415 0cb09c322647
child 36417 199443c55463
showconfig: use set to filter sections and entry names Before, an entry matching the specified section could be printed twice if the selector wasn't unique. "sections" and "items" are renamed because it's hard to distinguish "sections" from the loop variable "section".
mercurial/commands.py
tests/test-hgrc.t
--- a/mercurial/commands.py	Wed Feb 21 22:20:27 2018 +0900
+++ b/mercurial/commands.py	Wed Feb 21 23:02:39 2018 +0900
@@ -1693,11 +1693,16 @@
         else:
             raise error.ProgrammingError('unknown rctype: %s' % t)
     untrusted = bool(opts.get('untrusted'))
+
+    selsections = selentries = []
     if values:
-        sections = [v for v in values if '.' not in v]
-        items = [v for v in values if '.' in v]
-        if len(items) > 1 or items and sections:
+        selsections = [v for v in values if '.' not in v]
+        selentries = [v for v in values if '.' in v]
+        if len(selentries) > 1 or selentries and selsections:
             raise error.Abort(_('only one config item permitted'))
+    selsections = set(selsections)
+    selentries = set(selentries)
+
     matched = False
     for section, name, value in ui.walkconfig(untrusted=untrusted):
         source = ui.configsource(section, name, untrusted)
@@ -1707,18 +1712,17 @@
             value = value.replace('\n', '\\n')
         entryname = section + '.' + name
         if values:
-            for v in values:
-                if v == section:
-                    fm.startitem()
-                    fm.condwrite(ui.debugflag, 'source', '%s: ', source)
-                    fm.write('name value', '%s=%s\n', entryname, value)
-                    matched = True
-                elif v == entryname:
-                    fm.startitem()
-                    fm.condwrite(ui.debugflag, 'source', '%s: ', source)
-                    fm.write('value', '%s\n', value)
-                    fm.data(name=entryname)
-                    matched = True
+            if section in selsections:
+                fm.startitem()
+                fm.condwrite(ui.debugflag, 'source', '%s: ', source)
+                fm.write('name value', '%s=%s\n', entryname, value)
+                matched = True
+            elif entryname in selentries:
+                fm.startitem()
+                fm.condwrite(ui.debugflag, 'source', '%s: ', source)
+                fm.write('value', '%s\n', value)
+                fm.data(name=entryname)
+                matched = True
         else:
             fm.startitem()
             fm.condwrite(ui.debugflag, 'source', '%s: ', source)
--- a/tests/test-hgrc.t	Wed Feb 21 22:20:27 2018 +0900
+++ b/tests/test-hgrc.t	Wed Feb 21 23:02:39 2018 +0900
@@ -126,6 +126,11 @@
   $ hg showconfig alias defaults
   alias.log=log -g
   defaults.identify=-n
+  $ hg showconfig alias alias
+  alias.log=log -g
+  $ hg showconfig alias.log alias.log
+  abort: only one config item permitted
+  [255]
   $ hg showconfig alias defaults.identify
   abort: only one config item permitted
   [255]