comparison rust/hg-core/src/config/config.rs @ 49513:467d9df98c68

rhg: centralize PlainInfo
author Arseniy Alekseyev <aalekseyev@janestreet.com>
date Thu, 22 Sep 2022 17:16:54 -0400
parents 6939d5ed20e0
children e37416d432e9
comparison
equal deleted inserted replaced
49512:6939d5ed20e0 49513:467d9df98c68
10 use super::layer; 10 use super::layer;
11 use super::values; 11 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;
18 use std::env; 19 use std::env;
19 use std::fmt; 20 use std::fmt;
20 use std::path::{Path, PathBuf}; 21 use std::path::{Path, PathBuf};
21 use std::str; 22 use std::str;
22 23
23 use crate::errors::{HgResultExt, IoResultExt}; 24 use crate::errors::{HgResultExt, IoResultExt};
24
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 25
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)]
36 pub struct Config { 29 pub struct Config {
90 String::from_utf8_lossy(&self.value) 83 String::from_utf8_lossy(&self.value)
91 ) 84 )
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",
113 b"fallbackencoding", 106 b"fallbackencoding",
114 b"quiet", 107 b"quiet",
125 let sections_to_delete: &[&[u8]] = 118 let sections_to_delete: &[&[u8]] =
126 &[b"defaults", b"commands", b"command-templates"]; 119 &[b"defaults", b"commands", b"command-templates"];
127 return sections_to_delete.contains(&section); 120 return sections_to_delete.contains(&section);
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 {
143 Self { 126 Self {
144 layers: Vec::new(), 127 layers: Vec::new(),
476 fn get_inner( 459 fn get_inner(
477 &self, 460 &self,
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, &section, &item) { 466 if should_ignore(&self.plain, &section, &item) {
482 return None; 467 return None;
483 } 468 }
484 for layer in self.layers.iter().rev() { 469 for layer in self.layers.iter().rev() {
485 if !layer.trusted { 470 if !layer.trusted {