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