configitems: move blackbox's config items to the new configitems.toml
authorRaphaël Gomès <rgomes@octobus.net>
Wed, 05 Jul 2023 23:59:22 +0200
changeset 50765 7f8f6fe13fa9
parent 50764 8ff187fbbfea
child 50766 58aa5ee9c846
configitems: move blackbox's config items to the new configitems.toml In order for the Rust code to gain access to default values of in-core extensions that have a Rust implementation, we need to centralize them alongside the core items declarations. This is the first and so far only one of the extensions that have gained Rust support, I don't think it's worth the churn to move the rest of the extension's configitems yet.
hgext/blackbox.py
mercurial/configitems.py
mercurial/configitems.toml
mercurial/ui.py
rust/hg-core/src/config/config_items.rs
rust/hg-core/src/config/mod.rs
rust/rhg/src/blackbox.rs
--- a/hgext/blackbox.py	Mon Feb 13 18:11:48 2023 +0100
+++ b/hgext/blackbox.py	Wed Jul 05 23:59:22 2023 +0200
@@ -67,41 +67,6 @@
 cmdtable = {}
 command = registrar.command(cmdtable)
 
-configtable = {}
-configitem = registrar.configitem(configtable)
-
-configitem(
-    b'blackbox',
-    b'dirty',
-    default=False,
-)
-configitem(
-    b'blackbox',
-    b'maxsize',
-    default=b'1 MB',
-)
-configitem(
-    b'blackbox',
-    b'logsource',
-    default=False,
-)
-configitem(
-    b'blackbox',
-    b'maxfiles',
-    default=7,
-)
-configitem(
-    b'blackbox',
-    b'track',
-    default=lambda: [b'*'],
-)
-configitem(
-    b'blackbox',
-    b'ignore',
-    default=lambda: [b'chgserver', b'cmdserver', b'extension'],
-)
-configitem(b'blackbox', b'date-format', default=b'')
-
 _lastlogger = loggingutil.proxylogger()
 
 
--- a/mercurial/configitems.py	Mon Feb 13 18:11:48 2023 +0100
+++ b/mercurial/configitems.py	Wed Jul 05 23:59:22 2023 +0200
@@ -59,6 +59,7 @@
         priority=0,
         experimental=False,
         documentation="",
+        in_core_extension=None,
     ):
         self.section = section
         self.name = name
@@ -69,6 +70,7 @@
         self.priority = priority
         self.experimental = experimental
         self._re = None
+        self.in_core_extension = in_core_extension
         if generic:
             self._re = re.compile(self.name)
 
--- a/mercurial/configitems.toml	Mon Feb 13 18:11:48 2023 +0100
+++ b/mercurial/configitems.toml	Wed Jul 05 23:59:22 2023 +0200
@@ -23,6 +23,7 @@
 # - alias: list of 2-tuples of strings
 # - experimental: boolean
 # - documentation: string
+# - in_core_extension: string
 #
 # ## Template
 #
@@ -2695,6 +2696,8 @@
 section = "worker"
 name = "numcpus"
 
+# Templates and template applications
+
 [[template-applications]]
 template = "diff-options"
 section = "annotate"
@@ -2757,3 +2760,48 @@
 suffix = "word-diff"
 default = false
 
+# In-core extensions
+
+[[items]]
+section = "blackbox"
+name = "dirty"
+default = false
+in_core_extension = "blackbox"
+
+[[items]]
+section = "blackbox"
+name = "maxsize"
+default = "1 MB"
+in_core_extension = "blackbox"
+
+[[items]]
+section = "blackbox"
+name = "logsource"
+default = false
+in_core_extension = "blackbox"
+
+[[items]]
+section = "blackbox"
+name = "maxfiles"
+default = 7
+in_core_extension = "blackbox"
+
+[[items]]
+section = "blackbox"
+name = "track"
+default-type = "lambda"
+default = ["*"]
+in_core_extension = "blackbox"
+
+[[items]]
+section = "blackbox"
+name = "ignore"
+default-type = "lambda"
+default = ["chgserver", "cmdserver", "extension"]
+in_core_extension = "blackbox"
+
+[[items]]
+section = "blackbox"
+name = "date-format"
+default = ""
+in_core_extension = "blackbox"
--- a/mercurial/ui.py	Mon Feb 13 18:11:48 2023 +0100
+++ b/mercurial/ui.py	Wed Jul 05 23:59:22 2023 +0200
@@ -47,6 +47,7 @@
     configitems,
     encoding,
     error,
+    extensions,
     formatter,
     loggingutil,
     progress,
@@ -659,6 +660,12 @@
         item = self._knownconfig.get(section, {}).get(name)
         alternates = [(section, name)]
 
+        if item is not None and item.in_core_extension is not None:
+            # Only return the default for an in-core extension item if said
+            # extension is enabled
+            if item.in_core_extension in extensions.extensions(self):
+                item = None
+
         if item is not None:
             alternates.extend(item.alias)
             if callable(item.default):
--- a/rust/hg-core/src/config/config_items.rs	Mon Feb 13 18:11:48 2023 +0100
+++ b/rust/hg-core/src/config/config_items.rs	Wed Jul 05 23:59:22 2023 +0200
@@ -40,6 +40,11 @@
     /// The (possibly empty) docstring for the item
     #[serde(default)]
     documentation: String,
