| 1 |
|
| 2 |
|
| 3 |
use super::*; |
| 4 |
|
| 5 |
|
| 6 |
#[derive(Debug, Clone, PartialEq, Eq, Default)] |
| 7 |
pub struct FetchReport { |
| 8 |
|
| 9 |
pub refs: Vec<(String, Vec<ChangeHash>, usize)>, |
| 10 |
|
| 11 |
pub new_tags: usize, |
| 12 |
|
| 13 |
|
| 14 |
pub unverified: bool, |
| 15 |
} |
| 16 |
|
| 17 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 18 |
pub struct PullReport { |
| 19 |
pub heads: Vec<ChangeHash>, |
| 20 |
pub changed: bool, |
| 21 |
pub checked_out: bool, |
| 22 |
} |
| 23 |
|
| 24 |
impl Repo { |
| 25 |
|
| 26 |
|
| 27 |
fn ref_attest_path(&self, bookmark: &str) -> PathBuf { |
| 28 |
self.oxy.join("attest").join(bookmark) |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
pub fn latest_ref_attest(&self, bookmark: &str) -> Result<Option<(ObjectRef, SignedAttest)>> { |
| 33 |
match fs::read_to_string(self.ref_attest_path(bookmark)) { |
| 34 |
Ok(h) => { |
| 35 |
let r = ObjectRef::from_hex(h.trim()).ok_or_else(|| RepoError::Msg("corrupt attestation pointer".into()))?; |
| 36 |
Ok(Some((r, SignedAttest { attest: self.store.get_attest(&r)?, sigs: self.store.signatures(&r)? }))) |
| 37 |
} |
| 38 |
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), |
| 39 |
Err(e) => Err(e.into()), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
fn set_ref_attest(&self, bookmark: &str, r: &ObjectRef) -> Result<()> { |
| 44 |
fs::create_dir_all(self.oxy.join("attest"))?; |
| 45 |
write_atomic(&self.ref_attest_path(bookmark), r.to_hex().as_bytes()) |
| 46 |
} |
| 47 |
|
| 48 |
pub fn remote_refs(&self) -> Result<RefsResponse> { |
| 49 |
let view = self.view()?; |
| 50 |
let mut out = Vec::new(); |
| 51 |
for (name, state) in &view.bookmarks { |
| 52 |
let latest = self.latest_ref_attest(name)?.map(|(_, a)| a); |
| 53 |
let last_serial = latest.as_ref().and_then(|a| match a.attest.body { |
| 54 |
AttestBody::Ref { serial, .. } => Some(serial), |
| 55 |
_ => None, |
| 56 |
}); |
| 57 |
let attestation = latest.filter(|a| matches!(&a.attest.body, AttestBody::Ref { new_state, .. } if new_state == state)); |
| 58 |
out.push(RemoteRef { name: name.clone(), state: *state, heads: self.store.get_state(state)?.heads, attestation, last_serial }); |
| 59 |
} |
| 60 |
Ok(RefsResponse { refs: out, policy: self.current_policy()?.map(|(_, sp)| sp), tags: self.tag_refs()? }) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
fn closure_in_store(&self, heads: &[ChangeHash]) -> Result<BTreeSet<ChangeHash>> { |
| 65 |
let mut seen = BTreeSet::new(); |
| 66 |
let mut stack: Vec<ChangeHash> = heads.to_vec(); |
| 67 |
while let Some(h) = stack.pop() { |
| 68 |
if seen.contains(&h) || !self.store.has(&h) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
seen.insert(h); |
| 72 |
stack.extend(self.store.get_change(&h)?.deps); |
| 73 |
} |
| 74 |
Ok(seen) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
pub fn shallow_changes(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> { |
| 80 |
let mut out = Vec::new(); |
| 81 |
let mut seen: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 82 |
let mut layer: Vec<ChangeHash> = heads.to_vec(); |
| 83 |
for _ in 0..depth.max(1) { |
| 84 |
let mut next = Vec::new(); |
| 85 |
for h in layer { |
| 86 |
if !seen.insert(h) || !self.store.has(&h) { |
| 87 |
continue; |
| 88 |
} |
| 89 |
let c = self.store.get_change(&h)?; |
| 90 |
next.extend(c.deps.iter().copied()); |
| 91 |
out.push(h); |
| 92 |
} |
| 93 |
if next.is_empty() { |
| 94 |
break; |
| 95 |
} |
| 96 |
layer = next; |
| 97 |
} |
| 98 |
out.reverse(); |
| 99 |
Ok(out) |
| 100 |
} |
| 101 |
|
| 102 |
fn shallow_path(&self) -> PathBuf { |
| 103 |
self.oxy.join("shallow") |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
pub fn boundary(&self) -> BTreeSet<ChangeHash> { |
| 108 |
fs::read_to_string(self.shallow_path()).map(|t| t.lines().filter_map(ObjectRef::from_hex).collect()).unwrap_or_default() |
| 109 |
} |
| 110 |
|
| 111 |
pub fn is_shallow(&self) -> bool { |
| 112 |
!self.boundary().is_empty() |
| 113 |
} |
| 114 |
|
| 115 |
fn write_boundary(&self, boundary: &BTreeSet<ChangeHash>) -> Result<()> { |
| 116 |
if boundary.is_empty() { |
| 117 |
let _ = fs::remove_file(self.shallow_path()); |
| 118 |
return Ok(()); |
| 119 |
} |
| 120 |
let text: String = boundary.iter().map(|h| format!("{}\n", h.to_hex())).collect(); |
| 121 |
write_atomic(&self.shallow_path(), text.as_bytes()) |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
fn refresh_boundary(&self, imported: &[ChangeHash]) -> Result<()> { |
| 127 |
let mut boundary = self.boundary(); |
| 128 |
boundary.retain(|h| !self.store.has(h)); |
| 129 |
for h in imported { |
| 130 |
for d in self.store.get_change(h)?.deps { |
| 131 |
if !self.store.has(&d) { |
| 132 |
boundary.insert(d); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
self.write_boundary(&boundary) |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
pub fn deepen(&self, remote_name: &str, depth: u32) -> Result<usize> { |
| 141 |
let boundary = self.boundary(); |
| 142 |
if boundary.is_empty() { |
| 143 |
return Ok(0); |
| 144 |
} |
| 145 |
let remote = self.open_named_remote(remote_name)?; |
| 146 |
let wanted = remote.shallow(&boundary.iter().copied().collect::<Vec<_>>(), depth)?; |
| 147 |
let bundle = remote.fetch(&wanted, self.config.max_blob)?; |
| 148 |
let imported = self.import_bundle_shallow(&bundle, false)?; |
| 149 |
if self.config.max_blob.is_some() { |
| 150 |
self.register_missing_blobs(&imported, remote_name)?; |
| 151 |
} |
| 152 |
self.refresh_boundary(&imported)?; |
| 153 |
Ok(imported.len()) |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
pub fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> { |
| 158 |
let have = self.closure_in_store(have)?; |
| 159 |
|
| 160 |
|
| 161 |
let boundary = self.boundary(); |
| 162 |
let mut out = Vec::new(); |
| 163 |
let mut done: BTreeSet<ChangeHash> = have.clone(); |
| 164 |
done.extend(boundary.iter().copied()); |
| 165 |
|
| 166 |
let mut stack: Vec<(ChangeHash, bool)> = want.iter().rev().map(|h| (*h, false)).collect(); |
| 167 |
while let Some((h, expanded)) = stack.pop() { |
| 168 |
if done.contains(&h) { |
| 169 |
continue; |
| 170 |
} |
| 171 |
if !self.store.has(&h) { |
| 172 |
return fail(format!("change {} is not in this repository", h.short(12))); |
| 173 |
} |
| 174 |
if expanded { |
| 175 |
done.insert(h); |
| 176 |
out.push(h); |
| 177 |
continue; |
| 178 |
} |
| 179 |
stack.push((h, true)); |
| 180 |
for d in self.store.get_change(&h)?.deps { |
| 181 |
if !done.contains(&d) { |
| 182 |
stack.push((d, false)); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
Ok(out) |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
pub fn bundle_for(&self, changes: &[ChangeHash]) -> Result<Bundle> { |
| 192 |
self.bundle_for_filtered(changes, None) |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
pub fn objects_for(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> { |
| 197 |
let mut out = Vec::new(); |
| 198 |
for r in refs { |
| 199 |
let (ty, body) = match self.store.get(r) { |
| 200 |
Ok(x) => x, |
| 201 |
Err(StoreError::NotFound(_)) => continue, |
| 202 |
Err(e) => return Err(e.into()), |
| 203 |
}; |
| 204 |
if ty == ObjectType::Blob { |
| 205 |
for c in Blob::decode(&body)?.chunks { |
| 206 |
if let Ok((cty, cb)) = self.store.get(&c) { |
| 207 |
out.push((cty, cb)); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
out.push((ty, body)); |
| 212 |
} |
| 213 |
Ok(out) |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
pub fn bundle_for_filtered(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> { |
| 219 |
let mut bundle = Bundle::default(); |
| 220 |
let mut objects: BTreeSet<ObjectRef> = BTreeSet::new(); |
| 221 |
let mut seals: BTreeSet<ObjectRef> = BTreeSet::new(); |
| 222 |
let add_object = |r: &ObjectRef, objects: &mut BTreeSet<ObjectRef>| { |
| 223 |
if !r.is_zero() && self.store.has(r) { |
| 224 |
objects.insert(*r); |
| 225 |
} |
| 226 |
}; |
| 227 |
for h in changes { |
| 228 |
let c = self.store.get_change(h)?; |
| 229 |
let sigs = self.store.signatures(h)?; |
| 230 |
add_object(&c.author, &mut objects); |
| 231 |
for s in &sigs { |
| 232 |
add_object(&s.key, &mut objects); |
| 233 |
} |
| 234 |
for e in &c.edits { |
| 235 |
if let Edit::Replace { old, new, .. } = e { |
| 236 |
for b in [old, new].into_iter().flatten() { |
| 237 |
if self.store.has(b) { |
| 238 |
if let Ok(blob) = Blob::decode(&self.store.get_typed(b, ObjectType::Blob)?) { |
| 239 |
if max_blob.is_some_and(|m| blob.length > m) { |
| 240 |
continue; |
| 241 |
} |
| 242 |
objects.insert(*b); |
| 243 |
objects.extend(blob.chunks.iter().copied()); |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
for s in self.store.seals_for(h)? { |
| 250 |
seals.insert(s); |
| 251 |
} |
| 252 |
bundle.changes.push((c, sigs)); |
| 253 |
} |
| 254 |
for r in seals { |
| 255 |
let attest = self.store.get_attest(&r)?; |
| 256 |
let sigs = self.store.signatures(&r)?; |
| 257 |
for s in &sigs { |
| 258 |
add_object(&s.key, &mut objects); |
| 259 |
} |
| 260 |
bundle.attests.push(SignedAttest { attest, sigs }); |
| 261 |
} |
| 262 |
for r in objects { |
| 263 |
let (ty, body) = self.store.get(&r)?; |
| 264 |
bundle.objects.push((ty, body)); |
| 265 |
} |
| 266 |
Ok(bundle) |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
pub fn import_bundle(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> { |
| 272 |
self.import_bundle_inner(bundle, strict, false) |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
pub fn import_bundle_shallow(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> { |
| 278 |
self.import_bundle_inner(bundle, strict, true) |
| 279 |
} |
| 280 |
|
| 281 |
fn import_bundle_inner(&self, bundle: &Bundle, strict: bool, shallow: bool) -> Result<Vec<ChangeHash>> { |
| 282 |
for (ty, body) in &bundle.objects { |
| 283 |
match ty { |
| 284 |
ObjectType::Chunk | ObjectType::Blob | ObjectType::Identity | ObjectType::State | ObjectType::Checkpoint => { |
| 285 |
self.store.put(*ty, body)?; |
| 286 |
} |
| 287 |
other => return fail(format!("bundle may not carry {} objects", other.name())), |
| 288 |
} |
| 289 |
} |
| 290 |
for sa in &bundle.attests { |
| 291 |
let r = self.store.put_attest(&sa.attest)?; |
| 292 |
for sig in &sa.sigs { |
| 293 |
self.store.put_signature(&r, sig)?; |
| 294 |
} |
| 295 |
if let AttestBody::Seal { hashes } = &sa.attest.body { |
| 296 |
for h in hashes { |
| 297 |
self.store.index_seal(h, &r)?; |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
let incoming: BTreeSet<ChangeHash> = bundle.changes.iter().map(|(c, _)| c.hash()).collect(); |
| 302 |
let boundary = self.boundary(); |
| 303 |
let mut imported = Vec::new(); |
| 304 |
for (c, sigs) in &bundle.changes { |
| 305 |
let h = c.hash(); |
| 306 |
for d in &c.deps { |
| 307 |
if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d) { |
| 308 |
return fail(format!("change {} depends on {}, which is neither stored nor in the bundle", h.short(12), d.short(12))); |
| 309 |
} |
| 310 |
} |
| 311 |
self.store.put_change(c)?; |
| 312 |
for sig in sigs { |
| 313 |
self.store.put_signature(&h, sig)?; |
| 314 |
} |
| 315 |
imported.push(h); |
| 316 |
} |
| 317 |
if strict { |
| 318 |
for h in &imported { |
| 319 |
if !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) { |
| 320 |
return fail(format!("change {} is not authenticated by its author", h.short(12))); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
Ok(imported) |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
fn check_ref_attest(&self, sa: &SignedAttest, name: &str, old_state: &ObjectRef, new_state: &ObjectRef, last_serial: Option<u64>, allow_equal: bool) -> std::result::Result<u64, String> { |
| 331 |
let AttestBody::Ref { name: n, old_state: o, new_state: nw, serial } = &sa.attest.body else { |
| 332 |
return Err("attestation is not a ref attestation".into()); |
| 333 |
}; |
| 334 |
if n != name || o != old_state || nw != new_state { |
| 335 |
return Err("attestation does not describe this update".into()); |
| 336 |
} |
| 337 |
if sa.attest.repo != self.id().map_err(|e| e.to_string())? { |
| 338 |
return Err("attestation is for a different repository".into()); |
| 339 |
} |
| 340 |
if let Some(last) = last_serial { |
| 341 |
if *serial < last || (*serial == last && !allow_equal) { |
| 342 |
return Err(format!("attestation serial {serial} is not greater than the last seen {last}")); |
| 343 |
} |
| 344 |
} |
| 345 |
let r = sa.attest.hash(); |
| 346 |
let sig = sa.sigs.first().ok_or("attestation carries no signature")?; |
| 347 |
let id = self.store.get_identity(&sig.key).map_err(|_| format!("unknown key {}", sig.key.short(12)))?; |
| 348 |
verify_signature(&id, sig, Domain::Attest, &r).map_err(|e| e.to_string())?; |
| 349 |
Ok(*serial) |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
pub fn receive_push(&self, bundle: Bundle, updates: Vec<RefUpdate>, policy: Option<SignedPolicy>, tags: Vec<crate::protocol::TagRef>) -> Result<Vec<RefResult>> { |
| 358 |
let mut results = Vec::new(); |
| 359 |
if let Some(sp) = policy { |
| 360 |
let current = self.current_policy()?.map(|(_, c)| c.policy); |
| 361 |
match self.check_policy(&sp, current.as_ref()) { |
| 362 |
Ok(()) => { |
| 363 |
self.set_policy(&sp, &format!("push received: policy serial {}", sp.policy.serial))?; |
| 364 |
results.push(RefResult { name: "policy".into(), ok: true, reason: format!("serial {}", sp.policy.serial) }); |
| 365 |
} |
| 366 |
Err(e) => { |
| 367 |
results.push(RefResult { name: "policy".into(), ok: false, reason: e }); |
| 368 |
for u in updates { |
| 369 |
results.push(RefResult { name: u.name, ok: false, reason: "policy update was rejected".into() }); |
| 370 |
} |
| 371 |
return Ok(results); |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
let policy = self.current_policy()?.map(|(_, sp)| sp.policy); |
| 376 |
if let Err(e) = self.import_bundle(&bundle, policy.is_some()) { |
| 377 |
for u in updates { |
| 378 |
results.push(RefResult { name: u.name, ok: false, reason: e.to_string() }); |
| 379 |
} |
| 380 |
return Ok(results); |
| 381 |
} |
| 382 |
let (op_ref, op) = self.head_op()?; |
| 383 |
let mut view = op.view.clone(); |
| 384 |
let mut names = Vec::new(); |
| 385 |
for u in updates { |
| 386 |
let outcome = self.apply_update(&mut view, &u, policy.as_ref()); |
| 387 |
let (ok, reason) = match outcome { |
| 388 |
Ok(()) => (true, String::new()), |
| 389 |
Err(e) => (false, e), |
| 390 |
}; |
| 391 |
if ok { |
| 392 |
names.push(u.name.clone()); |
| 393 |
} |
| 394 |
results.push(RefResult { name: u.name, ok, reason }); |
| 395 |
} |
| 396 |
for t in &tags { |
| 397 |
let name = format!("tags/{}", t.name); |
| 398 |
match self.adopt_tag(&mut view, t, policy.as_ref()) { |
| 399 |
Ok(()) => { |
| 400 |
names.push(name.clone()); |
| 401 |
results.push(RefResult { name, ok: true, reason: String::new() }); |
| 402 |
} |
| 403 |
Err(e) => results.push(RefResult { name, ok: false, reason: e }), |
| 404 |
} |
| 405 |
} |
| 406 |
if !names.is_empty() { |
| 407 |
self.record_op(&[op_ref], &format!("push received: {}", names.join(" ")), view)?; |
| 408 |
} |
| 409 |
Ok(results) |
| 410 |
} |
| 411 |
|
| 412 |
fn apply_update(&self, view: &mut View, u: &RefUpdate, policy: Option<&crate::policy::Policy>) -> std::result::Result<(), String> { |
| 413 |
if u.name == "policy" { |
| 414 |
return Err("the policy reference is updated by sending a policy, not a bookmark update".into()); |
| 415 |
} |
| 416 |
let empty = self.store.put_state(&StateObject::new(vec![])).map_err(|e| e.to_string())?; |
| 417 |
let current = view.bookmarks.get(&u.name).copied().unwrap_or(empty); |
| 418 |
if current != u.old_state { |
| 419 |
return Err("stale: the remote bookmark has moved; pull first".into()); |
| 420 |
} |
| 421 |
let new_state = self.store.get_state(&u.new_state).map_err(|_| "new state object is missing".to_string())?; |
| 422 |
for h in &new_state.heads { |
| 423 |
if !self.store.has(h) { |
| 424 |
return Err(format!("head {} is missing", h.short(12))); |
| 425 |
} |
| 426 |
} |
| 427 |
if !u.force { |
| 428 |
let old_heads = self.store.get_state(¤t).map_err(|e| e.to_string())?.heads; |
| 429 |
let closure = self.closure_in_store(&new_state.heads).map_err(|e| e.to_string())?; |
| 430 |
if let Some(h) = old_heads.iter().find(|h| !closure.contains(h)) { |
| 431 |
return Err(format!("not a fast-forward: current head {} would be dropped (use --force)", h.short(12))); |
| 432 |
} |
| 433 |
} |
| 434 |
if let Some(p) = policy { |
| 435 |
let Some(sa) = &u.attestation else { |
| 436 |
return Err(format!("{:?} requires a signed reference attestation under this repository's policy", u.name)); |
| 437 |
}; |
| 438 |
let signer = sa.sigs.first().map(|s| s.key).ok_or("attestation carries no signature")?; |
| 439 |
if !p.authorizes(&signer, &u.name) { |
| 440 |
return Err(format!("key {} may not update {:?} under this repository's policy", signer.short(12), u.name)); |
| 441 |
} |
| 442 |
let old_heads = self.store.get_state(¤t).map_err(|e| e.to_string())?.heads; |
| 443 |
self.state_allowed(&old_heads, &new_state.heads, p, &u.name)?; |
| 444 |
} |
| 445 |
if let Some(sa) = &u.attestation { |
| 446 |
let last = self.latest_ref_attest(&u.name).map_err(|e| e.to_string())?.and_then(|(_, a)| match a.attest.body { |
| 447 |
AttestBody::Ref { serial, .. } => Some(serial), |
| 448 |
_ => None, |
| 449 |
}); |
| 450 |
self.check_ref_attest(sa, &u.name, &u.old_state, &u.new_state, last, false)?; |
| 451 |
let r = self.store.put_attest(&sa.attest).map_err(|e| e.to_string())?; |
| 452 |
for sig in &sa.sigs { |
| 453 |
self.store.put_signature(&r, sig).map_err(|e| e.to_string())?; |
| 454 |
} |
| 455 |
self.set_ref_attest(&u.name, &r).map_err(|e| e.to_string())?; |
| 456 |
} |
| 457 |
view.bookmarks.insert(u.name.clone(), u.new_state); |
| 458 |
Ok(()) |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
fn remote_url(&self, name: &str) -> Result<String> { |
| 464 |
self.config.remotes.get(name).cloned().ok_or_else(|| RepoError::Msg(format!("no remote named {name:?}; add one with `oxy remote add`"))) |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
fn remote_options(&self, name: &str) -> RemoteOptions { |
| 470 |
RemoteOptions { |
| 471 |
token: self.config.remote_tokens.get(name).cloned().or_else(|| std::env::var("OXY_TOKEN").ok()), |
| 472 |
ca: self.config.remote_cas.get(name).map(PathBuf::from).or_else(|| std::env::var("OXY_CA").ok().map(PathBuf::from)), |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
fn open_named_remote(&self, name: &str) -> Result<Box<dyn crate::protocol::Remote>> { |
| 477 |
open_remote(&self.remote_url(name)?, &self.remote_options(name)) |
| 478 |
} |
| 479 |
|
| 480 |
fn serial_path(&self, remote: &str, bookmark: &str) -> PathBuf { |
| 481 |
self.oxy.join("remotes").join(remote).join(format!("{bookmark}.serial")) |
| 482 |
} |
| 483 |
|
| 484 |
fn last_seen_serial(&self, remote: &str, bookmark: &str) -> Option<u64> { |
| 485 |
fs::read_to_string(self.serial_path(remote, bookmark)).ok().and_then(|s| s.trim().parse().ok()) |
| 486 |
} |
| 487 |
|
| 488 |
fn record_serial(&self, remote: &str, bookmark: &str, serial: u64) -> Result<()> { |
| 489 |
let p = self.serial_path(remote, bookmark); |
| 490 |
fs::create_dir_all(p.parent().unwrap())?; |
| 491 |
write_atomic(&p, serial.to_string().as_bytes()) |
| 492 |
} |
| 493 |
|
| 494 |
fn policy_serial_path(&self, remote: &str) -> PathBuf { |
| 495 |
self.oxy.join("remotes").join(remote).join("policy.serial") |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
fn remote_policy_serial(&self, remote: &str) -> Option<u64> { |
| 500 |
fs::read_to_string(self.policy_serial_path(remote)).ok().and_then(|s| s.trim().parse().ok()) |
| 501 |
} |
| 502 |
|
| 503 |
fn record_policy_serial(&self, remote: &str, serial: u64) -> Result<()> { |
| 504 |
let p = self.policy_serial_path(remote); |
| 505 |
fs::create_dir_all(p.parent().unwrap())?; |
| 506 |
write_atomic(&p, serial.to_string().as_bytes()) |
| 507 |
} |
| 508 |
|
| 509 |
fn lazy_dir(&self) -> PathBuf { |
| 510 |
self.oxy.join("lazy") |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
fn register_lazy(&self, blob: &ObjectRef, remote: &str) -> Result<()> { |
| 515 |
fs::create_dir_all(self.lazy_dir())?; |
| 516 |
write_atomic(&self.lazy_dir().join(blob.to_hex()), remote.as_bytes()) |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
pub fn lazy_remote(&self, blob: &ObjectRef) -> Option<String> { |
| 521 |
fs::read_to_string(self.lazy_dir().join(blob.to_hex())).ok().map(|s| s.trim().to_string()) |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
pub(crate) fn fetch_lazy_blob(&self, blob: &ObjectRef) -> Result<()> { |
| 526 |
let Some(remote_name) = self.lazy_remote(blob) else { |
| 527 |
return Err(RepoError::Store(StoreError::NotFound(*blob))); |
| 528 |
}; |
| 529 |
let remote = self.open_named_remote(&remote_name)?; |
| 530 |
let objects = remote.objects(&[*blob])?; |
| 531 |
for (ty, body) in objects { |
| 532 |
if matches!(ty, ObjectType::Blob | ObjectType::Chunk) { |
| 533 |
self.store.put(ty, &body)?; |
| 534 |
} |
| 535 |
} |
| 536 |
if !self.store.has(blob) { |
| 537 |
return fail(format!("{remote_name} did not send blob {}", blob.short(12))); |
| 538 |
} |
| 539 |
let _ = fs::remove_file(self.lazy_dir().join(blob.to_hex())); |
| 540 |
Ok(()) |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
fn register_missing_blobs(&self, changes: &[ChangeHash], remote: &str) -> Result<usize> { |
| 546 |
let mut n = 0; |
| 547 |
for h in changes { |
| 548 |
let c = self.store.get_change(h)?; |
| 549 |
for e in &c.edits { |
| 550 |
if let Edit::Replace { old, new, .. } = e { |
| 551 |
for b in [old, new].into_iter().flatten() { |
| 552 |
if !self.store.has(b) { |
| 553 |
self.register_lazy(b, remote)?; |
| 554 |
n += 1; |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
Ok(n) |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
fn known_heads(&self, view: &View) -> Result<Vec<ChangeHash>> { |
| 565 |
let mut have: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 566 |
for st in view.bookmarks.values().chain(view.remotes.values().flat_map(|b| b.values())) { |
| 567 |
if let Ok(s) = self.store.get_state(st) { |
| 568 |
have.extend(s.heads); |
| 569 |
} |
| 570 |
} |
| 571 |
have.extend(view.workspaces.values().copied()); |
| 572 |
Ok(have.into_iter().collect()) |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
pub fn fetch(&self, remote_name: &str) -> Result<FetchReport> { |
| 577 |
let remote = self.open_named_remote(remote_name)?; |
| 578 |
if remote.repo_id()? != self.id()? { |
| 579 |
return fail(format!("remote {remote_name:?} is a different repository (repository IDs differ)")); |
| 580 |
} |
| 581 |
let resp = remote.refs()?; |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
let local_policy = self.current_policy()?.map(|(_, sp)| sp.policy); |
| 588 |
let seen = self.remote_policy_serial(remote_name); |
| 589 |
let mut unverified = false; |
| 590 |
match &resp.policy { |
| 591 |
Some(sp) => { |
| 592 |
if seen.is_some_and(|s| sp.policy.serial < s) { |
| 593 |
return fail(format!("{remote_name} presents policy serial {} but presented {} before: refusing the downgrade", sp.policy.serial, seen.unwrap())); |
| 594 |
} |
| 595 |
match &local_policy { |
| 596 |
Some(l) if sp.policy.serial < l.serial => {} |
| 597 |
Some(l) if sp.policy.serial == l.serial => { |
| 598 |
if sp.policy.hash() != l.hash() { |
| 599 |
return fail(format!("{remote_name} presents a different policy with the same serial {}", l.serial)); |
| 600 |
} |
| 601 |
} |
| 602 |
local => { |
| 603 |
self.check_policy(sp, local.as_ref()).map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?; |
| 604 |
self.set_policy(sp, &format!("oxy fetch {remote_name} (policy serial {})", sp.policy.serial))?; |
| 605 |
} |
| 606 |
} |
| 607 |
self.record_policy_serial(remote_name, sp.policy.serial)?; |
| 608 |
} |
| 609 |
None => { |
| 610 |
if seen.is_some() { |
| 611 |
return fail(format!("{remote_name} presented a policy before but has none now: refusing the downgrade")); |
| 612 |
} |
| 613 |
unverified = true; |
| 614 |
} |
| 615 |
} |
| 616 |
let policy = if unverified { None } else { self.current_policy()?.map(|(_, sp)| sp.policy) }; |
| 617 |
let (op_ref, op) = self.head_op()?; |
| 618 |
let mut view = op.view.clone(); |
| 619 |
let have = self.known_heads(&view)?; |
| 620 |
let mut report = Vec::new(); |
| 621 |
let max_blob = self.config.max_blob; |
| 622 |
for r in resp.refs { |
| 623 |
let needed = remote.negotiate(&have, &r.heads)?; |
| 624 |
let bundle = remote.fetch(&needed, max_blob)?; |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
let imported = self.import_bundle(&bundle, false)?; |
| 629 |
if max_blob.is_some() { |
| 630 |
self.register_missing_blobs(&imported, remote_name)?; |
| 631 |
} |
| 632 |
for h in &r.heads { |
| 633 |
if !self.store.has(h) { |
| 634 |
return fail(format!("remote did not send head {}", h.short(12))); |
| 635 |
} |
| 636 |
} |
| 637 |
let state = self.store.put_state(&StateObject::new(r.heads.clone()))?; |
| 638 |
if state != r.state { |
| 639 |
return fail(format!("remote state {} does not match its heads", r.state.short(12))); |
| 640 |
} |
| 641 |
if let Some(p) = &policy { |
| 642 |
let Some(sa) = &r.attestation else { |
| 643 |
return fail(format!("{remote_name}/{} has no attestation, which its policy requires", r.name)); |
| 644 |
}; |
| 645 |
let signer = sa.sigs.first().map(|s| s.key).ok_or_else(|| RepoError::Msg("attestation carries no signature".into()))?; |
| 646 |
if !p.authorizes(&signer, &r.name) { |
| 647 |
return fail(format!("{remote_name}/{}: attested by {}, which the policy does not authorize", r.name, signer.short(12))); |
| 648 |
} |
| 649 |
} |
| 650 |
if let Some(sa) = &r.attestation { |
| 651 |
let AttestBody::Ref { old_state, new_state, .. } = &sa.attest.body else { |
| 652 |
return fail("remote sent a non-ref attestation for a bookmark"); |
| 653 |
}; |
| 654 |
let (old_state, new_state) = (*old_state, *new_state); |
| 655 |
let serial = self |
| 656 |
.check_ref_attest(sa, &r.name, &old_state, &new_state, self.last_seen_serial(remote_name, &r.name), true) |
| 657 |
.map_err(|e| RepoError::Msg(format!("attestation for {}/{}: {e}", remote_name, r.name)))?; |
| 658 |
if new_state != r.state { |
| 659 |
return fail(format!("attestation for {}/{} does not cover its current state", remote_name, r.name)); |
| 660 |
} |
| 661 |
let ar = self.store.put_attest(&sa.attest)?; |
| 662 |
for sig in &sa.sigs { |
| 663 |
self.store.put_signature(&ar, sig)?; |
| 664 |
} |
| 665 |
self.record_serial(remote_name, &r.name, serial)?; |
| 666 |
} |
| 667 |
view.remotes.entry(remote_name.to_string()).or_default().insert(r.name.clone(), state); |
| 668 |
report.push((r.name, r.heads, imported.len())); |
| 669 |
} |
| 670 |
let mut new_tags = 0; |
| 671 |
for t in &resp.tags { |
| 672 |
if view.tags.contains_key(&t.name) { |
| 673 |
continue; |
| 674 |
} |
| 675 |
|
| 676 |
let missing: Vec<ChangeHash> = t.heads.iter().filter(|h| !self.store.has(h)).copied().collect(); |
| 677 |
if !missing.is_empty() { |
| 678 |
let needed = remote.negotiate(&self.known_heads(&view)?, &missing)?; |
| 679 |
let bundle = remote.fetch(&needed, max_blob)?; |
| 680 |
self.import_bundle(&bundle, false)?; |
| 681 |
} |
| 682 |
self.store.put_state(&StateObject::new(t.heads.clone()))?; |
| 683 |
self.adopt_tag(&mut view, t, policy.as_ref()).map_err(|e| RepoError::Msg(format!("{remote_name} {}: {e}", t.name)))?; |
| 684 |
new_tags += 1; |
| 685 |
} |
| 686 |
if view != op.view { |
| 687 |
self.record_op(&[op_ref], &format!("oxy fetch {remote_name}"), view)?; |
| 688 |
} |
| 689 |
let _ = self.run_hook( |
| 690 |
"post-fetch", |
| 691 |
&format!( |
| 692 |
"{{\"event\":\"post-fetch\",\"remote\":{},\"bookmarks\":{}}}", |
| 693 |
json_str(remote_name), |
| 694 |
json_array(report.iter().map(|(n, heads, _)| format!("{{\"name\":{},\"heads\":{}}}", json_str(n), json_array(heads.iter().map(|h| json_str(&h.to_hex())))))) |
| 695 |
), |
| 696 |
); |
| 697 |
Ok(FetchReport { refs: report, unverified, new_tags }) |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
pub fn fetch_shallow(&self, remote_name: &str, depth: u32) -> Result<usize> { |
| 703 |
let remote = self.open_named_remote(remote_name)?; |
| 704 |
if remote.repo_id()? != self.id()? { |
| 705 |
return fail(format!("remote {remote_name:?} is a different repository (repository IDs differ)")); |
| 706 |
} |
| 707 |
let resp = remote.refs()?; |
| 708 |
let bookmark = self.config.bookmark.clone(); |
| 709 |
if let Some(sp) = &resp.policy { |
| 710 |
let local = self.current_policy()?.map(|(_, sp)| sp.policy); |
| 711 |
self.check_policy(sp, local.as_ref()).map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?; |
| 712 |
self.set_policy(sp, &format!("oxy clone --depth (policy serial {})", sp.policy.serial))?; |
| 713 |
self.record_policy_serial(remote_name, sp.policy.serial)?; |
| 714 |
} |
| 715 |
let Some(r) = resp.refs.into_iter().find(|r| r.name == bookmark) else { |
| 716 |
return fail(format!("{remote_name} has no bookmark {bookmark:?}")); |
| 717 |
}; |
| 718 |
if r.heads.is_empty() { |
| 719 |
return Ok(0); |
| 720 |
} |
| 721 |
|
| 722 |
let cp_bundle = remote.checkpoint(&r.heads)?; |
| 723 |
let mut cp_ref = None; |
| 724 |
for (ty, body) in &cp_bundle.objects { |
| 725 |
let put = self.store.put(*ty, body)?; |
| 726 |
if *ty == ObjectType::Checkpoint { |
| 727 |
cp_ref = Some(put); |
| 728 |
} |
| 729 |
} |
| 730 |
let Some(cp_ref) = cp_ref else { return fail(format!("{remote_name} sent no checkpoint")) }; |
| 731 |
self.install_checkpoint(&bookmark, &cp_ref)?; |
| 732 |
|
| 733 |
let wanted = remote.shallow(&r.heads, depth)?; |
| 734 |
let bundle = remote.fetch(&wanted, self.config.max_blob)?; |
| 735 |
let imported = self.import_bundle_shallow(&bundle, false)?; |
| 736 |
if self.config.max_blob.is_some() { |
| 737 |
self.register_missing_blobs(&imported, remote_name)?; |
| 738 |
} |
| 739 |
self.refresh_boundary(&imported)?; |
| 740 |
if let Some(sa) = &r.attestation { |
| 741 |
let ar = self.store.put_attest(&sa.attest)?; |
| 742 |
for sig in &sa.sigs { |
| 743 |
self.store.put_signature(&ar, sig)?; |
| 744 |
} |
| 745 |
if let AttestBody::Ref { serial, .. } = sa.attest.body { |
| 746 |
self.record_serial(remote_name, &r.name, serial)?; |
| 747 |
} |
| 748 |
} |
| 749 |
let state = self.store.put_state(&StateObject::new(r.heads.clone()))?; |
| 750 |
let (op_ref, op) = self.head_op()?; |
| 751 |
let mut view = op.view.clone(); |
| 752 |
view.remotes.entry(remote_name.to_string()).or_default().insert(r.name.clone(), state); |
| 753 |
self.record_op(&[op_ref], &format!("oxy fetch {remote_name} --depth {depth}"), view)?; |
| 754 |
Ok(imported.len()) |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
pub fn pull(&self, remote_name: &str) -> Result<PullReport> { |
| 760 |
self.fetch(remote_name)?; |
| 761 |
let (op_ref, op) = self.head_op()?; |
| 762 |
let bookmark = self.config.bookmark.clone(); |
| 763 |
let local = self.heads_of(&op.view, &bookmark)?; |
| 764 |
let Some(remote_state) = op.view.remotes.get(remote_name).and_then(|b| b.get(&bookmark)).copied() else { |
| 765 |
return Ok(PullReport { heads: local, changed: false, checked_out: false }); |
| 766 |
}; |
| 767 |
let remote_heads = self.store.get_state(&remote_state)?.heads; |
| 768 |
let mut all = local.clone(); |
| 769 |
all.extend(remote_heads); |
| 770 |
let st = self.load_state(&all)?; |
| 771 |
let mut heads = st.maximal(&all); |
| 772 |
heads.sort(); |
| 773 |
let mut sorted_local = local.clone(); |
| 774 |
sorted_local.sort(); |
| 775 |
if heads == sorted_local { |
| 776 |
return Ok(PullReport { heads, changed: false, checked_out: false }); |
| 777 |
} |
| 778 |
let mut view = op.view.clone(); |
| 779 |
view.bookmarks.insert(bookmark.clone(), self.store.put_state(&StateObject::new(heads.clone()))?); |
| 780 |
self.record_op(&[op_ref], &format!("oxy pull {remote_name}"), view)?; |
| 781 |
|
| 782 |
let snap = self.snapshot("oxy pull")?; |
| 783 |
let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty(); |
| 784 |
let mut base = snap.base.maximal(&snap.wc.deps); |
| 785 |
base.sort(); |
| 786 |
let mut checked_out = false; |
| 787 |
if clean && base == sorted_local { |
| 788 |
self.checkout(&heads, false, "oxy pull (checkout)")?; |
| 789 |
checked_out = true; |
| 790 |
} |
| 791 |
Ok(PullReport { heads, changed: true, checked_out }) |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
pub fn push(&self, remote_name: &str, force: bool) -> Result<Vec<RefResult>> { |
| 797 |
let (op_ref, op) = self.head_op()?; |
| 798 |
let bookmark = self.config.bookmark.clone(); |
| 799 |
let local_heads = self.heads_of(&op.view, &bookmark)?; |
| 800 |
|
| 801 |
self.run_hook( |
| 802 |
"pre-push", |
| 803 |
&format!("{{\"event\":\"pre-push\",\"remote\":{},\"bookmark\":{},\"heads\":{}}}", json_str(remote_name), json_str(&bookmark), json_array(local_heads.iter().map(|h| json_str(&h.to_hex())))), |
| 804 |
)?; |
| 805 |
let remote = self.open_named_remote(remote_name)?; |
| 806 |
if remote.repo_id()? != self.id()? { |
| 807 |
return fail(format!("remote {remote_name:?} is a different repository (repository IDs differ)")); |
| 808 |
} |
| 809 |
let resp = remote.refs()?; |
| 810 |
let local_policy = self.current_policy()?.map(|(_, sp)| sp); |
| 811 |
let policy_to_send = match (&local_policy, &resp.policy) { |
| 812 |
(Some(l), Some(r)) if l.policy.serial > r.policy.serial => Some(l.clone()), |
| 813 |
(Some(l), None) => Some(l.clone()), |
| 814 |
(Some(l), Some(r)) if l.policy.serial < r.policy.serial => { |
| 815 |
return fail(format!("{remote_name} has a newer policy (serial {}); fetch first", r.policy.serial)); |
| 816 |
} |
| 817 |
_ => None, |
| 818 |
}; |
| 819 |
let remote_has_policy = resp.policy.is_some() || policy_to_send.is_some(); |
| 820 |
if remote_has_policy && self.secret_key()?.is_none() { |
| 821 |
return fail(format!("{remote_name} enforces a trust policy; pushing needs an identity (`oxy identity create`)")); |
| 822 |
} |
| 823 |
let remote_tags: BTreeSet<String> = resp.tags.iter().map(|t| t.name.clone()).collect(); |
| 824 |
let tags_to_send: Vec<crate::protocol::TagRef> = self.tag_refs()?.into_iter().filter(|t| !remote_tags.contains(&t.name)).collect(); |
| 825 |
let refs = resp.refs; |
| 826 |
let empty = self.store.put_state(&StateObject::new(vec![]))?; |
| 827 |
let (remote_state, remote_heads, last_serial) = match refs.iter().find(|r| r.name == bookmark) { |
| 828 |
Some(r) => (r.state, r.heads.clone(), r.last_serial), |
| 829 |
None => (empty, vec![], None), |
| 830 |
}; |
| 831 |
let new_state = self.store.put_state(&StateObject::new(local_heads.clone()))?; |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
if new_state == remote_state && policy_to_send.is_none() && tags_to_send.is_empty() { |
| 837 |
return Ok(vec![RefResult { name: bookmark, ok: true, reason: "already up to date".into() }]); |
| 838 |
} |
| 839 |
for h in &remote_heads { |
| 840 |
if !self.store.has(h) { |
| 841 |
return fail(format!("the remote has change {} which you do not; pull first", h.short(12))); |
| 842 |
} |
| 843 |
} |
| 844 |
if !force { |
| 845 |
let closure = self.closure_in_store(&local_heads)?; |
| 846 |
if let Some(h) = remote_heads.iter().find(|h| !closure.contains(h)) { |
| 847 |
return fail(format!("not a fast-forward: remote head {} is not in your state; pull first or use --force", h.short(12))); |
| 848 |
} |
| 849 |
} |
| 850 |
let to_send = self.negotiate(&remote_heads, &local_heads)?; |
| 851 |
let mut bundle = self.bundle_for(&to_send)?; |
| 852 |
let key = self.secret_key()?.map(|(k, _)| k); |
| 853 |
if let Some(key) = key { |
| 854 |
|
| 855 |
let mut unsigned = Vec::new(); |
| 856 |
for h in &to_send { |
| 857 |
let c = self.store.get_change(h)?; |
| 858 |
if c.author == key && !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) { |
| 859 |
unsigned.push(*h); |
| 860 |
} |
| 861 |
} |
| 862 |
for chunk in unsigned.chunks(crate::attest::MAX_SEAL_HASHES) { |
| 863 |
let sa = self.sign_attest(&Attest::seal(self.id()?, chunk.to_vec()))?; |
| 864 |
bundle.attests.push(sa); |
| 865 |
} |
| 866 |
bundle = self.bundle_for(&to_send)?; |
| 867 |
} |
| 868 |
bundle.objects.push((ObjectType::State, StateObject::new(local_heads.clone()).encode())); |
| 869 |
let attestation = match key { |
| 870 |
Some(_) => Some(self.sign_attest(&Attest { |
| 871 |
repo: self.id()?, |
| 872 |
hlc: Hlc::now(), |
| 873 |
body: AttestBody::Ref { name: bookmark.clone(), old_state: remote_state, new_state, serial: last_serial.unwrap_or(0) + 1 }, |
| 874 |
})?), |
| 875 |
None => None, |
| 876 |
}; |
| 877 |
let serial = attestation.as_ref().and_then(|a| match a.attest.body { |
| 878 |
AttestBody::Ref { serial, .. } => Some(serial), |
| 879 |
_ => None, |
| 880 |
}); |
| 881 |
let sent_serial = policy_to_send.as_ref().map(|p| p.policy.serial); |
| 882 |
|
| 883 |
for t in &tags_to_send { |
| 884 |
let extra = self.negotiate(&remote_heads, &t.heads)?; |
| 885 |
let extra: Vec<ChangeHash> = extra.into_iter().filter(|h| !to_send.contains(h)).collect(); |
| 886 |
if !extra.is_empty() { |
| 887 |
let more = self.bundle_for(&extra)?; |
| 888 |
bundle.changes.extend(more.changes); |
| 889 |
bundle.attests.extend(more.attests); |
| 890 |
bundle.objects.extend(more.objects); |
| 891 |
} |
| 892 |
bundle.objects.push((ObjectType::State, StateObject::new(t.heads.clone()).encode())); |
| 893 |
} |
| 894 |
let results = remote.push(bundle, vec![RefUpdate { name: bookmark.clone(), old_state: remote_state, new_state, force, attestation }], policy_to_send, tags_to_send)?; |
| 895 |
if let (Some(serial), true) = (sent_serial, results.iter().any(|r| r.ok && r.name == "policy")) { |
| 896 |
self.record_policy_serial(remote_name, serial)?; |
| 897 |
} |
| 898 |
if results.iter().any(|r| r.ok && r.name == bookmark) { |
| 899 |
let mut view = op.view.clone(); |
| 900 |
view.remotes.entry(remote_name.to_string()).or_default().insert(bookmark.clone(), new_state); |
| 901 |
self.record_op(&[op_ref], &format!("oxy push {remote_name}"), view)?; |
| 902 |
if let Some(s) = serial { |
| 903 |
self.record_serial(remote_name, &bookmark, s)?; |
| 904 |
} |
| 905 |
} |
| 906 |
Ok(results) |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
pub fn clone(url: &str, dest: &Path) -> Result<Repo> { |
| 912 |
Self::clone_with(url, dest, None, &[]) |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
pub fn clone_with(url: &str, dest: &Path, max_blob: Option<u64>, sparse: &[String]) -> Result<Repo> { |
| 919 |
Self::clone_full(url, dest, max_blob, sparse, None) |
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
pub fn clone_shallow(url: &str, dest: &Path, depth: u32, max_blob: Option<u64>, sparse: &[String]) -> Result<Repo> { |
| 925 |
Self::clone_full(url, dest, max_blob, sparse, Some(depth)) |
| 926 |
} |
| 927 |
|
| 928 |
fn clone_full(url: &str, dest: &Path, max_blob: Option<u64>, sparse: &[String], depth: Option<u32>) -> Result<Repo> { |
| 929 |
let options = RemoteOptions { |
| 930 |
token: std::env::var("OXY_TOKEN").ok(), |
| 931 |
ca: std::env::var("OXY_CA").ok().map(PathBuf::from), |
| 932 |
}; |
| 933 |
let remote = open_remote(url, &options)?; |
| 934 |
let id = remote.repo_id()?; |
| 935 |
let mut repo = Repo::init_with_id(dest, id)?; |
| 936 |
repo.config.set("remote.origin.url", url)?; |
| 937 |
if let Some(m) = max_blob { |
| 938 |
repo.config.set("filter.max-blob", &m.to_string())?; |
| 939 |
} else if !sparse.is_empty() { |
| 940 |
repo.config.set("filter.max-blob", "0")?; |
| 941 |
} |
| 942 |
if !sparse.is_empty() { |
| 943 |
repo.config.set("filter.paths", &sparse.join(","))?; |
| 944 |
} |
| 945 |
repo.save_config()?; |
| 946 |
if let Some(depth) = depth { |
| 947 |
repo.fetch_shallow("origin", depth)?; |
| 948 |
} else { |
| 949 |
repo.fetch("origin")?; |
| 950 |
} |
| 951 |
let (op_ref, op) = repo.head_op()?; |
| 952 |
let bookmark = repo.config.bookmark.clone(); |
| 953 |
if let Some(state) = op.view.remotes.get("origin").and_then(|b| b.get(&bookmark)).copied() { |
| 954 |
let heads = repo.store.get_state(&state)?.heads; |
| 955 |
let mut view = op.view.clone(); |
| 956 |
view.bookmarks.insert(bookmark, state); |
| 957 |
repo.record_op(&[op_ref], &format!("oxy clone {url}"), view)?; |
| 958 |
repo.checkout(&heads, false, "oxy clone (checkout)")?; |
| 959 |
} |
| 960 |
Ok(repo) |
| 961 |
} |
| 962 |
} |