| 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 |
pub heads: Vec<ChangeHash>, |
| 38 |
pub changed: bool, |
| 39 |
pub checked_out: bool, |
| 40 |
} |
| 41 |
|
| 42 |
impl Repo { |
| 43 |
|
| 44 |
|
| 45 |
fn ref_attest_path(&self, bookmark: &str) -> PathBuf { |
| 46 |
self.oxy.join("attest").join(bookmark) |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
pub fn latest_ref_attest(&self, bookmark: &str) -> Result<Option<(ObjectRef, SignedAttest)>> { |
| 51 |
match fs::read_to_string(self.ref_attest_path(bookmark)) { |
| 52 |
Ok(h) => { |
| 53 |
let r = ObjectRef::from_hex(h.trim()) |
| 54 |
.ok_or_else(|| RepoError::Msg("corrupt attestation pointer".into()))?; |
| 55 |
Ok(Some(( |
| 56 |
r, |
| 57 |
SignedAttest { |
| 58 |
attest: self.store.get_attest(&r)?, |
| 59 |
sigs: self.store.signatures(&r)?, |
| 60 |
}, |
| 61 |
))) |
| 62 |
} |
| 63 |
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), |
| 64 |
Err(e) => Err(e.into()), |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
fn set_ref_attest(&self, bookmark: &str, r: &ObjectRef) -> Result<()> { |
| 69 |
fs::create_dir_all(self.oxy.join("attest"))?; |
| 70 |
write_atomic(&self.ref_attest_path(bookmark), r.to_hex().as_bytes()) |
| 71 |
} |
| 72 |
|
| 73 |
pub fn remote_refs(&self) -> Result<RefsResponse> { |
| 74 |
let view = self.view()?; |
| 75 |
let mut out = Vec::new(); |
| 76 |
for (name, state) in &view.bookmarks { |
| 77 |
let latest = self.latest_ref_attest(name)?.map(|(_, a)| a); |
| 78 |
let last_serial = latest.as_ref().and_then(|a| match a.attest.body { |
| 79 |
AttestBody::Ref { serial, .. } => Some(serial), |
| 80 |
_ => None, |
| 81 |
}); |
| 82 |
let attestation = latest.filter(|a| matches!(&a.attest.body, AttestBody::Ref { new_state, .. } if new_state == state)); |
| 83 |
out.push(RemoteRef { |
| 84 |
name: name.clone(), |
| 85 |
state: *state, |
| 86 |
heads: self.store.get_state(state)?.heads, |
| 87 |
attestation, |
| 88 |
last_serial, |
| 89 |
}); |
| 90 |
} |
| 91 |
let policy = self.current_policy()?.map(|(_, sp)| sp); |
| 92 |
let history = self |
| 93 |
.policy_history()? |
| 94 |
.into_iter() |
| 95 |
.filter(|sp| { |
| 96 |
policy |
| 97 |
.as_ref() |
| 98 |
.is_some_and(|p| sp.policy.serial < p.policy.serial) |
| 99 |
}) |
| 100 |
.collect(); |
| 101 |
Ok(RefsResponse { |
| 102 |
refs: out, |
| 103 |
policy, |
| 104 |
tags: self.tag_refs()?, |
| 105 |
history, |
| 106 |
}) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
fn closure_in_store(&self, heads: &[ChangeHash]) -> Result<BTreeSet<ChangeHash>> { |
| 111 |
let mut seen = BTreeSet::new(); |
| 112 |
let mut stack: Vec<ChangeHash> = heads.to_vec(); |
| 113 |
while let Some(h) = stack.pop() { |
| 114 |
if seen.contains(&h) || !self.store.has(&h) { |
| 115 |
continue; |
| 116 |
} |
| 117 |
seen.insert(h); |
| 118 |
stack.extend(self.store.get_change(&h)?.deps); |
| 119 |
} |
| 120 |
Ok(seen) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
pub fn shallow_changes(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> { |
| 126 |
let mut out = Vec::new(); |
| 127 |
let mut seen: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 128 |
let mut layer: Vec<ChangeHash> = heads.to_vec(); |
| 129 |
for _ in 0..depth.max(1) { |
| 130 |
let mut next = Vec::new(); |
| 131 |
for h in layer { |
| 132 |
if !seen.insert(h) || !self.store.has(&h) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
let c = self.store.get_change(&h)?; |
| 136 |
next.extend(c.deps.iter().copied()); |
| 137 |
out.push(h); |
| 138 |
} |
| 139 |
if next.is_empty() { |
| 140 |
break; |
| 141 |
} |
| 142 |
layer = next; |
| 143 |
} |
| 144 |
out.reverse(); |
| 145 |
Ok(out) |
| 146 |
} |
| 147 |
|
| 148 |
fn shallow_path(&self) -> PathBuf { |
| 149 |
self.oxy.join("shallow") |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
pub fn boundary(&self) -> BTreeSet<ChangeHash> { |
| 154 |
fs::read_to_string(self.shallow_path()) |
| 155 |
.map(|t| t.lines().filter_map(ObjectRef::from_hex).collect()) |
| 156 |
.unwrap_or_default() |
| 157 |
} |
| 158 |
|
| 159 |
pub fn is_shallow(&self) -> bool { |
| 160 |
!self.boundary().is_empty() |
| 161 |
} |
| 162 |
|
| 163 |
fn write_boundary(&self, boundary: &BTreeSet<ChangeHash>) -> Result<()> { |
| 164 |
if boundary.is_empty() { |
| 165 |
let _ = fs::remove_file(self.shallow_path()); |
| 166 |
return Ok(()); |
| 167 |
} |
| 168 |
let text: String = boundary |
| 169 |
.iter() |
| 170 |
.map(|h| format!("{}\n", h.to_hex())) |
| 171 |
.collect(); |
| 172 |
write_atomic(&self.shallow_path(), text.as_bytes()) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
fn refresh_boundary(&self, imported: &[ChangeHash]) -> Result<()> { |
| 178 |
let mut boundary = self.boundary(); |
| 179 |
boundary.retain(|h| !self.store.has(h)); |
| 180 |
for h in imported { |
| 181 |
for d in self.store.get_change(h)?.deps { |
| 182 |
if !self.store.has(&d) { |
| 183 |
boundary.insert(d); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
self.write_boundary(&boundary) |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
pub fn deepen(&self, remote_name: &str, depth: u32) -> Result<usize> { |
| 192 |
let boundary = self.boundary(); |
| 193 |
if boundary.is_empty() { |
| 194 |
return Ok(0); |
| 195 |
} |
| 196 |
let remote = self.open_named_remote(remote_name)?; |
| 197 |
let wanted = remote.shallow(&boundary.iter().copied().collect::<Vec<_>>(), depth)?; |
| 198 |
let bundle = remote.fetch(&wanted, self.config.max_blob)?; |
| 199 |
let imported = self.import_bundle_shallow(&bundle, false)?; |
| 200 |
if self.config.max_blob.is_some() { |
| 201 |
self.register_missing_blobs(&imported, remote_name)?; |
| 202 |
} |
| 203 |
self.refresh_boundary(&imported)?; |
| 204 |
Ok(imported.len()) |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
pub fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> { |
| 209 |
let have = self.closure_in_store(have)?; |
| 210 |
|
| 211 |
|
| 212 |
let boundary = self.boundary(); |
| 213 |
let mut out = Vec::new(); |
| 214 |
let mut done: BTreeSet<ChangeHash> = have.clone(); |
| 215 |
done.extend(boundary.iter().copied()); |
| 216 |
|
| 217 |
let mut stack: Vec<(ChangeHash, bool)> = want.iter().rev().map(|h| (*h, false)).collect(); |
| 218 |
while let Some((h, expanded)) = stack.pop() { |
| 219 |
if done.contains(&h) { |
| 220 |
continue; |
| 221 |
} |
| 222 |
if !self.store.has(&h) { |
| 223 |
return fail(format!("change {} is not in this repository", h.short(12))); |
| 224 |
} |
| 225 |
if expanded { |
| 226 |
done.insert(h); |
| 227 |
out.push(h); |
| 228 |
continue; |
| 229 |
} |
| 230 |
stack.push((h, true)); |
| 231 |
for d in self.store.get_change(&h)?.deps { |
| 232 |
if !done.contains(&d) { |
| 233 |
stack.push((d, false)); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
Ok(out) |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
pub fn bundle_for(&self, changes: &[ChangeHash]) -> Result<Bundle> { |
| 243 |
self.bundle_for_filtered(changes, None) |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
pub fn objects_for(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> { |
| 248 |
let mut out = Vec::new(); |
| 249 |
for r in refs { |
| 250 |
let (ty, body) = match self.store.get(r) { |
| 251 |
Ok(x) => x, |
| 252 |
Err(StoreError::NotFound(_)) => continue, |
| 253 |
Err(e) => return Err(e.into()), |
| 254 |
}; |
| 255 |
if ty == ObjectType::Blob { |
| 256 |
for c in Blob::decode(&body)?.chunks { |
| 257 |
if let Ok((cty, cb)) = self.store.get(&c) { |
| 258 |
out.push((cty, cb)); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
out.push((ty, body)); |
| 263 |
} |
| 264 |
Ok(out) |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
pub fn bundle_for_filtered( |
| 270 |
&self, |
| 271 |
changes: &[ChangeHash], |
| 272 |
max_blob: Option<u64>, |
| 273 |
) -> Result<Bundle> { |
| 274 |
let mut bundle = Bundle::default(); |
| 275 |
let mut objects: BTreeSet<ObjectRef> = BTreeSet::new(); |
| 276 |
let mut seals: BTreeSet<ObjectRef> = BTreeSet::new(); |
| 277 |
let add_object = |r: &ObjectRef, objects: &mut BTreeSet<ObjectRef>| { |
| 278 |
if !r.is_zero() && self.store.has(r) { |
| 279 |
objects.insert(*r); |
| 280 |
} |
| 281 |
}; |
| 282 |
for h in changes { |
| 283 |
let c = self.store.get_change(h)?; |
| 284 |
let sigs = self.store.signatures(h)?; |
| 285 |
add_object(&c.author, &mut objects); |
| 286 |
for s in &sigs { |
| 287 |
add_object(&s.key, &mut objects); |
| 288 |
} |
| 289 |
for e in &c.edits { |
| 290 |
if let Edit::Replace { old, new, .. } = e { |
| 291 |
for b in [old, new].into_iter().flatten() { |
| 292 |
if self.store.has(b) { |
| 293 |
if let Ok(blob) = |
| 294 |
Blob::decode(&self.store.get_typed(b, ObjectType::Blob)?) |
| 295 |
{ |
| 296 |
if max_blob.is_some_and(|m| blob.length > m) { |
| 297 |
continue; |
| 298 |
} |
| 299 |
objects.insert(*b); |
| 300 |
objects.extend(blob.chunks.iter().copied()); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
for s in self.store.seals_for(h)? { |
| 307 |
seals.insert(s); |
| 308 |
} |
| 309 |
bundle.changes.push((c, sigs)); |
| 310 |
} |
| 311 |
for r in seals { |
| 312 |
let attest = self.store.get_attest(&r)?; |
| 313 |
let sigs = self.store.signatures(&r)?; |
| 314 |
for s in &sigs { |
| 315 |
add_object(&s.key, &mut objects); |
| 316 |
} |
| 317 |
bundle.attests.push(SignedAttest { attest, sigs }); |
| 318 |
} |
| 319 |
for r in objects { |
| 320 |
let (ty, body) = self.store.get(&r)?; |
| 321 |
bundle.objects.push((ty, body)); |
| 322 |
} |
| 323 |
Ok(bundle) |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
pub fn import_bundle(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> { |
| 329 |
self.import_bundle_inner(bundle, strict, false) |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
pub fn import_bundle_shallow(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> { |
| 335 |
self.import_bundle_inner(bundle, strict, true) |
| 336 |
} |
| 337 |
|
| 338 |
fn import_bundle_inner( |
| 339 |
&self, |
| 340 |
bundle: &Bundle, |
| 341 |
strict: bool, |
| 342 |
shallow: bool, |
| 343 |
) -> Result<Vec<ChangeHash>> { |
| 344 |
let batch = self.store.begin_batch()?; |
| 345 |
let result = self.import_bundle_batched(bundle, strict, shallow); |
| 346 |
batch.finish()?; |
| 347 |
result |
| 348 |
} |
| 349 |
|
| 350 |
fn import_bundle_batched( |
| 351 |
&self, |
| 352 |
bundle: &Bundle, |
| 353 |
strict: bool, |
| 354 |
shallow: bool, |
| 355 |
) -> Result<Vec<ChangeHash>> { |
| 356 |
for (ty, body) in &bundle.objects { |
| 357 |
match ty { |
| 358 |
ObjectType::Chunk |
| 359 |
| ObjectType::Blob |
| 360 |
| ObjectType::Identity |
| 361 |
| ObjectType::State |
| 362 |
| ObjectType::Checkpoint => { |
| 363 |
self.store.put(*ty, body)?; |
| 364 |
} |
| 365 |
other => return fail(format!("bundle may not carry {} objects", other.name())), |
| 366 |
} |
| 367 |
} |
| 368 |
for sa in &bundle.attests { |
| 369 |
let r = self.store.put_attest(&sa.attest)?; |
| 370 |
for sig in &sa.sigs { |
| 371 |
self.store.put_signature(&r, sig)?; |
| 372 |
} |
| 373 |
if let AttestBody::Seal { hashes } = &sa.attest.body { |
| 374 |
for h in hashes { |
| 375 |
self.store.index_seal(h, &r)?; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
let incoming: BTreeSet<ChangeHash> = bundle.changes.iter().map(|(c, _)| c.hash()).collect(); |
| 380 |
let boundary = self.boundary(); |
| 381 |
let mut imported = Vec::new(); |
| 382 |
for (c, sigs) in &bundle.changes { |
| 383 |
let h = c.hash(); |
| 384 |
for d in &c.deps { |
| 385 |
if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d) |
| 386 |
{ |
| 387 |
return fail(format!( |
| 388 |
"change {} depends on {}, which is neither stored nor in the bundle", |
| 389 |
h.short(12), |
| 390 |
d.short(12) |
| 391 |
)); |
| 392 |
} |
| 393 |
} |
| 394 |
self.store.put_change(c)?; |
| 395 |
for sig in sigs { |
| 396 |
self.store.put_signature(&h, sig)?; |
| 397 |
} |
| 398 |
imported.push(h); |
| 399 |
} |
| 400 |
if strict { |
| 401 |
for h in &imported { |
| 402 |
if !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) { |
| 403 |
return fail(format!( |
| 404 |
"change {} is not authenticated by its author", |
| 405 |
h.short(12) |
| 406 |
)); |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
Ok(imported) |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
fn delegate_authorized(&self, p: &crate::policy::Policy, signer: &KeyId, reference: &str) -> bool { |
| 421 |
if p.is_revoked(signer) { |
| 422 |
return false; |
| 423 |
} |
| 424 |
let Some(rule) = p.rule_for(reference) else { return false }; |
| 425 |
rule.keys.iter().any(|k| { |
| 426 |
!p.is_revoked(k) |
| 427 |
&& p.identity(k) |
| 428 |
.cloned() |
| 429 |
.or_else(|| self.store.get_identity(k).ok()) |
| 430 |
.is_some_and(|id| id.delegates.contains(signer)) |
| 431 |
}) |
| 432 |
} |
| 433 |
|
| 434 |
fn check_ref_attest( |
| 435 |
&self, |
| 436 |
sa: &SignedAttest, |
| 437 |
name: &str, |
| 438 |
old_state: &ObjectRef, |
| 439 |
new_state: &ObjectRef, |
| 440 |
last_serial: Option<u64>, |
| 441 |
allow_equal: bool, |
| 442 |
) -> std::result::Result<u64, String> { |
| 443 |
let AttestBody::Ref { |
| 444 |
name: n, |
| 445 |
old_state: o, |
| 446 |
new_state: nw, |
| 447 |
serial, |
| 448 |
} = &sa.attest.body |
| 449 |
else { |
| 450 |
return Err("attestation is not a ref attestation".into()); |
| 451 |
}; |
| 452 |
if n != name || o != old_state || nw != new_state { |
| 453 |
return Err("attestation does not describe this update".into()); |
| 454 |
} |
| 455 |
if sa.attest.repo != self.id().map_err(|e| e.to_string())? { |
| 456 |
return Err("attestation is for a different repository".into()); |
| 457 |
} |
| 458 |
if let Some(last) = last_serial { |
| 459 |
if *serial < last || (*serial == last && !allow_equal) { |
| 460 |
return Err(format!( |
| 461 |
"attestation serial {serial} is not greater than the last seen {last}" |
| 462 |
)); |
| 463 |
} |
| 464 |
} |
| 465 |
let r = sa.attest.hash(); |
| 466 |
let sig = sa.sigs.first().ok_or("attestation carries no signature")?; |
| 467 |
let id = self |
| 468 |
.store |
| 469 |
.get_identity(&sig.key) |
| 470 |
.map_err(|_| format!("unknown key {}", sig.key.short(12)))?; |
| 471 |
verify_signature(&id, sig, Domain::Attest, &r).map_err(|e| e.to_string())?; |
| 472 |
Ok(*serial) |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
pub fn receive_push( |
| 481 |
&self, |
| 482 |
bundle: Bundle, |
| 483 |
updates: Vec<RefUpdate>, |
| 484 |
policy: Option<SignedPolicy>, |
| 485 |
tags: Vec<crate::protocol::TagRef>, |
| 486 |
) -> Result<Vec<RefResult>> { |
| 487 |
Ok(self |
| 488 |
.receive_push_report(bundle, updates, policy, tags)? |
| 489 |
.results) |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
pub fn receive_push_report( |
| 496 |
&self, |
| 497 |
bundle: Bundle, |
| 498 |
updates: Vec<RefUpdate>, |
| 499 |
policy: Option<SignedPolicy>, |
| 500 |
tags: Vec<crate::protocol::TagRef>, |
| 501 |
) -> Result<PushReport> { |
| 502 |
let mut report = PushReport::default(); |
| 503 |
let results = &mut report.results; |
| 504 |
if let Some(sp) = policy { |
| 505 |
match self.adopt_policy_chain(&bundle.policies, &sp, "push received") { |
| 506 |
Ok(()) => { |
| 507 |
results.push(RefResult { |
| 508 |
name: "policy".into(), |
| 509 |
ok: true, |
| 510 |
reason: format!("serial {}", sp.policy.serial), |
| 511 |
}); |
| 512 |
} |
| 513 |
Err(e) => { |
| 514 |
results.push(RefResult { |
| 515 |
name: "policy".into(), |
| 516 |
ok: false, |
| 517 |
reason: e, |
| 518 |
}); |
| 519 |
for u in updates { |
| 520 |
results.push(RefResult { |
| 521 |
name: u.name, |
| 522 |
ok: false, |
| 523 |
reason: "policy update was rejected".into(), |
| 524 |
}); |
| 525 |
} |
| 526 |
return Ok(report); |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
let policy = self.current_policy()?.map(|(_, sp)| sp.policy); |
| 531 |
match self.import_bundle(&bundle, policy.is_some()) { |
| 532 |
Ok(imported) => report.imported = imported, |
| 533 |
Err(e) => { |
| 534 |
for u in updates { |
| 535 |
results.push(RefResult { |
| 536 |
name: u.name, |
| 537 |
ok: false, |
| 538 |
reason: e.to_string(), |
| 539 |
}); |
| 540 |
} |
| 541 |
return Ok(report); |
| 542 |
} |
| 543 |
} |
| 544 |
let (op_ref, op) = self.head_op()?; |
| 545 |
let mut view = op.view.clone(); |
| 546 |
let mut names = Vec::new(); |
| 547 |
for u in updates { |
| 548 |
let outcome = self.apply_update(&mut view, &u, policy.as_ref()); |
| 549 |
let (ok, reason) = match outcome { |
| 550 |
Ok(()) => (true, String::new()), |
| 551 |
Err(e) => (false, e), |
| 552 |
}; |
| 553 |
if ok { |
| 554 |
names.push(u.name.clone()); |
| 555 |
let heads = self |
| 556 |
.store |
| 557 |
.get_state(&u.new_state) |
| 558 |
.map(|s| s.heads) |
| 559 |
.unwrap_or_default(); |
| 560 |
report.updated.push(RefUpdated { |
| 561 |
name: u.name.clone(), |
| 562 |
old_state: u.old_state, |
| 563 |
new_state: u.new_state, |
| 564 |
heads, |
| 565 |
}); |
| 566 |
} |
| 567 |
results.push(RefResult { |
| 568 |
name: u.name, |
| 569 |
ok, |
| 570 |
reason, |
| 571 |
}); |
| 572 |
} |
| 573 |
for t in &tags { |
| 574 |
let name = format!("tags/{}", t.name); |
| 575 |
match self.adopt_tag(&mut view, t, policy.as_ref()) { |
| 576 |
Ok(()) => { |
| 577 |
names.push(name.clone()); |
| 578 |
results.push(RefResult { |
| 579 |
name, |
| 580 |
ok: true, |
| 581 |
reason: String::new(), |
| 582 |
}); |
| 583 |
} |
| 584 |
Err(e) => results.push(RefResult { |
| 585 |
name, |
| 586 |
ok: false, |
| 587 |
reason: e, |
| 588 |
}), |
| 589 |
} |
| 590 |
} |
| 591 |
if !names.is_empty() { |
| 592 |
self.record_op( |
| 593 |
&[op_ref], |
| 594 |
&format!("push received: {}", names.join(" ")), |
| 595 |
view, |
| 596 |
)?; |
| 597 |
} |
| 598 |
Ok(report) |
| 599 |
} |
| 600 |
|
| 601 |
fn apply_update( |
| 602 |
&self, |
| 603 |
view: &mut View, |
| 604 |
u: &RefUpdate, |
| 605 |
policy: Option<&crate::policy::Policy>, |
| 606 |
) -> std::result::Result<(), String> { |
| 607 |
if u.name == "policy" { |
| 608 |
return Err( |
| 609 |
"the policy reference is updated by sending a policy, not a bookmark update".into(), |
| 610 |
); |
| 611 |
} |
| 612 |
let empty = self |
| 613 |
.store |
| 614 |
.put_state(&StateObject::new(vec![])) |
| 615 |
.map_err(|e| e.to_string())?; |
| 616 |
let current = view.bookmarks.get(&u.name).copied().unwrap_or(empty); |
| 617 |
if current != u.old_state { |
| 618 |
return Err("stale: the remote bookmark has moved; pull first".into()); |
| 619 |
} |
| 620 |
let new_state = self |
| 621 |
.store |
| 622 |
.get_state(&u.new_state) |
| 623 |
.map_err(|_| "new state object is missing".to_string())?; |
| 624 |
for h in &new_state.heads { |
| 625 |
if !self.store.has(h) { |
| 626 |
return Err(format!("head {} is missing", h.short(12))); |
| 627 |
} |
| 628 |
} |
| 629 |
if !u.force { |
| 630 |
let old_heads = self |
| 631 |
.store |
| 632 |
.get_state(¤t) |
| 633 |
.map_err(|e| e.to_string())? |
| 634 |
.heads; |
| 635 |
let closure = self |
| 636 |
.closure_in_store(&new_state.heads) |
| 637 |
.map_err(|e| e.to_string())?; |
| 638 |
if let Some(h) = old_heads.iter().find(|h| !closure.contains(h)) { |
| 639 |
return Err(format!( |
| 640 |
"not a fast-forward: current head {} would be dropped (use --force)", |
| 641 |
h.short(12) |
| 642 |
)); |
| 643 |
} |
| 644 |
} |
| 645 |
if let Some(p) = policy { |
| 646 |
let Some(sa) = &u.attestation else { |
| 647 |
return Err(format!( |
| 648 |
"{:?} requires a signed reference attestation under this repository's policy", |
| 649 |
u.name |
| 650 |
)); |
| 651 |
}; |
| 652 |
let signer = sa |
| 653 |
.sigs |
| 654 |
.first() |
| 655 |
.map(|s| s.key) |
| 656 |
.ok_or("attestation carries no signature")?; |
| 657 |
if !p.authorizes(&signer, &u.name) && !self.delegate_authorized(p, &signer, &u.name) { |
| 658 |
return Err(format!( |
| 659 |
"key {} may not update {:?} under this repository's policy", |
| 660 |
signer.short(12), |
| 661 |
u.name |
| 662 |
)); |
| 663 |
} |
| 664 |
let old_heads = self |
| 665 |
.store |
| 666 |
.get_state(¤t) |
| 667 |
.map_err(|e| e.to_string())? |
| 668 |
.heads; |
| 669 |
self.state_allowed(&old_heads, &new_state.heads, p, &u.name)?; |
| 670 |
} |
| 671 |
if let Some(sa) = &u.attestation { |
| 672 |
let last = self |
| 673 |
.latest_ref_attest(&u.name) |
| 674 |
.map_err(|e| e.to_string())? |
| 675 |
.and_then(|(_, a)| match a.attest.body { |
| 676 |
AttestBody::Ref { serial, .. } => Some(serial), |
| 677 |
_ => None, |
| 678 |
}); |
| 679 |
self.check_ref_attest(sa, &u.name, &u.old_state, &u.new_state, last, false)?; |
| 680 |
let r = self |
| 681 |
.store |
| 682 |
.put_attest(&sa.attest) |
| 683 |
.map_err(|e| e.to_string())?; |
| 684 |
for sig in &sa.sigs { |
| 685 |
self.store |
| 686 |
.put_signature(&r, sig) |
| 687 |
.map_err(|e| e.to_string())?; |
| 688 |
} |
| 689 |
self.set_ref_attest(&u.name, &r) |
| 690 |
.map_err(|e| e.to_string())?; |
| 691 |
} |
| 692 |
view.bookmarks.insert(u.name.clone(), u.new_state); |
| 693 |
Ok(()) |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
fn remote_url(&self, name: &str) -> Result<String> { |
| 699 |
self.config.remotes.get(name).cloned().ok_or_else(|| { |
| 700 |
RepoError::Msg(format!( |
| 701 |
"no remote named {name:?}; add one with `oxy remote add`" |
| 702 |
)) |
| 703 |
}) |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
fn check_remote_id( |
| 712 |
&self, |
| 713 |
remote: &dyn crate::protocol::Remote, |
| 714 |
remote_name: &str, |
| 715 |
allow_unbound: bool, |
| 716 |
) -> Result<()> { |
| 717 |
let theirs = remote.repo_id()?; |
| 718 |
if theirs == self.id()? { |
| 719 |
return Ok(()); |
| 720 |
} |
| 721 |
if theirs == crate::protocol::UNBOUND_ID { |
| 722 |
if allow_unbound { |
| 723 |
return Ok(()); |
| 724 |
} |
| 725 |
return fail(format!( |
| 726 |
"remote {remote_name:?} is empty and unbound: push to it first" |
| 727 |
)); |
| 728 |
} |
| 729 |
fail(format!( |
| 730 |
"remote {remote_name:?} is a different repository (repository IDs differ); check its URL with `oxy remote list`" |
| 731 |
)) |
| 732 |
} |
| 733 |
|
| 734 |
pub(crate) fn remote_options(&self, name: &str) -> RemoteOptions { |
| 735 |
RemoteOptions { |
| 736 |
repo_id: self.id().ok(), |
| 737 |
token: self |
| 738 |
.config |
| 739 |
.remote_tokens |
| 740 |
.get(name) |
| 741 |
.cloned() |
| 742 |
.or_else(|| std::env::var("OXY_TOKEN").ok()), |
| 743 |
ca: self |
| 744 |
.config |
| 745 |
.remote_cas |
| 746 |
.get(name) |
| 747 |
.map(PathBuf::from) |
| 748 |
.or_else(|| std::env::var("OXY_CA").ok().map(PathBuf::from)), |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
pub fn push_seals(&self, remote_name: &str, seals: &[ObjectRef]) -> Result<()> { |
| 755 |
let remote = self.open_named_remote(remote_name)?; |
| 756 |
self.check_remote_id(remote.as_ref(), remote_name, false)?; |
| 757 |
let mut bundle = Bundle::default(); |
| 758 |
for r in seals { |
| 759 |
let attest = self.store.get_attest(r)?; |
| 760 |
let sigs = self.store.signatures(r)?; |
| 761 |
bundle |
| 762 |
.attests |
| 763 |
.push(crate::protocol::SignedAttest { attest, sigs }); |
| 764 |
} |
| 765 |
let results = remote.push(bundle, vec![], None, vec![])?; |
| 766 |
if let Some(r) = results.iter().find(|r| !r.ok) { |
| 767 |
return fail(format!("{}: {}", r.name, r.reason)); |
| 768 |
} |
| 769 |
Ok(()) |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
pub fn remote_for_url(&self, base: &str) -> Option<String> { |
| 774 |
let base = base.trim_end_matches('/'); |
| 775 |
self.config |
| 776 |
.remotes |
| 777 |
.iter() |
| 778 |
.find(|(_, url)| url.trim_end_matches('/') == base) |
| 779 |
.map(|(name, _)| name.clone()) |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
pub fn open_remote_at(&self, url: &str) -> Result<(String, Box<dyn crate::protocol::Remote>)> { |
| 785 |
if let Some(name) = self.remote_for_url(url) { |
| 786 |
return Ok((name.clone(), self.open_named_remote(&name)?)); |
| 787 |
} |
| 788 |
let options = RemoteOptions { |
| 789 |
token: std::env::var("OXY_TOKEN").ok(), |
| 790 |
ca: std::env::var("OXY_CA").ok().map(PathBuf::from), |
| 791 |
repo_id: self.id().ok(), |
| 792 |
}; |
| 793 |
Ok((url.to_string(), open_remote(url, &options)?)) |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
pub fn options_for_url(&self, url: &str) -> RemoteOptions { |
| 799 |
match self.remote_for_url(url) { |
| 800 |
Some(name) => self.remote_options(&name), |
| 801 |
None => RemoteOptions { |
| 802 |
token: std::env::var("OXY_TOKEN").ok(), |
| 803 |
ca: std::env::var("OXY_CA").ok().map(PathBuf::from), |
| 804 |
repo_id: self.id().ok(), |
| 805 |
}, |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
fn open_named_remote(&self, name: &str) -> Result<Box<dyn crate::protocol::Remote>> { |
| 810 |
open_remote(&self.remote_url(name)?, &self.remote_options(name)) |
| 811 |
} |
| 812 |
|
| 813 |
fn serial_path(&self, remote: &str, bookmark: &str) -> PathBuf { |
| 814 |
self.oxy |
| 815 |
.join("remotes") |
| 816 |
.join(remote) |
| 817 |
.join(format!("{bookmark}.serial")) |
| 818 |
} |
| 819 |
|
| 820 |
fn last_seen_serial(&self, remote: &str, bookmark: &str) -> Option<u64> { |
| 821 |
fs::read_to_string(self.serial_path(remote, bookmark)) |
| 822 |
.ok() |
| 823 |
.and_then(|s| s.trim().parse().ok()) |
| 824 |
} |
| 825 |
|
| 826 |
fn record_serial(&self, remote: &str, bookmark: &str, serial: u64) -> Result<()> { |
| 827 |
let p = self.serial_path(remote, bookmark); |
| 828 |
fs::create_dir_all(p.parent().unwrap())?; |
| 829 |
write_atomic(&p, serial.to_string().as_bytes()) |
| 830 |
} |
| 831 |
|
| 832 |
fn policy_serial_path(&self, remote: &str) -> PathBuf { |
| 833 |
self.oxy.join("remotes").join(remote).join("policy.serial") |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
fn remote_policy_serial(&self, remote: &str) -> Option<u64> { |
| 838 |
fs::read_to_string(self.policy_serial_path(remote)) |
| 839 |
.ok() |
| 840 |
.and_then(|s| s.trim().parse().ok()) |
| 841 |
} |
| 842 |
|
| 843 |
fn record_policy_serial(&self, remote: &str, serial: u64) -> Result<()> { |
| 844 |
let p = self.policy_serial_path(remote); |
| 845 |
fs::create_dir_all(p.parent().unwrap())?; |
| 846 |
write_atomic(&p, serial.to_string().as_bytes()) |
| 847 |
} |
| 848 |
|
| 849 |
fn lazy_dir(&self) -> PathBuf { |
| 850 |
self.oxy.join("lazy") |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
fn register_lazy(&self, blob: &ObjectRef, remote: &str) -> Result<()> { |
| 855 |
fs::create_dir_all(self.lazy_dir())?; |
| 856 |
write_atomic(&self.lazy_dir().join(blob.to_hex()), remote.as_bytes()) |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
pub fn lazy_remote(&self, blob: &ObjectRef) -> Option<String> { |
| 861 |
fs::read_to_string(self.lazy_dir().join(blob.to_hex())) |
| 862 |
.ok() |
| 863 |
.map(|s| s.trim().to_string()) |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
pub(crate) fn fetch_lazy_blob(&self, blob: &ObjectRef) -> Result<()> { |
| 868 |
let Some(remote_name) = self.lazy_remote(blob) else { |
| 869 |
return Err(RepoError::Store(StoreError::NotFound(*blob))); |
| 870 |
}; |
| 871 |
let remote = self.open_named_remote(&remote_name)?; |
| 872 |
let objects = remote.objects(&[*blob])?; |
| 873 |
for (ty, body) in objects { |
| 874 |
if matches!(ty, ObjectType::Blob | ObjectType::Chunk) { |
| 875 |
self.store.put(ty, &body)?; |
| 876 |
} |
| 877 |
} |
| 878 |
if !self.store.has(blob) { |
| 879 |
return fail(format!( |
| 880 |
"{remote_name} did not send blob {}", |
| 881 |
blob.short(12) |
| 882 |
)); |
| 883 |
} |
| 884 |
let _ = fs::remove_file(self.lazy_dir().join(blob.to_hex())); |
| 885 |
Ok(()) |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
fn register_missing_blobs(&self, changes: &[ChangeHash], remote: &str) -> Result<usize> { |
| 891 |
let mut n = 0; |
| 892 |
for h in changes { |
| 893 |
let c = self.store.get_change(h)?; |
| 894 |
for e in &c.edits { |
| 895 |
if let Edit::Replace { old, new, .. } = e { |
| 896 |
for b in [old, new].into_iter().flatten() { |
| 897 |
if !self.store.has(b) { |
| 898 |
self.register_lazy(b, remote)?; |
| 899 |
n += 1; |
| 900 |
} |
| 901 |
} |
| 902 |
} |
| 903 |
} |
| 904 |
} |
| 905 |
Ok(n) |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
pub fn known_heads(&self, view: &View) -> Result<Vec<ChangeHash>> { |
| 910 |
let mut have: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 911 |
for st in view |
| 912 |
.bookmarks |
| 913 |
.values() |
| 914 |
.chain(view.remotes.values().flat_map(|b| b.values())) |
| 915 |
{ |
| 916 |
if let Ok(s) = self.store.get_state(st) { |
| 917 |
have.extend(s.heads); |
| 918 |
} |
| 919 |
} |
| 920 |
have.extend(view.workspaces.values().copied()); |
| 921 |
Ok(have.into_iter().collect()) |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
pub fn fetch(&self, remote_name: &str) -> Result<FetchReport> { |
| 926 |
let remote = self.open_named_remote(remote_name)?; |
| 927 |
self.check_remote_id(remote.as_ref(), remote_name, false)?; |
| 928 |
let resp = remote.refs()?; |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
let local_policy = self.current_policy()?.map(|(_, sp)| sp.policy); |
| 935 |
let seen = self.remote_policy_serial(remote_name); |
| 936 |
let mut unverified = false; |
| 937 |
match &resp.policy { |
| 938 |
Some(sp) => { |
| 939 |
if seen.is_some_and(|s| sp.policy.serial < s) { |
| 940 |
return fail(format!("{remote_name} presents policy serial {} but presented {} before: refusing the downgrade", sp.policy.serial, seen.unwrap())); |
| 941 |
} |
| 942 |
match &local_policy { |
| 943 |
Some(l) if sp.policy.serial < l.serial => {} |
| 944 |
Some(l) if sp.policy.serial == l.serial => { |
| 945 |
if sp.policy.hash() != l.hash() { |
| 946 |
return fail(format!( |
| 947 |
"{remote_name} presents a different policy with the same serial {}", |
| 948 |
l.serial |
| 949 |
)); |
| 950 |
} |
| 951 |
} |
| 952 |
_ => { |
| 953 |
self.adopt_policy_chain( |
| 954 |
&resp.history, |
| 955 |
sp, |
| 956 |
&format!("oxy fetch {remote_name}"), |
| 957 |
) |
| 958 |
.map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?; |
| 959 |
} |
| 960 |
} |
| 961 |
self.record_policy_serial(remote_name, sp.policy.serial)?; |
| 962 |
} |
| 963 |
None => { |
| 964 |
if seen.is_some() { |
| 965 |
return fail(format!("{remote_name} presented a policy before but has none now: refusing the downgrade")); |
| 966 |
} |
| 967 |
unverified = true; |
| 968 |
} |
| 969 |
} |
| 970 |
let policy = if unverified { |
| 971 |
None |
| 972 |
} else { |
| 973 |
self.current_policy()?.map(|(_, sp)| sp.policy) |
| 974 |
}; |
| 975 |
let (op_ref, op) = self.head_op()?; |
| 976 |
let mut view = op.view.clone(); |
| 977 |
let have = self.known_heads(&view)?; |
| 978 |
let mut report = Vec::new(); |
| 979 |
let max_blob = self.config.max_blob; |
| 980 |
for r in resp.refs { |
| 981 |
let needed = remote.negotiate(&have, &r.heads)?; |
| 982 |
let bundle = remote.fetch(&needed, max_blob)?; |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
let imported = self.import_bundle(&bundle, false)?; |
| 987 |
if max_blob.is_some() { |
| 988 |
self.register_missing_blobs(&imported, remote_name)?; |
| 989 |
} |
| 990 |
for h in &r.heads { |
| 991 |
if !self.store.has(h) { |
| 992 |
return fail(format!("remote did not send head {}", h.short(12))); |
| 993 |
} |
| 994 |
} |
| 995 |
let state = self.store.put_state(&StateObject::new(r.heads.clone()))?; |
| 996 |
if state != r.state { |
| 997 |
return fail(format!( |
| 998 |
"remote state {} does not match its heads", |
| 999 |
r.state.short(12) |
| 1000 |
)); |
| 1001 |
} |
| 1002 |
if let Some(p) = &policy { |
| 1003 |
let Some(sa) = &r.attestation else { |
| 1004 |
return fail(format!( |
| 1005 |
"{remote_name}/{} has no attestation, which its policy requires", |
| 1006 |
r.name |
| 1007 |
)); |
| 1008 |
}; |
| 1009 |
let signer = sa |
| 1010 |
.sigs |
| 1011 |
.first() |
| 1012 |
.map(|s| s.key) |
| 1013 |
.ok_or_else(|| RepoError::Msg("attestation carries no signature".into()))?; |
| 1014 |
if !p.authorizes(&signer, &r.name) { |
| 1015 |
return fail(format!( |
| 1016 |
"{remote_name}/{}: attested by {}, which the policy does not authorize for this bookmark; a policy owner can grant it with `oxy policy grant`", |
| 1017 |
r.name, |
| 1018 |
signer.short(12) |
| 1019 |
)); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
if let Some(sa) = &r.attestation { |
| 1023 |
let AttestBody::Ref { |
| 1024 |
old_state, |
| 1025 |
new_state, |
| 1026 |
.. |
| 1027 |
} = &sa.attest.body |
| 1028 |
else { |
| 1029 |
return fail("remote sent a non-ref attestation for a bookmark"); |
| 1030 |
}; |
| 1031 |
let (old_state, new_state) = (*old_state, *new_state); |
| 1032 |
let serial = self |
| 1033 |
.check_ref_attest( |
| 1034 |
sa, |
| 1035 |
&r.name, |
| 1036 |
&old_state, |
| 1037 |
&new_state, |
| 1038 |
self.last_seen_serial(remote_name, &r.name), |
| 1039 |
true, |
| 1040 |
) |
| 1041 |
.map_err(|e| { |
| 1042 |
RepoError::Msg(format!("attestation for {}/{}: {e}", remote_name, r.name)) |
| 1043 |
})?; |
| 1044 |
if new_state != r.state { |
| 1045 |
return fail(format!( |
| 1046 |
"attestation for {}/{} does not cover its current state", |
| 1047 |
remote_name, r.name |
| 1048 |
)); |
| 1049 |
} |
| 1050 |
let ar = self.store.put_attest(&sa.attest)?; |
| 1051 |
for sig in &sa.sigs { |
| 1052 |
self.store.put_signature(&ar, sig)?; |
| 1053 |
} |
| 1054 |
self.record_serial(remote_name, &r.name, serial)?; |
| 1055 |
} |
| 1056 |
view.remotes |
| 1057 |
.entry(remote_name.to_string()) |
| 1058 |
.or_default() |
| 1059 |
.insert(r.name.clone(), state); |
| 1060 |
report.push((r.name, r.heads, imported.len())); |
| 1061 |
} |
| 1062 |
let mut new_tags = 0; |
| 1063 |
for t in &resp.tags { |
| 1064 |
if view.tags.contains_key(&t.name) { |
| 1065 |
continue; |
| 1066 |
} |
| 1067 |
|
| 1068 |
let missing: Vec<ChangeHash> = t |
| 1069 |
.heads |
| 1070 |
.iter() |
| 1071 |
.filter(|h| !self.store.has(h)) |
| 1072 |
.copied() |
| 1073 |
.collect(); |
| 1074 |
if !missing.is_empty() { |
| 1075 |
let needed = remote.negotiate(&self.known_heads(&view)?, &missing)?; |
| 1076 |
let bundle = remote.fetch(&needed, max_blob)?; |
| 1077 |
self.import_bundle(&bundle, false)?; |
| 1078 |
} |
| 1079 |
self.store.put_state(&StateObject::new(t.heads.clone()))?; |
| 1080 |
self.adopt_tag(&mut view, t, policy.as_ref()) |
| 1081 |
.map_err(|e| RepoError::Msg(format!("{remote_name} {}: {e}", t.name)))?; |
| 1082 |
new_tags += 1; |
| 1083 |
} |
| 1084 |
if view != op.view { |
| 1085 |
self.record_op(&[op_ref], &format!("oxy fetch {remote_name}"), view)?; |
| 1086 |
} |
| 1087 |
let _ = self.run_hook( |
| 1088 |
"post-fetch", |
| 1089 |
&format!( |
| 1090 |
"{{\"event\":\"post-fetch\",\"remote\":{},\"bookmarks\":{}}}", |
| 1091 |
json_str(remote_name), |
| 1092 |
json_array(report.iter().map(|(n, heads, _)| format!( |
| 1093 |
"{{\"name\":{},\"heads\":{}}}", |
| 1094 |
json_str(n), |
| 1095 |
json_array(heads.iter().map(|h| json_str(&h.to_hex()))) |
| 1096 |
))) |
| 1097 |
), |
| 1098 |
); |
| 1099 |
Ok(FetchReport { |
| 1100 |
refs: report, |
| 1101 |
unverified, |
| 1102 |
new_tags, |
| 1103 |
}) |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
pub fn fetch_shallow(&self, remote_name: &str, depth: u32) -> Result<usize> { |
| 1109 |
let remote = self.open_named_remote(remote_name)?; |
| 1110 |
self.check_remote_id(remote.as_ref(), remote_name, false)?; |
| 1111 |
let resp = remote.refs()?; |
| 1112 |
let bookmark = self.config.bookmark.clone(); |
| 1113 |
if let Some(sp) = &resp.policy { |
| 1114 |
self.adopt_policy_chain(&resp.history, sp, "oxy clone --depth") |
| 1115 |
.map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?; |
| 1116 |
self.record_policy_serial(remote_name, sp.policy.serial)?; |
| 1117 |
} |
| 1118 |
let Some(r) = resp.refs.into_iter().find(|r| r.name == bookmark) else { |
| 1119 |
return fail(format!("{remote_name} has no bookmark {bookmark:?}")); |
| 1120 |
}; |
| 1121 |
if r.heads.is_empty() { |
| 1122 |
return Ok(0); |
| 1123 |
} |
| 1124 |
|
| 1125 |
let cp_bundle = remote.checkpoint(&r.heads)?; |
| 1126 |
let mut cp_ref = None; |
| 1127 |
for (ty, body) in &cp_bundle.objects { |
| 1128 |
let put = self.store.put(*ty, body)?; |
| 1129 |
if *ty == ObjectType::Checkpoint { |
| 1130 |
cp_ref = Some(put); |
| 1131 |
} |
| 1132 |
} |
| 1133 |
let Some(cp_ref) = cp_ref else { |
| 1134 |
return fail(format!("{remote_name} sent no checkpoint")); |
| 1135 |
}; |
| 1136 |
self.install_checkpoint(&bookmark, &cp_ref)?; |
| 1137 |
|
| 1138 |
let wanted = remote.shallow(&r.heads, depth)?; |
| 1139 |
let bundle = remote.fetch(&wanted, self.config.max_blob)?; |
| 1140 |
let imported = self.import_bundle_shallow(&bundle, false)?; |
| 1141 |
if self.config.max_blob.is_some() { |
| 1142 |
self.register_missing_blobs(&imported, remote_name)?; |
| 1143 |
} |
| 1144 |
self.refresh_boundary(&imported)?; |
| 1145 |
if let Some(sa) = &r.attestation { |
| 1146 |
let ar = self.store.put_attest(&sa.attest)?; |
| 1147 |
for sig in &sa.sigs { |
| 1148 |
self.store.put_signature(&ar, sig)?; |
| 1149 |
} |
| 1150 |
if let AttestBody::Ref { serial, .. } = sa.attest.body { |
| 1151 |
self.record_serial(remote_name, &r.name, serial)?; |
| 1152 |
} |
| 1153 |
} |
| 1154 |
let state = self.store.put_state(&StateObject::new(r.heads.clone()))?; |
| 1155 |
let (op_ref, op) = self.head_op()?; |
| 1156 |
let mut view = op.view.clone(); |
| 1157 |
view.remotes |
| 1158 |
.entry(remote_name.to_string()) |
| 1159 |
.or_default() |
| 1160 |
.insert(r.name.clone(), state); |
| 1161 |
self.record_op( |
| 1162 |
&[op_ref], |
| 1163 |
&format!("oxy fetch {remote_name} --depth {depth}"), |
| 1164 |
view, |
| 1165 |
)?; |
| 1166 |
Ok(imported.len()) |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
pub fn pull(&self, remote_name: &str) -> Result<PullReport> { |
| 1172 |
self.fetch(remote_name)?; |
| 1173 |
let (op_ref, op) = self.head_op()?; |
| 1174 |
let bookmark = self.config.bookmark.clone(); |
| 1175 |
let local = self.heads_of(&op.view, &bookmark)?; |
| 1176 |
let Some(remote_state) = op |
| 1177 |
.view |
| 1178 |
.remotes |
| 1179 |
.get(remote_name) |
| 1180 |
.and_then(|b| b.get(&bookmark)) |
| 1181 |
.copied() |
| 1182 |
else { |
| 1183 |
return Ok(PullReport { |
| 1184 |
heads: local, |
| 1185 |
changed: false, |
| 1186 |
checked_out: false, |
| 1187 |
}); |
| 1188 |
}; |
| 1189 |
let remote_heads = self.store.get_state(&remote_state)?.heads; |
| 1190 |
let mut all = local.clone(); |
| 1191 |
all.extend(remote_heads); |
| 1192 |
let st = self.load_state(&all)?; |
| 1193 |
let mut heads = st.maximal(&all); |
| 1194 |
heads.sort(); |
| 1195 |
let mut sorted_local = local.clone(); |
| 1196 |
sorted_local.sort(); |
| 1197 |
if heads == sorted_local { |
| 1198 |
return Ok(PullReport { |
| 1199 |
heads, |
| 1200 |
changed: false, |
| 1201 |
checked_out: false, |
| 1202 |
}); |
| 1203 |
} |
| 1204 |
let mut view = op.view.clone(); |
| 1205 |
view.bookmarks.insert( |
| 1206 |
bookmark.clone(), |
| 1207 |
self.store.put_state(&StateObject::new(heads.clone()))?, |
| 1208 |
); |
| 1209 |
self.record_op(&[op_ref], &format!("oxy pull {remote_name}"), view)?; |
| 1210 |
|
| 1211 |
let snap = self.snapshot("oxy pull")?; |
| 1212 |
let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty(); |
| 1213 |
let mut base = snap.base.maximal(&snap.wc.deps); |
| 1214 |
base.sort(); |
| 1215 |
let mut checked_out = false; |
| 1216 |
if clean && base == sorted_local { |
| 1217 |
self.checkout(&heads, false, "oxy pull (checkout)")?; |
| 1218 |
checked_out = true; |
| 1219 |
} |
| 1220 |
Ok(PullReport { |
| 1221 |
heads, |
| 1222 |
changed: true, |
| 1223 |
checked_out, |
| 1224 |
}) |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
pub fn push(&self, remote_name: &str, force: bool) -> Result<Vec<RefResult>> { |
| 1230 |
let bookmark = self.config.bookmark.clone(); |
| 1231 |
self.push_bookmark(remote_name, &bookmark, force) |
| 1232 |
} |
| 1233 |
|
| 1234 |
|
| 1235 |
pub fn push_bookmark( |
| 1236 |
&self, |
| 1237 |
remote_name: &str, |
| 1238 |
bookmark: &str, |
| 1239 |
force: bool, |
| 1240 |
) -> Result<Vec<RefResult>> { |
| 1241 |
let (op_ref, op) = self.head_op()?; |
| 1242 |
if !op.view.bookmarks.contains_key(bookmark) { |
| 1243 |
return fail(format!("no bookmark {bookmark}")); |
| 1244 |
} |
| 1245 |
let bookmark = bookmark.to_string(); |
| 1246 |
let local_heads = self.heads_of(&op.view, &bookmark)?; |
| 1247 |
|
| 1248 |
self.run_hook( |
| 1249 |
"pre-push", |
| 1250 |
&format!( |
| 1251 |
"{{\"event\":\"pre-push\",\"remote\":{},\"bookmark\":{},\"heads\":{}}}", |
| 1252 |
json_str(remote_name), |
| 1253 |
json_str(&bookmark), |
| 1254 |
json_array(local_heads.iter().map(|h| json_str(&h.to_hex()))) |
| 1255 |
), |
| 1256 |
)?; |
| 1257 |
let remote = self.open_named_remote(remote_name)?; |
| 1258 |
self.check_remote_id(remote.as_ref(), remote_name, true)?; |
| 1259 |
let resp = remote.refs()?; |
| 1260 |
let local_policy = self.current_policy()?.map(|(_, sp)| sp); |
| 1261 |
let resp_policy_serial = resp.policy.as_ref().map(|p| p.policy.serial); |
| 1262 |
let policy_to_send = match (&local_policy, &resp.policy) { |
| 1263 |
(Some(l), Some(r)) if l.policy.serial > r.policy.serial => Some(l.clone()), |
| 1264 |
(Some(l), None) => Some(l.clone()), |
| 1265 |
(Some(l), Some(r)) if l.policy.serial < r.policy.serial => { |
| 1266 |
return fail(format!( |
| 1267 |
"{remote_name} has a newer policy (serial {}); fetch first", |
| 1268 |
r.policy.serial |
| 1269 |
)); |
| 1270 |
} |
| 1271 |
_ => None, |
| 1272 |
}; |
| 1273 |
let remote_has_policy = resp.policy.is_some() || policy_to_send.is_some(); |
| 1274 |
if remote_has_policy && self.secret_key()?.is_none() { |
| 1275 |
return fail(format!("{remote_name} enforces a trust policy; pushing needs an identity (`oxy identity create`)")); |
| 1276 |
} |
| 1277 |
let remote_tags: BTreeSet<String> = resp.tags.iter().map(|t| t.name.clone()).collect(); |
| 1278 |
let tags_to_send: Vec<crate::protocol::TagRef> = self |
| 1279 |
.tag_refs()? |
| 1280 |
.into_iter() |
| 1281 |
.filter(|t| !remote_tags.contains(&t.name)) |
| 1282 |
.collect(); |
| 1283 |
let refs = resp.refs; |
| 1284 |
let empty = self.store.put_state(&StateObject::new(vec![]))?; |
| 1285 |
let (remote_state, remote_heads, last_serial) = |
| 1286 |
match refs.iter().find(|r| r.name == bookmark) { |
| 1287 |
Some(r) => (r.state, r.heads.clone(), r.last_serial), |
| 1288 |
None => (empty, vec![], None), |
| 1289 |
}; |
| 1290 |
let new_state = self |
| 1291 |
.store |
| 1292 |
.put_state(&StateObject::new(local_heads.clone()))?; |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
if new_state == remote_state && policy_to_send.is_none() && tags_to_send.is_empty() { |
| 1298 |
return Ok(vec![RefResult { |
| 1299 |
name: bookmark, |
| 1300 |
ok: true, |
| 1301 |
reason: "already up to date".into(), |
| 1302 |
}]); |
| 1303 |
} |
| 1304 |
for h in &remote_heads { |
| 1305 |
if !self.store.has(h) { |
| 1306 |
return fail(format!( |
| 1307 |
"the remote has change {} which you do not; pull first (`oxy pull {remote_name}`), then push again", |
| 1308 |
h.short(12) |
| 1309 |
)); |
| 1310 |
} |
| 1311 |
} |
| 1312 |
if !force { |
| 1313 |
let closure = self.closure_in_store(&local_heads)?; |
| 1314 |
if let Some(h) = remote_heads.iter().find(|h| !closure.contains(h)) { |
| 1315 |
return fail(format!("not a fast-forward: remote head {} is not in your state; pull first or use --force", h.short(12))); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
let to_send = self.negotiate(&remote_heads, &local_heads)?; |
| 1319 |
let mut bundle = self.bundle_for(&to_send)?; |
| 1320 |
let key = self.secret_key()?.map(|(k, _)| k); |
| 1321 |
if let Some(key) = key { |
| 1322 |
|
| 1323 |
|
| 1324 |
let mut unsigned = Vec::new(); |
| 1325 |
for h in &to_send { |
| 1326 |
let c = self.store.get_change(h)?; |
| 1327 |
if (c.author == key || c.author.is_zero()) |
| 1328 |
&& !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) |
| 1329 |
{ |
| 1330 |
unsigned.push(*h); |
| 1331 |
} |
| 1332 |
} |
| 1333 |
for chunk in unsigned.chunks(crate::attest::MAX_SEAL_HASHES) { |
| 1334 |
let sa = self.sign_attest(&Attest::seal(self.id()?, chunk.to_vec()))?; |
| 1335 |
bundle.attests.push(sa); |
| 1336 |
} |
| 1337 |
bundle = self.bundle_for(&to_send)?; |
| 1338 |
} |
| 1339 |
bundle.objects.push(( |
| 1340 |
ObjectType::State, |
| 1341 |
StateObject::new(local_heads.clone()).encode(), |
| 1342 |
)); |
| 1343 |
let attestation = match key { |
| 1344 |
Some(_) => Some(self.sign_attest(&Attest { |
| 1345 |
repo: self.id()?, |
| 1346 |
hlc: Hlc::now(), |
| 1347 |
body: AttestBody::Ref { |
| 1348 |
name: bookmark.clone(), |
| 1349 |
old_state: remote_state, |
| 1350 |
new_state, |
| 1351 |
serial: last_serial.unwrap_or(0) + 1, |
| 1352 |
}, |
| 1353 |
})?), |
| 1354 |
None => None, |
| 1355 |
}; |
| 1356 |
let serial = attestation.as_ref().and_then(|a| match a.attest.body { |
| 1357 |
AttestBody::Ref { serial, .. } => Some(serial), |
| 1358 |
_ => None, |
| 1359 |
}); |
| 1360 |
let sent_serial = policy_to_send.as_ref().map(|p| p.policy.serial); |
| 1361 |
|
| 1362 |
for t in &tags_to_send { |
| 1363 |
let extra = self.negotiate(&remote_heads, &t.heads)?; |
| 1364 |
let extra: Vec<ChangeHash> = |
| 1365 |
extra.into_iter().filter(|h| !to_send.contains(h)).collect(); |
| 1366 |
if !extra.is_empty() { |
| 1367 |
let more = self.bundle_for(&extra)?; |
| 1368 |
bundle.changes.extend(more.changes); |
| 1369 |
bundle.attests.extend(more.attests); |
| 1370 |
bundle.objects.extend(more.objects); |
| 1371 |
} |
| 1372 |
bundle.objects.push(( |
| 1373 |
ObjectType::State, |
| 1374 |
StateObject::new(t.heads.clone()).encode(), |
| 1375 |
)); |
| 1376 |
} |
| 1377 |
if let Some(l) = &policy_to_send { |
| 1378 |
let remote_serial = resp_policy_serial; |
| 1379 |
bundle.policies = self |
| 1380 |
.policy_history()? |
| 1381 |
.into_iter() |
| 1382 |
.filter(|sp| { |
| 1383 |
remote_serial.is_none_or(|r| sp.policy.serial > r) |
| 1384 |
&& sp.policy.serial < l.policy.serial |
| 1385 |
}) |
| 1386 |
.collect(); |
| 1387 |
} |
| 1388 |
let results = remote.push( |
| 1389 |
bundle, |
| 1390 |
vec![RefUpdate { |
| 1391 |
name: bookmark.clone(), |
| 1392 |
old_state: remote_state, |
| 1393 |
new_state, |
| 1394 |
force, |
| 1395 |
attestation, |
| 1396 |
}], |
| 1397 |
policy_to_send, |
| 1398 |
tags_to_send, |
| 1399 |
)?; |
| 1400 |
if let (Some(serial), true) = ( |
| 1401 |
sent_serial, |
| 1402 |
results.iter().any(|r| r.ok && r.name == "policy"), |
| 1403 |
) { |
| 1404 |
self.record_policy_serial(remote_name, serial)?; |
| 1405 |
} |
| 1406 |
if results.iter().any(|r| r.ok && r.name == bookmark) { |
| 1407 |
let mut view = op.view.clone(); |
| 1408 |
view.remotes |
| 1409 |
.entry(remote_name.to_string()) |
| 1410 |
.or_default() |
| 1411 |
.insert(bookmark.clone(), new_state); |
| 1412 |
self.record_op(&[op_ref], &format!("oxy push {remote_name}"), view)?; |
| 1413 |
if let Some(s) = serial { |
| 1414 |
self.record_serial(remote_name, &bookmark, s)?; |
| 1415 |
} |
| 1416 |
} |
| 1417 |
Ok(results) |
| 1418 |
} |
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
pub fn clone(url: &str, dest: &Path) -> Result<Repo> { |
| 1423 |
Self::clone_with(url, dest, None, &[]) |
| 1424 |
} |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
pub fn clone_with( |
| 1430 |
url: &str, |
| 1431 |
dest: &Path, |
| 1432 |
max_blob: Option<u64>, |
| 1433 |
sparse: &[String], |
| 1434 |
) -> Result<Repo> { |
| 1435 |
Self::clone_full(url, dest, max_blob, sparse, None) |
| 1436 |
} |
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
pub fn clone_shallow( |
| 1441 |
url: &str, |
| 1442 |
dest: &Path, |
| 1443 |
depth: u32, |
| 1444 |
max_blob: Option<u64>, |
| 1445 |
sparse: &[String], |
| 1446 |
) -> Result<Repo> { |
| 1447 |
Self::clone_full(url, dest, max_blob, sparse, Some(depth)) |
| 1448 |
} |
| 1449 |
|
| 1450 |
fn clone_full( |
| 1451 |
url: &str, |
| 1452 |
dest: &Path, |
| 1453 |
max_blob: Option<u64>, |
| 1454 |
sparse: &[String], |
| 1455 |
depth: Option<u32>, |
| 1456 |
) -> Result<Repo> { |
| 1457 |
let options = RemoteOptions { |
| 1458 |
token: std::env::var("OXY_TOKEN").ok(), |
| 1459 |
ca: std::env::var("OXY_CA").ok().map(PathBuf::from), |
| 1460 |
repo_id: None, |
| 1461 |
}; |
| 1462 |
let remote = open_remote(url, &options)?; |
| 1463 |
let id = remote.repo_id()?; |
| 1464 |
if id == crate::protocol::UNBOUND_ID { |
| 1465 |
return fail(format!( |
| 1466 |
"{url} is empty and unbound: push a repository to it first" |
| 1467 |
)); |
| 1468 |
} |
| 1469 |
let mut repo = Repo::init_with_id(dest, id)?; |
| 1470 |
repo.config.set("remote.origin.url", url)?; |
| 1471 |
if let Some(m) = max_blob { |
| 1472 |
repo.config.set("filter.max-blob", &m.to_string())?; |
| 1473 |
} else if !sparse.is_empty() { |
| 1474 |
repo.config.set("filter.max-blob", "0")?; |
| 1475 |
} |
| 1476 |
if !sparse.is_empty() { |
| 1477 |
repo.config.set("filter.paths", &sparse.join(","))?; |
| 1478 |
} |
| 1479 |
repo.save_config()?; |
| 1480 |
if let Some(depth) = depth { |
| 1481 |
repo.fetch_shallow("origin", depth)?; |
| 1482 |
} else { |
| 1483 |
repo.fetch("origin")?; |
| 1484 |
} |
| 1485 |
let (op_ref, op) = repo.head_op()?; |
| 1486 |
let bookmark = repo.config.bookmark.clone(); |
| 1487 |
if let Some(state) = op |
| 1488 |
.view |
| 1489 |
.remotes |
| 1490 |
.get("origin") |
| 1491 |
.and_then(|b| b.get(&bookmark)) |
| 1492 |
.copied() |
| 1493 |
{ |
| 1494 |
let heads = repo.store.get_state(&state)?.heads; |
| 1495 |
let mut view = op.view.clone(); |
| 1496 |
view.bookmarks.insert(bookmark, state); |
| 1497 |
repo.record_op(&[op_ref], &format!("oxy clone {url}"), view)?; |
| 1498 |
repo.checkout(&heads, false, "oxy clone (checkout)")?; |
| 1499 |
} |
| 1500 |
Ok(repo) |
| 1501 |
} |
| 1502 |
} |