| 1 | |
|
| 2 | |
|
| 3 | | use super::tags::TagCheck; |
| 4 | | use super::*; |
| 5 | |
|
| 6 | |
|
| 7 | | #[derive(Clone, Debug, Default)] |
| 8 | | pub struct PushReport { |
| 9 | | pub results: Vec<RefResult>, |
| 10 | | |
| 11 | | pub updated: Vec<RefUpdated>, |
| 12 | | |
| 13 | | pub imported: Vec<ChangeHash>, |
| 14 | | } |
| 15 | |
|
| 16 | | #[derive(Clone, Debug)] |
| 17 | | pub struct RefUpdated { |
| 18 | | pub name: String, |
| 19 | | pub old_state: ObjectRef, |
| 20 | | pub new_state: ObjectRef, |
| 21 | | pub heads: Vec<ChangeHash>, |
| 22 | | } |
| 23 | |
|
| 24 | |
|
| 25 | | #[derive(Debug, Clone, PartialEq, Eq, Default)] |
| 26 | | pub struct FetchReport { |
| 27 | | |
| 28 | | pub refs: Vec<(String, Vec<ChangeHash>, usize)>, |
| 29 | | |
| 30 | | pub new_tags: usize, |
| 31 | | |
| 32 | | pub withdrawn_tags: Vec<String>, |
| 33 | | |
| 34 | | |
| 35 | | pub unverified: bool, |
| 36 | | |
| 37 | | |
| 38 | | |
| 39 | | pub log_notes: Vec<String>, |
| 40 | | |
| 41 | | |
| 42 | | |
| 43 | | |
| 44 | | pub skipped: Vec<(String, String)>, |
| 45 | | } |
| 46 | |
|
| 47 | | #[derive(Debug, Clone, PartialEq, Eq)] |
| 48 | | pub struct PullReport { |
| 49 | | |
| 50 | | pub heads: Vec<ChangeHash>, |
| 51 | | |
| 52 | | pub changed: bool, |
| 53 | | pub checked_out: bool, |
| 54 | | |
| 55 | | |
| 56 | | pub others: Vec<String>, |
| 57 | | |
| 58 | | pub skipped: Vec<(String, String)>, |
| 59 | | } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | | pub const OBJECTS_RESULT: &str = "objects"; |
| 64 | |
|
| 65 | | impl Repo { |
| 66 | | |
| 67 | |
|
| 68 | 1834× | fn ref_attest_path(&self, bookmark: &str) -> PathBuf { |
| 69 | 1834× | self.oxy.join("attest").join(name_file(bookmark)) |
| 70 | 1834× | } |
| 71 | |
|
| 72 | | |
| 73 | 1419× | pub fn latest_ref_attest(&self, bookmark: &str) -> Result<Option<(ObjectRef, SignedAttest)>> { |
| 74 | 1419× | match fs::read_to_string(self.ref_attest_path(bookmark)) { |
| 75 | 345× | Ok(h) => { |
| 76 | 345× | let r = ObjectRef::from_hex(h.trim()) |
| 77 | 345× | .ok_or_else(|| RepoError::Msg("corrupt attestation pointer".into()))?; |
| 78 | | Ok(Some(( |
| 79 | 345× | r, |
| 80 | | SignedAttest { |
| 81 | 345× | attest: self.store.get_attest(&r)?, |
| 82 | 345× | sigs: self.store.signatures(&r)?, |
| 83 | | }, |
| 84 | | ))) |
| 85 | | } |
| 86 | 1074× | Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), |
| 87 | 0× | Err(e) => Err(e.into()), |
| 88 | | } |
| 89 | 1419× | } |
| 90 | |
|
| 91 | 109× | pub(crate) fn set_ref_attest(&self, bookmark: &str, r: &ObjectRef) -> Result<()> { |
| 92 | 109× | fs::create_dir_all(self.oxy.join("attest"))?; |
| 93 | 109× | write_atomic(&self.ref_attest_path(bookmark), r.to_hex().as_bytes()) |
| 94 | 109× | } |
| 95 | |
|
| 96 | 1130× | pub fn remote_refs(&self) -> Result<RefsResponse> { |
| 97 | 1130× | let view = self.view()?; |
| 98 | 1130× | let mut out = Vec::new(); |
| 99 | 1183× | for (name, state) in &view.bookmarks { |
| 100 | 1183× | let latest = self.latest_ref_attest(name)?.map(|(_, a)| a); |
| 101 | 1183× | let last_serial = latest.as_ref().and_then(|a| match a.attest.body { |
| 102 | 230× | AttestBody::Ref { serial, .. } => Some(serial), |
| 103 | 0× | _ => None, |
| 104 | 230× | }); |
| 105 | 1183× | let attestation = latest.filter(|a| matches!(&a.attest.body, AttestBody::Ref { new_state, .. } if new_state == state)); |
| 106 | 1183× | let accepted = match &attestation { |
| 107 | 219× | Some(a) => self.acceptance_of(&a.attest.hash())?, |
| 108 | 964× | None => None, |
| 109 | | }; |
| 110 | 1183× | out.push(RemoteRef { |
| 111 | 1183× | name: name.clone(), |
| 112 | 1183× | state: *state, |
| 113 | 1183× | heads: self.store.get_state(state)?.heads, |
| 114 | 1183× | attestation, |
| 115 | 1183× | last_serial, |
| 116 | 1183× | accepted, |
| 117 | 1183× | logged: None, |
| 118 | 1183× | integration: vec![], |
| 119 | | }); |
| 120 | | } |
| 121 | 1130× | let policy = self.current_policy()?.map(|(_, sp)| sp); |
| 122 | 1130× | let history = self |
| 123 | 1130× | .policy_history()? |
| 124 | 1130× | .into_iter() |
| 125 | 1130× | .filter(|sp| { |
| 126 | 221× | policy |
| 127 | 221× | .as_ref() |
| 128 | 221× | .is_some_and(|p| sp.policy.serial < p.policy.serial) |
| 129 | 221× | }) |
| 130 | 1130× | .collect(); |
| 131 | | Ok(RefsResponse { |
| 132 | 1130× | refs: out, |
| 133 | 1130× | policy, |
| 134 | 1130× | tags: self.tag_refs()?, |
| 135 | 1130× | history, |
| 136 | 1130× | default_bookmark: Some(self.config.bookmark.clone()), |
| 137 | 1130× | log: None, |
| 138 | 1130× | witnesses: vec![], |
| 139 | 1130× | withdrawn: self.withdrawn_refs()?, |
| 140 | | }) |
| 141 | 1130× | } |
| 142 | |
|
| 143 | | |
| 144 | 1547× | fn closure_in_store(&self, heads: &[ChangeHash]) -> Result<BTreeSet<ChangeHash>> { |
| 145 | 1547× | let mut seen = BTreeSet::new(); |
| 146 | 1547× | let mut stack: Vec<ChangeHash> = heads.to_vec(); |
| 147 | 5325× | while let Some(h) = stack.pop() { |
| 148 | 3778× | if seen.contains(&h) || !self.store.has(&h) { |
| 149 | 1114× | continue; |
| 150 | 2664× | } |
| 151 | 2664× | seen.insert(h); |
| 152 | 2664× | stack.extend(self.store.get_change_meta(&h)?.deps); |
| 153 | | } |
| 154 | 1547× | Ok(seen) |
| 155 | 1547× | } |
| 156 | |
|
| 157 | | |
| 158 | | |
| 159 | 3× | pub fn shallow_changes(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> { |
| 160 | 3× | let mut out = Vec::new(); |
| 161 | 3× | let mut seen: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 162 | 3× | let mut layer: Vec<ChangeHash> = heads.to_vec(); |
| 163 | 3× | for _ in 0..depth.max(1) { |
| 164 | 8× | let mut next = Vec::new(); |
| 165 | 12× | for h in layer { |
| 166 | 12× | if !seen.insert(h) || !self.store.has(&h) { |
| 167 | 2× | continue; |
| 168 | 10× | } |
| 169 | 10× | let c = self.store.get_change(&h)?; |
| 170 | 10× | next.extend(c.deps.iter().copied()); |
| 171 | 10× | out.push(h); |
| 172 | | } |
| 173 | 8× | if next.is_empty() { |
| 174 | 1× | break; |
| 175 | 7× | } |
| 176 | 7× | layer = next; |
| 177 | | } |
| 178 | 3× | out.reverse(); |
| 179 | 3× | Ok(out) |
| 180 | 3× | } |
| 181 | |
|
| 182 | 2183× | fn shallow_path(&self) -> PathBuf { |
| 183 | 2183× | self.oxy.join("shallow") |
| 184 | 2183× | } |
| 185 | |
|
| 186 | | |
| 187 | 2180× | pub fn boundary(&self) -> BTreeSet<ChangeHash> { |
| 188 | 2180× | fs::read_to_string(self.shallow_path()) |
| 189 | 2180× | .map(|t| t.lines().filter_map(ObjectRef::from_hex).collect()) |
| 190 | 2180× | .unwrap_or_default() |
| 191 | 2180× | } |
| 192 | |
|
| 193 | 190× | pub fn is_shallow(&self) -> bool { |
| 194 | 190× | !self.boundary().is_empty() |
| 195 | 190× | } |
| 196 | |
|
| 197 | 3× | fn write_boundary(&self, boundary: &BTreeSet<ChangeHash>) -> Result<()> { |
| 198 | 3× | if boundary.is_empty() { |
| 199 | 1× | let _ = fs::remove_file(self.shallow_path()); |
| 200 | 1× | return Ok(()); |
| 201 | 2× | } |
| 202 | 2× | let text: String = boundary |
| 203 | 2× | .iter() |
| 204 | 2× | .map(|h| format!("{}\n", h.to_hex())) |
| 205 | 2× | .collect(); |
| 206 | 2× | write_atomic(&self.shallow_path(), text.as_bytes()) |
| 207 | 3× | } |
| 208 | |
|
| 209 | | |
| 210 | | |
| 211 | 3× | fn refresh_boundary(&self, imported: &[ChangeHash]) -> Result<()> { |
| 212 | 3× | let mut boundary = self.boundary(); |
| 213 | 3× | boundary.retain(|h| !self.store.has(h)); |
| 214 | 10× | for h in imported { |
| 215 | 13× | for d in self.store.get_change_meta(h)?.deps { |
| 216 | 13× | if !self.store.has(&d) { |
| 217 | 2× | boundary.insert(d); |
| 218 | 11× | } |
| 219 | | } |
| 220 | | } |
| 221 | 3× | self.write_boundary(&boundary) |
| 222 | 3× | } |
| 223 | |
|
| 224 | | |
| 225 | 2× | pub fn deepen(&self, remote_name: &str, depth: u32) -> Result<usize> { |
| 226 | 2× | let boundary = self.boundary(); |
| 227 | 2× | if boundary.is_empty() { |
| 228 | 0× | return Ok(0); |
| 229 | 2× | } |
| 230 | 2× | let remote = self.open_named_remote(remote_name)?; |
| 231 | 2× | let wanted = remote.shallow(&boundary.iter().copied().collect::<Vec<_>>(), depth)?; |
| 232 | 2× | let bundle = remote.fetch(&wanted, self.config.max_blob)?; |
| 233 | 2× | let imported = self.import_bundle_shallow(&bundle, false)?; |
| 234 | 2× | if self.config.max_blob.is_some() { |
| 235 | 0× | self.register_missing_blobs(&imported, remote_name)?; |
| 236 | 2× | } |
| 237 | 2× | self.refresh_boundary(&imported)?; |
| 238 | 2× | Ok(imported.len()) |
| 239 | 2× | } |
| 240 | |
|
| 241 | | |
| 242 | 898× | pub fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> { |
| 243 | 898× | let have = self.closure_in_store(have)?; |
| 244 | | |
| 245 | | |
| 246 | 898× | let boundary = self.boundary(); |
| 247 | 898× | let mut out = Vec::new(); |
| 248 | 898× | let mut done: BTreeSet<ChangeHash> = have.clone(); |
| 249 | 898× | done.extend(boundary.iter().copied()); |
| 250 | | |
| 251 | 1427× | let mut stack: Vec<(ChangeHash, bool)> = want.iter().rev().map(|h| (*h, false)).collect(); |
| 252 | 3718× | while let Some((h, expanded)) = stack.pop() { |
| 253 | 2820× | if done.contains(&h) { |
| 254 | 498× | continue; |
| 255 | 2322× | } |
| 256 | 2322× | if !self.store.has(&h) { |
| 257 | 0× | return fail(format!("change {} is not in this repository", h.short(12))); |
| 258 | 2322× | } |
| 259 | 2322× | if expanded { |
| 260 | 1161× | done.insert(h); |
| 261 | 1161× | out.push(h); |
| 262 | 1161× | continue; |
| 263 | 1161× | } |
| 264 | 1161× | stack.push((h, true)); |
| 265 | 1161× | for d in self.store.get_change_meta(&h)?.deps { |
| 266 | 782× | if !done.contains(&d) { |
| 267 | 232× | stack.push((d, false)); |
| 268 | 550× | } |
| 269 | | } |
| 270 | | } |
| 271 | 898× | Ok(out) |
| 272 | 898× | } |
| 273 | |
|
| 274 | | |
| 275 | | |
| 276 | 343× | pub fn bundle_for(&self, changes: &[ChangeHash]) -> Result<Bundle> { |
| 277 | 343× | self.bundle_of(changes, None, false, false) |
| 278 | 343× | } |
| 279 | |
|
| 280 | | |
| 281 | 4× | pub fn objects_for(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> { |
| 282 | 4× | let mut out = Vec::new(); |
| 283 | 4× | for r in refs { |
| 284 | 4× | let (ty, body) = match self.store.get(r) { |
| 285 | 4× | Ok(x) => x, |
| 286 | 0× | Err(StoreError::NotFound(_)) => continue, |
| 287 | 0× | Err(e) => return Err(e.into()), |
| 288 | | }; |
| 289 | 4× | if ty == ObjectType::Blob { |
| 290 | 5× | for c in Blob::decode(&body)?.chunks { |
| 291 | 5× | if let Ok((cty, cb)) = self.store.get(&c) { |
| 292 | 5× | out.push((cty, cb)); |
| 293 | 5× | } |
| 294 | | } |
| 295 | 0× | } |
| 296 | 4× | out.push((ty, body)); |
| 297 | | } |
| 298 | 4× | Ok(out) |
| 299 | 4× | } |
| 300 | |
|
| 301 | | |
| 302 | | |
| 303 | | |
| 304 | | |
| 305 | | |
| 306 | | |
| 307 | 55× | pub fn bundle_for_filtered( |
| 308 | 55× | &self, |
| 309 | 55× | changes: &[ChangeHash], |
| 310 | 55× | max_blob: Option<u64>, |
| 311 | 55× | ) -> Result<Bundle> { |
| 312 | 55× | self.bundle_of(changes, max_blob, false, true) |
| 313 | 55× | } |
| 314 | |
|
| 315 | | |
| 316 | | |
| 317 | | |
| 318 | | |
| 319 | 380× | pub fn bundle_raw_for(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> { |
| 320 | 380× | self.bundle_of(changes, max_blob, true, true) |
| 321 | 380× | } |
| 322 | |
|
| 323 | | |
| 324 | | |
| 325 | | |
| 326 | | |
| 327 | | |
| 328 | | |
| 329 | | |
| 330 | | |
| 331 | | |
| 332 | | |
| 333 | | |
| 334 | 435× | pub fn served_attest_signers(&self) -> Result<BTreeSet<KeyId>> { |
| 335 | | type Signers = std::collections::HashMap<(PathBuf, ObjectRef), BTreeSet<KeyId>>; |
| 336 | | static SERVED: std::sync::Mutex<Option<Signers>> = std::sync::Mutex::new(None); |
| 337 | 435× | let key = (self.oxy.clone(), self.head_op()?.0); |
| 338 | 435× | if let Some(keys) = SERVED |
| 339 | 435× | .lock() |
| 340 | 435× | .unwrap_or_else(|p| p.into_inner()) |
| 341 | 435× | .as_ref() |
| 342 | 435× | .and_then(|m| m.get(&key)) |
| 343 | | { |
| 344 | 139× | return Ok(keys.clone()); |
| 345 | 296× | } |
| 346 | 296× | let keys = self.attest_signers_now()?; |
| 347 | 296× | let mut served = SERVED.lock().unwrap_or_else(|p| p.into_inner()); |
| 348 | 296× | let map = served.get_or_insert_with(Default::default); |
| 349 | | |
| 350 | | |
| 351 | 6168× | map.retain(|(oxy, _), _| *oxy != key.0); |
| 352 | 296× | if map.len() >= 1024 { |
| 353 | 0× | map.clear(); |
| 354 | 296× | } |
| 355 | 296× | map.insert(key, keys.clone()); |
| 356 | 296× | Ok(keys) |
| 357 | 435× | } |
| 358 | |
|
| 359 | 296× | fn attest_signers_now(&self) -> Result<BTreeSet<KeyId>> { |
| 360 | 296× | let view = self.view()?; |
| 361 | 296× | let pointers = view |
| 362 | 296× | .bookmarks |
| 363 | 296× | .keys() |
| 364 | 306× | .map(|b| self.ref_attest_path(b)) |
| 365 | 296× | .chain(view.tags.keys().map(|t| self.tag_attest_path(t))); |
| 366 | 296× | let mut keys = BTreeSet::new(); |
| 367 | 328× | for p in pointers { |
| 368 | 328× | let Ok(text) = fs::read_to_string(&p) else { |
| 369 | 265× | continue; |
| 370 | | }; |
| 371 | 63× | let Some(r) = ObjectRef::from_hex(text.trim()) else { |
| 372 | 0× | continue; |
| 373 | | }; |
| 374 | 63× | keys.extend(self.store.signatures(&r)?.into_iter().map(|s| s.key)); |
| 375 | | } |
| 376 | 296× | Ok(keys) |
| 377 | 296× | } |
| 378 | |
|
| 379 | | |
| 380 | | |
| 381 | 778× | fn bundle_of( |
| 382 | 778× | &self, |
| 383 | 778× | changes: &[ChangeHash], |
| 384 | 778× | max_blob: Option<u64>, |
| 385 | 778× | raw: bool, |
| 386 | 778× | serving: bool, |
| 387 | 778× | ) -> Result<Bundle> { |
| 388 | 778× | let mut bundle = Bundle::default(); |
| 389 | 778× | let mut objects: BTreeSet<ObjectRef> = BTreeSet::new(); |
| 390 | 778× | let mut seals: BTreeSet<ObjectRef> = BTreeSet::new(); |
| 391 | 1333× | let add_object = |r: &ObjectRef, objects: &mut BTreeSet<ObjectRef>| { |
| 392 | 1333× | if !r.is_zero() && self.store.has(r) { |
| 393 | 384× | objects.insert(*r); |
| 394 | 949× | } |
| 395 | 1333× | }; |
| 396 | 1075× | for h in changes { |
| 397 | | |
| 398 | 1075× | let (c, encoded) = if raw { |
| 399 | 584× | let bytes = self.store.get_typed(h, ObjectType::Change)?; |
| 400 | 584× | let (skel, texts) = Change::decode_skeleton(&bytes, *h)?; |
| 401 | 5034× | for t in texts { |
| 402 | 5034× | if let crate::objects::TextRef::Blob(r, _, _) = t { |
| 403 | 3× | let blob = Blob::decode(&self.store.get_typed(&r, ObjectType::Blob)?)?; |
| 404 | 3× | objects.insert(r); |
| 405 | 3× | objects.extend(blob.chunks); |
| 406 | 5031× | } |
| 407 | | } |
| 408 | 584× | (skel, Some(bytes)) |
| 409 | | } else { |
| 410 | 491× | (self.store.get_change(h)?, None) |
| 411 | | }; |
| 412 | 1075× | let sigs = self.store.signatures(h)?; |
| 413 | 1075× | add_object(&c.author, &mut objects); |
| 414 | 1075× | for s in &sigs { |
| 415 | 37× | add_object(&s.key, &mut objects); |
| 416 | 37× | } |
| 417 | 6082× | for e in &c.edits { |
| 418 | 6082× | if let Edit::Replace { old, new, .. } = e { |
| 419 | 22× | for b in [old, new].into_iter().flatten() { |
| 420 | 22× | if self.store.has(b) { |
| 421 | 22× | if let Ok(blob) = |
| 422 | 22× | Blob::decode(&self.store.get_typed(b, ObjectType::Blob)?) |
| 423 | | { |
| 424 | 22× | if max_blob.is_some_and(|m| blob.length > m) { |
| 425 | 4× | continue; |
| 426 | 18× | } |
| 427 | 18× | objects.insert(*b); |
| 428 | 18× | objects.extend(blob.chunks.iter().copied()); |
| 429 | 0× | } |
| 430 | 0× | } |
| 431 | | } |
| 432 | 6062× | } |
| 433 | | } |
| 434 | 1075× | for s in self.store.seals_for(h)? { |
| 435 | 110× | seals.insert(s); |
| 436 | 110× | } |
| 437 | 1075× | match encoded { |
| 438 | 584× | Some(bytes) => bundle.raw_changes.push((bytes, sigs)), |
| 439 | 491× | None => { |
| 440 | 491× | bundle.changes.push((c, sigs)); |
| 441 | 491× | bundle.hashes.push(*h); |
| 442 | 491× | } |
| 443 | | } |
| 444 | | } |
| 445 | 778× | for r in seals { |
| 446 | 105× | let attest = self.store.get_attest(&r)?; |
| 447 | 105× | let sigs = self.store.signatures(&r)?; |
| 448 | 105× | for s in &sigs { |
| 449 | 105× | add_object(&s.key, &mut objects); |
| 450 | 105× | } |
| 451 | 105× | bundle.attests.push(SignedAttest { attest, sigs }); |
| 452 | | } |
| 453 | 778× | let signers: Vec<KeyId> = if serving { |
| 454 | 435× | let signers = self.served_attest_signers()?; |
| 455 | 435× | for k in &signers { |
| 456 | 116× | add_object(k, &mut objects); |
| 457 | 116× | } |
| 458 | 435× | signers.into_iter().collect() |
| 459 | | } else { |
| 460 | 343× | Vec::new() |
| 461 | | }; |
| 462 | 778× | for r in objects { |
| 463 | 243× | let (ty, body) = self.store.get(&r)?; |
| 464 | 243× | bundle.objects.push((ty, body)); |
| 465 | | } |
| 466 | 778× | self.attach_certificates_for(&mut bundle, &signers)?; |
| 467 | 778× | Ok(bundle) |
| 468 | 778× | } |
| 469 | |
|
| 470 | | |
| 471 | | |
| 472 | | |
| 473 | | |
| 474 | 905× | pub(crate) fn attach_certificates_for( |
| 475 | 905× | &self, |
| 476 | 905× | bundle: &mut Bundle, |
| 477 | 905× | also: &[KeyId], |
| 478 | 905× | ) -> Result<()> { |
| 479 | 905× | let mut keys: BTreeSet<KeyId> = also.iter().copied().collect(); |
| 480 | 905× | keys.extend( |
| 481 | 905× | bundle |
| 482 | 905× | .changes |
| 483 | 905× | .iter() |
| 484 | 905× | .flat_map(|(_, s)| s.iter().map(|s| s.key)), |
| 485 | | ); |
| 486 | 905× | keys.extend( |
| 487 | 905× | bundle |
| 488 | 905× | .raw_changes |
| 489 | 905× | .iter() |
| 490 | 905× | .flat_map(|(_, s)| s.iter().map(|s| s.key)), |
| 491 | | ); |
| 492 | 905× | keys.extend( |
| 493 | 905× | bundle |
| 494 | 905× | .attests |
| 495 | 905× | .iter() |
| 496 | 905× | .flat_map(|a| a.sigs.iter().map(|s| s.key)), |
| 497 | | ); |
| 498 | 905× | for k in keys { |
| 499 | 350× | let Some(r) = self.store.certificate_of(&k)? else { |
| 500 | 301× | continue; |
| 501 | | }; |
| 502 | 49× | let cert = self.store.get_certificate(&r)?; |
| 503 | 49× | if bundle |
| 504 | 49× | .certificates |
| 505 | 49× | .iter() |
| 506 | 49× | .any(|c| c.cert.hash() == cert.cert.hash()) |
| 507 | | { |
| 508 | 12× | continue; |
| 509 | 37× | } |
| 510 | | |
| 511 | | |
| 512 | 37× | let grantor = cert.cert.grant.as_ref().map(|g| g.grant.grantor); |
| 513 | 81× | for id in [Some(k), Some(cert.cert.issuer), grantor].iter().flatten() { |
| 514 | 81× | if let Ok(body) = self.store.get_typed(id, ObjectType::Identity) { |
| 515 | 80× | if !bundle |
| 516 | 80× | .objects |
| 517 | 80× | .iter() |
| 518 | 144× | .any(|(t, b)| *t == ObjectType::Identity && *b == body) |
| 519 | 35× | { |
| 520 | 35× | bundle.objects.push((ObjectType::Identity, body)); |
| 521 | 45× | } |
| 522 | 1× | } |
| 523 | | } |
| 524 | 37× | bundle.certificates.push(cert); |
| 525 | | } |
| 526 | 905× | Ok(()) |
| 527 | 905× | } |
| 528 | |
|
| 529 | | |
| 530 | | |
| 531 | 826× | fn adopt_certificates(&self, certs: &[crate::cert::SignedCertificate]) -> Result<()> { |
| 532 | 826× | for c in certs { |
| 533 | 36× | let issuer = self.store.get_identity(&c.cert.issuer).map_err(|_| { |
| 534 | 0× | RepoError::Msg(format!( |
| 535 | 0× | "a certificate names issuer {}, whose identity did not come with it", |
| 536 | 0× | c.cert.issuer.short(12) |
| 537 | 0× | )) |
| 538 | 0× | })?; |
| 539 | 36× | c.verify(&issuer) |
| 540 | 36× | .map_err(|e| RepoError::Msg(format!("a certificate does not verify: {e}")))?; |
| 541 | 36× | self.store.put_identity(&c.cert.key)?; |
| 542 | 36× | self.store.put_certificate(c)?; |
| 543 | | } |
| 544 | 826× | Ok(()) |
| 545 | 826× | } |
| 546 | |
|
| 547 | | |
| 548 | | |
| 549 | | |
| 550 | | |
| 551 | 57× | pub(crate) fn certified_authorized( |
| 552 | 57× | &self, |
| 553 | 57× | p: &crate::policy::Policy, |
| 554 | 57× | signer: &KeyId, |
| 555 | 57× | reference: &str, |
| 556 | 57× | scope: Scope, |
| 557 | 57× | at_ms: u64, |
| 558 | 57× | ) -> bool { |
| 559 | 57× | let Ok(Some(r)) = self.store.certificate_of(signer) else { |
| 560 | 5× | return false; |
| 561 | | }; |
| 562 | 52× | let Ok(c) = self.store.get_certificate(&r) else { |
| 563 | 0× | return false; |
| 564 | | }; |
| 565 | 52× | let Some(issuer) = p |
| 566 | 52× | .identity(&c.cert.issuer) |
| 567 | 52× | .cloned() |
| 568 | 52× | .or_else(|| self.store.get_identity(&c.cert.issuer).ok()) |
| 569 | | else { |
| 570 | 0× | return false; |
| 571 | | }; |
| 572 | 52× | c.verify(&issuer).is_ok() |
| 573 | 52× | && c.cert.key_id() == *signer |
| 574 | 52× | && c.cert.covers(scope, at_ms) |
| 575 | 42× | && p.subject_grant(reference, &c.cert.issuer, &c.cert.subject, scope) |
| 576 | 57× | } |
| 577 | |
|
| 578 | | |
| 579 | | |
| 580 | 823× | pub fn import_bundle(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> { |
| 581 | 823× | self.import_bundle_inner(bundle, strict, false) |
| 582 | 823× | } |
| 583 | |
|
| 584 | | |
| 585 | | |
| 586 | 3× | pub fn import_bundle_shallow(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> { |
| 587 | 3× | self.import_bundle_inner(bundle, strict, true) |
| 588 | 3× | } |
| 589 | |
|
| 590 | 826× | fn import_bundle_inner( |
| 591 | 826× | &self, |
| 592 | 826× | bundle: &Bundle, |
| 593 | 826× | strict: bool, |
| 594 | 826× | shallow: bool, |
| 595 | 826× | ) -> Result<Vec<ChangeHash>> { |
| 596 | 826× | let batch = self.store.begin_batch()?; |
| 597 | 826× | let result = self.import_bundle_batched(bundle, strict, shallow); |
| 598 | 826× | batch.finish()?; |
| 599 | 824× | result |
| 600 | 826× | } |
| 601 | |
|
| 602 | 826× | fn import_bundle_batched( |
| 603 | 826× | &self, |
| 604 | 826× | bundle: &Bundle, |
| 605 | 826× | strict: bool, |
| 606 | 826× | shallow: bool, |
| 607 | 826× | ) -> Result<Vec<ChangeHash>> { |
| 608 | 826× | for (ty, _) in &bundle.objects { |
| 609 | 0× | if !matches!( |
| 610 | 716× | ty, |
| 611 | | ObjectType::Chunk |
| 612 | | | ObjectType::Blob |
| 613 | | | ObjectType::Identity |
| 614 | | | ObjectType::State |
| 615 | | | ObjectType::Checkpoint |
| 616 | | ) { |
| 617 | 0× | return fail(format!("bundle may not carry {} objects", ty.name())); |
| 618 | 716× | } |
| 619 | | } |
| 620 | | |
| 621 | 826× | let objects: Vec<(ObjectType, &[u8])> = bundle |
| 622 | 826× | .objects |
| 623 | 826× | .iter() |
| 624 | 826× | .map(|(ty, body)| (*ty, body.as_slice())) |
| 625 | 826× | .collect(); |
| 626 | 826× | self.store.put_many(&objects)?; |
| 627 | 826× | self.adopt_certificates(&bundle.certificates)?; |
| 628 | 826× | for sa in &bundle.attests { |
| 629 | 108× | let r = self.store.put_attest(&sa.attest)?; |
| 630 | 108× | for sig in &sa.sigs { |
| 631 | 108× | self.store.put_signature(&r, sig)?; |
| 632 | | } |
| 633 | 108× | if let AttestBody::Seal { hashes } = &sa.attest.body { |
| 634 | 113× | for h in hashes { |
| 635 | 113× | self.store.index_seal(h, &r)?; |
| 636 | | } |
| 637 | 0× | } |
| 638 | | } |
| 639 | | |
| 640 | | |
| 641 | | |
| 642 | 826× | let hashes: Vec<ChangeHash> = if bundle.hashes.len() == bundle.changes.len() { |
| 643 | 817× | bundle.hashes.clone() |
| 644 | | } else { |
| 645 | 9× | bundle.changes.iter().map(|(c, _)| c.hash()).collect() |
| 646 | | }; |
| 647 | 826× | let raw_hashes: Vec<ChangeHash> = bundle |
| 648 | 826× | .raw_changes |
| 649 | 826× | .iter() |
| 650 | 826× | .map(|(b, _)| hash_object(ObjectType::Change, b)) |
| 651 | 826× | .collect(); |
| 652 | 826× | let incoming: BTreeSet<ChangeHash> = hashes.iter().chain(&raw_hashes).copied().collect(); |
| 653 | 826× | let boundary = self.boundary(); |
| 654 | 826× | let mut imported = Vec::new(); |
| 655 | | |
| 656 | | |
| 657 | 826× | for ((bytes, sigs), h) in bundle.raw_changes.iter().zip(raw_hashes) { |
| 658 | 585× | let (skel, texts) = Change::decode_skeleton(bytes, h)?; |
| 659 | 585× | for d in &skel.deps { |
| 660 | 429× | if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d) |
| 661 | | { |
| 662 | 0× | return fail(format!( |
| 663 | | "change {} depends on {}, which is neither stored nor in the bundle", |
| 664 | 0× | h.short(12), |
| 665 | 0× | d.short(12) |
| 666 | | )); |
| 667 | 429× | } |
| 668 | | } |
| 669 | 5035× | for t in texts { |
| 670 | 5035× | if let crate::objects::TextRef::Blob(r, _, _) = t { |
| 671 | 4× | if !self.store.has(&r) { |
| 672 | 1× | return fail(format!( |
| 673 | | "change {} came without its text blob {}", |
| 674 | 1× | h.short(12), |
| 675 | 1× | r.short(12) |
| 676 | | )); |
| 677 | 3× | } |
| 678 | 5031× | } |
| 679 | | } |
| 680 | 584× | self.store.put(ObjectType::Change, bytes)?; |
| 681 | 584× | for sig in sigs { |
| 682 | 13× | self.store.put_signature(&h, sig)?; |
| 683 | | } |
| 684 | 584× | imported.push(h); |
| 685 | | } |
| 686 | 825× | for ((c, sigs), h) in bundle.changes.iter().zip(hashes) { |
| 687 | 518× | for d in &c.deps { |
| 688 | 307× | if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d) |
| 689 | | { |
| 690 | 3× | return fail(format!( |
| 691 | | "change {} depends on {}, which is neither stored nor in the bundle", |
| 692 | 3× | h.short(12), |
| 693 | 3× | d.short(12) |
| 694 | | )); |
| 695 | 304× | } |
| 696 | | } |
| 697 | 515× | let stored = self.store.put_change(c)?; |
| 698 | 515× | if stored != h { |
| 699 | 0× | return fail(format!( |
| 700 | | "change {} is not the change the bundle names", |
| 701 | 0× | h.short(12) |
| 702 | | )); |
| 703 | 515× | } |
| 704 | 515× | for sig in sigs { |
| 705 | 33× | self.store.put_signature(&h, sig)?; |
| 706 | | } |
| 707 | 515× | imported.push(h); |
| 708 | | } |
| 709 | 822× | if strict { |
| 710 | 67× | for h in &imported { |
| 711 | 39× | if !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) { |
| 712 | 4× | return fail(format!( |
| 713 | | "change {} is not authenticated by its author", |
| 714 | 4× | h.short(12) |
| 715 | | )); |
| 716 | 35× | } |
| 717 | | } |
| 718 | 755× | } |
| 719 | 818× | Ok(imported) |
| 720 | 826× | } |
| 721 | |
|
| 722 | | |
| 723 | | |
| 724 | | |
| 725 | | |
| 726 | | |
| 727 | | |
| 728 | | |
| 729 | | |
| 730 | 72× | pub(crate) fn delegate_authorized( |
| 731 | 72× | &self, |
| 732 | 72× | p: &crate::policy::Policy, |
| 733 | 72× | signer: &KeyId, |
| 734 | 72× | reference: &str, |
| 735 | 72× | at_ms: u64, |
| 736 | 72× | ) -> bool { |
| 737 | 72× | if p.is_revoked(signer) { |
| 738 | 1× | return false; |
| 739 | 71× | } |
| 740 | 71× | let Some(rule) = p.rule_for(reference) else { |
| 741 | 0× | return false; |
| 742 | | }; |
| 743 | 72× | rule.keys.iter().any(|k| { |
| 744 | 72× | !p.is_revoked(k) |
| 745 | 72× | && p.identity(k) |
| 746 | 72× | .cloned() |
| 747 | 72× | .or_else(|| self.store.get_identity(k).ok()) |
| 748 | 72× | .is_some_and(|id| id.delegates_to(signer, Scope::REFS, at_ms)) |
| 749 | 72× | }) |
| 750 | 72× | } |
| 751 | |
|
| 752 | | |
| 753 | | |
| 754 | | |
| 755 | | |
| 756 | | |
| 757 | | |
| 758 | 27× | pub(crate) fn predecessor_authorized( |
| 759 | 27× | &self, |
| 760 | 27× | p: &crate::policy::Policy, |
| 761 | 27× | signer: &KeyId, |
| 762 | 27× | reference: &str, |
| 763 | 27× | at_ms: u64, |
| 764 | 27× | ) -> bool { |
| 765 | 27× | if p.is_revoked(signer) { |
| 766 | 0× | return false; |
| 767 | 27× | } |
| 768 | 27× | let Some(rule) = p.rule_for(reference) else { |
| 769 | 0× | return false; |
| 770 | | }; |
| 771 | 28× | let identity = |k: &KeyId| { |
| 772 | 28× | p.identity(k) |
| 773 | 28× | .cloned() |
| 774 | 28× | .or_else(|| self.store.get_identity(k).ok()) |
| 775 | 28× | }; |
| 776 | 27× | rule.keys.iter().any(|k| { |
| 777 | 27× | if p.is_revoked(k) { |
| 778 | 0× | return false; |
| 779 | 27× | } |
| 780 | 27× | let mut cur = identity(k); |
| 781 | 27× | for _ in 0..32 { |
| 782 | 28× | let Some(id) = cur else { |
| 783 | 0× | return false; |
| 784 | | }; |
| 785 | 1× | match id.previous { |
| 786 | 6× | Some(prev) if prev == *signer => return at_ms < id.created, |
| 787 | 1× | Some(prev) if !p.is_revoked(&prev) => cur = identity(&prev), |
| 788 | 22× | _ => return false, |
| 789 | | } |
| 790 | | } |
| 791 | 0× | false |
| 792 | 27× | }) |
| 793 | 27× | } |
| 794 | |
|
| 795 | 106× | fn check_ref_attest( |
| 796 | 106× | &self, |
| 797 | 106× | sa: &SignedAttest, |
| 798 | 106× | name: &str, |
| 799 | 106× | old_state: &ObjectRef, |
| 800 | 106× | new_state: &ObjectRef, |
| 801 | 106× | last_serial: Option<u64>, |
| 802 | 106× | allow_equal: bool, |
| 803 | 106× | ) -> std::result::Result<u64, String> { |
| 804 | 104× | let serial = |
| 805 | 106× | self.check_ref_attest_body(sa, name, old_state, new_state, last_serial, allow_equal)?; |
| 806 | 104× | self.verify_attest_signature(sa)?; |
| 807 | 104× | Ok(serial) |
| 808 | 106× | } |
| 809 | |
|
| 810 | | |
| 811 | | |
| 812 | | |
| 813 | 179× | fn check_ref_attest_body( |
| 814 | 179× | &self, |
| 815 | 179× | sa: &SignedAttest, |
| 816 | 179× | name: &str, |
| 817 | 179× | old_state: &ObjectRef, |
| 818 | 179× | new_state: &ObjectRef, |
| 819 | 179× | last_serial: Option<u64>, |
| 820 | 179× | allow_equal: bool, |
| 821 | 179× | ) -> std::result::Result<u64, String> { |
| 822 | | let AttestBody::Ref { |
| 823 | 179× | name: n, |
| 824 | 179× | old_state: o, |
| 825 | 179× | new_state: nw, |
| 826 | 179× | serial, |
| 827 | 179× | } = &sa.attest.body |
| 828 | | else { |
| 829 | 0× | return Err("attestation is not a ref attestation".into()); |
| 830 | | }; |
| 831 | 179× | if n != name || o != old_state || nw != new_state { |
| 832 | 1× | return Err("attestation does not describe this update".into()); |
| 833 | 178× | } |
| 834 | 178× | if sa.attest.repo != self.id().map_err(|e| e.to_string())? { |
| 835 | 0× | return Err("attestation is for a different repository".into()); |
| 836 | 178× | } |
| 837 | 178× | if let Some(last) = last_serial { |
| 838 | 93× | if *serial < last || (*serial == last && !allow_equal) { |
| 839 | 1× | return Err(format!( |
| 840 | 1× | "attestation serial {serial} is not greater than the last seen {last}" |
| 841 | 1× | )); |
| 842 | 92× | } |
| 843 | 85× | } |
| 844 | 177× | Ok(*serial) |
| 845 | 179× | } |
| 846 | |
|
| 847 | | |
| 848 | | |
| 849 | 177× | fn verify_attest_signature(&self, sa: &SignedAttest) -> std::result::Result<(), String> { |
| 850 | 177× | let r = sa.attest.hash(); |
| 851 | 177× | let sig = sa.sigs.first().ok_or("attestation carries no signature")?; |
| 852 | 177× | let id = self |
| 853 | 177× | .store |
| 854 | 177× | .get_identity(&sig.key) |
| 855 | 177× | .map_err(|_| format!("unknown key {}", sig.key.short(12)))?; |
| 856 | 171× | verify_signature(&id, sig, Domain::Attest, &r).map_err(|e| e.to_string()) |
| 857 | 177× | } |
| 858 | |
|
| 859 | | |
| 860 | | |
| 861 | | |
| 862 | | |
| 863 | | |
| 864 | 317× | pub fn receive_push( |
| 865 | 317× | &self, |
| 866 | 317× | bundle: Bundle, |
| 867 | 317× | updates: Vec<RefUpdate>, |
| 868 | 317× | policy: Option<SignedPolicy>, |
| 869 | 317× | tags: Vec<crate::protocol::TagRef>, |
| 870 | 317× | ) -> Result<Vec<RefResult>> { |
| 871 | 317× | Ok(self |
| 872 | 317× | .receive_push_report(bundle, updates, policy, tags)? |
| 873 | | .results) |
| 874 | 317× | } |
| 875 | |
|
| 876 | | |
| 877 | | |
| 878 | | |
| 879 | 367× | pub fn receive_push_report( |
| 880 | 367× | &self, |
| 881 | 367× | bundle: Bundle, |
| 882 | 367× | updates: Vec<RefUpdate>, |
| 883 | 367× | policy: Option<SignedPolicy>, |
| 884 | 367× | tags: Vec<crate::protocol::TagRef>, |
| 885 | 367× | ) -> Result<PushReport> { |
| 886 | | |
| 887 | | |
| 888 | | |
| 889 | 367× | self.refresh()?; |
| 890 | 367× | let mut report = PushReport::default(); |
| 891 | 367× | let results = &mut report.results; |
| 892 | 367× | if let Some(sp) = policy { |
| 893 | 36× | match self.adopt_policy_chain(&bundle.policies, &sp, "push received") { |
| 894 | 34× | Ok(()) => { |
| 895 | 34× | results.push(RefResult { |
| 896 | 34× | name: "policy".into(), |
| 897 | 34× | ok: true, |
| 898 | 34× | reason: format!("serial {}", sp.policy.serial), |
| 899 | 34× | }); |
| 900 | 34× | } |
| 901 | 2× | Err(e) => { |
| 902 | 2× | results.push(RefResult { |
| 903 | 2× | name: "policy".into(), |
| 904 | 2× | ok: false, |
| 905 | 2× | reason: e, |
| 906 | 2× | }); |
| 907 | 2× | for u in updates { |
| 908 | 0× | results.push(RefResult { |
| 909 | 0× | name: u.name, |
| 910 | 0× | ok: false, |
| 911 | 0× | reason: "policy update was rejected".into(), |
| 912 | 0× | }); |
| 913 | 0× | } |
| 914 | 2× | return Ok(report); |
| 915 | | } |
| 916 | | } |
| 917 | 331× | } |
| 918 | 365× | let policy = self.current_policy()?.map(|(_, sp)| sp.policy); |
| 919 | | |
| 920 | | |
| 921 | 365× | let objects_only = updates.is_empty() && tags.is_empty(); |
| 922 | 365× | match self.import_bundle(&bundle, policy.is_some()) { |
| 923 | 359× | Ok(imported) => { |
| 924 | 359× | if objects_only { |
| 925 | 11× | results.push(RefResult { |
| 926 | 11× | name: OBJECTS_RESULT.into(), |
| 927 | 11× | ok: true, |
| 928 | 11× | reason: format!("{} changes stored", imported.len()), |
| 929 | 11× | }); |
| 930 | 348× | } |
| 931 | 359× | report.imported = imported; |
| 932 | | } |
| 933 | 6× | Err(e) => { |
| 934 | 6× | if objects_only { |
| 935 | 0× | results.push(RefResult { |
| 936 | 0× | name: OBJECTS_RESULT.into(), |
| 937 | 0× | ok: false, |
| 938 | 0× | reason: e.to_string(), |
| 939 | 0× | }); |
| 940 | 6× | } |
| 941 | 6× | for u in updates { |
| 942 | 6× | results.push(RefResult { |
| 943 | 6× | name: u.name, |
| 944 | 6× | ok: false, |
| 945 | 6× | reason: e.to_string(), |
| 946 | 6× | }); |
| 947 | 6× | } |
| 948 | 6× | return Ok(report); |
| 949 | | } |
| 950 | | } |
| 951 | 359× | let (op_ref, op) = self.head_op()?; |
| 952 | 359× | let mut view = op.view.clone(); |
| 953 | 359× | let mut names = Vec::new(); |
| 954 | 359× | for u in updates { |
| 955 | 343× | let outcome = self.apply_update(&mut view, &u, policy.as_ref()); |
| 956 | 343× | let (ok, reason) = match outcome { |
| 957 | 309× | Ok(()) => (true, String::new()), |
| 958 | 34× | Err(e) => (false, e), |
| 959 | | }; |
| 960 | 343× | if ok { |
| 961 | 309× | names.push(u.name.clone()); |
| 962 | 309× | let heads = self |
| 963 | 309× | .store |
| 964 | 309× | .get_state(&u.new_state) |
| 965 | 309× | .map(|s| s.heads) |
| 966 | 309× | .unwrap_or_default(); |
| 967 | 309× | report.updated.push(RefUpdated { |
| 968 | 309× | name: u.name.clone(), |
| 969 | 309× | old_state: u.old_state, |
| 970 | 309× | new_state: u.new_state, |
| 971 | 309× | heads, |
| 972 | 309× | }); |
| 973 | 34× | } |
| 974 | 343× | results.push(RefResult { |
| 975 | 343× | name: u.name, |
| 976 | 343× | ok, |
| 977 | 343× | reason, |
| 978 | 343× | }); |
| 979 | | } |
| 980 | 359× | for t in &tags { |
| 981 | 22× | let name = format!("tags/{}", t.name); |
| 982 | 22× | if t.is_withdrawal() { |
| 983 | 3× | match self.adopt_withdrawal(&mut view, t, policy.as_ref(), &mut TagCheck::Received) |
| 984 | | { |
| 985 | 2× | Ok(removed) => { |
| 986 | | |
| 987 | | |
| 988 | 2× | if removed { |
| 989 | 2× | report.updated.push(RefUpdated { |
| 990 | 2× | name: name.clone(), |
| 991 | 2× | old_state: t.state, |
| 992 | 2× | new_state: ObjectRef::ZERO, |
| 993 | 2× | heads: vec![], |
| 994 | 2× | }); |
| 995 | 2× | } |
| 996 | 2× | names.push(name.clone()); |
| 997 | 2× | results.push(RefResult { |
| 998 | 2× | name, |
| 999 | | ok: true, |
| 1000 | 2× | reason: if removed { |
| 1001 | 2× | "withdrawn" |
| 1002 | | } else { |
| 1003 | 0× | "already withdrawn" |
| 1004 | | } |
| 1005 | 2× | .into(), |
| 1006 | | }); |
| 1007 | | } |
| 1008 | 1× | Err(e) => results.push(RefResult { |
| 1009 | 1× | name, |
| 1010 | 1× | ok: false, |
| 1011 | 1× | reason: e.reason, |
| 1012 | 1× | }), |
| 1013 | | } |
| 1014 | 3× | continue; |
| 1015 | 19× | } |
| 1016 | 19× | let new = !view.tags.contains_key(&t.name); |
| 1017 | 19× | match self.adopt_tag(&mut view, t, policy.as_ref(), &mut TagCheck::Received) { |
| 1018 | | Ok(()) => { |
| 1019 | | |
| 1020 | | |
| 1021 | | |
| 1022 | 14× | if new { |
| 1023 | 13× | report.updated.push(RefUpdated { |
| 1024 | 13× | name: name.clone(), |
| 1025 | 13× | old_state: ObjectRef::ZERO, |
| 1026 | 13× | new_state: t.state, |
| 1027 | 13× | heads: t.heads.clone(), |
| 1028 | 13× | }); |
| 1029 | 13× | } |
| 1030 | 14× | names.push(name.clone()); |
| 1031 | 14× | results.push(RefResult { |
| 1032 | 14× | name, |
| 1033 | 14× | ok: true, |
| 1034 | 14× | reason: String::new(), |
| 1035 | 14× | }); |
| 1036 | | } |
| 1037 | 5× | Err(e) => results.push(RefResult { |
| 1038 | 5× | name, |
| 1039 | 5× | ok: false, |
| 1040 | 5× | reason: e.reason, |
| 1041 | 5× | }), |
| 1042 | | } |
| 1043 | | } |
| 1044 | 359× | if !names.is_empty() { |
| 1045 | 311× | self.record_op( |
| 1046 | 311× | &[op_ref], |
| 1047 | 311× | &format!("push received: {}", names.join(" ")), |
| 1048 | 311× | view, |
| 1049 | 3× | )?; |
| 1050 | 48× | } |
| 1051 | 356× | Ok(report) |
| 1052 | 367× | } |
| 1053 | |
|
| 1054 | | |
| 1055 | | |
| 1056 | | |
| 1057 | | |
| 1058 | 106× | fn record_update_attest( |
| 1059 | 106× | &self, |
| 1060 | 106× | u: &RefUpdate, |
| 1061 | 106× | sa: &SignedAttest, |
| 1062 | 106× | ) -> std::result::Result<(), String> { |
| 1063 | 106× | let name = &u.name; |
| 1064 | 106× | let last = self |
| 1065 | 106× | .latest_ref_attest(name) |
| 1066 | 106× | .map_err(|e| format!("reading the last attestation of {name:?}: {e}"))? |
| 1067 | 106× | .and_then(|(_, a)| match a.attest.body { |
| 1068 | 65× | AttestBody::Ref { serial, .. } => Some(serial), |
| 1069 | 0× | _ => None, |
| 1070 | 65× | }); |
| 1071 | 106× | self.check_ref_attest(sa, name, &u.old_state, &u.new_state, last, false)?; |
| 1072 | 104× | let store = |e: &dyn std::fmt::Display| format!("storing the attestation of {name:?}: {e}"); |
| 1073 | 104× | let r = self.store.put_attest(&sa.attest).map_err(|e| store(&e))?; |
| 1074 | 103× | for sig in &sa.sigs { |
| 1075 | 103× | self.store.put_signature(&r, sig).map_err(|e| store(&e))?; |
| 1076 | | } |
| 1077 | 103× | self.set_ref_attest(name, &r).map_err(|e| store(&e))?; |
| 1078 | | |
| 1079 | 100× | let certified = sa |
| 1080 | 100× | .sigs |
| 1081 | 100× | .first() |
| 1082 | 100× | .is_some_and(|s| self.store.certificate_of(&s.key).ok().flatten().is_some()); |
| 1083 | 100× | if certified { |
| 1084 | 6× | self.record_acceptance(&r).map_err(|e| store(&e))?; |
| 1085 | 94× | } |
| 1086 | 100× | Ok(()) |
| 1087 | 106× | } |
| 1088 | |
|
| 1089 | | |
| 1090 | | |
| 1091 | 11× | pub(crate) fn record_acceptance(&self, r: &ObjectRef) -> Result<()> { |
| 1092 | 11× | let Some(acceptor) = &self.acceptor else { |
| 1093 | 7× | return Ok(()); |
| 1094 | | }; |
| 1095 | 4× | let (id, sk) = (&acceptor.0, &acceptor.1); |
| 1096 | 4× | let attest = Attest { |
| 1097 | 4× | repo: self.id()?, |
| 1098 | 4× | hlc: Hlc::now(), |
| 1099 | 4× | body: AttestBody::Accepted { attest: *r }, |
| 1100 | | }; |
| 1101 | 4× | let sig = sk.sign(Domain::Attest, &id.hash(), &attest.hash()); |
| 1102 | 4× | self.store.put_identity(id)?; |
| 1103 | 4× | let a = self.store.put_attest(&attest)?; |
| 1104 | 4× | self.store.put_signature(&a, &sig)?; |
| 1105 | 4× | self.store.index_add_pub("accepted", r, &a)?; |
| 1106 | 4× | Ok(()) |
| 1107 | 11× | } |
| 1108 | |
|
| 1109 | | |
| 1110 | 227× | pub fn acceptance_of(&self, r: &ObjectRef) -> Result<Option<SignedAttest>> { |
| 1111 | 227× | let Some(a) = self.store.index_get_pub("accepted", r)?.into_iter().next() else { |
| 1112 | 206× | return Ok(None); |
| 1113 | | }; |
| 1114 | | Ok(Some(SignedAttest { |
| 1115 | 21× | attest: self.store.get_attest(&a)?, |
| 1116 | 21× | sigs: self.store.signatures(&a)?, |
| 1117 | | })) |
| 1118 | 227× | } |
| 1119 | |
|
| 1120 | | |
| 1121 | | |
| 1122 | | |
| 1123 | | |
| 1124 | 31× | pub(crate) fn certified_time( |
| 1125 | 31× | &self, |
| 1126 | 31× | signer: &KeyId, |
| 1127 | 31× | sa: &SignedAttest, |
| 1128 | 31× | accepted: Option<&SignedAttest>, |
| 1129 | 31× | ) -> u64 { |
| 1130 | 31× | self.issuer_acceptance(signer, sa, accepted) |
| 1131 | 31× | .unwrap_or(sa.attest.hlc.wall_ms) |
| 1132 | 31× | } |
| 1133 | |
|
| 1134 | | |
| 1135 | | |
| 1136 | 101× | pub(crate) fn issuer_acceptance( |
| 1137 | 101× | &self, |
| 1138 | 101× | signer: &KeyId, |
| 1139 | 101× | sa: &SignedAttest, |
| 1140 | 101× | accepted: Option<&SignedAttest>, |
| 1141 | 101× | ) -> Option<u64> { |
| 1142 | 101× | let acc = accepted?; |
| 1143 | 46× | let c = self.store.certificate_of(signer).ok().flatten()?; |
| 1144 | 46× | let cert = self.store.get_certificate(&c).ok()?; |
| 1145 | 46× | let AttestBody::Accepted { attest } = &acc.attest.body else { |
| 1146 | 0× | return None; |
| 1147 | | }; |
| 1148 | 46× | let sig = acc.sigs.iter().find(|s| s.key == cert.cert.issuer)?; |
| 1149 | 43× | let issuer = self.store.get_identity(&cert.cert.issuer).ok()?; |
| 1150 | 43× | (*attest == sa.attest.hash() |
| 1151 | 42× | && verify_signature(&issuer, sig, Domain::Attest, &acc.attest.hash()).is_ok()) |
| 1152 | 43× | .then_some(acc.attest.hlc.wall_ms) |
| 1153 | 101× | } |
| 1154 | |
|
| 1155 | | |
| 1156 | | |
| 1157 | | |
| 1158 | 9× | pub(crate) fn keep_acceptance(&self, r: &ObjectRef, acc: &SignedAttest) -> Result<()> { |
| 1159 | 9× | let a = self.store.put_attest(&acc.attest)?; |
| 1160 | 11× | for sig in &acc.sigs { |
| 1161 | 11× | self.store.put_signature(&a, sig)?; |
| 1162 | | } |
| 1163 | 9× | self.store.index_add_pub("accepted", r, &a)?; |
| 1164 | 9× | Ok(()) |
| 1165 | 9× | } |
| 1166 | |
|
| 1167 | | |
| 1168 | | |
| 1169 | | |
| 1170 | | |
| 1171 | | |
| 1172 | | |
| 1173 | | |
| 1174 | | |
| 1175 | | |
| 1176 | | #[allow(clippy::too_many_arguments)] |
| 1177 | 27× | pub(crate) fn authorize_in_time( |
| 1178 | 27× | &self, |
| 1179 | 27× | p: &crate::policy::Policy, |
| 1180 | 27× | remote_name: &str, |
| 1181 | 27× | r: &crate::protocol::RemoteRef, |
| 1182 | 27× | sa: &SignedAttest, |
| 1183 | 27× | signer: &KeyId, |
| 1184 | 27× | log: &super::transparency::LogCheck, |
| 1185 | 27× | notes: &mut Vec<String>, |
| 1186 | 27× | ) -> Result<()> { |
| 1187 | | use super::transparency::{gap_note, judge, Timed}; |
| 1188 | 27× | let own = sa.attest.hlc.wall_ms; |
| 1189 | 27× | let at = self.certified_time(signer, sa, r.accepted.as_ref()); |
| 1190 | 27× | let time = log.time_of(&r.name); |
| 1191 | 29× | let check = |t_delegate: u64, t_cert: u64| { |
| 1192 | 29× | self.delegate_authorized(p, signer, &r.name, t_delegate) |
| 1193 | 27× | || self.certified_authorized(p, signer, &r.name, Scope::REFS, t_cert) |
| 1194 | 29× | }; |
| 1195 | 27× | let before = if at != own { |
| 1196 | 7× | "the host's acceptance" |
| 1197 | | } else { |
| 1198 | 20× | "the time it claims" |
| 1199 | | }; |
| 1200 | 27× | let what = format!("{remote_name}/{}", r.name); |
| 1201 | 27× | match judge(time.as_ref().ok().map(|i| i.ms()), |t| check(t, t), || check(own, at)) { |
| 1202 | | Timed::Refused => { |
| 1203 | 1× | return fail(format!( |
| 1204 | | "{what}: attested by {}, which the policy does not authorize for this bookmark; a policy owner can grant it with `oxy policy grant`", |
| 1205 | 1× | signer.short(12) |
| 1206 | | )) |
| 1207 | | } |
| 1208 | 3× | Timed::Integrated => {} |
| 1209 | 1× | Timed::Disagrees => notes.push(format!( |
| 1210 | | "{what}: {}'s authority had lapsed when witness {} first saw its attestation, though not at {before}; accepted while transparency logs are new", |
| 1211 | 1× | signer.short(12), |
| 1212 | 1× | time.as_ref().map(|i| i.witness.as_str()).unwrap_or("?") |
| 1213 | | )), |
| 1214 | 1× | Timed::Fallback if log.kept => notes.push(format!( |
| 1215 | | "{what}: {} checked at {before}, not at a witnessed time: {}", |
| 1216 | 1× | signer.short(12), |
| 1217 | 1× | time.as_ref().err().map(String::as_str).unwrap_or("none") |
| 1218 | | )), |
| 1219 | 21× | Timed::Fallback => {} |
| 1220 | | } |
| 1221 | 26× | if let Some(n) = time.ok().and_then(|i| gap_note(own, i)) { |
| 1222 | 2× | notes.push(format!("{what}'s attestation {n}")); |
| 1223 | 24× | } |
| 1224 | 26× | Ok(()) |
| 1225 | 27× | } |
| 1226 | |
|
| 1227 | | |
| 1228 | | |
| 1229 | | |
| 1230 | | |
| 1231 | | |
| 1232 | | |
| 1233 | | |
| 1234 | 3× | fn apply_deletion( |
| 1235 | 3× | &self, |
| 1236 | 3× | view: &mut View, |
| 1237 | 3× | u: &RefUpdate, |
| 1238 | 3× | policy: Option<&crate::policy::Policy>, |
| 1239 | 3× | ) -> std::result::Result<(), String> { |
| 1240 | 3× | let Some(current) = view.bookmarks.get(&u.name).copied() else { |
| 1241 | 0× | return Err(format!("no bookmark {:?} to delete", u.name)); |
| 1242 | | }; |
| 1243 | 3× | if current != u.old_state { |
| 1244 | 0× | return Err("stale: the remote bookmark has moved; pull first".into()); |
| 1245 | 3× | } |
| 1246 | 3× | if !self.is_bare() && u.name == self.config.bookmark { |
| 1247 | 1× | return Err(format!( |
| 1248 | 1× | "{:?} is this repository's default bookmark (checked out there) and cannot be deleted", |
| 1249 | 1× | u.name |
| 1250 | 1× | )); |
| 1251 | 2× | } |
| 1252 | 2× | if let Some(p) = policy { |
| 1253 | 0× | let Some(sa) = &u.attestation else { |
| 1254 | 0× | return Err(format!( |
| 1255 | 0× | "deleting {:?} requires a signed reference attestation under this repository's policy", |
| 1256 | 0× | u.name |
| 1257 | 0× | )); |
| 1258 | | }; |
| 1259 | 0× | let signer = sa |
| 1260 | 0× | .sigs |
| 1261 | 0× | .first() |
| 1262 | 0× | .map(|s| s.key) |
| 1263 | 0× | .ok_or("attestation carries no signature")?; |
| 1264 | | |
| 1265 | 0× | let now = Hlc::now().wall_ms; |
| 1266 | 0× | if !p.authorizes(&signer, &u.name) |
| 1267 | 0× | && !self.delegate_authorized(p, &signer, &u.name, sa.attest.hlc.wall_ms) |
| 1268 | 0× | && !self.certified_authorized(p, &signer, &u.name, Scope::REFS, now) |
| 1269 | | { |
| 1270 | 0× | return Err(format!( |
| 1271 | 0× | "key {} may not delete {:?} under this repository's policy", |
| 1272 | 0× | signer.short(12), |
| 1273 | 0× | u.name |
| 1274 | 0× | )); |
| 1275 | 0× | } |
| 1276 | 2× | } |
| 1277 | 2× | if let Some(sa) = &u.attestation { |
| 1278 | 1× | self.record_update_attest(u, sa)?; |
| 1279 | 1× | } |
| 1280 | 2× | view.bookmarks.remove(&u.name); |
| 1281 | 2× | Ok(()) |
| 1282 | 3× | } |
| 1283 | |
|
| 1284 | 343× | fn apply_update( |
| 1285 | 343× | &self, |
| 1286 | 343× | view: &mut View, |
| 1287 | 343× | u: &RefUpdate, |
| 1288 | 343× | policy: Option<&crate::policy::Policy>, |
| 1289 | 343× | ) -> std::result::Result<(), String> { |
| 1290 | 343× | if u.name == "policy" { |
| 1291 | 0× | return Err( |
| 1292 | 0× | "the policy reference is updated by sending a policy, not a bookmark update".into(), |
| 1293 | 0× | ); |
| 1294 | 343× | } |
| 1295 | 343× | let empty = self |
| 1296 | 343× | .store |
| 1297 | 343× | .put_state(&StateObject::new(vec![])) |
| 1298 | 343× | .map_err(|e| e.to_string())?; |
| 1299 | 343× | if u.new_state.is_zero() { |
| 1300 | 3× | return self.apply_deletion(view, u, policy); |
| 1301 | 340× | } |
| 1302 | 340× | let current = view.bookmarks.get(&u.name).copied().unwrap_or(empty); |
| 1303 | 340× | if current != u.old_state { |
| 1304 | 14× | return Err("stale: the remote bookmark has moved; pull first".into()); |
| 1305 | 326× | } |
| 1306 | 326× | let new_state = self |
| 1307 | 326× | .store |
| 1308 | 326× | .get_state(&u.new_state) |
| 1309 | 326× | .map_err(|_| "new state object is missing".to_string())?; |
| 1310 | 638× | for h in &new_state.heads { |
| 1311 | 638× | if !self.store.has(h) { |
| 1312 | 1× | return Err(format!("head {} is missing", h.short(12))); |
| 1313 | 637× | } |
| 1314 | | } |
| 1315 | 325× | if !u.force { |
| 1316 | 324× | let old_heads = self |
| 1317 | 324× | .store |
| 1318 | 324× | .get_state(¤t) |
| 1319 | 324× | .map_err(|e| e.to_string())? |
| 1320 | | .heads; |
| 1321 | 324× | let closure = self |
| 1322 | 324× | .closure_in_store(&new_state.heads) |
| 1323 | 324× | .map_err(|e| e.to_string())?; |
| 1324 | 345× | if let Some(h) = old_heads.iter().find(|h| !closure.contains(h)) { |
| 1325 | 1× | return Err(format!( |
| 1326 | 1× | "not a fast-forward: current head {} would be dropped (use --force)", |
| 1327 | 1× | h.short(12) |
| 1328 | 1× | )); |
| 1329 | 323× | } |
| 1330 | 1× | } |
| 1331 | 324× | if let Some(p) = policy { |
| 1332 | 59× | let Some(sa) = &u.attestation else { |
| 1333 | 1× | return Err(format!( |
| 1334 | 1× | "{:?} requires a signed reference attestation under this repository's policy", |
| 1335 | 1× | u.name |
| 1336 | 1× | )); |
| 1337 | | }; |
| 1338 | 58× | let signer = sa |
| 1339 | 58× | .sigs |
| 1340 | 58× | .first() |
| 1341 | 58× | .map(|s| s.key) |
| 1342 | 58× | .ok_or("attestation carries no signature")?; |
| 1343 | | |
| 1344 | 58× | let now = Hlc::now().wall_ms; |
| 1345 | 58× | if !p.authorizes(&signer, &u.name) |
| 1346 | 19× | && !self.delegate_authorized(p, &signer, &u.name, sa.attest.hlc.wall_ms) |
| 1347 | 16× | && !self.certified_authorized(p, &signer, &u.name, Scope::REFS, now) |
| 1348 | | { |
| 1349 | 10× | return Err(format!( |
| 1350 | 10× | "key {} may not update {:?} under this repository's policy", |
| 1351 | 10× | signer.short(12), |
| 1352 | 10× | u.name |
| 1353 | 10× | )); |
| 1354 | 48× | } |
| 1355 | 48× | let old_heads = self |
| 1356 | 48× | .store |
| 1357 | 48× | .get_state(¤t) |
| 1358 | 48× | .map_err(|e| e.to_string())? |
| 1359 | | .heads; |
| 1360 | 48× | self.state_allowed(&old_heads, &new_state.heads, p, &u.name)?; |
| 1361 | 265× | } |
| 1362 | 313× | if let Some(sa) = &u.attestation { |
| 1363 | 105× | self.record_update_attest(u, sa)?; |
| 1364 | 208× | } |
| 1365 | 307× | view.bookmarks.insert(u.name.clone(), u.new_state); |
| 1366 | 307× | Ok(()) |
| 1367 | 343× | } |
| 1368 | |
|
| 1369 | | |
| 1370 | |
|
| 1371 | 1503× | fn remote_url(&self, name: &str) -> Result<String> { |
| 1372 | 1503× | self.config.remotes.get(name).cloned().ok_or_else(|| { |
| 1373 | 3× | let names: Vec<&str> = self.config.remotes.keys().map(String::as_str).collect(); |
| 1374 | 3× | RepoError::Msg(match names.as_slice() { |
| 1375 | 3× | [] => format!("no remote named {name:?}; add one with `oxy remote add`"), |
| 1376 | 1× | _ if name == "origin" && names.len() > 1 && self.config.default_remote.is_none() => format!( |
| 1377 | | "this repository has no remote named origin, and several others ({}): name one (`oxy pull {}`), or make one the default with `oxy config core.remote {}`", |
| 1378 | 1× | names.join(", "), |
| 1379 | 1× | names[0], |
| 1380 | 1× | names[0] |
| 1381 | | ), |
| 1382 | 0× | _ => format!( |
| 1383 | | "no remote named {name:?}; this repository has {}", |
| 1384 | 0× | names.join(", ") |
| 1385 | | ), |
| 1386 | | }) |
| 1387 | 3× | }) |
| 1388 | 1503× | } |
| 1389 | |
|
| 1390 | | |
| 1391 | | |
| 1392 | | |
| 1393 | 819× | fn check_remote_id( |
| 1394 | 819× | &self, |
| 1395 | 819× | remote: &dyn crate::protocol::Remote, |
| 1396 | 819× | remote_name: &str, |
| 1397 | 819× | allow_unbound: bool, |
| 1398 | 819× | ) -> Result<()> { |
| 1399 | 819× | let theirs = remote.repo_id()?; |
| 1400 | 818× | if theirs == self.id()? { |
| 1401 | 790× | return Ok(()); |
| 1402 | 28× | } |
| 1403 | 28× | if theirs == crate::protocol::UNBOUND_ID { |
| 1404 | 25× | if allow_unbound { |
| 1405 | 24× | return Ok(()); |
| 1406 | 1× | } |
| 1407 | 1× | return fail(format!( |
| 1408 | | "remote {remote_name:?} is empty and unbound: push to it first" |
| 1409 | | )); |
| 1410 | 3× | } |
| 1411 | 3× | fail(format!( |
| 1412 | | "remote {remote_name:?} is a different repository (repository IDs differ); check its URL with `oxy remote list`" |
| 1413 | | )) |
| 1414 | 819× | } |
| 1415 | |
|
| 1416 | | |
| 1417 | | |
| 1418 | | |
| 1419 | 1118× | pub(crate) fn remote_options(&self, name: &str) -> RemoteOptions { |
| 1420 | 1118× | let url = self.config.remotes.get(name).cloned().unwrap_or_default(); |
| 1421 | 1118× | let mut options = RemoteOptions { |
| 1422 | 1118× | repo_id: self.id().ok(), |
| 1423 | 1118× | token: self.config.remote_tokens.get(name).cloned(), |
| 1424 | 1118× | ca: self.config.remote_cas.get(name).map(PathBuf::from), |
| 1425 | 1118× | cache_dir: Some(self.oxy.join("remotes").join(name).join("strata")), |
| 1426 | 1118× | ..RemoteOptions::default() |
| 1427 | 1118× | }; |
| 1428 | 1118× | self.fill_credentials(&url, &mut options); |
| 1429 | 1118× | options |
| 1430 | 1118× | } |
| 1431 | |
|
| 1432 | | |
| 1433 | | |
| 1434 | 1139× | pub fn credentials(&self) -> crate::credentials::Credentials { |
| 1435 | 1139× | crate::credentials::Credentials::at( |
| 1436 | 1139× | self.user_config |
| 1437 | 1139× | .parent() |
| 1438 | 1139× | .map(Path::to_path_buf) |
| 1439 | 1139× | .unwrap_or_else(default_home), |
| 1440 | | ) |
| 1441 | 1139× | } |
| 1442 | |
|
| 1443 | | |
| 1444 | | |
| 1445 | | |
| 1446 | | |
| 1447 | | |
| 1448 | | |
| 1449 | | |
| 1450 | 1140× | fn fill_credentials(&self, url: &str, options: &mut RemoteOptions) { |
| 1451 | 1140× | if options.token.is_some() && options.ca.is_some() { |
| 1452 | 1× | return; |
| 1453 | 1139× | } |
| 1454 | 1139× | let explicit = |
| 1455 | 1139× | options.token.is_some() || std::env::var("OXY_TOKEN").is_ok_and(|t| !t.is_empty()); |
| 1456 | 1139× | let stored = self.credentials().options_for(url); |
| 1457 | 1139× | options.token = options.token.take().or(stored.token); |
| 1458 | 1139× | options.ca = options.ca.take().or(stored.ca); |
| 1459 | 1139× | let enclosing = self.enclosing_credentials(url); |
| 1460 | 1139× | if options.token.is_none() { |
| 1461 | 1129× | options.token = enclosing.iter().find_map(|(t, _)| t.clone()); |
| 1462 | 10× | } |
| 1463 | 1139× | if options.ca.is_none() { |
| 1464 | 1123× | options.ca = enclosing.iter().find_map(|(_, c)| c.clone()); |
| 1465 | 16× | } |
| 1466 | 1139× | if !explicit { |
| 1467 | 1132× | for t in enclosing.into_iter().filter_map(|(t, _)| t) { |
| 1468 | 5× | if options.token.as_ref() != Some(&t) && !options.fallback_tokens.contains(&t) { |
| 1469 | 2× | options.fallback_tokens.push(t); |
| 1470 | 3× | } |
| 1471 | | } |
| 1472 | 7× | } |
| 1473 | 1140× | } |
| 1474 | |
|
| 1475 | | |
| 1476 | | |
| 1477 | 1139× | fn enclosing_credentials(&self, url: &str) -> Vec<(Option<String>, Option<PathBuf>)> { |
| 1478 | 1139× | let mut out = Vec::new(); |
| 1479 | 1139× | let Some(hub) = crate::credentials::hub_of(url) else { |
| 1480 | 985× | return out; |
| 1481 | | }; |
| 1482 | 154× | let mut at = self.root.parent(); |
| 1483 | 624× | while let Some(dir) = at { |
| 1484 | 470× | let Ok(cfg) = read_config_file(&dir.join(OXY_DIR).join("config")) else { |
| 1485 | 0× | break; |
| 1486 | | }; |
| 1487 | 470× | for (key, remote_url) in &cfg { |
| 1488 | 15× | let Some(name) = key |
| 1489 | 15× | .strip_prefix("remote.") |
| 1490 | 15× | .and_then(|k| k.strip_suffix(".url")) |
| 1491 | | else { |
| 1492 | 7× | continue; |
| 1493 | | }; |
| 1494 | 8× | if crate::credentials::hub_of(remote_url).as_deref() != Some(hub.as_str()) { |
| 1495 | 1× | continue; |
| 1496 | 7× | } |
| 1497 | 7× | out.push(( |
| 1498 | 7× | cfg.get(&format!("remote.{name}.token")).cloned(), |
| 1499 | 7× | cfg.get(&format!("remote.{name}.ca")).map(PathBuf::from), |
| 1500 | 7× | )); |
| 1501 | | } |
| 1502 | 470× | at = dir.parent(); |
| 1503 | | } |
| 1504 | 154× | out |
| 1505 | 1139× | } |
| 1506 | |
|
| 1507 | | |
| 1508 | | |
| 1509 | 0× | pub fn push_seals(&self, remote_name: &str, seals: &[ObjectRef]) -> Result<()> { |
| 1510 | 0× | let remote = self.open_named_remote(remote_name)?; |
| 1511 | 0× | self.check_remote_id(remote.as_ref(), remote_name, false)?; |
| 1512 | 0× | let mut bundle = Bundle::default(); |
| 1513 | 0× | for r in seals { |
| 1514 | 0× | let attest = self.store.get_attest(r)?; |
| 1515 | 0× | let sigs = self.store.signatures(r)?; |
| 1516 | 0× | bundle |
| 1517 | 0× | .attests |
| 1518 | 0× | .push(crate::protocol::SignedAttest { attest, sigs }); |
| 1519 | | } |
| 1520 | 0× | let results = remote.push(bundle, vec![], None, vec![])?; |
| 1521 | 0× | if let Some(r) = results.iter().find(|r| !r.ok) { |
| 1522 | 0× | return fail(format!("{}: {}", r.name, r.reason)); |
| 1523 | 0× | } |
| 1524 | 0× | Ok(()) |
| 1525 | 0× | } |
| 1526 | |
|
| 1527 | | |
| 1528 | 4× | pub fn remote_for_url(&self, base: &str) -> Option<String> { |
| 1529 | 4× | let base = base.trim_end_matches('/'); |
| 1530 | 4× | self.config |
| 1531 | 4× | .remotes |
| 1532 | 4× | .iter() |
| 1533 | 4× | .find(|(_, url)| url.trim_end_matches('/') == base) |
| 1534 | 4× | .map(|(name, _)| name.clone()) |
| 1535 | 4× | } |
| 1536 | |
|
| 1537 | | |
| 1538 | | |
| 1539 | 0× | pub fn open_remote_at(&self, url: &str) -> Result<(String, Box<dyn crate::protocol::Remote>)> { |
| 1540 | 0× | if let Some(name) = self.remote_for_url(url) { |
| 1541 | 0× | return Ok((name.clone(), self.open_named_remote(&name)?)); |
| 1542 | 0× | } |
| 1543 | 0× | let options = RemoteOptions { |
| 1544 | 0× | repo_id: self.id().ok(), |
| 1545 | 0× | ..self.credentials_for(url) |
| 1546 | 0× | }; |
| 1547 | 0× | Ok((url.to_string(), open_remote(url, &options)?)) |
| 1548 | 0× | } |
| 1549 | |
|
| 1550 | | |
| 1551 | | |
| 1552 | 4× | pub fn options_for_url(&self, url: &str) -> RemoteOptions { |
| 1553 | 4× | match self.remote_for_url(url) { |
| 1554 | 4× | Some(name) => self.remote_options(&name), |
| 1555 | 0× | None => RemoteOptions { |
| 1556 | 0× | repo_id: self.id().ok(), |
| 1557 | 0× | ..self.credentials_for(url) |
| 1558 | 0× | }, |
| 1559 | | } |
| 1560 | 4× | } |
| 1561 | |
|
| 1562 | 1101× | fn open_named_remote(&self, name: &str) -> Result<Box<dyn crate::protocol::Remote>> { |
| 1563 | 1101× | open_remote(&self.remote_url(name)?, &self.remote_options(name)) |
| 1564 | 1101× | } |
| 1565 | |
|
| 1566 | 220× | fn serial_path(&self, remote: &str, bookmark: &str) -> PathBuf { |
| 1567 | 220× | self.oxy |
| 1568 | 220× | .join("remotes") |
| 1569 | 220× | .join(remote) |
| 1570 | 220× | .join(format!("{bookmark}.serial")) |
| 1571 | 220× | } |
| 1572 | |
|
| 1573 | 73× | fn last_seen_serial(&self, remote: &str, bookmark: &str) -> Option<u64> { |
| 1574 | 73× | fs::read_to_string(self.serial_path(remote, bookmark)) |
| 1575 | 73× | .ok() |
| 1576 | 73× | .and_then(|s| s.trim().parse().ok()) |
| 1577 | 73× | } |
| 1578 | |
|
| 1579 | 147× | fn record_serial(&self, remote: &str, bookmark: &str, serial: u64) -> Result<()> { |
| 1580 | 147× | let p = self.serial_path(remote, bookmark); |
| 1581 | 147× | fs::create_dir_all(p.parent().unwrap())?; |
| 1582 | 147× | write_atomic(&p, serial.to_string().as_bytes()) |
| 1583 | 147× | } |
| 1584 | |
|
| 1585 | 474× | fn policy_serial_path(&self, remote: &str) -> PathBuf { |
| 1586 | 474× | self.oxy.join("remotes").join(remote).join("policy.serial") |
| 1587 | 474× | } |
| 1588 | |
|
| 1589 | | |
| 1590 | 402× | fn remote_policy_serial(&self, remote: &str) -> Option<u64> { |
| 1591 | 402× | fs::read_to_string(self.policy_serial_path(remote)) |
| 1592 | 402× | .ok() |
| 1593 | 402× | .and_then(|s| s.trim().parse().ok()) |
| 1594 | 402× | } |
| 1595 | |
|
| 1596 | 72× | fn record_policy_serial(&self, remote: &str, serial: u64) -> Result<()> { |
| 1597 | 72× | let p = self.policy_serial_path(remote); |
| 1598 | 72× | fs::create_dir_all(p.parent().unwrap())?; |
| 1599 | 72× | write_atomic(&p, serial.to_string().as_bytes()) |
| 1600 | 72× | } |
| 1601 | |
|
| 1602 | 27× | fn lazy_dir(&self) -> PathBuf { |
| 1603 | 27× | self.oxy.join("lazy") |
| 1604 | 27× | } |
| 1605 | |
|
| 1606 | | |
| 1607 | 4× | fn register_lazy(&self, blob: &ObjectRef, remote: &str) -> Result<()> { |
| 1608 | 4× | fs::create_dir_all(self.lazy_dir())?; |
| 1609 | 4× | write_atomic(&self.lazy_dir().join(blob.to_hex()), remote.as_bytes()) |
| 1610 | 4× | } |
| 1611 | |
|
| 1612 | | |
| 1613 | 15× | pub fn lazy_remote(&self, blob: &ObjectRef) -> Option<String> { |
| 1614 | 15× | fs::read_to_string(self.lazy_dir().join(blob.to_hex())) |
| 1615 | 15× | .ok() |
| 1616 | 15× | .map(|s| s.trim().to_string()) |
| 1617 | 15× | } |
| 1618 | |
|
| 1619 | | |
| 1620 | 4× | pub(crate) fn fetch_lazy_blob(&self, blob: &ObjectRef) -> Result<()> { |
| 1621 | 4× | let Some(remote_name) = self.lazy_remote(blob) else { |
| 1622 | 0× | return Err(RepoError::Store(StoreError::NotFound(*blob))); |
| 1623 | | }; |
| 1624 | 4× | let remote = self.open_named_remote(&remote_name)?; |
| 1625 | 4× | let objects = remote.objects(&[*blob])?; |
| 1626 | 9× | for (ty, body) in objects { |
| 1627 | 9× | if matches!(ty, ObjectType::Blob | ObjectType::Chunk) { |
| 1628 | 9× | self.store.put(ty, &body)?; |
| 1629 | 0× | } |
| 1630 | | } |
| 1631 | 4× | if !self.store.has(blob) { |
| 1632 | 0× | return fail(format!( |
| 1633 | | "{remote_name} did not send blob {}", |
| 1634 | 0× | blob.short(12) |
| 1635 | | )); |
| 1636 | 4× | } |
| 1637 | 4× | let _ = fs::remove_file(self.lazy_dir().join(blob.to_hex())); |
| 1638 | 4× | Ok(()) |
| 1639 | 4× | } |
| 1640 | |
|
| 1641 | | |
| 1642 | | |
| 1643 | 4× | fn register_missing_blobs(&self, changes: &[ChangeHash], remote: &str) -> Result<usize> { |
| 1644 | 4× | let mut n = 0; |
| 1645 | 4× | for h in changes { |
| 1646 | 4× | let c = self.store.get_change(h)?; |
| 1647 | 13× | for e in &c.edits { |
| 1648 | 13× | if let Edit::Replace { old, new, .. } = e { |
| 1649 | 6× | for b in [old, new].into_iter().flatten() { |
| 1650 | 6× | if !self.store.has(b) { |
| 1651 | 4× | self.register_lazy(b, remote)?; |
| 1652 | 4× | n += 1; |
| 1653 | 2× | } |
| 1654 | | } |
| 1655 | 7× | } |
| 1656 | | } |
| 1657 | | } |
| 1658 | 4× | Ok(n) |
| 1659 | 4× | } |
| 1660 | |
|
| 1661 | | |
| 1662 | 402× | pub fn known_heads(&self, view: &View) -> Result<Vec<ChangeHash>> { |
| 1663 | 402× | let mut have: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 1664 | 545× | for st in view |
| 1665 | 402× | .bookmarks |
| 1666 | 402× | .values() |
| 1667 | 402× | .chain(view.remotes.values().flat_map(|b| b.values())) |
| 1668 | | { |
| 1669 | 545× | if let Ok(s) = self.store.get_state(st) { |
| 1670 | 545× | have.extend(s.heads); |
| 1671 | 545× | } |
| 1672 | | } |
| 1673 | 402× | have.extend(view.workspaces.values().copied()); |
| 1674 | 402× | Ok(have.into_iter().collect()) |
| 1675 | 402× | } |
| 1676 | |
|
| 1677 | | |
| 1678 | 408× | pub fn fetch(&self, remote_name: &str) -> Result<FetchReport> { |
| 1679 | 408× | let remote = self.open_named_remote(remote_name)?; |
| 1680 | 405× | self.check_remote_id(remote.as_ref(), remote_name, false)?; |
| 1681 | 403× | let resp = remote.refs()?; |
| 1682 | | |
| 1683 | 402× | let log = self.check_log( |
| 1684 | 402× | &self.remote_url(remote_name)?, |
| 1685 | 402× | self.config.remote_cas.get(remote_name).map(Path::new), |
| 1686 | 402× | &resp, |
| 1687 | 0× | )?; |
| 1688 | 402× | let mut log_notes = log.notes.clone(); |
| 1689 | | |
| 1690 | | |
| 1691 | | |
| 1692 | | |
| 1693 | | |
| 1694 | 402× | let local_policy = self.current_policy()?.map(|(_, sp)| sp.policy); |
| 1695 | 402× | let seen = self.remote_policy_serial(remote_name); |
| 1696 | 402× | let mut unverified = false; |
| 1697 | 402× | match &resp.policy { |
| 1698 | 38× | Some(sp) => { |
| 1699 | 38× | if seen.is_some_and(|s| sp.policy.serial < s) { |
| 1700 | 0× | return fail(format!("{remote_name} presents policy serial {} but presented {} before: refusing the downgrade", sp.policy.serial, seen.unwrap())); |
| 1701 | 38× | } |
| 1702 | 14× | match &local_policy { |
| 1703 | 14× | Some(l) if sp.policy.serial < l.serial => {} |
| 1704 | 14× | Some(l) if sp.policy.serial == l.serial => { |
| 1705 | 10× | if sp.policy.hash() != l.hash() { |
| 1706 | 0× | return fail(format!( |
| 1707 | | "{remote_name} presents a different policy with the same serial {}", |
| 1708 | | l.serial |
| 1709 | | )); |
| 1710 | 10× | } |
| 1711 | | } |
| 1712 | | _ => { |
| 1713 | 28× | self.adopt_policy_chain( |
| 1714 | 28× | &resp.history, |
| 1715 | 28× | sp, |
| 1716 | 28× | &format!("oxy fetch {remote_name}"), |
| 1717 | | ) |
| 1718 | 28× | .map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?; |
| 1719 | | } |
| 1720 | | } |
| 1721 | 38× | self.record_policy_serial(remote_name, sp.policy.serial)?; |
| 1722 | | } |
| 1723 | | None => { |
| 1724 | 364× | if seen.is_some() { |
| 1725 | 0× | return fail(format!("{remote_name} presented a policy before but has none now: refusing the downgrade")); |
| 1726 | 364× | } |
| 1727 | 364× | unverified = true; |
| 1728 | | } |
| 1729 | | } |
| 1730 | 402× | let policy = if unverified { |
| 1731 | 364× | None |
| 1732 | | } else { |
| 1733 | 38× | self.current_policy()?.map(|(_, sp)| sp.policy) |
| 1734 | | }; |
| 1735 | 402× | let (op_ref, op) = self.head_op()?; |
| 1736 | 402× | let mut view = op.view.clone(); |
| 1737 | 402× | let have = self.known_heads(&view)?; |
| 1738 | 402× | let mut report = Vec::new(); |
| 1739 | 402× | let mut skipped = Vec::new(); |
| 1740 | 402× | let max_blob = self.config.max_blob; |
| 1741 | 428× | for r in resp.refs { |
| 1742 | 428× | let needed = { |
| 1743 | 428× | let _t = crate::timing::phase("fetch: negotiate"); |
| 1744 | 428× | remote.negotiate(&have, &r.heads)? |
| 1745 | | }; |
| 1746 | 428× | let bundle = { |
| 1747 | 428× | let _t = crate::timing::phase("fetch: bundle"); |
| 1748 | 428× | remote.fetch(&needed, max_blob)? |
| 1749 | | }; |
| 1750 | | |
| 1751 | | |
| 1752 | | |
| 1753 | 428× | let imported = { |
| 1754 | 428× | let _t = crate::timing::phase("fetch: import"); |
| 1755 | 428× | self.import_bundle(&bundle, false)? |
| 1756 | | }; |
| 1757 | 428× | if max_blob.is_some() { |
| 1758 | 4× | self.register_missing_blobs(&imported, remote_name)?; |
| 1759 | 424× | } |
| 1760 | 595× | for h in &r.heads { |
| 1761 | 595× | if !self.store.has(h) { |
| 1762 | 0× | return fail(format!("remote did not send head {}", h.short(12))); |
| 1763 | 595× | } |
| 1764 | | } |
| 1765 | 428× | let state = self.store.put_state(&StateObject::new(r.heads.clone()))?; |
| 1766 | 428× | if state != r.state { |
| 1767 | 0× | return fail(format!( |
| 1768 | | "remote state {} does not match its heads", |
| 1769 | 0× | r.state.short(12) |
| 1770 | | )); |
| 1771 | 428× | } |
| 1772 | 428× | if let Some(p) = &policy { |
| 1773 | 41× | let Some(sa) = &r.attestation else { |
| 1774 | 0× | return fail(format!( |
| 1775 | | "{remote_name}/{} has no attestation, which its policy requires", |
| 1776 | | r.name |
| 1777 | | )); |
| 1778 | | }; |
| 1779 | 41× | let signer = sa |
| 1780 | 41× | .sigs |
| 1781 | 41× | .first() |
| 1782 | 41× | .map(|s| s.key) |
| 1783 | 41× | .ok_or_else(|| RepoError::Msg("attestation carries no signature".into()))?; |
| 1784 | 41× | if !p.authorizes(&signer, &r.name) { |
| 1785 | 20× | self.authorize_in_time(p, remote_name, &r, sa, &signer, &log, &mut log_notes)?; |
| 1786 | 21× | } |
| 1787 | 387× | } |
| 1788 | 428× | if let Some(sa) = &r.attestation { |
| 1789 | | let AttestBody::Ref { |
| 1790 | 73× | old_state, |
| 1791 | 73× | new_state, |
| 1792 | | .. |
| 1793 | 73× | } = &sa.attest.body |
| 1794 | | else { |
| 1795 | 0× | return fail("remote sent a non-ref attestation for a bookmark"); |
| 1796 | | }; |
| 1797 | 73× | let (old_state, new_state) = (*old_state, *new_state); |
| 1798 | 73× | let serial = self |
| 1799 | 73× | .check_ref_attest_body( |
| 1800 | 73× | sa, |
| 1801 | 73× | &r.name, |
| 1802 | 73× | &old_state, |
| 1803 | 73× | &new_state, |
| 1804 | 73× | self.last_seen_serial(remote_name, &r.name), |
| 1805 | | true, |
| 1806 | | ) |
| 1807 | 73× | .map_err(|e| { |
| 1808 | 0× | RepoError::Msg(format!("attestation for {}/{}: {e}", remote_name, r.name)) |
| 1809 | 0× | })?; |
| 1810 | 73× | if new_state != r.state { |
| 1811 | 0× | return fail(format!( |
| 1812 | | "attestation for {}/{} does not cover its current state", |
| 1813 | | remote_name, r.name |
| 1814 | | )); |
| 1815 | 73× | } |
| 1816 | | |
| 1817 | | |
| 1818 | | |
| 1819 | | |
| 1820 | | |
| 1821 | | |
| 1822 | 73× | if let Err(e) = self.verify_attest_signature(sa) { |
| 1823 | 12× | let why = format!("attestation for {remote_name}/{}: {e}", r.name); |
| 1824 | 12× | if r.name == self.config.bookmark { |
| 1825 | 3× | return fail(why); |
| 1826 | 9× | } |
| 1827 | 9× | skipped.push((r.name, why)); |
| 1828 | 9× | continue; |
| 1829 | 61× | } |
| 1830 | 61× | let ar = self.store.put_attest(&sa.attest)?; |
| 1831 | 61× | for sig in &sa.sigs { |
| 1832 | 61× | self.store.put_signature(&ar, sig)?; |
| 1833 | | } |
| 1834 | 61× | self.record_serial(remote_name, &r.name, serial)?; |
| 1835 | 355× | } |
| 1836 | 416× | view.remotes |
| 1837 | 416× | .entry(remote_name.to_string()) |
| 1838 | 416× | .or_default() |
| 1839 | 416× | .insert(r.name.clone(), state); |
| 1840 | 416× | report.push((r.name, r.heads, imported.len())); |
| 1841 | | } |
| 1842 | 399× | let mut new_tags = 0; |
| 1843 | 399× | for t in &resp.tags { |
| 1844 | 31× | if view.tags.contains_key(&t.name) { |
| 1845 | 7× | continue; |
| 1846 | 24× | } |
| 1847 | | |
| 1848 | 24× | if self.tag_withdrawal(&t.name)?.is_some() { |
| 1849 | 0× | continue; |
| 1850 | 24× | } |
| 1851 | | |
| 1852 | 24× | let missing: Vec<ChangeHash> = t |
| 1853 | 24× | .heads |
| 1854 | 24× | .iter() |
| 1855 | 24× | .filter(|h| !self.store.has(h)) |
| 1856 | 24× | .copied() |
| 1857 | 24× | .collect(); |
| 1858 | 24× | if !missing.is_empty() { |
| 1859 | 0× | let needed = remote.negotiate(&self.known_heads(&view)?, &missing)?; |
| 1860 | 0× | let bundle = remote.fetch(&needed, max_blob)?; |
| 1861 | 0× | self.import_bundle(&bundle, false)?; |
| 1862 | 24× | } |
| 1863 | 24× | self.store.put_state(&StateObject::new(t.heads.clone()))?; |
| 1864 | 24× | let mut check = TagCheck::Fetched { |
| 1865 | 24× | remote: remote_name, |
| 1866 | 24× | log: &log, |
| 1867 | 24× | notes: &mut log_notes, |
| 1868 | 24× | }; |
| 1869 | 24× | match self.adopt_tag(&mut view, t, policy.as_ref(), &mut check) { |
| 1870 | 22× | Ok(()) => new_tags += 1, |
| 1871 | | |
| 1872 | | |
| 1873 | 2× | Err(e) if e.untimed => { |
| 1874 | 2× | skipped.push((format!("tags/{}", t.name), e.reason)); |
| 1875 | 2× | } |
| 1876 | 0× | Err(e) => return fail(format!("{remote_name} {}: {}", t.name, e.reason)), |
| 1877 | | } |
| 1878 | | } |
| 1879 | | |
| 1880 | | |
| 1881 | | |
| 1882 | 399× | let mut withdrawn_tags = Vec::new(); |
| 1883 | 399× | for t in &resp.withdrawn { |
| 1884 | 1× | if !view.tags.contains_key(&t.name) { |
| 1885 | 0× | continue; |
| 1886 | 1× | } |
| 1887 | 1× | let mut check = TagCheck::Fetched { |
| 1888 | 1× | remote: remote_name, |
| 1889 | 1× | log: &log, |
| 1890 | 1× | notes: &mut log_notes, |
| 1891 | 1× | }; |
| 1892 | 1× | match self.adopt_withdrawal(&mut view, t, policy.as_ref(), &mut check) { |
| 1893 | 1× | Ok(true) => withdrawn_tags.push(t.name.clone()), |
| 1894 | 0× | Ok(false) => {} |
| 1895 | 0× | Err(e) => skipped.push(( |
| 1896 | 0× | format!("tags/{}", t.name), |
| 1897 | 0× | format!("withdrawn on {remote_name}, kept here: {}", e.reason), |
| 1898 | 0× | )), |
| 1899 | | } |
| 1900 | | } |
| 1901 | 399× | if view != op.view { |
| 1902 | 383× | self.record_op(&[op_ref], &format!("oxy fetch {remote_name}"), view)?; |
| 1903 | 16× | } |
| 1904 | 399× | let _ = self.run_hook( |
| 1905 | 399× | "post-fetch", |
| 1906 | 399× | &format!( |
| 1907 | | "{{\"event\":\"post-fetch\",\"remote\":{},\"bookmarks\":{}}}", |
| 1908 | 399× | json_str(remote_name), |
| 1909 | 413× | json_array(report.iter().map(|(n, heads, _)| format!( |
| 1910 | | "{{\"name\":{},\"heads\":{}}}", |
| 1911 | 413× | json_str(n), |
| 1912 | 580× | json_array(heads.iter().map(|h| json_str(&h.to_hex()))) |
| 1913 | | ))) |
| 1914 | | ), |
| 1915 | | ); |
| 1916 | 399× | Ok(FetchReport { |
| 1917 | 399× | refs: report, |
| 1918 | 399× | unverified, |
| 1919 | 399× | new_tags, |
| 1920 | 399× | withdrawn_tags, |
| 1921 | 399× | log_notes, |
| 1922 | 399× | skipped, |
| 1923 | 399× | }) |
| 1924 | 408× | } |
| 1925 | |
|
| 1926 | | |
| 1927 | | |
| 1928 | 1× | pub fn fetch_shallow(&self, remote_name: &str, depth: u32) -> Result<usize> { |
| 1929 | 1× | let remote = self.open_named_remote(remote_name)?; |
| 1930 | 1× | self.check_remote_id(remote.as_ref(), remote_name, false)?; |
| 1931 | 1× | let resp = remote.refs()?; |
| 1932 | 1× | let bookmark = self.config.bookmark.clone(); |
| 1933 | 1× | if let Some(sp) = &resp.policy { |
| 1934 | 0× | self.adopt_policy_chain(&resp.history, sp, "oxy clone --depth") |
| 1935 | 0× | .map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?; |
| 1936 | 0× | self.record_policy_serial(remote_name, sp.policy.serial)?; |
| 1937 | 1× | } |
| 1938 | 1× | let Some(r) = resp.refs.into_iter().find(|r| r.name == bookmark) else { |
| 1939 | 0× | return fail(format!("{remote_name} has no bookmark {bookmark:?}")); |
| 1940 | | }; |
| 1941 | 1× | if r.heads.is_empty() { |
| 1942 | 0× | return Ok(0); |
| 1943 | 1× | } |
| 1944 | | |
| 1945 | 1× | let cp_bundle = remote.checkpoint(&r.heads)?; |
| 1946 | 1× | let mut cp_ref = None; |
| 1947 | 3× | for (ty, body) in &cp_bundle.objects { |
| 1948 | 3× | let put = self.store.put(*ty, body)?; |
| 1949 | 3× | if *ty == ObjectType::Checkpoint { |
| 1950 | 1× | cp_ref = Some(put); |
| 1951 | 2× | } |
| 1952 | | } |
| 1953 | 1× | let Some(cp_ref) = cp_ref else { |
| 1954 | 0× | return fail(format!("{remote_name} sent no checkpoint")); |
| 1955 | | }; |
| 1956 | 1× | self.install_checkpoint(&bookmark, &cp_ref)?; |
| 1957 | | |
| 1958 | 1× | let wanted = remote.shallow(&r.heads, depth)?; |
| 1959 | 1× | let bundle = remote.fetch(&wanted, self.config.max_blob)?; |
| 1960 | 1× | let imported = self.import_bundle_shallow(&bundle, false)?; |
| 1961 | 1× | if self.config.max_blob.is_some() { |
| 1962 | 0× | self.register_missing_blobs(&imported, remote_name)?; |
| 1963 | 1× | } |
| 1964 | 1× | self.refresh_boundary(&imported)?; |
| 1965 | 1× | if let Some(sa) = &r.attestation { |
| 1966 | 0× | let ar = self.store.put_attest(&sa.attest)?; |
| 1967 | 0× | for sig in &sa.sigs { |
| 1968 | 0× | self.store.put_signature(&ar, sig)?; |
| 1969 | | } |
| 1970 | 0× | if let AttestBody::Ref { serial, .. } = sa.attest.body { |
| 1971 | 0× | self.record_serial(remote_name, &r.name, serial)?; |
| 1972 | 0× | } |
| 1973 | 1× | } |
| 1974 | 1× | let state = self.store.put_state(&StateObject::new(r.heads.clone()))?; |
| 1975 | 1× | let (op_ref, op) = self.head_op()?; |
| 1976 | 1× | let mut view = op.view.clone(); |
| 1977 | 1× | view.remotes |
| 1978 | 1× | .entry(remote_name.to_string()) |
| 1979 | 1× | .or_default() |
| 1980 | 1× | .insert(r.name.clone(), state); |
| 1981 | 1× | self.record_op( |
| 1982 | 1× | &[op_ref], |
| 1983 | 1× | &format!("oxy fetch {remote_name} --depth {depth}"), |
| 1984 | 1× | view, |
| 1985 | 0× | )?; |
| 1986 | 1× | Ok(imported.len()) |
| 1987 | 1× | } |
| 1988 | |
|
| 1989 | | |
| 1990 | | |
| 1991 | 113× | pub fn pull(&self, remote_name: &str) -> Result<PullReport> { |
| 1992 | 113× | let _wc = self.lock_working_copy()?; |
| 1993 | 113× | let skipped = self.fetch(remote_name)?.skipped; |
| 1994 | 109× | let (op_ref, op) = self.head_op()?; |
| 1995 | 109× | let current = self.config.bookmark.clone(); |
| 1996 | 109× | let mut local_current = self.heads_of(&op.view, ¤t)?; |
| 1997 | 109× | local_current.sort(); |
| 1998 | 109× | let mut view = op.view.clone(); |
| 1999 | 109× | let mut current_heads = local_current.clone(); |
| 2000 | 109× | let mut changed = false; |
| 2001 | 109× | let mut others = Vec::new(); |
| 2002 | 109× | let tracked = op |
| 2003 | 109× | .view |
| 2004 | 109× | .remotes |
| 2005 | 109× | .get(remote_name) |
| 2006 | 109× | .cloned() |
| 2007 | 109× | .unwrap_or_default(); |
| 2008 | 114× | for (name, remote_state) in tracked { |
| 2009 | | |
| 2010 | | |
| 2011 | 114× | let local_state = op.view.bookmarks.get(&name).copied(); |
| 2012 | 114× | if local_state.is_none() && name != current { |
| 2013 | 1× | continue; |
| 2014 | 113× | } |
| 2015 | 113× | if local_state == Some(remote_state) { |
| 2016 | 10× | continue; |
| 2017 | 103× | } |
| 2018 | 103× | let local = self.heads_of(&op.view, &name)?; |
| 2019 | 103× | let remote_heads = self.store.get_state(&remote_state)?.heads; |
| 2020 | 158× | if remote_heads.iter().all(|h| local.contains(h)) { |
| 2021 | 0× | continue; |
| 2022 | 103× | } |
| 2023 | 103× | let mut all = local.clone(); |
| 2024 | 103× | all.extend(remote_heads); |
| 2025 | 103× | let st = self.load_state_history(&all)?; |
| 2026 | 103× | let mut heads = st.maximal(&all); |
| 2027 | 103× | heads.sort(); |
| 2028 | 103× | let mut sorted_local = local; |
| 2029 | 103× | sorted_local.sort(); |
| 2030 | 103× | if heads == sorted_local { |
| 2031 | 1× | continue; |
| 2032 | 102× | } |
| 2033 | 102× | view.bookmarks.insert( |
| 2034 | 102× | name.clone(), |
| 2035 | 102× | self.store.put_state(&StateObject::new(heads.clone()))?, |
| 2036 | | ); |
| 2037 | 102× | if name == current { |
| 2038 | 100× | changed = true; |
| 2039 | 100× | current_heads = heads; |
| 2040 | 100× | } else { |
| 2041 | 2× | others.push(name); |
| 2042 | 2× | } |
| 2043 | | } |
| 2044 | 109× | if changed || !others.is_empty() { |
| 2045 | 101× | self.record_op(&[op_ref], &format!("oxy pull {remote_name}"), view)?; |
| 2046 | 8× | } |
| 2047 | | |
| 2048 | | |
| 2049 | | |
| 2050 | 109× | let checked_out = self.follow_bookmark(¤t_heads, ¤t, "oxy pull")?; |
| 2051 | | Ok(PullReport { |
| 2052 | 109× | heads: if changed { |
| 2053 | 100× | current_heads |
| 2054 | | } else { |
| 2055 | 9× | local_current |
| 2056 | | }, |
| 2057 | 109× | changed, |
| 2058 | 109× | checked_out, |
| 2059 | 109× | others, |
| 2060 | 109× | skipped, |
| 2061 | | }) |
| 2062 | 113× | } |
| 2063 | |
|
| 2064 | | |
| 2065 | | |
| 2066 | | |
| 2067 | | |
| 2068 | | |
| 2069 | 7× | pub fn working_copy_behind(&self) -> Result<Option<usize>> { |
| 2070 | 7× | let (_, wc) = self.working_copy()?; |
| 2071 | 7× | let mut base = wc.deps.clone(); |
| 2072 | 7× | base.sort(); |
| 2073 | 7× | let mut heads = self.bookmark_heads()?; |
| 2074 | 7× | heads.sort(); |
| 2075 | 7× | if base == heads || heads.is_empty() { |
| 2076 | 6× | return Ok(None); |
| 2077 | 1× | } |
| 2078 | 1× | let state = self.load_state_history(&heads)?; |
| 2079 | 1× | if !base.iter().all(|h| state.contains(h)) { |
| 2080 | 0× | return Ok(None); |
| 2081 | 1× | } |
| 2082 | 1× | let had = if base.is_empty() { |
| 2083 | 0× | 0 |
| 2084 | | } else { |
| 2085 | 1× | self.load_state_history(&base)?.len() |
| 2086 | | }; |
| 2087 | 1× | Ok(Some(state.len().saturating_sub(had)).filter(|n| *n > 0)) |
| 2088 | 7× | } |
| 2089 | |
|
| 2090 | | |
| 2091 | | |
| 2092 | | |
| 2093 | | |
| 2094 | | |
| 2095 | | |
| 2096 | | |
| 2097 | | |
| 2098 | | |
| 2099 | 109× | pub fn follow_bookmark( |
| 2100 | 109× | &self, |
| 2101 | 109× | heads: &[ChangeHash], |
| 2102 | 109× | bookmark: &str, |
| 2103 | 109× | command: &str, |
| 2104 | 109× | ) -> Result<bool> { |
| 2105 | 109× | let snap = self.snapshot(command)?; |
| 2106 | 109× | let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty(); |
| 2107 | 109× | let mut base = snap.base.maximal(&snap.wc.deps); |
| 2108 | 109× | base.sort(); |
| 2109 | 109× | let mut target = heads.to_vec(); |
| 2110 | 109× | target.sort(); |
| 2111 | 109× | if base == target { |
| 2112 | 8× | return Ok(false); |
| 2113 | 101× | } |
| 2114 | 101× | let state = self.load_state_history(heads)?; |
| 2115 | 197× | if !base.iter().all(|h| state.contains(h)) { |
| 2116 | | |
| 2117 | 0× | return Ok(false); |
| 2118 | 101× | } |
| 2119 | 101× | if clean { |
| 2120 | 101× | self.checkout(heads, false, &format!("{command} (checkout)"))?; |
| 2121 | 101× | Ok(true) |
| 2122 | | } else { |
| 2123 | 0× | Ok(self |
| 2124 | 0× | .checkout_carry(heads, bookmark, &format!("{command} (checkout --carry)")) |
| 2125 | 0× | .is_ok()) |
| 2126 | | } |
| 2127 | 109× | } |
| 2128 | |
|
| 2129 | | |
| 2130 | | |
| 2131 | | |
| 2132 | | |
| 2133 | 3× | pub fn push_tag_withdrawal(&self, remote_name: &str, tag: &str) -> Result<Vec<RefResult>> { |
| 2134 | 3× | let remote = self.open_named_remote(remote_name)?; |
| 2135 | 3× | self.check_remote_id(remote.as_ref(), remote_name, true)?; |
| 2136 | 3× | let resp = remote.refs()?; |
| 2137 | 3× | let name = format!("tags/{tag}"); |
| 2138 | 3× | if resp.withdrawn.iter().any(|t| t.name == tag) { |
| 2139 | 1× | return Ok(vec![RefResult { |
| 2140 | 1× | name, |
| 2141 | 1× | ok: true, |
| 2142 | 1× | reason: "already withdrawn".into(), |
| 2143 | 1× | }]); |
| 2144 | 2× | } |
| 2145 | 2× | let Some(there) = resp.tags.iter().find(|t| t.name == tag) else { |
| 2146 | 0× | return fail(format!("{remote_name} has no tag {tag}")); |
| 2147 | | }; |
| 2148 | 2× | if resp.policy.is_some() && self.secret_key()?.is_none() { |
| 2149 | 0× | return fail(format!("{remote_name} enforces a trust policy; withdrawing a tag needs an identity (`oxy identity create`)")); |
| 2150 | 2× | } |
| 2151 | 2× | if self.tag_withdrawal(tag)?.is_none() { |
| 2152 | 2× | match self.view()?.tags.get(tag) { |
| 2153 | 2× | Some(st) if *st == there.state => { |
| 2154 | 2× | self.tag_delete(tag, "")?; |
| 2155 | | } |
| 2156 | | Some(_) => { |
| 2157 | 0× | return fail(format!( |
| 2158 | | "tag {tag} names another state here than on {remote_name}" |
| 2159 | | )) |
| 2160 | | } |
| 2161 | | None => { |
| 2162 | 0× | return fail(format!( |
| 2163 | | "there is no tag {tag} here to withdraw: `oxy fetch {remote_name}` first" |
| 2164 | | )) |
| 2165 | | } |
| 2166 | | } |
| 2167 | 0× | } |
| 2168 | 2× | let Some(w) = self |
| 2169 | 2× | .withdrawn_refs()? |
| 2170 | 2× | .into_iter() |
| 2171 | 2× | .find(|w| w.name == tag && w.state == there.state) |
| 2172 | | else { |
| 2173 | 0× | return fail(format!( |
| 2174 | | "tag {tag} was withdrawn here at another state than {remote_name}'s" |
| 2175 | | )); |
| 2176 | | }; |
| 2177 | 2× | let mut bundle = Bundle::default(); |
| 2178 | 2× | let signers: Vec<KeyId> = w |
| 2179 | 2× | .attestation |
| 2180 | 2× | .iter() |
| 2181 | 2× | .flat_map(|a| a.sigs.iter().map(|s| s.key)) |
| 2182 | 2× | .collect(); |
| 2183 | 2× | for k in &signers { |
| 2184 | 2× | if let Ok(id) = self.identity(k) { |
| 2185 | 2× | bundle.objects.push((ObjectType::Identity, id.encode())); |
| 2186 | 2× | } |
| 2187 | | } |
| 2188 | 2× | self.attach_certificates_for(&mut bundle, &signers)?; |
| 2189 | 2× | remote.push(bundle, vec![], None, vec![w]) |
| 2190 | 3× | } |
| 2191 | |
|
| 2192 | | |
| 2193 | | |
| 2194 | | |
| 2195 | | |
| 2196 | | |
| 2197 | 8× | pub fn push_delete(&self, remote_name: &str, bookmark: &str) -> Result<Vec<RefResult>> { |
| 2198 | 8× | if let Some(tag) = bookmark.strip_prefix("tags/") { |
| 2199 | 3× | return self.push_tag_withdrawal(remote_name, tag); |
| 2200 | 5× | } |
| 2201 | 5× | let remote = self.open_named_remote(remote_name)?; |
| 2202 | 5× | self.check_remote_id(remote.as_ref(), remote_name, true)?; |
| 2203 | 5× | let resp = remote.refs()?; |
| 2204 | 7× | let Some(r) = resp.refs.iter().find(|r| r.name == bookmark) else { |
| 2205 | 1× | return fail(format!("{remote_name} has no bookmark {bookmark}")); |
| 2206 | | }; |
| 2207 | 4× | if resp.policy.is_some() && self.secret_key()?.is_none() { |
| 2208 | 0× | return fail(format!("{remote_name} enforces a trust policy; deleting needs an identity (`oxy identity create`)")); |
| 2209 | 4× | } |
| 2210 | 4× | let attestation = match self.secret_key()? { |
| 2211 | 2× | Some(_) => Some(self.sign_attest(&Attest { |
| 2212 | 2× | repo: self.id()?, |
| 2213 | 2× | hlc: Hlc::now(), |
| 2214 | 2× | body: AttestBody::Ref { |
| 2215 | 2× | name: bookmark.to_string(), |
| 2216 | 2× | old_state: r.state, |
| 2217 | 2× | new_state: ObjectRef::ZERO, |
| 2218 | 2× | serial: r.last_serial.unwrap_or(0) + 1, |
| 2219 | 2× | }, |
| 2220 | 0× | })?), |
| 2221 | 2× | None => None, |
| 2222 | | }; |
| 2223 | 4× | let results = remote.push( |
| 2224 | 4× | Bundle::default(), |
| 2225 | 4× | vec![RefUpdate { |
| 2226 | 4× | name: bookmark.to_string(), |
| 2227 | 4× | old_state: r.state, |
| 2228 | 4× | new_state: ObjectRef::ZERO, |
| 2229 | 4× | force: true, |
| 2230 | 4× | attestation, |
| 2231 | 4× | }], |
| 2232 | 4× | None, |
| 2233 | 4× | vec![], |
| 2234 | 0× | )?; |
| 2235 | 4× | if results.iter().any(|x| x.ok && x.name == bookmark) { |
| 2236 | 2× | let (op_ref, op) = self.head_op()?; |
| 2237 | 2× | let mut view = op.view.clone(); |
| 2238 | 2× | if let Some(m) = view.remotes.get_mut(remote_name) { |
| 2239 | 2× | m.remove(bookmark); |
| 2240 | 2× | } |
| 2241 | 2× | self.record_op( |
| 2242 | 2× | &[op_ref], |
| 2243 | 2× | &format!("oxy push {remote_name} --delete {bookmark}"), |
| 2244 | 2× | view, |
| 2245 | 0× | )?; |
| 2246 | 2× | } |
| 2247 | 4× | Ok(results) |
| 2248 | 8× | } |
| 2249 | |
|
| 2250 | | |
| 2251 | | |
| 2252 | | |
| 2253 | | |
| 2254 | 3× | pub fn remove_remote(&mut self, name: &str) -> Result<()> { |
| 2255 | 3× | if !self.config.remotes.contains_key(name) { |
| 2256 | 1× | return fail(format!("no remote {name}: `oxy remote list` shows them")); |
| 2257 | 2× | } |
| 2258 | 2× | let prefix = format!("remote.{name}."); |
| 2259 | 2× | let keys: Vec<String> = self |
| 2260 | 2× | .config |
| 2261 | 2× | .repo_layer() |
| 2262 | 2× | .keys() |
| 2263 | 4× | .filter(|k| k.starts_with(&prefix)) |
| 2264 | 2× | .cloned() |
| 2265 | 2× | .collect(); |
| 2266 | 3× | for k in keys { |
| 2267 | 3× | self.config.set(&k, "")?; |
| 2268 | | } |
| 2269 | 2× | self.config.remotes.remove(name); |
| 2270 | 2× | self.save_config()?; |
| 2271 | 2× | let (op_ref, op) = self.head_op()?; |
| 2272 | 2× | if op.view.remotes.contains_key(name) { |
| 2273 | 1× | let mut view = op.view.clone(); |
| 2274 | 1× | view.remotes.remove(name); |
| 2275 | 1× | self.record_op(&[op_ref], &format!("oxy remote remove {name}"), view)?; |
| 2276 | 1× | } |
| 2277 | 2× | let mirror = self.oxy.join("remotes").join(name); |
| 2278 | 2× | if mirror.is_dir() { |
| 2279 | 0× | fs::remove_dir_all(&mirror)?; |
| 2280 | 2× | } |
| 2281 | 2× | Ok(()) |
| 2282 | 3× | } |
| 2283 | |
|
| 2284 | | |
| 2285 | | |
| 2286 | | |
| 2287 | | |
| 2288 | | |
| 2289 | | |
| 2290 | | |
| 2291 | 329× | fn send_objects_ahead( |
| 2292 | 329× | &self, |
| 2293 | 329× | remote: &dyn crate::protocol::Remote, |
| 2294 | 329× | remote_name: &str, |
| 2295 | 329× | mut bundle: Bundle, |
| 2296 | 329× | progress: crate::store::Progress, |
| 2297 | 329× | ) -> Result<Bundle> { |
| 2298 | 329× | let limit = self.config.push_batch_bytes; |
| 2299 | 329× | let changes: u64 = bundle |
| 2300 | 329× | .changes |
| 2301 | 329× | .iter() |
| 2302 | 436× | .map(|(c, _)| c.encode().len() as u64) |
| 2303 | 329× | .sum(); |
| 2304 | 329× | let objects: u64 = bundle.objects.iter().map(|(_, b)| b.len() as u64).sum(); |
| 2305 | 329× | if changes + objects <= limit { |
| 2306 | 322× | return Ok(bundle); |
| 2307 | 7× | } |
| 2308 | 7× | let (mut kept, mut parts, mut part, mut bytes) = (Vec::new(), Vec::new(), Vec::new(), 0u64); |
| 2309 | 9× | for (ty, body) in std::mem::take(&mut bundle.objects) { |
| 2310 | 9× | if !matches!(ty, ObjectType::Blob | ObjectType::Chunk) { |
| 2311 | 0× | kept.push((ty, body)); |
| 2312 | 0× | continue; |
| 2313 | 9× | } |
| 2314 | 9× | if !part.is_empty() && bytes + body.len() as u64 > limit { |
| 2315 | 3× | parts.push(std::mem::take(&mut part)); |
| 2316 | 3× | bytes = 0; |
| 2317 | 6× | } |
| 2318 | 9× | bytes += body.len() as u64; |
| 2319 | 9× | part.push((ty, body)); |
| 2320 | | } |
| 2321 | 7× | if !part.is_empty() { |
| 2322 | 1× | parts.push(part); |
| 2323 | 6× | } |
| 2324 | 7× | let n = parts.len(); |
| 2325 | 7× | for (i, objects) in parts.into_iter().enumerate() { |
| 2326 | 4× | progress(&format!( |
| 2327 | 4× | "objects {} of {n}: {} blobs and chunks", |
| 2328 | 4× | i + 1, |
| 2329 | 4× | objects.len() |
| 2330 | 4× | )); |
| 2331 | 4× | let results = remote.push( |
| 2332 | 4× | Bundle { |
| 2333 | 4× | objects, |
| 2334 | 4× | ..Default::default() |
| 2335 | 4× | }, |
| 2336 | 4× | vec![], |
| 2337 | 4× | None, |
| 2338 | 4× | vec![], |
| 2339 | 0× | )?; |
| 2340 | 4× | match results.iter().find(|r| r.name == OBJECTS_RESULT) { |
| 2341 | 4× | Some(r) if r.ok => {} |
| 2342 | 0× | Some(r) => { |
| 2343 | 0× | return fail(format!( |
| 2344 | | "{remote_name} refused objects part {} of {n}: {}", |
| 2345 | 0× | i + 1, |
| 2346 | | r.reason |
| 2347 | | )) |
| 2348 | | } |
| 2349 | | None => { |
| 2350 | 0× | return fail(format!( |
| 2351 | | "{remote_name} does not take a push in parts, and this one is too large for one: update the server's oxy" |
| 2352 | | )) |
| 2353 | | } |
| 2354 | | } |
| 2355 | | } |
| 2356 | 7× | bundle.objects = kept; |
| 2357 | 7× | Ok(bundle) |
| 2358 | 329× | } |
| 2359 | |
|
| 2360 | | |
| 2361 | | |
| 2362 | | |
| 2363 | 324× | fn push_batches(&self, changes: &[ChangeHash]) -> Result<Vec<Vec<ChangeHash>>> { |
| 2364 | 324× | let blob_len = |r: &ObjectRef| -> u64 { |
| 2365 | 33× | self.store |
| 2366 | 33× | .get_typed(r, ObjectType::Blob) |
| 2367 | 33× | .ok() |
| 2368 | 33× | .and_then(|b| Blob::decode(&b).ok()) |
| 2369 | 33× | .map_or(0, |b| b.length) |
| 2370 | 33× | }; |
| 2371 | 324× | let (mut out, mut batch, mut bytes) = (Vec::new(), Vec::new(), 0u64); |
| 2372 | 436× | for h in changes { |
| 2373 | 436× | let raw = self.store.get_typed(h, ObjectType::Change)?; |
| 2374 | 436× | let (c, texts) = Change::decode_skeleton(&raw, *h)?; |
| 2375 | 436× | let mut weight = raw.len() as u64; |
| 2376 | 538× | for t in &texts { |
| 2377 | 538× | if let crate::objects::TextRef::Blob(r, _, _) = t { |
| 2378 | 26× | weight += blob_len(r); |
| 2379 | 512× | } |
| 2380 | | } |
| 2381 | 701× | for e in &c.edits { |
| 2382 | 7× | if let Edit::Replace { new: Some(b), .. } = e { |
| 2383 | 7× | weight += blob_len(b); |
| 2384 | 694× | } |
| 2385 | | } |
| 2386 | 436× | if !batch.is_empty() && bytes + weight > self.config.push_batch_bytes { |
| 2387 | 5× | out.push(std::mem::take(&mut batch)); |
| 2388 | 5× | bytes = 0; |
| 2389 | 431× | } |
| 2390 | 436× | batch.push(*h); |
| 2391 | 436× | bytes += weight; |
| 2392 | | } |
| 2393 | 324× | out.push(batch); |
| 2394 | 324× | Ok(out) |
| 2395 | 324× | } |
| 2396 | |
|
| 2397 | 384× | pub fn push(&self, remote_name: &str, force: bool) -> Result<Vec<RefResult>> { |
| 2398 | 384× | let bookmark = self.config.bookmark.clone(); |
| 2399 | 384× | self.push_bookmark(remote_name, &bookmark, force) |
| 2400 | 384× | } |
| 2401 | |
|
| 2402 | | |
| 2403 | 397× | pub fn push_bookmark( |
| 2404 | 397× | &self, |
| 2405 | 397× | remote_name: &str, |
| 2406 | 397× | bookmark: &str, |
| 2407 | 397× | force: bool, |
| 2408 | 397× | ) -> Result<Vec<RefResult>> { |
| 2409 | 397× | self.push_bookmark_with(remote_name, bookmark, force, &mut |_| {}) |
| 2410 | 397× | } |
| 2411 | |
|
| 2412 | | |
| 2413 | | |
| 2414 | 406× | pub fn push_bookmark_with( |
| 2415 | 406× | &self, |
| 2416 | 406× | remote_name: &str, |
| 2417 | 406× | bookmark: &str, |
| 2418 | 406× | force: bool, |
| 2419 | 406× | progress: crate::store::Progress, |
| 2420 | 406× | ) -> Result<Vec<RefResult>> { |
| 2421 | 406× | let (op_ref, op) = self.head_op()?; |
| 2422 | 406× | if !op.view.bookmarks.contains_key(bookmark) { |
| 2423 | 0× | return fail(format!("no bookmark {bookmark}")); |
| 2424 | 406× | } |
| 2425 | 406× | let bookmark = bookmark.to_string(); |
| 2426 | 406× | let local_heads = self.heads_of(&op.view, &bookmark)?; |
| 2427 | | |
| 2428 | 406× | self.run_hook( |
| 2429 | 406× | "pre-push", |
| 2430 | 406× | &format!( |
| 2431 | | "{{\"event\":\"pre-push\",\"remote\":{},\"bookmark\":{},\"heads\":{}}}", |
| 2432 | 406× | json_str(remote_name), |
| 2433 | 406× | json_str(&bookmark), |
| 2434 | 823× | json_array(local_heads.iter().map(|h| json_str(&h.to_hex()))) |
| 2435 | | ), |
| 2436 | 1× | )?; |
| 2437 | 405× | let remote = self.open_named_remote(remote_name)?; |
| 2438 | 405× | self.check_remote_id(remote.as_ref(), remote_name, true)?; |
| 2439 | 402× | let resp = remote.refs()?; |
| 2440 | 402× | let local_policy = self.current_policy()?.map(|(_, sp)| sp); |
| 2441 | 402× | let resp_policy_serial = resp.policy.as_ref().map(|p| p.policy.serial); |
| 2442 | 402× | let policy_to_send = match (&local_policy, &resp.policy) { |
| 2443 | 44× | (Some(l), Some(r)) if l.policy.serial > r.policy.serial => Some(l.clone()), |
| 2444 | 16× | (Some(l), None) => Some(l.clone()), |
| 2445 | 26× | (Some(l), Some(r)) if l.policy.serial < r.policy.serial => { |
| 2446 | 1× | return fail(format!( |
| 2447 | | "{remote_name} has a newer policy (serial {}); fetch first", |
| 2448 | | r.policy.serial |
| 2449 | | )); |
| 2450 | | } |
| 2451 | 367× | _ => None, |
| 2452 | | }; |
| 2453 | 401× | let remote_has_policy = resp.policy.is_some() || policy_to_send.is_some(); |
| 2454 | 401× | if remote_has_policy && self.secret_key()?.is_none() { |
| 2455 | 1× | return fail(format!("{remote_name} enforces a trust policy; pushing needs an identity (`oxy identity create`)")); |
| 2456 | 400× | } |
| 2457 | 400× | let remote_tags: BTreeSet<String> = resp |
| 2458 | 400× | .tags |
| 2459 | 400× | .iter() |
| 2460 | 400× | .chain(&resp.withdrawn) |
| 2461 | 400× | .map(|t| t.name.clone()) |
| 2462 | 400× | .collect(); |
| 2463 | 400× | let tags_to_send: Vec<crate::protocol::TagRef> = self |
| 2464 | 400× | .tag_refs()? |
| 2465 | 400× | .into_iter() |
| 2466 | 400× | .filter(|t| !remote_tags.contains(&t.name)) |
| 2467 | 400× | .collect(); |
| 2468 | | |
| 2469 | | |
| 2470 | 400× | let withdrawals_to_send: Vec<crate::protocol::TagRef> = self |
| 2471 | 400× | .withdrawn_refs()? |
| 2472 | 400× | .into_iter() |
| 2473 | 400× | .filter(|w| { |
| 2474 | 2× | resp.tags |
| 2475 | 2× | .iter() |
| 2476 | 2× | .any(|t| t.name == w.name && t.state == w.state) |
| 2477 | 2× | }) |
| 2478 | 400× | .collect(); |
| 2479 | 400× | let refs = resp.refs; |
| 2480 | 400× | let empty = self.store.put_state(&StateObject::new(vec![]))?; |
| 2481 | 400× | let (remote_state, remote_heads, last_serial) = |
| 2482 | 401× | match refs.iter().find(|r| r.name == bookmark) { |
| 2483 | 378× | Some(r) => (r.state, r.heads.clone(), r.last_serial), |
| 2484 | 22× | None => (empty, vec![], None), |
| 2485 | | }; |
| 2486 | 400× | let new_state = self |
| 2487 | 400× | .store |
| 2488 | 400× | .put_state(&StateObject::new(local_heads.clone()))?; |
| 2489 | | |
| 2490 | | |
| 2491 | | |
| 2492 | | |
| 2493 | 400× | if new_state == remote_state |
| 2494 | 35× | && policy_to_send.is_none() |
| 2495 | 18× | && tags_to_send.is_empty() |
| 2496 | 9× | && withdrawals_to_send.is_empty() |
| 2497 | | { |
| 2498 | 8× | return Ok(vec![RefResult { |
| 2499 | 8× | name: bookmark, |
| 2500 | 8× | ok: true, |
| 2501 | 8× | reason: "already up to date".into(), |
| 2502 | 8× | }]); |
| 2503 | 392× | } |
| 2504 | 454× | for h in &remote_heads { |
| 2505 | 454× | if !self.store.has(h) { |
| 2506 | 67× | return fail(format!( |
| 2507 | | "the remote has change {} which you do not; pull first (`oxy pull {remote_name}`), then push again", |
| 2508 | 67× | h.short(12) |
| 2509 | | )); |
| 2510 | 387× | } |
| 2511 | | } |
| 2512 | 325× | if !force { |
| 2513 | 325× | let closure = self.closure_in_store(&local_heads)?; |
| 2514 | 353× | if let Some(h) = remote_heads.iter().find(|h| !closure.contains(h)) { |
| 2515 | 0× | return fail(format!("not a fast-forward: remote head {} is not in your state; pull first or use --force", h.short(12))); |
| 2516 | 325× | } |
| 2517 | 0× | } |
| 2518 | 325× | let to_send = self.negotiate(&remote_heads, &local_heads)?; |
| 2519 | 325× | let key = self.secret_key()?.map(|(k, _)| k); |
| 2520 | 325× | if let Some(key) = key { |
| 2521 | | |
| 2522 | | |
| 2523 | | |
| 2524 | 112× | let mut unsigned = Vec::new(); |
| 2525 | 112× | for h in &to_send { |
| 2526 | 92× | let c = self.store.get_change_meta(h)?; |
| 2527 | 92× | if self.seals_for_author(&c.author, &key) |
| 2528 | 87× | && !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) |
| 2529 | 53× | { |
| 2530 | 53× | unsigned.push(*h); |
| 2531 | 53× | } |
| 2532 | | } |
| 2533 | 112× | for chunk in unsigned.chunks(crate::attest::MAX_SEAL_HASHES) { |
| 2534 | 50× | self.sign_attest(&Attest::seal(self.id()?, chunk.to_vec()))?; |
| 2535 | | } |
| 2536 | 213× | } |
| 2537 | | |
| 2538 | | |
| 2539 | | |
| 2540 | | |
| 2541 | 324× | let batches = self.push_batches(&to_send)?; |
| 2542 | 324× | let (last, earlier) = batches.split_last().expect("at least one batch"); |
| 2543 | 324× | for (i, batch) in earlier.iter().enumerate() { |
| 2544 | 5× | progress(&format!( |
| 2545 | 5× | "part {} of {}: {} changes", |
| 2546 | 5× | i + 1, |
| 2547 | 5× | batches.len(), |
| 2548 | 5× | batch.len() |
| 2549 | 5× | )); |
| 2550 | 5× | let bundle = self.send_objects_ahead( |
| 2551 | 5× | remote.as_ref(), |
| 2552 | 5× | remote_name, |
| 2553 | 5× | self.bundle_for(batch)?, |
| 2554 | 5× | &mut *progress, |
| 2555 | 0× | )?; |
| 2556 | 5× | let results = remote.push(bundle, vec![], None, vec![])?; |
| 2557 | 5× | match results.iter().find(|r| r.name == OBJECTS_RESULT) { |
| 2558 | 5× | Some(r) if r.ok => {} |
| 2559 | 0× | Some(r) => { |
| 2560 | 0× | return fail(format!( |
| 2561 | | "{remote_name} refused part {} of the push: {}", |
| 2562 | 0× | i + 1, |
| 2563 | | r.reason |
| 2564 | | )) |
| 2565 | | } |
| 2566 | | None => { |
| 2567 | 0× | return fail(format!( |
| 2568 | | "{remote_name} does not take a push in parts, and this one is too long for one: update the server's oxy" |
| 2569 | | )) |
| 2570 | | } |
| 2571 | | } |
| 2572 | | } |
| 2573 | 324× | if batches.len() > 1 { |
| 2574 | 1× | progress(&format!( |
| 2575 | 1× | "part {} of {}: {} changes", |
| 2576 | 1× | batches.len(), |
| 2577 | 1× | batches.len(), |
| 2578 | 1× | last.len() |
| 2579 | 1× | )); |
| 2580 | 323× | } |
| 2581 | 324× | let mut bundle = self.send_objects_ahead( |
| 2582 | 324× | remote.as_ref(), |
| 2583 | 324× | remote_name, |
| 2584 | 324× | self.bundle_for(last)?, |
| 2585 | 324× | &mut *progress, |
| 2586 | 0× | )?; |
| 2587 | 324× | bundle.objects.push(( |
| 2588 | 324× | ObjectType::State, |
| 2589 | 324× | StateObject::new(local_heads.clone()).encode(), |
| 2590 | 324× | )); |
| 2591 | 324× | let attestation = match key { |
| 2592 | 111× | Some(_) => Some(self.sign_attest(&Attest { |
| 2593 | 111× | repo: self.id()?, |
| 2594 | 111× | hlc: Hlc::now(), |
| 2595 | 111× | body: AttestBody::Ref { |
| 2596 | 111× | name: bookmark.clone(), |
| 2597 | 111× | old_state: remote_state, |
| 2598 | 111× | new_state, |
| 2599 | 111× | serial: next_serial(last_serial), |
| 2600 | 111× | }, |
| 2601 | 1× | })?), |
| 2602 | 213× | None => None, |
| 2603 | | }; |
| 2604 | | |
| 2605 | | |
| 2606 | | |
| 2607 | 323× | if attestation.is_some() { |
| 2608 | 220× | for k in [self.signing_key(), self.config.author_key] |
| 2609 | 110× | .into_iter() |
| 2610 | 110× | .flatten() |
| 2611 | | { |
| 2612 | 220× | if let Ok(id) = self.identity(&k) { |
| 2613 | 220× | let body = id.encode(); |
| 2614 | 220× | if !bundle |
| 2615 | 220× | .objects |
| 2616 | 220× | .iter() |
| 2617 | 267× | .any(|(t, b)| *t == ObjectType::Identity && *b == body) |
| 2618 | 32× | { |
| 2619 | 32× | bundle.objects.push((ObjectType::Identity, body)); |
| 2620 | 188× | } |
| 2621 | 0× | } |
| 2622 | | } |
| 2623 | | |
| 2624 | 110× | let signers: Vec<KeyId> = self.signing_key().into_iter().collect(); |
| 2625 | 110× | self.attach_certificates_for(&mut bundle, &signers)?; |
| 2626 | 213× | } |
| 2627 | 323× | let serial = attestation.as_ref().and_then(|a| match a.attest.body { |
| 2628 | 110× | AttestBody::Ref { serial, .. } => Some(serial), |
| 2629 | 0× | _ => None, |
| 2630 | 110× | }); |
| 2631 | 323× | let sent_serial = policy_to_send.as_ref().map(|p| p.policy.serial); |
| 2632 | | |
| 2633 | 323× | for t in &tags_to_send { |
| 2634 | 16× | let extra = self.negotiate(&remote_heads, &t.heads)?; |
| 2635 | 16× | let extra: Vec<ChangeHash> = |
| 2636 | 16× | extra.into_iter().filter(|h| !to_send.contains(h)).collect(); |
| 2637 | 16× | if !extra.is_empty() { |
| 2638 | 0× | let more = self.bundle_for(&extra)?; |
| 2639 | 0× | bundle.changes.extend(more.changes); |
| 2640 | 0× | bundle.hashes.extend(more.hashes); |
| 2641 | 0× | bundle.attests.extend(more.attests); |
| 2642 | 0× | bundle.objects.extend(more.objects); |
| 2643 | 16× | } |
| 2644 | 16× | bundle.objects.push(( |
| 2645 | 16× | ObjectType::State, |
| 2646 | 16× | StateObject::new(t.heads.clone()).encode(), |
| 2647 | 16× | )); |
| 2648 | | } |
| 2649 | | |
| 2650 | | |
| 2651 | | |
| 2652 | 323× | let tag_signers: Vec<KeyId> = tags_to_send |
| 2653 | 323× | .iter() |
| 2654 | 323× | .chain(&withdrawals_to_send) |
| 2655 | 323× | .filter_map(|t| t.attestation.as_ref()) |
| 2656 | 323× | .flat_map(|a| a.sigs.iter().map(|s| s.key)) |
| 2657 | 323× | .collect(); |
| 2658 | 323× | for k in &tag_signers { |
| 2659 | 16× | if let Ok(id) = self.identity(k) { |
| 2660 | 16× | let body = id.encode(); |
| 2661 | 16× | if !bundle |
| 2662 | 16× | .objects |
| 2663 | 16× | .iter() |
| 2664 | 30× | .any(|(t, b)| *t == ObjectType::Identity && *b == body) |
| 2665 | 2× | { |
| 2666 | 2× | bundle.objects.push((ObjectType::Identity, body)); |
| 2667 | 14× | } |
| 2668 | 0× | } |
| 2669 | | } |
| 2670 | 323× | if !tag_signers.is_empty() { |
| 2671 | 15× | self.attach_certificates_for(&mut bundle, &tag_signers)?; |
| 2672 | 308× | } |
| 2673 | 323× | if let Some(l) = &policy_to_send { |
| 2674 | 34× | let remote_serial = resp_policy_serial; |
| 2675 | 34× | bundle.policies = self |
| 2676 | 34× | .policy_history()? |
| 2677 | 34× | .into_iter() |
| 2678 | 66× | .filter(|sp| { |
| 2679 | 66× | remote_serial.is_none_or(|r| sp.policy.serial > r) |
| 2680 | 36× | && sp.policy.serial < l.policy.serial |
| 2681 | 66× | }) |
| 2682 | 34× | .collect(); |
| 2683 | 289× | } |
| 2684 | 323× | let results = remote.push( |
| 2685 | 323× | bundle, |
| 2686 | 323× | vec![RefUpdate { |
| 2687 | 323× | name: bookmark.clone(), |
| 2688 | 323× | old_state: remote_state, |
| 2689 | 323× | new_state, |
| 2690 | 323× | force, |
| 2691 | 323× | attestation, |
| 2692 | 323× | }], |
| 2693 | 323× | policy_to_send, |
| 2694 | 323× | tags_to_send |
| 2695 | 323× | .into_iter() |
| 2696 | 323× | .chain(withdrawals_to_send) |
| 2697 | 323× | .collect(), |
| 2698 | 8× | )?; |
| 2699 | 34× | if let (Some(serial), true) = ( |
| 2700 | 315× | sent_serial, |
| 2701 | 329× | results.iter().any(|r| r.ok && r.name == "policy"), |
| 2702 | | ) { |
| 2703 | 34× | self.record_policy_serial(remote_name, serial)?; |
| 2704 | 281× | } |
| 2705 | 352× | if results.iter().any(|r| r.ok && r.name == bookmark) { |
| 2706 | 284× | let mut view = op.view.clone(); |
| 2707 | 284× | view.remotes |
| 2708 | 284× | .entry(remote_name.to_string()) |
| 2709 | 284× | .or_default() |
| 2710 | 284× | .insert(bookmark.clone(), new_state); |
| 2711 | 284× | self.record_op(&[op_ref], &format!("oxy push {remote_name}"), view)?; |
| 2712 | 281× | if let Some(s) = serial { |
| 2713 | 86× | self.record_serial(remote_name, &bookmark, s)?; |
| 2714 | 195× | } |
| 2715 | 31× | } |
| 2716 | 310× | Ok(results) |
| 2717 | 406× | } |
| 2718 | |
|
| 2719 | | |
| 2720 | | |
| 2721 | 261× | pub fn clone(url: &str, dest: &Path) -> Result<Repo> { |
| 2722 | 261× | Self::clone_with(url, dest, None, &[]) |
| 2723 | 261× | } |
| 2724 | |
|
| 2725 | | |
| 2726 | | |
| 2727 | | |
| 2728 | 264× | pub fn clone_with( |
| 2729 | 264× | url: &str, |
| 2730 | 264× | dest: &Path, |
| 2731 | 264× | max_blob: Option<u64>, |
| 2732 | 264× | sparse: &[String], |
| 2733 | 264× | ) -> Result<Repo> { |
| 2734 | 264× | Self::clone_full(url, dest, max_blob, sparse, None) |
| 2735 | 264× | } |
| 2736 | |
|
| 2737 | | |
| 2738 | | |
| 2739 | 1× | pub fn clone_shallow( |
| 2740 | 1× | url: &str, |
| 2741 | 1× | dest: &Path, |
| 2742 | 1× | depth: u32, |
| 2743 | 1× | max_blob: Option<u64>, |
| 2744 | 1× | sparse: &[String], |
| 2745 | 1× | ) -> Result<Repo> { |
| 2746 | 1× | Self::clone_full(url, dest, max_blob, sparse, Some(depth)) |
| 2747 | 1× | } |
| 2748 | |
|
| 2749 | 265× | fn clone_full( |
| 2750 | 265× | url: &str, |
| 2751 | 265× | dest: &Path, |
| 2752 | 265× | max_blob: Option<u64>, |
| 2753 | 265× | sparse: &[String], |
| 2754 | 265× | depth: Option<u32>, |
| 2755 | 265× | ) -> Result<Repo> { |
| 2756 | 265× | Ok(Self::clone_reporting(url, dest, max_blob, sparse, depth)?.0) |
| 2757 | 265× | } |
| 2758 | |
|
| 2759 | | |
| 2760 | | |
| 2761 | | |
| 2762 | | |
| 2763 | | |
| 2764 | 271× | pub fn clone_reporting( |
| 2765 | 271× | url: &str, |
| 2766 | 271× | dest: &Path, |
| 2767 | 271× | max_blob: Option<u64>, |
| 2768 | 271× | sparse: &[String], |
| 2769 | 271× | depth: Option<u32>, |
| 2770 | 271× | ) -> Result<(Repo, Vec<(String, String)>)> { |
| 2771 | 271× | let options = crate::credentials::Credentials::user().options_for(url); |
| 2772 | 271× | Self::clone_using(url, dest, max_blob, sparse, depth, &options) |
| 2773 | 271× | } |
| 2774 | |
|
| 2775 | | |
| 2776 | | |
| 2777 | | |
| 2778 | 10× | pub(crate) fn clone_as_subrepo(&self, url: &str, dest: &Path) -> Result<Repo> { |
| 2779 | 10× | let options = self.credentials_for(url); |
| 2780 | 10× | Ok(Self::clone_using(url, dest, None, &[], None, &options)?.0) |
| 2781 | 10× | } |
| 2782 | |
|
| 2783 | | |
| 2784 | | |
| 2785 | 23× | fn remote_on_same_hub(&self, url: &str) -> Option<String> { |
| 2786 | 27× | let origin = |u: &str| -> Option<String> { |
| 2787 | 27× | let (scheme, rest) = u.split_once("://")?; |
| 2788 | 8× | if !matches!(scheme, "http" | "https") { |
| 2789 | 0× | return None; |
| 2790 | 8× | } |
| 2791 | 8× | let host = rest.split('/').next()?; |
| 2792 | 8× | Some(format!( |
| 2793 | 8× | "{}://{}", |
| 2794 | 8× | scheme.to_ascii_lowercase(), |
| 2795 | 8× | host.to_ascii_lowercase() |
| 2796 | 8× | )) |
| 2797 | 27× | }; |
| 2798 | 23× | let want = origin(url)?; |
| 2799 | 4× | self.config |
| 2800 | 4× | .remotes |
| 2801 | 4× | .iter() |
| 2802 | 4× | .find(|(_, u)| origin(u).as_deref() == Some(want.as_str())) |
| 2803 | 4× | .map(|(name, _)| name.clone()) |
| 2804 | 23× | } |
| 2805 | |
|
| 2806 | | |
| 2807 | | |
| 2808 | | |
| 2809 | | |
| 2810 | 23× | pub(crate) fn credentials_for(&self, url: &str) -> RemoteOptions { |
| 2811 | 23× | match self.remote_on_same_hub(url) { |
| 2812 | 1× | Some(name) => RemoteOptions { |
| 2813 | 1× | repo_id: None, |
| 2814 | 1× | cache_dir: None, |
| 2815 | 1× | ..self.remote_options(&name) |
| 2816 | 1× | }, |
| 2817 | | None => { |
| 2818 | 22× | let mut options = RemoteOptions::default(); |
| 2819 | 22× | self.fill_credentials(url, &mut options); |
| 2820 | 22× | options |
| 2821 | | } |
| 2822 | | } |
| 2823 | 23× | } |
| 2824 | |
|
| 2825 | 281× | fn clone_using( |
| 2826 | 281× | url: &str, |
| 2827 | 281× | dest: &Path, |
| 2828 | 281× | max_blob: Option<u64>, |
| 2829 | 281× | sparse: &[String], |
| 2830 | 281× | depth: Option<u32>, |
| 2831 | 281× | options: &RemoteOptions, |
| 2832 | 281× | ) -> Result<(Repo, Vec<(String, String)>)> { |
| 2833 | 281× | let remote = open_remote(url, options)?; |
| 2834 | 281× | let id = remote.repo_id()?; |
| 2835 | 277× | if id == crate::protocol::UNBOUND_ID { |
| 2836 | 2× | return fail(format!( |
| 2837 | | "{url} is empty and unbound: push a repository to it first" |
| 2838 | | )); |
| 2839 | 275× | } |
| 2840 | 275× | let mut repo = Repo::init_with_id(dest, id)?; |
| 2841 | 275× | repo.config |
| 2842 | 275× | .set("remote.origin.url", &crate::protocol::stored_url(url))?; |
| 2843 | 275× | if let Some(m) = max_blob { |
| 2844 | 2× | repo.config.set("filter.max-blob", &m.to_string())?; |
| 2845 | 273× | } else if !sparse.is_empty() { |
| 2846 | 1× | repo.config.set("filter.max-blob", "0")?; |
| 2847 | 272× | } |
| 2848 | 275× | if !sparse.is_empty() { |
| 2849 | 1× | repo.config.set("filter.paths", &sparse.join(","))?; |
| 2850 | 274× | } |
| 2851 | 275× | repo.save_config()?; |
| 2852 | 275× | let skipped = if let Some(depth) = depth { |
| 2853 | 1× | repo.fetch_shallow("origin", depth)?; |
| 2854 | 1× | Vec::new() |
| 2855 | | } else { |
| 2856 | 274× | repo.fetch("origin")?.skipped |
| 2857 | | }; |
| 2858 | 273× | let (op_ref, op) = repo.head_op()?; |
| 2859 | | |
| 2860 | | |
| 2861 | | |
| 2862 | 273× | let mut theirs = op.view.remotes.get("origin").cloned().unwrap_or_default(); |
| 2863 | 273× | for (name, _) in &skipped { |
| 2864 | 3× | theirs.insert(name.clone(), ObjectRef::ZERO); |
| 2865 | 3× | } |
| 2866 | | |
| 2867 | | |
| 2868 | 273× | let advertised = repo |
| 2869 | 273× | .open_named_remote("origin") |
| 2870 | 273× | .ok() |
| 2871 | 273× | .and_then(|r| r.refs().ok()) |
| 2872 | 273× | .and_then(|r| r.default_bookmark); |
| 2873 | 273× | let bookmark = advertised |
| 2874 | 273× | .filter(|b| theirs.contains_key(b)) |
| 2875 | 273× | .or_else(|| { |
| 2876 | 7× | theirs |
| 2877 | 7× | .contains_key(&repo.config.bookmark) |
| 2878 | 7× | .then(|| repo.config.bookmark.clone()) |
| 2879 | 7× | }) |
| 2880 | 273× | .or_else(|| { |
| 2881 | 0× | (theirs.len() == 1) |
| 2882 | 0× | .then(|| theirs.keys().next().cloned()) |
| 2883 | 0× | .flatten() |
| 2884 | 0× | }) |
| 2885 | 273× | .unwrap_or_else(|| repo.config.bookmark.clone()); |
| 2886 | 273× | if let Some((_, why)) = skipped.iter().find(|(name, _)| *name == bookmark) { |
| 2887 | 0× | return fail(format!("cannot check out {bookmark}: {why}")); |
| 2888 | 273× | } |
| 2889 | 273× | let initial = repo.config.bookmark.clone(); |
| 2890 | 273× | if bookmark != repo.config.bookmark { |
| 2891 | 1× | fs::create_dir_all(repo.workspace_bookmark_path().parent().unwrap())?; |
| 2892 | 1× | write_atomic(&repo.workspace_bookmark_path(), bookmark.as_bytes())?; |
| 2893 | 1× | repo.config.bookmark = bookmark.clone(); |
| 2894 | 272× | } |
| 2895 | 273× | if let Some(state) = theirs.get(&bookmark).copied() { |
| 2896 | 273× | let heads = repo.store.get_state(&state)?.heads; |
| 2897 | 273× | let mut view = op.view.clone(); |
| 2898 | | |
| 2899 | 273× | if initial != bookmark && repo.heads_of(&view, &initial)?.is_empty() { |
| 2900 | 1× | view.bookmarks.remove(&initial); |
| 2901 | 272× | } |
| 2902 | 273× | view.bookmarks.insert(bookmark, state); |
| 2903 | 273× | repo.record_op(&[op_ref], &format!("oxy clone {url}"), view)?; |
| 2904 | 273× | repo.checkout(&heads, false, "oxy clone (checkout)")?; |
| 2905 | 0× | } |
| 2906 | 273× | Ok((repo, skipped)) |
| 2907 | 281× | } |
| 2908 | | } |
| 2909 | |
|
| 2910 | |
|
| 2911 | |
|
| 2912 | |
|
| 2913 | |
|
| 2914 | 111× | fn next_serial(last: Option<u64>) -> u64 { |
| 2915 | 111× | match last { |
| 2916 | 69× | Some(l) => l + 1, |
| 2917 | 42× | None => Hlc::now().wall_ms.max(1), |
| 2918 | | } |
| 2919 | 111× | } |