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