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