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