Show More
@@ -0,0 +1,79 b'' | |||||
|
1 | use crate::utils::files::get_bytes_from_os_string; | |||
|
2 | use std::env; | |||
|
3 | ||||
|
4 | /// Keeps information on whether plain mode is active. | |||
|
5 | /// | |||
|
6 | /// Plain mode means that all configuration variables which affect | |||
|
7 | /// the behavior and output of Mercurial should be | |||
|
8 | /// ignored. Additionally, the output should be stable, | |||
|
9 | /// reproducible and suitable for use in scripts or applications. | |||
|
10 | /// | |||
|
11 | /// The only way to trigger plain mode is by setting either the | |||
|
12 | /// `HGPLAIN' or `HGPLAINEXCEPT' environment variables. | |||
|
13 | /// | |||
|
14 | /// The return value can either be | |||
|
15 | /// - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT | |||
|
16 | /// - False if feature is disabled by default and not included in HGPLAIN | |||
|
17 | /// - True otherwise | |||
|
18 | #[derive(Clone)] | |||
|
19 | pub struct PlainInfo { | |||
|
20 | is_plain: bool, | |||
|
21 | except: Vec<Vec<u8>>, | |||
|
22 | } | |||
|
23 | ||||
|
24 | impl PlainInfo { | |||
|
25 | fn plain_except(except: Vec<Vec<u8>>) -> Self { | |||
|
26 | PlainInfo { | |||
|
27 | is_plain: true, | |||
|
28 | except, | |||
|
29 | } | |||
|
30 | } | |||
|
31 | ||||
|
32 | pub fn empty() -> PlainInfo { | |||
|
33 | PlainInfo { | |||
|
34 | is_plain: false, | |||
|
35 | except: vec![], | |||
|
36 | } | |||
|
37 | } | |||
|
38 | ||||
|
39 | pub fn from_env() -> PlainInfo { | |||
|
40 | if let Some(except) = env::var_os("HGPLAINEXCEPT") { | |||
|
41 | PlainInfo::plain_except( | |||
|
42 | get_bytes_from_os_string(except) | |||
|
43 | .split(|&byte| byte == b',') | |||
|
44 | .map(|x| x.to_vec()) | |||
|
45 | .collect(), | |||
|
46 | ) | |||
|
47 | } else { | |||
|
48 | PlainInfo { | |||
|
49 | is_plain: env::var_os("HGPLAIN").is_some(), | |||
|
50 | except: vec![], | |||
|
51 | } | |||
|
52 | } | |||
|
53 | } | |||
|
54 | ||||
|
55 | pub fn is_feature_plain(&self, feature: &str) -> bool { | |||
|
56 | return self.is_plain | |||
|
57 | && !self | |||
|
58 | .except | |||
|
59 | .iter() | |||
|
60 | .any(|exception| exception.as_slice() == feature.as_bytes()); | |||
|
61 | } | |||
|
62 | ||||
|
63 | pub fn is_plain(&self) -> bool { | |||
|
64 | self.is_plain | |||
|
65 | } | |||
|
66 | ||||
|
67 | pub fn plainalias(&self) -> bool { | |||
|
68 | self.is_feature_plain("alias") | |||
|
69 | } | |||
|
70 | pub fn plainrevsetalias(&self) -> bool { | |||
|
71 | self.is_feature_plain("revsetalias") | |||
|
72 | } | |||
|
73 | pub fn plaintemplatealias(&self) -> bool { | |||
|
74 | self.is_feature_plain("templatealias") | |||
|
75 | } | |||
|
76 | pub fn plaintweakdefaults(&self) -> bool { | |||
|
77 | self.is_feature_plain("tweakdefaults") | |||
|
78 | } | |||
|
79 | } |
@@ -11,6 +11,8 b'' | |||||
11 |
|
11 | |||
12 | mod config; |
|
12 | mod config; | |
13 | mod layer; |
|
13 | mod layer; | |
|
14 | mod plain_info; | |||
14 | mod values; |
|
15 | mod values; | |
15 |
pub use config::{Config, ConfigSource, ConfigValueParseError |
|
16 | pub use config::{Config, ConfigSource, ConfigValueParseError}; | |
16 | pub use layer::{ConfigError, ConfigOrigin, ConfigParseError}; |
|
17 | pub use layer::{ConfigError, ConfigOrigin, ConfigParseError}; | |
|
18 | pub use plain_info::PlainInfo; |
@@ -12,6 +12,7 b' use super::values;' | |||||
12 | use crate::config::layer::{ |
|
12 | use crate::config::layer::{ | |
13 | ConfigError, ConfigLayer, ConfigOrigin, ConfigValue, |
|
13 | ConfigError, ConfigLayer, ConfigOrigin, ConfigValue, | |
14 | }; |
|
14 | }; | |
|
15 | use crate::config::plain_info::PlainInfo; | |||
15 | use crate::utils::files::get_bytes_from_os_str; |
|
16 | use crate::utils::files::get_bytes_from_os_str; | |
16 | use format_bytes::{write_bytes, DisplayBytes}; |
|
17 | use format_bytes::{write_bytes, DisplayBytes}; | |
17 | use std::collections::HashSet; |
|
18 | use std::collections::HashSet; | |
@@ -22,14 +23,6 b' use std::str;' | |||||
22 |
|
23 | |||
23 | use crate::errors::{HgResultExt, IoResultExt}; |
|
24 | use crate::errors::{HgResultExt, IoResultExt}; | |
24 |
|
25 | |||
25 | #[derive(Clone)] |
|
|||
26 | pub struct PlainInfo { |
|
|||
27 | pub plain: bool, |
|
|||
28 | pub plainalias: bool, |
|
|||
29 | pub plainrevsetalias: bool, |
|
|||
30 | pub plaintemplatealias: bool, |
|
|||
31 | } |
|
|||
32 |
|
||||
33 | /// Holds the config values for the current repository |
|
26 | /// Holds the config values for the current repository | |
34 | /// TODO update this docstring once we support more sources |
|
27 | /// TODO update this docstring once we support more sources | |
35 | #[derive(Clone)] |
|
28 | #[derive(Clone)] | |
@@ -92,21 +85,21 b' impl fmt::Display for ConfigValueParseEr' | |||||
92 | } |
|
85 | } | |
93 | } |
|
86 | } | |
94 |
|
87 | |||
|
88 | /// Returns true if the config item is disabled by PLAIN or PLAINEXCEPT | |||
95 | fn should_ignore(plain: &PlainInfo, section: &[u8], item: &[u8]) -> bool { |
|
89 | fn should_ignore(plain: &PlainInfo, section: &[u8], item: &[u8]) -> bool { | |
96 | // duplication with [_applyconfig] in [ui.py], |
|
90 | // duplication with [_applyconfig] in [ui.py], | |
97 | if !plain.plain { |
|
91 | if !plain.is_plain() { | |
98 | return false; |
|
92 | return false; | |
99 | } |
|
93 | } | |
100 | if section == b"alias" { |
|
94 | if section == b"alias" { | |
101 | return plain.plainalias; |
|
95 | return plain.plainalias(); | |
102 | } |
|
96 | } | |
103 | if section == b"revsetalias" { |
|
97 | if section == b"revsetalias" { | |
104 | return plain.plainrevsetalias; |
|
98 | return plain.plainrevsetalias(); | |
105 | } |
|
99 | } | |
106 | if section == b"templatealias" { |
|
100 | if section == b"templatealias" { | |
107 | return plain.plaintemplatealias; |
|
101 | return plain.plaintemplatealias(); | |
108 | } |
|
102 | } | |
109 |
|
||||
110 | if section == b"ui" { |
|
103 | if section == b"ui" { | |
111 | let to_delete: &[&[u8]] = &[ |
|
104 | let to_delete: &[&[u8]] = &[ | |
112 | b"debug", |
|
105 | b"debug", | |
@@ -127,16 +120,6 b' fn should_ignore(plain: &PlainInfo, sect' | |||||
127 | return sections_to_delete.contains(§ion); |
|
120 | return sections_to_delete.contains(§ion); | |
128 | } |
|
121 | } | |
129 |
|
122 | |||
130 | impl PlainInfo { |
|
|||
131 | pub fn empty() -> Self { |
|
|||
132 | Self { |
|
|||
133 | plain: false, |
|
|||
134 | plainalias: false, |
|
|||
135 | plainrevsetalias: false, |
|
|||
136 | plaintemplatealias: false, |
|
|||
137 | } |
|
|||
138 | } |
|
|||
139 | } |
|
|||
140 | impl Config { |
|
123 | impl Config { | |
141 | /// The configuration to use when printing configuration-loading errors |
|
124 | /// The configuration to use when printing configuration-loading errors | |
142 | pub fn empty() -> Self { |
|
125 | pub fn empty() -> Self { | |
@@ -478,6 +461,8 b' impl Config {' | |||||
478 | section: &[u8], |
|
461 | section: &[u8], | |
479 | item: &[u8], |
|
462 | item: &[u8], | |
480 | ) -> Option<(&ConfigLayer, &ConfigValue)> { |
|
463 | ) -> Option<(&ConfigLayer, &ConfigValue)> { | |
|
464 | // Filter out the config items that are hidden by [PLAIN]. | |||
|
465 | // This differs from python hg where we delete them from the config. | |||
481 | if should_ignore(&self.plain, §ion, &item) { |
|
466 | if should_ignore(&self.plain, §ion, &item) { | |
482 | return None; |
|
467 | return None; | |
483 | } |
|
468 | } |
@@ -329,14 +329,7 b' fn rhg_main(argv: Vec<OsString>) -> ! {' | |||||
329 | }; |
|
329 | }; | |
330 |
|
330 | |||
331 | let mut config_cow = Cow::Borrowed(config); |
|
331 | let mut config_cow = Cow::Borrowed(config); | |
332 | if ui::plain(None) { |
|
332 | config_cow.to_mut().apply_plain(PlainInfo::from_env()); | |
333 | config_cow.to_mut().apply_plain(PlainInfo { |
|
|||
334 | plain: true, |
|
|||
335 | plainalias: ui::plain(Some("alias")), |
|
|||
336 | plainrevsetalias: ui::plain(Some("revsetalias")), |
|
|||
337 | plaintemplatealias: ui::plain(Some("templatealias")), |
|
|||
338 | }) |
|
|||
339 | }; |
|
|||
340 | let config = config_cow.as_ref(); |
|
333 | let config = config_cow.as_ref(); | |
341 |
|
334 | |||
342 | let ui = Ui::new(&config).unwrap_or_else(|error| { |
|
335 | let ui = Ui::new(&config).unwrap_or_else(|error| { |
@@ -3,10 +3,9 b' use crate::color::Effect;' | |||||
3 | use format_bytes::format_bytes; |
|
3 | use format_bytes::format_bytes; | |
4 | use format_bytes::write_bytes; |
|
4 | use format_bytes::write_bytes; | |
5 | use hg::config::Config; |
|
5 | use hg::config::Config; | |
|
6 | use hg::config::PlainInfo; | |||
6 | use hg::errors::HgError; |
|
7 | use hg::errors::HgError; | |
7 | use hg::utils::files::get_bytes_from_os_string; |
|
|||
8 | use std::borrow::Cow; |
|
8 | use std::borrow::Cow; | |
9 | use std::env; |
|
|||
10 | use std::io; |
|
9 | use std::io; | |
11 | use std::io::{ErrorKind, Write}; |
|
10 | use std::io::{ErrorKind, Write}; | |
12 |
|
11 | |||
@@ -129,29 +128,13 b' impl Ui {' | |||||
129 | } |
|
128 | } | |
130 | } |
|
129 | } | |
131 |
|
130 | |||
132 | /// Return whether plain mode is active. |
|
131 | // TODO: pass the PlainInfo to call sites directly and | |
133 | /// |
|
132 | // delete this function | |
134 | /// Plain mode means that all configuration variables which affect |
|
|||
135 | /// the behavior and output of Mercurial should be |
|
|||
136 | /// ignored. Additionally, the output should be stable, |
|
|||
137 | /// reproducible and suitable for use in scripts or applications. |
|
|||
138 | /// |
|
|||
139 | /// The only way to trigger plain mode is by setting either the |
|
|||
140 | /// `HGPLAIN' or `HGPLAINEXCEPT' environment variables. |
|
|||
141 | /// |
|
|||
142 | /// The return value can either be |
|
|||
143 | /// - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT |
|
|||
144 | /// - False if feature is disabled by default and not included in HGPLAIN |
|
|||
145 | /// - True otherwise |
|
|||
146 | pub fn plain(opt_feature: Option<&str>) -> bool { |
|
133 | pub fn plain(opt_feature: Option<&str>) -> bool { | |
147 | if let Some(except) = env::var_os("HGPLAINEXCEPT") { |
|
134 | let plain_info = PlainInfo::from_env(); | |
148 | opt_feature.map_or(true, |feature| { |
|
135 | match opt_feature { | |
149 | get_bytes_from_os_string(except) |
|
136 | None => plain_info.is_plain(), | |
150 | .split(|&byte| byte == b',') |
|
137 | Some(feature) => plain_info.is_feature_plain(feature), | |
151 | .all(|exception| exception != feature.as_bytes()) |
|
|||
152 | }) |
|
|||
153 | } else { |
|
|||
154 | env::var_os("HGPLAIN").is_some() |
|
|||
155 | } |
|
138 | } | |
156 | } |
|
139 | } | |
157 |
|
140 |
General Comments 0
You need to be logged in to leave comments.
Login now