| 1 | |
|
| 2 | |
|
| 3 | | use super::*; |
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 9 | | pub struct Config { |
| 10 | | |
| 11 | | repo_layer: BTreeMap<String, String>, |
| 12 | | pub author_name: String, |
| 13 | | pub author_email: String, |
| 14 | | |
| 15 | | pub author_key: Option<KeyId>, |
| 16 | | |
| 17 | | |
| 18 | | |
| 19 | | |
| 20 | | pub signer_key: Option<KeyId>, |
| 21 | | |
| 22 | | |
| 23 | | pub sign_changes: bool, |
| 24 | | pub bookmark: String, |
| 25 | | |
| 26 | | pub remotes: BTreeMap<String, String>, |
| 27 | | |
| 28 | | pub remote_tokens: BTreeMap<String, String>, |
| 29 | | |
| 30 | | pub remote_cas: BTreeMap<String, String>, |
| 31 | | |
| 32 | | |
| 33 | | pub default_remote: Option<String>, |
| 34 | | |
| 35 | | |
| 36 | | pub git_claim: bool, |
| 37 | | |
| 38 | | pub op_retention_days: u64, |
| 39 | | |
| 40 | | pub gc_grace_days: u64, |
| 41 | | |
| 42 | | pub checkpoint_interval: u64, |
| 43 | | |
| 44 | | |
| 45 | | pub push_batch_bytes: u64, |
| 46 | | |
| 47 | | pub max_blob: Option<u64>, |
| 48 | | |
| 49 | | pub sparse_paths: Vec<String>, |
| 50 | | |
| 51 | | pub semantic_deps: bool, |
| 52 | | |
| 53 | | pub revsets: BTreeMap<String, String>, |
| 54 | | |
| 55 | | |
| 56 | | |
| 57 | | pub storage_url: Option<String>, |
| 58 | | |
| 59 | | |
| 60 | | pub storage_cache: u64, |
| 61 | | } |
| 62 | |
|
| 63 | | impl Config { |
| 64 | | |
| 65 | | |
| 66 | 2× | pub fn is_repo_set(&self, key: &str) -> bool { |
| 67 | 2× | self.repo_layer.contains_key(key) |
| 68 | 2× | } |
| 69 | |
|
| 70 | 2598× | pub(super) fn default_for_user() -> Config { |
| 71 | 2598× | let user = std::env::var("USER").unwrap_or_else(|_| "unknown".into()); |
| 72 | | Config { |
| 73 | 2598× | repo_layer: BTreeMap::new(), |
| 74 | 2598× | author_name: std::env::var("OXY_AUTHOR_NAME").unwrap_or_else(|_| user.clone()), |
| 75 | 2598× | author_email: std::env::var("OXY_AUTHOR_EMAIL") |
| 76 | 2598× | .unwrap_or_else(|_| format!("{user}@localhost")), |
| 77 | 2598× | author_key: None, |
| 78 | 2598× | signer_key: std::env::var("OXY_SIGNER") |
| 79 | 2598× | .ok() |
| 80 | 2598× | .and_then(|v| KeyId::from_hex(&v)), |
| 81 | | sign_changes: false, |
| 82 | 2598× | bookmark: "main".into(), |
| 83 | 2598× | remotes: BTreeMap::new(), |
| 84 | 2598× | remote_tokens: BTreeMap::new(), |
| 85 | 2598× | remote_cas: BTreeMap::new(), |
| 86 | 2598× | default_remote: None, |
| 87 | | git_claim: false, |
| 88 | | op_retention_days: 90, |
| 89 | | gc_grace_days: 14, |
| 90 | | checkpoint_interval: 256, |
| 91 | 2598× | push_batch_bytes: 256 << 20, |
| 92 | 2598× | max_blob: None, |
| 93 | 2598× | sparse_paths: Vec::new(), |
| 94 | | semantic_deps: false, |
| 95 | 2598× | revsets: BTreeMap::new(), |
| 96 | 2598× | storage_url: None, |
| 97 | | storage_cache: DEFAULT_STORAGE_CACHE, |
| 98 | | } |
| 99 | 2598× | } |
| 100 | |
|
| 101 | | |
| 102 | 451× | pub fn set(&mut self, key: &str, value: &str) -> Result<()> { |
| 103 | 451× | self.apply(key, value)?; |
| 104 | 451× | if value.is_empty() { |
| 105 | 4× | self.repo_layer.remove(key); |
| 106 | 447× | } else { |
| 107 | 447× | self.repo_layer.insert(key.to_string(), value.to_string()); |
| 108 | 447× | } |
| 109 | 451× | Ok(()) |
| 110 | 451× | } |
| 111 | |
|
| 112 | | |
| 113 | 91× | pub fn set_user(user_path: &Path, key: &str, value: &str) -> Result<()> { |
| 114 | 91× | Config::default_for_user().apply(key, value)?; |
| 115 | 90× | let mut entries = read_config_file(user_path)?; |
| 116 | 90× | if value.is_empty() { |
| 117 | 0× | entries.remove(key); |
| 118 | 90× | } else { |
| 119 | 90× | entries.insert(key.to_string(), value.to_string()); |
| 120 | 90× | } |
| 121 | 90× | if let Some(parent) = user_path.parent() { |
| 122 | 90× | fs::create_dir_all(parent)?; |
| 123 | 0× | } |
| 124 | 90× | write_config_file(user_path, &entries) |
| 125 | 91× | } |
| 126 | |
|
| 127 | | |
| 128 | 1489× | pub(super) fn apply(&mut self, key: &str, value: &str) -> Result<()> { |
| 129 | 3× | match key { |
| 130 | 1489× | "author.name" => self.author_name = value.into(), |
| 131 | 1243× | "author.email" => self.author_email = value.into(), |
| 132 | 997× | "author.key" => { |
| 133 | 248× | self.author_key = if value.is_empty() { |
| 134 | 0× | None |
| 135 | | } else { |
| 136 | 248× | Some(KeyId::from_hex(value).ok_or_else(|| { |
| 137 | 0× | RepoError::Msg("author.key must be a 64-character hex KeyId".into()) |
| 138 | 0× | })?) |
| 139 | | } |
| 140 | | } |
| 141 | 749× | "author.signer" => { |
| 142 | 3× | self.signer_key = if value.is_empty() { |
| 143 | 0× | None |
| 144 | | } else { |
| 145 | 3× | Some(KeyId::from_hex(value).ok_or_else(|| { |
| 146 | 0× | RepoError::Msg("author.signer must be a 64-character hex KeyId".into()) |
| 147 | 0× | })?) |
| 148 | | } |
| 149 | | } |
| 150 | 746× | "core.sign" => { |
| 151 | 83× | self.sign_changes = match value { |
| 152 | 83× | "change" => true, |
| 153 | 7× | "seal" => false, |
| 154 | 0× | _ => return fail("core.sign must be \"change\" or \"seal\""), |
| 155 | | } |
| 156 | | } |
| 157 | 663× | "workspace.bookmark" => self.bookmark = value.into(), |
| 158 | | |
| 159 | 659× | "auth.helper" => {} |
| 160 | 659× | "core.op-retention" => self.op_retention_days = parse_days(value)?, |
| 161 | 647× | "core.gc-grace" => self.gc_grace_days = parse_days(value)?, |
| 162 | 647× | "filter.max-blob" => { |
| 163 | 5× | self.max_blob = if value.is_empty() { |
| 164 | 0× | None |
| 165 | | } else { |
| 166 | 5× | Some(parse_size(value)?) |
| 167 | | } |
| 168 | | } |
| 169 | 642× | "storage.url" => { |
| 170 | 5× | self.storage_url = (!value.is_empty()).then(|| value.to_string()); |
| 171 | | } |
| 172 | 637× | "storage.cache" => { |
| 173 | 0× | self.storage_cache = if value.is_empty() { |
| 174 | 0× | DEFAULT_STORAGE_CACHE |
| 175 | | } else { |
| 176 | 0× | parse_size(value)? |
| 177 | | } |
| 178 | | } |
| 179 | 637× | "core.remote" => { |
| 180 | 1× | self.default_remote = (!value.is_empty()).then(|| value.to_string()); |
| 181 | | } |
| 182 | 636× | "hooks.semantic-deps" => self.semantic_deps = matches!(value, "true" | "1" | "yes"), |
| 183 | 635× | "git.claim" => self.git_claim = matches!(value, "true" | "1" | "yes"), |
| 184 | 633× | "filter.paths" => { |
| 185 | 3× | self.sparse_paths = value |
| 186 | 3× | .split(',') |
| 187 | 4× | .map(|p| { |
| 188 | 4× | p.trim() |
| 189 | 4× | .trim_start_matches("./") |
| 190 | 4× | .trim_matches('/') |
| 191 | 4× | .to_string() |
| 192 | 4× | }) |
| 193 | 4× | .filter(|p| !p.is_empty()) |
| 194 | 3× | .collect(); |
| 195 | | } |
| 196 | 630× | "core.checkpoint-interval" => { |
| 197 | 3× | self.checkpoint_interval = value |
| 198 | 3× | .parse() |
| 199 | 3× | .map_err(|_| RepoError::Msg(format!("expected a number, got {value:?}")))?; |
| 200 | | } |
| 201 | 627× | "push.batch-bytes" => { |
| 202 | 1× | self.push_batch_bytes = value.parse().ok().filter(|n| *n > 0).ok_or_else(|| { |
| 203 | 0× | RepoError::Msg(format!("expected a number of bytes, got {value:?}")) |
| 204 | 0× | })?; |
| 205 | | } |
| 206 | 626× | k if k.starts_with("remote.") => { |
| 207 | 623× | let rest = &k["remote.".len()..]; |
| 208 | 623× | let Some((name, field)) = rest.rsplit_once('.') else { |
| 209 | 0× | return fail(format!("unknown configuration key {key:?}")); |
| 210 | | }; |
| 211 | 623× | if name.is_empty() || name.contains('/') { |
| 212 | 0× | return fail(format!("invalid remote name in {key:?}")); |
| 213 | 623× | } |
| 214 | 623× | let map = match field { |
| 215 | 623× | "url" => &mut self.remotes, |
| 216 | 12× | "token" => &mut self.remote_tokens, |
| 217 | 4× | "ca" => &mut self.remote_cas, |
| 218 | 0× | _ => return fail(format!("unknown configuration key {key:?}")), |
| 219 | | }; |
| 220 | 623× | if value.is_empty() { |
| 221 | 4× | map.remove(name); |
| 222 | 4× | if field == "url" { |
| 223 | 3× | self.remote_tokens.remove(name); |
| 224 | 3× | self.remote_cas.remove(name); |
| 225 | 3× | } |
| 226 | 619× | } else { |
| 227 | 619× | map.insert(name.to_string(), value.into()); |
| 228 | 619× | } |
| 229 | | } |
| 230 | 3× | k if k.starts_with("revset.") => { |
| 231 | 3× | let name = &k["revset.".len()..]; |
| 232 | 3× | if name.is_empty() |
| 233 | 3× | || !name |
| 234 | 3× | .chars() |
| 235 | 11× | .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') |
| 236 | | { |
| 237 | 0× | return fail(format!( |
| 238 | | "a revset's name is letters, digits, _ and -: {key:?}" |
| 239 | | )); |
| 240 | 3× | } |
| 241 | 3× | if value.is_empty() { |
| 242 | 0× | self.revsets.remove(name); |
| 243 | 0× | } else { |
| 244 | 3× | crate::revset::parse(value) |
| 245 | 3× | .map_err(|e| RepoError::Msg(format!("{key} = {value:?}: {e}")))?; |
| 246 | 2× | self.revsets.insert(name.to_string(), value.into()); |
| 247 | | } |
| 248 | | } |
| 249 | 0× | _ => return fail(format!("unknown configuration key {key:?}")), |
| 250 | | } |
| 251 | 1488× | Ok(()) |
| 252 | 1489× | } |
| 253 | |
|
| 254 | | |
| 255 | 2507× | pub(super) fn load(user_path: Option<&Path>, repo_path: &Path) -> Result<Config> { |
| 256 | 2507× | let mut c = Config::default_for_user(); |
| 257 | 2507× | if let Some(u) = user_path { |
| 258 | 2507× | for (k, v) in read_config_file(u)? { |
| 259 | 294× | let _ = c.apply(&k, &v); |
| 260 | 294× | } |
| 261 | 0× | } |
| 262 | 2507× | for (k, v) in read_config_file(repo_path)? { |
| 263 | 653× | if c.apply(&k, &v).is_ok() { |
| 264 | 653× | c.repo_layer.insert(k, v); |
| 265 | 653× | } |
| 266 | | } |
| 267 | 2507× | Ok(c) |
| 268 | 2507× | } |
| 269 | |
|
| 270 | 846× | pub(super) fn save(&self, path: &Path) -> Result<()> { |
| 271 | 846× | write_config_file(path, &self.repo_layer) |
| 272 | 846× | } |
| 273 | |
|
| 274 | | |
| 275 | 18× | pub fn repo_layer(&self) -> &BTreeMap<String, String> { |
| 276 | 18× | &self.repo_layer |
| 277 | 18× | } |
| 278 | | } |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | | pub const DEFAULT_STORAGE_CACHE: u64 = 2 << 30; |
| 283 | |
|
| 284 | 7× | pub fn parse_size(v: &str) -> Result<u64> { |
| 285 | 7× | let v = v.trim(); |
| 286 | 7× | let (num, mult) = match v.chars().last() { |
| 287 | 1× | Some('K' | 'k') => (&v[..v.len() - 1], 1u64 << 10), |
| 288 | 1× | Some('M' | 'm') => (&v[..v.len() - 1], 1u64 << 20), |
| 289 | 0× | Some('G' | 'g') => (&v[..v.len() - 1], 1u64 << 30), |
| 290 | 5× | _ => (v, 1), |
| 291 | | }; |
| 292 | 7× | let n: u64 = num |
| 293 | 7× | .trim() |
| 294 | 7× | .parse() |
| 295 | 7× | .map_err(|_| RepoError::Msg(format!("expected a size like 1M, got {v:?}")))?; |
| 296 | 6× | Ok(n * mult) |
| 297 | 7× | } |
| 298 | |
|
| 299 | |
|
| 300 | 12× | fn parse_days(v: &str) -> Result<u64> { |
| 301 | 12× | v.trim_end_matches('d') |
| 302 | 12× | .parse() |
| 303 | 12× | .map_err(|_| RepoError::Msg(format!("expected a number of days, got {v:?}"))) |
| 304 | 12× | } |
| 305 | |
|
| 306 | |
|
| 307 | 5547× | pub fn read_config_file(path: &Path) -> Result<BTreeMap<String, String>> { |
| 308 | 5547× | let text = match fs::read_to_string(path) { |
| 309 | 2179× | Ok(t) => t, |
| 310 | 3368× | Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(BTreeMap::new()), |
| 311 | 0× | Err(e) => return Err(e.into()), |
| 312 | | }; |
| 313 | 2179× | let mut out = BTreeMap::new(); |
| 314 | 2179× | for line in text.lines() { |
| 315 | 1151× | let line = line.trim(); |
| 316 | 1151× | if line.is_empty() || line.starts_with('#') { |
| 317 | 0× | continue; |
| 318 | 1151× | } |
| 319 | 1151× | let Some((k, v)) = line.split_once('=') else { |
| 320 | 0× | continue; |
| 321 | | }; |
| 322 | 1151× | out.insert(k.trim().to_string(), v.trim().trim_matches('"').to_string()); |
| 323 | | } |
| 324 | 2179× | Ok(out) |
| 325 | 5547× | } |
| 326 | |
|
| 327 | 936× | fn write_config_file(path: &Path, entries: &BTreeMap<String, String>) -> Result<()> { |
| 328 | 936× | let text: String = entries |
| 329 | 936× | .iter() |
| 330 | 936× | .map(|(k, v)| format!("{k} = \"{v}\"\n")) |
| 331 | 936× | .collect(); |
| 332 | 936× | write_atomic(path, text.as_bytes()) |
| 333 | 936× | } |