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