Patchwork [1,of,2] showconfig: use set to filter sections and entry names

login
register
mail settings
Submitter Yuya Nishihara
Date Feb. 21, 2018, 2:34 p.m.
Message ID <11003771245a19f65107.1519223646@mimosa>
Download mbox | patch
Permalink /patch/28186/
State Accepted
Headers show

Comments

Yuya Nishihara - Feb. 21, 2018, 2:34 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1519221759 -32400
#      Wed Feb 21 23:02:39 2018 +0900
# Node ID 11003771245a19f65107b141a9c1adc9461d43ea
# Parent  44e4662d7a61ff8272a96045a60c3b005a099f64
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".

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1692,11 +1692,16 @@  def config(ui, repo, *values, **opts):
         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)
@@ -1706,18 +1711,17 @@  def config(ui, repo, *values, **opts):
             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)
diff --git a/tests/test-hgrc.t b/tests/test-hgrc.t
--- a/tests/test-hgrc.t
+++ b/tests/test-hgrc.t
@@ -126,6 +126,11 @@  showconfig with multiple arguments
   $ 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]