+    /// Whether the item is part of an in-core extension. This allows us to
+    /// hide them if the extension is not enabled, to preserve legacy
+    /// behavior.
+    #[serde(default)]
+    in_core_extension: Option<String>,
 }
 
 /// Corresponds to the raw (i.e. on disk) structure of config items. Used as
@@ -61,6 +66,8 @@
     experimental: bool,
     #[serde(default)]
     documentation: String,
+    #[serde(default)]
+    in_core_extension: Option<String>,
 }
 
 impl TryFrom<RawDefaultConfigItem> for DefaultConfigItem {
@@ -82,6 +89,7 @@
             alias: value.alias,
             experimental: value.experimental,
             documentation: value.documentation,
+            in_core_extension: value.in_core_extension,
         })
     }
 }
@@ -90,6 +98,14 @@
     fn is_generic(&self) -> bool {
         self.priority.is_some()
     }
+
+    pub fn in_core_extension(&self) -> Option<&str> {
+        self.in_core_extension.as_deref()
+    }
+
+    pub fn section(&self) -> &str {
+        self.section.as_ref()
+    }
 }
 
 impl<'a> TryFrom<&'a DefaultConfigItem> for Option<&'a str> {
@@ -302,6 +318,7 @@
             alias: self.alias,
             experimental: self.experimental,
             documentation: self.documentation,
+            in_core_extension: None,
         }
     }
 }
@@ -596,6 +613,7 @@
             alias: vec![],
             experimental: true,
             documentation: "".into(),
+            in_core_extension: None,
         };
         assert_eq!(config.get(b"censor", b"policy"), Some(&expected));
 
@@ -609,6 +627,7 @@
             alias: vec![],
             experimental: false,
             documentation: "".into(),
+            in_core_extension: None,
         };
         assert_eq!(config.get(b"alias", b"abcdsomething"), Some(&expected));
 
@@ -621,6 +640,7 @@
             alias: vec![],
             experimental: false,
             documentation: "".into(),
+            in_core_extension: None,
         };
         assert_eq!(config.get(b"alias", b"something"), Some(&expected));
 
@@ -632,6 +652,7 @@
             alias: vec![],
             experimental: false,
             documentation: "".into(),
+            in_core_extension: None,
         };
         assert_eq!(config.get(b"chgserver", b"idletimeout"), Some(&expected));
 
@@ -647,6 +668,7 @@
             alias: vec![],
             experimental: false,
             documentation: "".into(),
+            in_core_extension: None,
         };
         assert_eq!(config.get(b"cmdserver", b"track-log"), Some(&expected));
 
@@ -660,6 +682,7 @@
             documentation:
                 "This is a docstring.\nThis is another line but this is not."
                     .into(),
+            in_core_extension: None,
         };
         assert_eq!(
             config.get(b"command-templates", b"graphnode"),
--- a/rust/hg-core/src/config/mod.rs	Mon Feb 13 18:11:48 2023 +0100
+++ b/rust/hg-core/src/config/mod.rs	Wed Jul 05 23:59:22 2023 +0200
@@ -373,7 +373,17 @@
                 Some("`mercurial/configitems.toml` is not valid".into()),
             )
         })?;
-        Ok(default_config.get(section, item))
+        let default_opt = default_config.get(section, item);
+        Ok(default_opt.filter(|default| {
+            default
+                .in_core_extension()
+                .map(|extension| {
+                    // Only return the default for an in-core extension item
+                    // if said extension is enabled
+                    self.is_extension_enabled(extension.as_bytes())
+                })
+                .unwrap_or(true)
+        }))
     }
 
     fn get_parse<'config, T: 'config>(
--- a/rust/rhg/src/blackbox.rs	Mon Feb 13 18:11:48 2023 +0100
+++ b/rust/rhg/src/blackbox.rs	Wed Jul 05 23:59:22 2023 +0200
@@ -7,12 +7,6 @@
 use hg::utils::{files::get_bytes_from_os_str, shell_quote};
 use std::ffi::OsString;
 
-const ONE_MEBIBYTE: u64 = 1 << 20;
-
-// TODO: somehow keep defaults in sync with `configitem` in `hgext/blackbox.py`
-const DEFAULT_MAX_SIZE: u64 = ONE_MEBIBYTE;
-const DEFAULT_MAX_FILES: u32 = 7;
-
 // Python does not support %.3f, only %f
 const DEFAULT_DATE_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.3f";
 
@@ -62,15 +56,28 @@
                     max_size: invocation
                         .config
                         .get_byte_size(b"blackbox", b"maxsize")?
-                        .unwrap_or(DEFAULT_MAX_SIZE),
+                        .expect(
+                            "blackbox.maxsize should have a default value",
+                        ),
                     max_files: invocation
                         .config
                         .get_u32(b"blackbox", b"maxfiles")?
-                        .unwrap_or(DEFAULT_MAX_FILES),
+                        .expect(
+                            "blackbox.maxfiles should have a default value",
+                        ),
                     date_format: invocation
                         .config
                         .get_str(b"blackbox", b"date-format")?
-                        .unwrap_or(DEFAULT_DATE_FORMAT),
+                        .map(|f| {
+                            if f.is_empty() {
+                                DEFAULT_DATE_FORMAT
+                            } else {
+                                f
+                            }
+                        })
+                        .expect(
+                            "blackbox.date-format should have a default value",
+                        ),
                 })
             }
         } else {