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