diff mbox series

[b4] config: fix multivals config from cmdline

Message ID 20240926-cmdline-config-override-multivals-v1-1-80ec93afc5a1@kernel.org (mailing list archive)
State New
Headers show
Series [b4] config: fix multivals config from cmdline | expand

Commit Message

Matthieu Baerts (NGI0) Sept. 26, 2024, 5:29 p.m. UTC
Specifying prep-perpatch-check-cmd from the command line didn't work:

  $ b4 -c "b4.prep-perpatch-check-cmd=true" prep --check
  config prep-perpatch-check-cmd true ['keyringsrc', 'am-perpatch-check-cmd', 'prep-perpatch-check-cmd']
  Checking patches using:
    t
    r
    u
    e
  ---
  Traceback (most recent call last):
  (...)
  FileNotFoundError: [Errno 2] No such file or directory: 't'

The fix is similar to the one from commit d5f3956 ("Fix
prep-perpatch-check-cmd in .b4-config"): 'prep-perpatch-check-cmd' is a
special type, and the given string should not be considered as a list of
characters.

Note that here, the arguments set via 'b4 -c' are overridden, existing
lists are not appended.

This commit treats all "multivals" types the same way, including
"smtpserveroption" from the "sendemail" section.

Signed-off-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
---
 src/b4/__init__.py | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)


---
base-commit: dedf88cb947bab87c418b49d975df11f83621692
change-id: 20240926-cmdline-config-override-multivals-e69bc1986fd1

Best regards,
diff mbox series

Patch

diff --git a/src/b4/__init__.py b/src/b4/__init__.py
index 811bbcaf028d78224242433eac9fdbeb03a306b3..b9b4ea2fa1c0baa6a3def7935597da7dc72df8d7 100644
--- a/src/b4/__init__.py
+++ b/src/b4/__init__.py
@@ -2809,18 +2809,27 @@  def setup_config(cmdargs: argparse.Namespace):
     _setup_sendemail_config(cmdargs)
 
 
-def _cmdline_config_override(cmdargs: argparse.Namespace, config: dict, section: str):
+def _cmdline_config_override(cmdargs: argparse.Namespace, config: dict, section: str,
+                             multivals: Optional[list] = None):
     """Use cmdline.config to set and override config values for section."""
     if not cmdargs.config:
         return
 
-    section += '.'
+    if multivals is None:
+        multivals = list()
 
-    config_override = {
-        key[len(section):]: val
-        for key, val in cmdargs.config.items()
-        if key.startswith(section)
-    }
+    section += '.'
+    config_override = {}
+
+    for key, val in cmdargs.config.items():
+        if key.startswith(section):
+            key = key[len(section):]
+            if key in multivals:
+                if key not in config_override:
+                    config_override[key] = list()
+                config_override[key].append(val)
+            else:
+                config_override[key] = val
 
     config.update(config_override)
 
@@ -2923,7 +2932,7 @@  def _setup_main_config(cmdargs: Optional[argparse.Namespace] = None) -> None:
             pass
 
     if cmdargs:
-        _cmdline_config_override(cmdargs, config, 'b4')
+        _cmdline_config_override(cmdargs, config, 'b4', multivals=multivals)
 
     MAIN_CONFIG = config
 
@@ -3782,11 +3791,12 @@  def _setup_sendemail_config(cmdargs: argparse.Namespace) -> None:
 
     # Get the default settings first
     config = get_main_config()
-    _basecfg = get_config_from_git(r'sendemail\.[^.]+$', multivals=['smtpserveroption'])
+    multivals = ['smtpserveroption']
+    _basecfg = get_config_from_git(r'sendemail\.[^.]+$', multivals=multivals)
     identity = config.get('sendemail-identity') or _basecfg.get('identity')
     if identity:
         # Use this identity to override what we got from the default one
-        sconfig = get_config_from_git(rf'sendemail\.{identity}\..*', multivals=['smtpserveroption'], defaults=_basecfg)
+        sconfig = get_config_from_git(rf'sendemail\.{identity}\..*', multivals=multivals, defaults=_basecfg)
         sectname = f'sendemail.{identity}'
         if not len(sconfig):
             raise smtplib.SMTPException('Unable to find %s settings in any applicable git config' % sectname)
@@ -3796,7 +3806,7 @@  def _setup_sendemail_config(cmdargs: argparse.Namespace) -> None:
     logger.debug('Using values from %s', sectname)
 
     # Note: This can't handle identity, need to use sendemail.key directly
-    _cmdline_config_override(cmdargs, sconfig, 'sendemail')
+    _cmdline_config_override(cmdargs, sconfig, 'sendemail', multivals=multivals)
 
     SENDEMAIL_CONFIG = sconfig