rhg: Fall back to Python if unsupported extensions are enabled
authorSimon Sapin <simon.sapin@octobus.net>
Thu, 04 Mar 2021 10:58:43 +0100
changeset 46733 1bac7764ceef
parent 46732 60fe9ebae29b
child 46734 1a036d33bc18
rhg: Fall back to Python if unsupported extensions are enabled Extensions might affect behavior in ways we can’t anticipate, so just ignoring them is not correct. Later we’ll add opt-in configuration to ignore specific extensions. Differential Revision: https://phab.mercurial-scm.org/D10112
rust/Cargo.lock
rust/hg-core/src/config/config.rs
rust/hg-core/src/config/layer.rs
rust/rhg/Cargo.toml
rust/rhg/src/main.rs
--- a/rust/Cargo.lock	Wed Mar 03 20:02:07 2021 +0100
+++ b/rust/Cargo.lock	Thu Mar 04 10:58:43 2021 +0100
@@ -286,9 +286,9 @@
 
 [[package]]
 name = "format-bytes"
-version = "0.2.0"
+version = "0.2.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cc35f5e45d6b31053cea13078ffc6fa52fa8617aa54b7ac2011720d9c009e04f"
+checksum = "8030ff4b04f0ca1c612d6fe49f2fc18caf56fb01497cb370b41cfd36d89b3b06"
 dependencies = [
  "format-bytes-macros",
  "proc-macro-hack",
--- a/rust/hg-core/src/config/config.rs	Wed Mar 03 20:02:07 2021 +0100
+++ b/rust/hg-core/src/config/config.rs	Thu Mar 04 10:58:43 2021 +0100
@@ -14,6 +14,7 @@
 };
 use crate::utils::files::get_bytes_from_os_str;
 use format_bytes::{write_bytes, DisplayBytes};
+use std::collections::HashSet;
 use std::env;
 use std::path::{Path, PathBuf};
 use std::str;
@@ -361,6 +362,14 @@
         None
     }
 
+    /// Return all keys defined for the given section
+    pub fn get_section_keys(&self, section: &[u8]) -> HashSet<&[u8]> {
+        self.layers
+            .iter()
+            .flat_map(|layer| layer.iter_keys(section))
+            .collect()
+    }
+
     /// Get raw values bytes from all layers (even untrusted ones) in order
     /// of precedence.
     #[cfg(test)]
--- a/rust/hg-core/src/config/layer.rs	Wed Mar 03 20:02:07 2021 +0100
+++ b/rust/hg-core/src/config/layer.rs	Thu Mar 04 10:58:43 2021 +0100
@@ -115,6 +115,14 @@
         Some(self.sections.get(section)?.get(item)?)
     }
 
+    /// Returns the keys defined in the given section
+    pub fn iter_keys(&self, section: &[u8]) -> impl Iterator<Item = &[u8]> {
+        self.sections
+            .get(section)
+            .into_iter()
+            .flat_map(|section| section.keys().map(|vec| &**vec))
+    }
+
     pub fn is_empty(&self) -> bool {
         self.sections.is_empty()
     }
--- a/rust/rhg/Cargo.toml	Wed Mar 03 20:02:07 2021 +0100
+++ b/rust/rhg/Cargo.toml	Thu Mar 04 10:58:43 2021 +0100
@@ -17,5 +17,5 @@
 micro-timer = "0.3.1"
 regex = "1.3.9"
 env_logger = "0.7.1"
-format-bytes = "0.2.0"
+format-bytes = "0.2.1"
 users = "0.11.0"
--- a/rust/rhg/src/main.rs	Wed Mar 03 20:02:07 2021 +0100
+++ b/rust/rhg/src/main.rs	Thu Mar 04 10:58:43 2021 +0100
@@ -4,7 +4,7 @@
 use clap::AppSettings;
 use clap::Arg;
 use clap::ArgMatches;
-use format_bytes::format_bytes;
+use format_bytes::{format_bytes, join};
 use hg::config::Config;
 use hg::repo::{Repo, RepoError};
 use hg::utils::files::{get_bytes_from_os_str, get_path_from_bytes};
@@ -25,6 +25,8 @@
     repo: Result<&Repo, &NoRepoInCwdError>,
     config: &Config,
 ) -> Result<(), CommandError> {
+    check_extensions(config)?;
+
     let app = App::new("rhg")
         .global_setting(AppSettings::AllowInvalidUtf8)
         .setting(AppSettings::SubcommandRequired)
@@ -352,3 +354,25 @@
         }
     }
 }
+
+const SUPPORTED_EXTENSIONS: &[&[u8]] = &[b"blackbox", b"share"];
+
+fn check_extensions(config: &Config) -> Result<(), CommandError> {
+    let enabled = config.get_section_keys(b"extensions");
+
+    let mut unsupported = enabled;
+    for supported in SUPPORTED_EXTENSIONS {
+        unsupported.remove(supported);
+    }
+
+    if unsupported.is_empty() {
+        Ok(())
+    } else {
+        Err(CommandError::UnsupportedFeature {
+            message: format_bytes!(
+                b"extensions: {}",
+                join(unsupported, b", ")
+            ),
+        })
+    }
+}