Patchwork D12163: rhg: Add support for HGPLAINEXPECT

login
register
mail settings
Submitter phabricator
Date Feb. 10, 2022, 7:19 p.m.
Message ID <differential-rev-PHID-DREV-bozr6v7qxlkg24f7tb76-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50498/
State New
Headers show

Comments

phabricator - Feb. 10, 2022, 7:19 p.m.
SimonSapin created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D12163

AFFECTED FILES
  rust/rhg/src/commands/status.rs
  rust/rhg/src/main.rs
  rust/rhg/src/ui.rs

CHANGE DETAILS




To: SimonSapin, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/rust/rhg/src/ui.rs b/rust/rhg/src/ui.rs
--- a/rust/rhg/src/ui.rs
+++ b/rust/rhg/src/ui.rs
@@ -1,4 +1,5 @@ 
 use format_bytes::format_bytes;
+use hg::utils::files::get_bytes_from_os_string;
 use std::borrow::Cow;
 use std::env;
 use std::io;
@@ -65,8 +66,19 @@ 
     /// - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT
     /// - False if feature is disabled by default and not included in HGPLAIN
     /// - True otherwise
-    pub fn plain(&self) -> bool {
-        // TODO: add support for HGPLAINEXCEPT
+    pub fn plain(&self, feature: Option<&str>) -> bool {
+        plain(feature)
+    }
+}
+
+fn plain(opt_feature: Option<&str>) -> bool {
+    if let Some(except) = env::var_os("HGPLAINEXCEPT") {
+        opt_feature.map_or(true, |feature| {
+            get_bytes_from_os_string(except)
+                .split(|&byte| byte == b',')
+                .any(|exception| exception == feature.as_bytes())
+        })
+    } else {
         env::var_os("HGPLAIN").is_some()
     }
 }
diff --git a/rust/rhg/src/main.rs b/rust/rhg/src/main.rs
--- a/rust/rhg/src/main.rs
+++ b/rust/rhg/src/main.rs
@@ -669,7 +669,9 @@ 
     }
 
     if let Some(color) = config.get(b"ui", b"color") {
-        if (color == b"always" || color == b"debug") && !ui.plain() {
+        if (color == b"always" || color == b"debug")
+            && !ui.plain(Some("color"))
+        {
             Err(CommandError::unsupported("colored output"))?
         }
     }
diff --git a/rust/rhg/src/commands/status.rs b/rust/rhg/src/commands/status.rs
--- a/rust/rhg/src/commands/status.rs
+++ b/rust/rhg/src/commands/status.rs
@@ -183,7 +183,7 @@ 
     let config = invocation.config;
     let args = invocation.subcommand_args;
 
-    let verbose = !ui.plain()
+    let verbose = !ui.plain(None)
         && !args.is_present("print0")
         && (config.get_bool(b"ui", b"verbose")?
             || config.get_bool(b"commands", b"status.verbose")?);
@@ -312,7 +312,7 @@ 
             }
         }
     }
-    let relative_paths = (!ui.plain())
+    let relative_paths = (!ui.plain(None))
         && config
             .get_option(b"commands", b"status.relative")?
             .unwrap_or(config.get_bool(b"ui", b"relative-paths")?);