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