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