| 1 |
use std::env; |
| 2 |
|
| 3 |
use crate::utils::files::get_bytes_from_os_string; |
| 4 |
|
| 5 |
/// Keeps information on whether plain mode is active. |
| 6 |
/// |
| 7 |
/// Plain mode means that all configuration variables which affect |
| 8 |
/// the behavior and output of Mercurial should be |
| 9 |
/// ignored. Additionally, the output should be stable, |
| 10 |
/// reproducible and suitable for use in scripts or applications. |
| 11 |
/// |
| 12 |
/// The only way to trigger plain mode is by setting either the |
| 13 |
/// `HGPLAIN' or `HGPLAINEXCEPT' environment variables. |
| 14 |
/// |
| 15 |
/// The return value can either be |
| 16 |
/// - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT |
| 17 |
/// - False if feature is disabled by default and not included in HGPLAIN |
| 18 |
/// - True otherwise |
| 19 |
#[derive(Clone)] |
| 20 |
pub struct PlainInfo { |
| 21 |
is_plain: bool, |
| 22 |
except: Vec<Vec<u8>>, |
| 23 |
} |
| 24 |
|
| 25 |
impl PlainInfo { |
| 26 |
fn plain_except(except: Vec<Vec<u8>>) -> Self { |
| 27 |
PlainInfo { is_plain: true, except } |
| 28 |
} |
| 29 |
|
| 30 |
pub fn empty() -> PlainInfo { |
| 31 |
PlainInfo { is_plain: false, except: vec![] } |
| 32 |
} |
| 33 |
|
| 34 |
pub fn from_env() -> PlainInfo { |
| 35 |
if let Some(except) = env::var_os("HGPLAINEXCEPT") { |
| 36 |
PlainInfo::plain_except( |
| 37 |
get_bytes_from_os_string(except) |
| 38 |
.split(|&byte| byte == b',') |
| 39 |
.map(|x| x.to_vec()) |
| 40 |
.collect(), |
| 41 |
) |
| 42 |
} else { |
| 43 |
PlainInfo { |
| 44 |
is_plain: env::var_os("HGPLAIN").is_some(), |
| 45 |
except: vec![], |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
pub fn is_feature_plain(&self, feature: &str) -> bool { |
| 51 |
self.is_plain |
| 52 |
&& !self |
| 53 |
.except |
| 54 |
.iter() |
| 55 |
.any(|exception| exception.as_slice() == feature.as_bytes()) |
| 56 |
} |
| 57 |
|
| 58 |
pub fn is_plain(&self) -> bool { |
| 59 |
self.is_plain |
| 60 |
} |
| 61 |
|
| 62 |
pub fn plainalias(&self) -> bool { |
| 63 |
self.is_feature_plain("alias") |
| 64 |
} |
| 65 |
pub fn plainrevsetalias(&self) -> bool { |
| 66 |
self.is_feature_plain("revsetalias") |
| 67 |
} |
| 68 |
pub fn plaintemplatealias(&self) -> bool { |
| 69 |
self.is_feature_plain("templatealias") |
| 70 |
} |
| 71 |
pub fn plaintweakdefaults(&self) -> bool { |
| 72 |
self.is_feature_plain("tweakdefaults") |
| 73 |
} |
| 74 |
} |