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