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