OXYGENHUB
◆ v0.6.0 oxygen / crates / oxy-core / src / repo / sync.rs
117.5 KiB · Rust · last changed Depth 0 takes no change, and a checkpoint is gathered in parallel (refs #137) 10 days ago source blame history raw
1
//! Serving and talking to remotes (spec §10.2, §16).
2
3
use super::tags::TagCheck;
4
use super::*;
5
6
/// What a push did, for hosts (hub spec §6.12).
7
#[derive(Clone, Debug, Default)]
8
pub struct PushReport {
9
    pub results: Vec<RefResult>,
10
    /// Accepted reference updates, in request order.
11
    pub updated: Vec<RefUpdated>,
12
    /// Changes this push stored that were not already present.
13
    pub imported: Vec<ChangeHash>,
14
}
15
16
#[derive(Clone, Debug)]
17
pub struct RefUpdated {
18
    pub name: String,
19
    pub old_state: ObjectRef,
20
    pub new_state: ObjectRef,
21
    pub heads: Vec<ChangeHash>,
22
}
23
24
/// What a fetch brought in.
25
#[derive(Debug, Clone, PartialEq, Eq, Default)]
26
pub struct FetchReport {
27
    /// Bookmark name, its heads, and how many changes were new.
28
    pub refs: Vec<(String, Vec<ChangeHash>, usize)>,
29
    /// Tags adopted from the remote.
30
    pub new_tags: usize,
31
    /// Tags the remote withdrew, and so withdrawn here (oxygenhub#301).
32
    pub withdrawn_tags: Vec<String>,
33
    /// The remote has never presented a policy, so nothing was verified
34
    /// against one (spec §14.5: warn loudly).
35
    pub unverified: bool,
36
    /// What checking the host's transparency log found short of a fork
37
    /// (spec §14.8): attestations not logged yet, a log that could not be
38
    /// read. A fork fails the fetch instead.
39
    pub log_notes: Vec<String>,
40
    /// Bookmarks left as they were because their attestation could not be
41
    /// verified (its signer unknown here, or its signature bad), each with
42
    /// why (oxygenhub#266). The current bookmark is never skipped: its
43
    /// fetch fails instead.
44
    pub skipped: Vec<(String, String)>,
45
}
46
47
#[derive(Debug, Clone, PartialEq, Eq)]
48
pub struct PullReport {
49
    /// The current bookmark's heads after the pull.
50
    pub heads: Vec<ChangeHash>,
51
    /// Whether the current bookmark changed.
52
    pub changed: bool,
53
    pub checked_out: bool,
54
    /// Other bookmarks the pull moved: each local bookmark tracks the
55
    /// remote's of the same name.
56
    pub others: Vec<String>,
57
    /// What the fetch left as it was ([`FetchReport::skipped`]).
58
    pub skipped: Vec<(String, String)>,
59
}
60
61
/// The result a push of objects alone reports (a batch of a long history):
62
/// not a reference.
63
pub const OBJECTS_RESULT: &str = "objects";
64
65
impl Repo {
66
    // ------------------------------------------------------------ serving (spec §16)
67
68
    fn ref_attest_path(&self, bookmark: &str) -> PathBuf {
69
        self.oxy.join("attest").join(name_file(bookmark))
70
    }
71
72
    /// The latest reference attestation stored for a bookmark.
73
    pub fn latest_ref_attest(&self, bookmark: &str) -> Result<Option<(ObjectRef, SignedAttest)>> {
74
        match fs::read_to_string(self.ref_attest_path(bookmark)) {
75
            Ok(h) => {
76
                let r = ObjectRef::from_hex(h.trim())
77
                    .ok_or_else(|| RepoError::Msg("corrupt attestation pointer".into()))?;
78
                Ok(Some((
79
                    r,
80
                    SignedAttest {
81
                        attest: self.store.get_attest(&r)?,
82
                        sigs: self.store.signatures(&r)?,
83
                    },
84
                )))
85
            }
86
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
87
            Err(e) => Err(e.into()),
88
        }
89
    }
90
91
    pub(crate) fn set_ref_attest(&self, bookmark: &str, r: &ObjectRef) -> Result<()> {
92
        fs::create_dir_all(self.oxy.join("attest"))?;
93
        write_atomic(&self.ref_attest_path(bookmark), r.to_hex().as_bytes())
94
    }
95
96
    pub fn remote_refs(&self) -> Result<RefsResponse> {
97
        let view = self.view()?;
98
        let mut out = Vec::new();
99
        for (name, state) in &view.bookmarks {
100
            let latest = self.latest_ref_attest(name)?.map(|(_, a)| a);
101
            let last_serial = latest.as_ref().and_then(|a| match a.attest.body {
102
                AttestBody::Ref { serial, .. } => Some(serial),
103
                _ => None,
104
            });
105
            let attestation = latest.filter(|a| matches!(&a.attest.body, AttestBody::Ref { new_state, .. } if new_state == state));
106
            let accepted = match &attestation {
107
                Some(a) => self.acceptance_of(&a.attest.hash())?,
108
                None => None,
109
            };
110
            out.push(RemoteRef {
111
                name: name.clone(),
112
                state: *state,
113
                heads: self.store.get_state(state)?.heads,
114
                attestation,
115
                last_serial,
116
                accepted,
117
                logged: None,
118
                integration: vec![],
119
            });
120
        }
121
        let policy = self.current_policy()?.map(|(_, sp)| sp);
122
        let history = self
123
            .policy_history()?
124
            .into_iter()
125
            .filter(|sp| {
126
                policy
127
                    .as_ref()
128
                    .is_some_and(|p| sp.policy.serial < p.policy.serial)
129
            })
130
            .collect();
131
        Ok(RefsResponse {
132
            refs: out,
133
            policy,
134
            tags: self.tag_refs()?,
135
            history,
136
            default_bookmark: Some(self.config.bookmark.clone()),
137
            log: None,
138
            witnesses: vec![],
139
            withdrawn: self.withdrawn_refs()?,
140
        })
141
    }
142
143
    /// Every change reachable from `heads` that is in the store.
144
    fn closure_in_store(&self, heads: &[ChangeHash]) -> Result<BTreeSet<ChangeHash>> {
145
        let mut seen = BTreeSet::new();
146
        let mut stack: Vec<ChangeHash> = heads.to_vec();
147
        while let Some(h) = stack.pop() {
148
            if seen.contains(&h) || !self.store.has(&h) {
149
                continue;
150
            }
151
            seen.insert(h);
152
            stack.extend(self.store.get_change_meta(&h)?.deps);
153
        }
154
        Ok(seen)
155
    }
156
157
    /// The changes within `depth` dependency steps of `heads` (spec §17),
158
    /// nearest first; depth 1 is the heads themselves.
159
    pub fn shallow_changes(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> {
160
        let mut out = Vec::new();
161
        let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
162
        let mut layer: Vec<ChangeHash> = heads.to_vec();
163
        for _ in 0..depth.max(1) {
164
            let mut next = Vec::new();
165
            for h in layer {
166
                if !seen.insert(h) || !self.store.has(&h) {
167
                    continue;
168
                }
169
                let c = self.store.get_change(&h)?;
170
                next.extend(c.deps.iter().copied());
171
                out.push(h);
172
            }
173
            if next.is_empty() {
174
                break;
175
            }
176
            layer = next;
177
        }
178
        out.reverse();
179
        Ok(out)
180
    }
181
182
    fn shallow_path(&self) -> PathBuf {
183
        self.oxy.join("shallow")
184
    }
185
186
    /// The shallow boundary: dependencies this repository does not hold (spec §17).
187
    pub fn boundary(&self) -> BTreeSet<ChangeHash> {
188
        fs::read_to_string(self.shallow_path())
189
            .map(|t| t.lines().filter_map(ObjectRef::from_hex).collect())
190
            .unwrap_or_default()
191
    }
192
193
    pub fn is_shallow(&self) -> bool {
194
        !self.boundary().is_empty()
195
    }
196
197
    fn write_boundary(&self, boundary: &BTreeSet<ChangeHash>) -> Result<()> {
198
        if boundary.is_empty() {
199
            let _ = fs::remove_file(self.shallow_path());
200
            return Ok(());
201
        }
202
        let text: String = boundary
203
            .iter()
204
            .map(|h| format!("{}\n", h.to_hex()))
205
            .collect();
206
        write_atomic(&self.shallow_path(), text.as_bytes())
207
    }
208
209
    /// Recompute the boundary from what is stored: every dependency of a
210
    /// stored change that is not itself stored.
211
    fn refresh_boundary(&self, imported: &[ChangeHash]) -> Result<()> {
212
        let mut boundary = self.boundary();
213
        boundary.retain(|h| !self.store.has(h));
214
        for h in imported {
215
            for d in self.store.get_change_meta(h)?.deps {
216
                if !self.store.has(&d) {
217
                    boundary.insert(d);
218
                }
219
            }
220
        }
221
        self.write_boundary(&boundary)
222
    }
223
224
    /// Fetch `depth` more levels of history below the shallow boundary.
225
    pub fn deepen(&self, remote_name: &str, depth: u32) -> Result<usize> {
226
        let boundary = self.boundary();
227
        if boundary.is_empty() {
228
            return Ok(0);
229
        }
230
        let remote = self.open_named_remote(remote_name)?;
231
        let wanted = remote.shallow(&boundary.iter().copied().collect::<Vec<_>>(), depth)?;
232
        let bundle = remote.fetch(&wanted, self.config.max_blob)?;
233
        let imported = self.import_bundle_shallow(&bundle, false)?;
234
        if self.config.max_blob.is_some() {
235
            self.register_missing_blobs(&imported, remote_name)?;
236
        }
237
        self.refresh_boundary(&imported)?;
238
        Ok(imported.len())
239
    }
240
241
    /// `closure(want) \ closure(have)`, dependencies first (spec §16.2).
242
    pub fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> {
243
        let have = self.closure_in_store(have)?;
244
        // In a shallow repository the walk stops at the boundary, whose
245
        // changes the remote is assumed to hold.
246
        let boundary = self.boundary();
247
        let mut out = Vec::new();
248
        let mut done: BTreeSet<ChangeHash> = have.clone();
249
        done.extend(boundary.iter().copied());
250
        // Iterative post-order DFS.
251
        let shallow = self.is_shallow();
252
        let mut stack: Vec<(ChangeHash, bool)> = want.iter().rev().map(|h| (*h, false)).collect();
253
        while let Some((h, expanded)) = stack.pop() {
254
            if done.contains(&h) {
255
                continue;
256
            }
257
            if !self.store.has(&h) {
258
                // A shallow repository holds only part of its history; a
259
                // change it lacks is below what it cloned (in a depth-0
260
                // clone, any change of the checkpoint), which the remote
261
                // is assumed to hold, as it is for the boundary (#137).
262
                if shallow {
263
                    done.insert(h);
264
                    continue;
265
                }
266
                return fail(format!("change {} is not in this repository", h.short(12)));
267
            }
268
            if expanded {
269
                done.insert(h);
270
                out.push(h);
271
                continue;
272
            }
273
            stack.push((h, true));
274
            for d in self.store.get_change_meta(&h)?.deps {
275
                if !done.contains(&d) {
276
                    stack.push((d, false));
277
                }
278
            }
279
        }
280
        Ok(out)
281
    }
282
283
    /// Bundle changes (in the given dependency order) with their signatures,
284
    /// seals, identities, blobs, chunks.
285
    pub fn bundle_for(&self, changes: &[ChangeHash]) -> Result<Bundle> {
286
        self.bundle_of(changes, None, false, false)
287
    }
288
289
    /// Objects by reference for the `objects` endpoint; a blob brings its chunks.
290
    pub fn objects_for(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> {
291
        let mut out = Vec::new();
292
        for r in refs {
293
            let (ty, body) = match self.store.get(r) {
294
                Ok(x) => x,
295
                Err(StoreError::NotFound(_)) => continue,
296
                Err(e) => return Err(e.into()),
297
            };
298
            if ty == ObjectType::Blob {
299
                for c in Blob::decode(&body)?.chunks {
300
                    if let Ok((cty, cb)) = self.store.get(&c) {
301
                        out.push((cty, cb));
302
                    }
303
                }
304
            }
305
            out.push((ty, body));
306
        }
307
        Ok(out)
308
    }
309
310
    /// Like `bundle_for`, leaving out blobs larger than `max_blob` bytes
311
    /// (and their chunks) so the receiver fetches them lazily (spec §13.5).
312
    ///
313
    /// This is what a server answers a fetch with, so it also carries what
314
    /// verifies the attestations it serves beside it (see
315
    /// [`Repo::served_attest_signers`]), also when `changes` is empty.
316
    pub fn bundle_for_filtered(
317
        &self,
318
        changes: &[ChangeHash],
319
        max_blob: Option<u64>,
320
    ) -> Result<Bundle> {
321
        self.bundle_of(changes, max_blob, false, true)
322
    }
323
324
    /// Like [`Repo::bundle_for_filtered`] for a fetch between repositories
325
    /// on one machine: changes as they are stored ([`Bundle::raw_changes`]),
326
    /// with their text blobs and chunks among the objects, so neither side
327
    /// decodes or encodes their text.
328
    pub fn bundle_raw_for(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> {
329
        self.bundle_of(changes, max_blob, true, true)
330
    }
331
332
    /// The keys that signed the attestations this repository serves with
333
    /// its references: each bookmark's latest and each tag's. A fetch
334
    /// bundle carries their identities and certificates (spec §16.2), so a
335
    /// client can check an attestation whose signer signed none of the
336
    /// changes it receives, a delegate or a short-lived key that only
337
    /// moved a bookmark (oxygenhub#266). One pointer and one signature
338
    /// list per reference, computed once per operation head and kept
339
    /// (every bookmark move and tag is an operation): a client sends one
340
    /// fetch per bookmark, and on a bucket store each signature list is a
341
    /// LIST request, so recomputing per fetch cost metalgear2 (145
342
    /// bookmarks) 14 s a fetch.
343
    pub fn served_attest_signers(&self) -> Result<BTreeSet<KeyId>> {
344
        type Signers = std::collections::HashMap<(PathBuf, ObjectRef), BTreeSet<KeyId>>;
345
        static SERVED: std::sync::Mutex<Option<Signers>> = std::sync::Mutex::new(None);
346
        let key = (self.oxy.clone(), self.head_op()?.0);
347
        if let Some(keys) = SERVED
348
            .lock()
349
            .unwrap_or_else(|p| p.into_inner())
350
            .as_ref()
351
            .and_then(|m| m.get(&key))
352
        {
353
            return Ok(keys.clone());
354
        }
355
        let keys = self.attest_signers_now()?;
356
        let mut served = SERVED.lock().unwrap_or_else(|p| p.into_inner());
357
        let map = served.get_or_insert_with(Default::default);
358
        // One entry per repository: an older operation head is never asked
359
        // for again once a newer one is current.
360
        map.retain(|(oxy, _), _| *oxy != key.0);
361
        if map.len() >= 1024 {
362
            map.clear();
363
        }
364
        map.insert(key, keys.clone());
365
        Ok(keys)
366
    }
367
368
    fn attest_signers_now(&self) -> Result<BTreeSet<KeyId>> {
369
        let view = self.view()?;
370
        let pointers = view
371
            .bookmarks
372
            .keys()
373
            .map(|b| self.ref_attest_path(b))
374
            .chain(view.tags.keys().map(|t| self.tag_attest_path(t)));
375
        let mut keys = BTreeSet::new();
376
        for p in pointers {
377
            let Ok(text) = fs::read_to_string(&p) else {
378
                continue;
379
            };
380
            let Some(r) = ObjectRef::from_hex(text.trim()) else {
381
                continue;
382
            };
383
            keys.extend(self.store.signatures(&r)?.into_iter().map(|s| s.key));
384
        }
385
        Ok(keys)
386
    }
387
388
    /// `serving`: a fetch's answer, which carries the identities and
389
    /// certificates of [`Repo::served_attest_signers`] too.
390
    fn bundle_of(
391
        &self,
392
        changes: &[ChangeHash],
393
        max_blob: Option<u64>,
394
        raw: bool,
395
        serving: bool,
396
    ) -> Result<Bundle> {
397
        let mut bundle = Bundle::default();
398
        let mut objects: BTreeSet<ObjectRef> = BTreeSet::new();
399
        let mut seals: BTreeSet<ObjectRef> = BTreeSet::new();
400
        let add_object = |r: &ObjectRef, objects: &mut BTreeSet<ObjectRef>| {
401
            if !r.is_zero() && self.store.has(r) {
402
                objects.insert(*r);
403
            }
404
        };
405
        for h in changes {
406
            // Raw: the stored bytes, and a skeleton for the rest.
407
            let (c, encoded) = if raw {
408
                let bytes = self.store.get_typed(h, ObjectType::Change)?;
409
                let (skel, texts) = Change::decode_skeleton(&bytes, *h)?;
410
                for t in texts {
411
                    if let crate::objects::TextRef::Blob(r, _, _) = t {
412
                        let blob = Blob::decode(&self.store.get_typed(&r, ObjectType::Blob)?)?;
413
                        objects.insert(r);
414
                        objects.extend(blob.chunks);
415
                    }
416
                }
417
                (skel, Some(bytes))
418
            } else {
419
                (self.store.get_change(h)?, None)
420
            };
421
            let sigs = self.store.signatures(h)?;
422
            add_object(&c.author, &mut objects);
423
            for s in &sigs {
424
                add_object(&s.key, &mut objects);
425
            }
426
            for e in &c.edits {
427
                if let Edit::Replace { old, new, .. } = e {
428
                    for b in [old, new].into_iter().flatten() {
429
                        if self.store.has(b) {
430
                            if let Ok(blob) =
431
                                Blob::decode(&self.store.get_typed(b, ObjectType::Blob)?)
432
                            {
433
                                if max_blob.is_some_and(|m| blob.length > m) {
434
                                    continue;
435
                                }
436
                                objects.insert(*b);
437
                                objects.extend(blob.chunks.iter().copied());
438
                            }
439
                        }
440
                    }
441
                }
442
            }
443
            for s in self.store.seals_for(h)? {
444
                seals.insert(s);
445
            }
446
            match encoded {
447
                Some(bytes) => bundle.raw_changes.push((bytes, sigs)),
448
                None => {
449
                    bundle.changes.push((c, sigs));
450
                    bundle.hashes.push(*h);
451
                }
452
            }
453
        }
454
        for r in seals {
455
            let attest = self.store.get_attest(&r)?;
456
            let sigs = self.store.signatures(&r)?;
457
            for s in &sigs {
458
                add_object(&s.key, &mut objects);
459
            }
460
            bundle.attests.push(SignedAttest { attest, sigs });
461
        }
462
        let signers: Vec<KeyId> = if serving {
463
            let signers = self.served_attest_signers()?;
464
            for k in &signers {
465
                add_object(k, &mut objects);
466
            }
467
            signers.into_iter().collect()
468
        } else {
469
            Vec::new()
470
        };
471
        for r in objects {
472
            let (ty, body) = self.store.get(&r)?;
473
            bundle.objects.push((ty, body));
474
        }
475
        self.attach_certificates_for(&mut bundle, &signers)?;
476
        Ok(bundle)
477
    }
478
479
    /// Add the certificates of short-lived keys that signed what `bundle`
480
    /// carries, and their issuers' identities, so a receiver can tell who
481
    /// they stand for (spec §14.7). `also` names more signers (a reference
482
    /// attestation sent beside the bundle).
483
    pub(crate) fn attach_certificates_for(
484
        &self,
485
        bundle: &mut Bundle,
486
        also: &[KeyId],
487
    ) -> Result<()> {
488
        let mut keys: BTreeSet<KeyId> = also.iter().copied().collect();
489
        keys.extend(
490
            bundle
491
                .changes
492
                .iter()
493
                .flat_map(|(_, s)| s.iter().map(|s| s.key)),
494
        );
495
        keys.extend(
496
            bundle
497
                .raw_changes
498
                .iter()
499
                .flat_map(|(_, s)| s.iter().map(|s| s.key)),
500
        );
501
        keys.extend(
502
            bundle
503
                .attests
504
                .iter()
505
                .flat_map(|a| a.sigs.iter().map(|s| s.key)),
506
        );
507
        for k in keys {
508
            let Some(r) = self.store.certificate_of(&k)? else {
509
                continue;
510
            };
511
            let cert = self.store.get_certificate(&r)?;
512
            if bundle
513
                .certificates
514
                .iter()
515
                .any(|c| c.cert.hash() == cert.cert.hash())
516
            {
517
                continue;
518
            }
519
            // An agent's certificate brings its grant's signer too, so the
520
            // receiver can check the grant (oxygen#134).
521
            let grantor = cert.cert.grant.as_ref().map(|g| g.grant.grantor);
522
            for id in [Some(k), Some(cert.cert.issuer), grantor].iter().flatten() {
523
                if let Ok(body) = self.store.get_typed(id, ObjectType::Identity) {
524
                    if !bundle
525
                        .objects
526
                        .iter()
527
                        .any(|(t, b)| *t == ObjectType::Identity && *b == body)
528
                    {
529
                        bundle.objects.push((ObjectType::Identity, body));
530
                    }
531
                }
532
            }
533
            bundle.certificates.push(cert);
534
        }
535
        Ok(())
536
    }
537
538
    /// Keep the certificates a bundle brings once each verifies with the
539
    /// identity of the issuer it names; one that does not refuses the bundle.
540
    fn adopt_certificates(&self, certs: &[crate::cert::SignedCertificate]) -> Result<()> {
541
        for c in certs {
542
            let issuer = self.store.get_identity(&c.cert.issuer).map_err(|_| {
543
                RepoError::Msg(format!(
544
                    "a certificate names issuer {}, whose identity did not come with it",
545
                    c.cert.issuer.short(12)
546
                ))
547
            })?;
548
            c.verify(&issuer)
549
                .map_err(|e| RepoError::Msg(format!("a certificate does not verify: {e}")))?;
550
            self.store.put_identity(&c.cert.key)?;
551
            self.store.put_certificate(c)?;
552
        }
553
        Ok(())
554
    }
555
556
    /// A signer authorized for `reference` by what an issuer certified it
557
    /// as (spec §14.7): a short-lived key with a kept certificate that
558
    /// verifies with its issuer, gives `scope`, covers `at_ms` (a time the
559
    /// signer did not choose) and names a subject the policy grants.
560
    pub(crate) fn certified_authorized(
561
        &self,
562
        p: &crate::policy::Policy,
563
        signer: &KeyId,
564
        reference: &str,
565
        scope: Scope,
566
        at_ms: u64,
567
    ) -> bool {
568
        let Ok(Some(r)) = self.store.certificate_of(signer) else {
569
            return false;
570
        };
571
        let Ok(c) = self.store.get_certificate(&r) else {
572
            return false;
573
        };
574
        let Some(issuer) = p
575
            .identity(&c.cert.issuer)
576
            .cloned()
577
            .or_else(|| self.store.get_identity(&c.cert.issuer).ok())
578
        else {
579
            return false;
580
        };
581
        c.verify(&issuer).is_ok()
582
            && c.cert.key_id() == *signer
583
            && c.cert.covers(scope, at_ms)
584
            && p.subject_grant(reference, &c.cert.issuer, &c.cert.subject, scope)
585
    }
586
587
    /// Store a bundle's contents. With `strict`, every change must
588
    /// authenticate (signature or seal by its author or a delegate).
589
    pub fn import_bundle(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> {
590
        self.import_bundle_inner(bundle, strict, false)
591
    }
592
593
    /// Like `import_bundle`, accepting changes whose dependencies are absent
594
    /// (they join the shallow boundary).
595
    pub fn import_bundle_shallow(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> {
596
        self.import_bundle_inner(bundle, strict, true)
597
    }
598
599
    fn import_bundle_inner(
600
        &self,
601
        bundle: &Bundle,
602
        strict: bool,
603
        shallow: bool,
604
    ) -> Result<Vec<ChangeHash>> {
605
        let batch = self.store.begin_batch()?;
606
        let result = self.import_bundle_batched(bundle, strict, shallow);
607
        batch.finish()?;
608
        result
609
    }
610
611
    fn import_bundle_batched(
612
        &self,
613
        bundle: &Bundle,
614
        strict: bool,
615
        shallow: bool,
616
    ) -> Result<Vec<ChangeHash>> {
617
        for (ty, _) in &bundle.objects {
618
            if !matches!(
619
                ty,
620
                ObjectType::Chunk
621
                    | ObjectType::Blob
622
                    | ObjectType::Identity
623
                    | ObjectType::State
624
                    | ObjectType::Checkpoint
625
            ) {
626
                return fail(format!("bundle may not carry {} objects", ty.name()));
627
            }
628
        }
629
        // All at once: in a large batch they are compressed on every core.
630
        let objects: Vec<(ObjectType, &[u8])> = bundle
631
            .objects
632
            .iter()
633
            .map(|(ty, body)| (*ty, body.as_slice()))
634
            .collect();
635
        self.store.put_many(&objects)?;
636
        self.adopt_certificates(&bundle.certificates)?;
637
        for sa in &bundle.attests {
638
            let r = self.store.put_attest(&sa.attest)?;
639
            for sig in &sa.sigs {
640
                self.store.put_signature(&r, sig)?;
641
            }
642
            if let AttestBody::Seal { hashes } = &sa.attest.body {
643
                for h in hashes {
644
                    self.store.index_seal(h, &r)?;
645
                }
646
            }
647
        }
648
        // Each change's hash: from the bundle when its maker knew it, else
649
        // worked out once (for a change adding a whole tree, hashing is
650
        // chunking all its text).
651
        let hashes: Vec<ChangeHash> = if bundle.hashes.len() == bundle.changes.len() {
652
            bundle.hashes.clone()
653
        } else {
654
            bundle.changes.iter().map(|(c, _)| c.hash()).collect()
655
        };
656
        let raw_hashes: Vec<ChangeHash> = bundle
657
            .raw_changes
658
            .iter()
659
            .map(|(b, _)| hash_object(ObjectType::Change, b))
660
            .collect();
661
        let incoming: BTreeSet<ChangeHash> = hashes.iter().chain(&raw_hashes).copied().collect();
662
        let boundary = self.boundary();
663
        let mut imported = Vec::new();
664
        // Changes as stored: checked, then stored as they are, their text
665
        // blobs having come with the objects.
666
        for ((bytes, sigs), h) in bundle.raw_changes.iter().zip(raw_hashes) {
667
            let (skel, texts) = Change::decode_skeleton(bytes, h)?;
668
            for d in &skel.deps {
669
                if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d)
670
                {
671
                    return fail(format!(
672
                        "change {} depends on {}, which is neither stored nor in the bundle",
673
                        h.short(12),
674
                        d.short(12)
675
                    ));
676
                }
677
            }
678
            for t in texts {
679
                if let crate::objects::TextRef::Blob(r, _, _) = t {
680
                    if !self.store.has(&r) {
681
                        return fail(format!(
682
                            "change {} came without its text blob {}",
683
                            h.short(12),
684
                            r.short(12)
685
                        ));
686
                    }
687
                }
688
            }
689
            self.store.put(ObjectType::Change, bytes)?;
690
            for sig in sigs {
691
                self.store.put_signature(&h, sig)?;
692
            }
693
            imported.push(h);
694
        }
695
        for ((c, sigs), h) in bundle.changes.iter().zip(hashes) {
696
            for d in &c.deps {
697
                if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d)
698
                {
699
                    return fail(format!(
700
                        "change {} depends on {}, which is neither stored nor in the bundle",
701
                        h.short(12),
702
                        d.short(12)
703
                    ));
704
                }
705
            }
706
            let stored = self.store.put_change(c)?;
707
            if stored != h {
708
                return fail(format!(
709
                    "change {} is not the change the bundle names",
710
                    h.short(12)
711
                ));
712
            }
713
            for sig in sigs {
714
                self.store.put_signature(&h, sig)?;
715
            }
716
            imported.push(h);
717
        }
718
        if strict {
719
            for h in &imported {
720
                if !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) {
721
                    return fail(format!(
722
                        "change {} is not authenticated by its author",
723
                        h.short(12)
724
                    ));
725
                }
726
            }
727
        }
728
        Ok(imported)
729
    }
730
731
    /// Verify a reference attestation against an update it claims to describe.
732
    /// `allow_equal` lets a client re-read the attestation it already has; a
733
    /// server receiving a push requires a strictly greater serial.
734
    /// A reference attestation signed by a delegate of an identity the
735
    /// rule lists is authorized as that identity (spec §14.5): the identity
736
    /// named the delegate with the `refs` scope, still in force at `at_ms`
737
    /// (the attestation's wall time), and the policy names the identity.
738
    /// Revoked keys authorize nothing either way.
739
    pub(crate) fn delegate_authorized(
740
        &self,
741
        p: &crate::policy::Policy,
742
        signer: &KeyId,
743
        reference: &str,
744
        at_ms: u64,
745
    ) -> bool {
746
        if p.is_revoked(signer) {
747
            return false;
748
        }
749
        let Some(rule) = p.rule_for(reference) else {
750
            return false;
751
        };
752
        rule.keys.iter().any(|k| {
753
            !p.is_revoked(k)
754
                && p.identity(k)
755
                    .cloned()
756
                    .or_else(|| self.store.get_identity(k).ok())
757
                    .is_some_and(|id| id.delegates_to(signer, Scope::REFS, at_ms))
758
        })
759
    }
760
761
    /// A lasting attestation (a tag) signed by a key the policy has since
762
    /// rotated away from (spec §14.2) is authorized as the key that
763
    /// succeeded it: a key the rule lists reaches the signer through
764
    /// `previous` links, and the attestation is dated (`at_ms`) before the
765
    /// signer's successor was made, so a superseded key makes nothing new.
766
    /// Revoked keys authorize nothing.
767
    pub(crate) fn predecessor_authorized(
768
        &self,
769
        p: &crate::policy::Policy,
770
        signer: &KeyId,
771
        reference: &str,
772
        at_ms: u64,
773
    ) -> bool {
774
        if p.is_revoked(signer) {
775
            return false;
776
        }
777
        let Some(rule) = p.rule_for(reference) else {
778
            return false;
779
        };
780
        let identity = |k: &KeyId| {
781
            p.identity(k)
782
                .cloned()
783
                .or_else(|| self.store.get_identity(k).ok())
784
        };
785
        rule.keys.iter().any(|k| {
786
            if p.is_revoked(k) {
787
                return false;
788
            }
789
            let mut cur = identity(k);
790
            for _ in 0..32 {
791
                let Some(id) = cur else {
792
                    return false;
793
                };
794
                match id.previous {
795
                    Some(prev) if prev == *signer => return at_ms < id.created,
796
                    Some(prev) if !p.is_revoked(&prev) => cur = identity(&prev),
797
                    _ => return false,
798
                }
799
            }
800
            false
801
        })
802
    }
803
804
    fn check_ref_attest(
805
        &self,
806
        sa: &SignedAttest,
807
        name: &str,
808
        old_state: &ObjectRef,
809
        new_state: &ObjectRef,
810
        last_serial: Option<u64>,
811
        allow_equal: bool,
812
    ) -> std::result::Result<u64, String> {
813
        let serial =
814
            self.check_ref_attest_body(sa, name, old_state, new_state, last_serial, allow_equal)?;
815
        self.verify_attest_signature(sa)?;
816
        Ok(serial)
817
    }
818
819
    /// What [`Repo::check_ref_attest`] checks short of the signature: the
820
    /// attestation describes this update of this repository with a serial
821
    /// past the last seen.
822
    fn check_ref_attest_body(
823
        &self,
824
        sa: &SignedAttest,
825
        name: &str,
826
        old_state: &ObjectRef,
827
        new_state: &ObjectRef,
828
        last_serial: Option<u64>,
829
        allow_equal: bool,
830
    ) -> std::result::Result<u64, String> {
831
        let AttestBody::Ref {
832
            name: n,
833
            old_state: o,
834
            new_state: nw,
835
            serial,
836
        } = &sa.attest.body
837
        else {
838
            return Err("attestation is not a ref attestation".into());
839
        };
840
        if n != name || o != old_state || nw != new_state {
841
            return Err("attestation does not describe this update".into());
842
        }
843
        if sa.attest.repo != self.id().map_err(|e| e.to_string())? {
844
            return Err("attestation is for a different repository".into());
845
        }
846
        if let Some(last) = last_serial {
847
            if *serial < last || (*serial == last && !allow_equal) {
848
                return Err(format!(
849
                    "attestation serial {serial} is not greater than the last seen {last}"
850
                ));
851
            }
852
        }
853
        Ok(*serial)
854
    }
855
856
    /// An attestation's first signature verifies with its signer's
857
    /// identity, which must be stored here.
858
    fn verify_attest_signature(&self, sa: &SignedAttest) -> std::result::Result<(), String> {
859
        let r = sa.attest.hash();
860
        let sig = sa.sigs.first().ok_or("attestation carries no signature")?;
861
        let id = self
862
            .store
863
            .get_identity(&sig.key)
864
            .map_err(|_| format!("unknown key {}", sig.key.short(12)))?;
865
        verify_signature(&id, sig, Domain::Attest, &r).map_err(|e| e.to_string())
866
    }
867
868
    /// Accept a push (spec §16.4). Without a trust policy every repository
869
    /// is unverified, so unsigned changes and unattested updates are accepted;
870
    /// attestations that are present must still be valid. With a policy,
871
    /// every change and update is checked against it. A newer policy in the
872
    /// push is applied first when it is signed by the current roots.
873
    pub fn receive_push(
874
        &self,
875
        bundle: Bundle,
876
        updates: Vec<RefUpdate>,
877
        policy: Option<SignedPolicy>,
878
        tags: Vec<crate::protocol::TagRef>,
879
    ) -> Result<Vec<RefResult>> {
880
        Ok(self
881
            .receive_push_report(bundle, updates, policy, tags)?
882
            .results)
883
    }
884
885
    /// `receive_push` with what a host wants to log: each accepted
886
    /// reference's old and new state and heads, and the changes this push
887
    /// stored (hub spec §6.12).
888
    pub fn receive_push_report(
889
        &self,
890
        bundle: Bundle,
891
        updates: Vec<RefUpdate>,
892
        policy: Option<SignedPolicy>,
893
        tags: Vec<crate::protocol::TagRef>,
894
    ) -> Result<PushReport> {
895
        // Updates are checked against the head as it is now, not as this
896
        // handle first read it (oxygenhub#249): another push may have
897
        // landed since.
898
        self.refresh()?;
899
        let mut report = PushReport::default();
900
        let results = &mut report.results;
901
        if let Some(sp) = policy {
902
            match self.adopt_policy_chain(&bundle.policies, &sp, "push received") {
903
                Ok(()) => {
904
                    results.push(RefResult {
905
                        name: "policy".into(),
906
                        ok: true,
907
                        reason: format!("serial {}", sp.policy.serial),
908
                    });
909
                }
910
                Err(e) => {
911
                    results.push(RefResult {
912
                        name: "policy".into(),
913
                        ok: false,
914
                        reason: e,
915
                    });
916
                    for u in updates {
917
                        results.push(RefResult {
918
                            name: u.name,
919
                            ok: false,
920
                            reason: "policy update was rejected".into(),
921
                        });
922
                    }
923
                    return Ok(report);
924
                }
925
            }
926
        }
927
        let policy = self.current_policy()?.map(|(_, sp)| sp.policy);
928
        // A push of objects alone (one batch of a long history, oxygen#95)
929
        // names no reference: its outcome is reported as `objects`.
930
        let objects_only = updates.is_empty() && tags.is_empty();
931
        match self.import_bundle(&bundle, policy.is_some()) {
932
            Ok(imported) => {
933
                if objects_only {
934
                    results.push(RefResult {
935
                        name: OBJECTS_RESULT.into(),
936
                        ok: true,
937
                        reason: format!("{} changes stored", imported.len()),
938
                    });
939
                }
940
                report.imported = imported;
941
            }
942
            Err(e) => {
943
                if objects_only {
944
                    results.push(RefResult {
945
                        name: OBJECTS_RESULT.into(),
946
                        ok: false,
947
                        reason: e.to_string(),
948
                    });
949
                }
950
                for u in updates {
951
                    results.push(RefResult {
952
                        name: u.name,
953
                        ok: false,
954
                        reason: e.to_string(),
955
                    });
956
                }
957
                return Ok(report);
958
            }
959
        }
960
        let (op_ref, op) = self.head_op()?;
961
        let mut view = op.view.clone();
962
        let mut names = Vec::new();
963
        for u in updates {
964
            let outcome = self.apply_update(&mut view, &u, policy.as_ref());
965
            let (ok, reason) = match outcome {
966
                Ok(()) => (true, String::new()),
967
                Err(e) => (false, e),
968
            };
969
            if ok {
970
                names.push(u.name.clone());
971
                let heads = self
972
                    .store
973
                    .get_state(&u.new_state)
974
                    .map(|s| s.heads)
975
                    .unwrap_or_default();
976
                report.updated.push(RefUpdated {
977
                    name: u.name.clone(),
978
                    old_state: u.old_state,
979
                    new_state: u.new_state,
980
                    heads,
981
                });
982
            }
983
            results.push(RefResult {
984
                name: u.name,
985
                ok,
986
                reason,
987
            });
988
        }
989
        for t in &tags {
990
            let name = format!("tags/{}", t.name);
991
            if t.is_withdrawal() {
992
                match self.adopt_withdrawal(&mut view, t, policy.as_ref(), &mut TagCheck::Received)
993
                {
994
                    Ok(removed) => {
995
                        // A withdrawal is an update to nothing, as a
996
                        // bookmark's deletion is.
997
                        if removed {
998
                            report.updated.push(RefUpdated {
999
                                name: name.clone(),
1000
                                old_state: t.state,
1001
                                new_state: ObjectRef::ZERO,
1002
                                heads: vec![],
1003
                            });
1004
                        }
1005
                        names.push(name.clone());
1006
                        results.push(RefResult {
1007
                            name,
1008
                            ok: true,
1009
                            reason: if removed {
1010
                                "withdrawn"
1011
                            } else {
1012
                                "already withdrawn"
1013
                            }
1014
                            .into(),
1015
                        });
1016
                    }
1017
                    Err(e) => results.push(RefResult {
1018
                        name,
1019
                        ok: false,
1020
                        reason: e.reason,
1021
                    }),
1022
                }
1023
                continue;
1024
            }
1025
            let new = !view.tags.contains_key(&t.name);
1026
            match self.adopt_tag(&mut view, t, policy.as_ref(), &mut TagCheck::Received) {
1027
                Ok(()) => {
1028
                    // A tag new here is an update too, so a hub can start
1029
                    // its pipeline's tag jobs (oxygenflow#24); one it
1030
                    // already had, sent again, is not.
1031
                    if new {
1032
                        report.updated.push(RefUpdated {
1033
                            name: name.clone(),
1034
                            old_state: ObjectRef::ZERO,
1035
                            new_state: t.state,
1036
                            heads: t.heads.clone(),
1037
                        });
1038
                    }
1039
                    names.push(name.clone());
1040
                    results.push(RefResult {
1041
                        name,
1042
                        ok: true,
1043
                        reason: String::new(),
1044
                    });
1045
                }
1046
                Err(e) => results.push(RefResult {
1047
                    name,
1048
                    ok: false,
1049
                    reason: e.reason,
1050
                }),
1051
            }
1052
        }
1053
        if !names.is_empty() {
1054
            self.record_op(
1055
                &[op_ref],
1056
                &format!("push received: {}", names.join(" ")),
1057
                view,
1058
            )?;
1059
        }
1060
        Ok(report)
1061
    }
1062
1063
    /// Check an update's attestation against the latest one for its
1064
    /// reference and store it as the new latest. Failures say what was
1065
    /// being done, for which reference, so a refused push can be diagnosed
1066
    /// from its reason alone.
1067
    fn record_update_attest(
1068
        &self,
1069
        u: &RefUpdate,
1070
        sa: &SignedAttest,
1071
    ) -> std::result::Result<(), String> {
1072
        let name = &u.name;
1073
        let last = self
1074
            .latest_ref_attest(name)
1075
            .map_err(|e| format!("reading the last attestation of {name:?}: {e}"))?
1076
            .and_then(|(_, a)| match a.attest.body {
1077
                AttestBody::Ref { serial, .. } => Some(serial),
1078
                _ => None,
1079
            });
1080
        self.check_ref_attest(sa, name, &u.old_state, &u.new_state, last, false)?;
1081
        let store = |e: &dyn std::fmt::Display| format!("storing the attestation of {name:?}: {e}");
1082
        let r = self.store.put_attest(&sa.attest).map_err(|e| store(&e))?;
1083
        for sig in &sa.sigs {
1084
            self.store.put_signature(&r, sig).map_err(|e| store(&e))?;
1085
        }
1086
        self.set_ref_attest(name, &r).map_err(|e| store(&e))?;
1087
        // Signed by a short-lived key: a host records when it accepted it.
1088
        let certified = sa
1089
            .sigs
1090
            .first()
1091
            .is_some_and(|s| self.store.certificate_of(&s.key).ok().flatten().is_some());
1092
        if certified {
1093
            self.record_acceptance(&r).map_err(|e| store(&e))?;
1094
        }
1095
        Ok(())
1096
    }
1097
1098
    /// Record that this host accepted attestation `r` now, signed with its
1099
    /// acceptor key (spec §14.7); nothing without one.
1100
    pub(crate) fn record_acceptance(&self, r: &ObjectRef) -> Result<()> {
1101
        let Some(acceptor) = &self.acceptor else {
1102
            return Ok(());
1103
        };
1104
        let (id, sk) = (&acceptor.0, &acceptor.1);
1105
        let attest = Attest {
1106
            repo: self.id()?,
1107
            hlc: Hlc::now(),
1108
            body: AttestBody::Accepted { attest: *r },
1109
        };
1110
        let sig = sk.sign(Domain::Attest, &id.hash(), &attest.hash());
1111
        self.store.put_identity(id)?;
1112
        let a = self.store.put_attest(&attest)?;
1113
        self.store.put_signature(&a, &sig)?;
1114
        self.store.index_add_pub("accepted", r, &a)?;
1115
        Ok(())
1116
    }
1117
1118
    /// The host's acceptance of attestation `r`, if one was recorded.
1119
    pub fn acceptance_of(&self, r: &ObjectRef) -> Result<Option<SignedAttest>> {
1120
        let Some(a) = self.store.index_get_pub("accepted", r)?.into_iter().next() else {
1121
            return Ok(None);
1122
        };
1123
        Ok(Some(SignedAttest {
1124
            attest: self.store.get_attest(&a)?,
1125
            sigs: self.store.signatures(&a)?,
1126
        }))
1127
    }
1128
1129
    /// When a certified signer's attestation `sa` is checked on a fetch: the
1130
    /// host's acceptance time, if `accepted` is signed by the issuer of the
1131
    /// signer's certificate and accepts `sa`; else the attestation's own
1132
    /// time, which the signer chose.
1133
    pub(crate) fn certified_time(
1134
        &self,
1135
        signer: &KeyId,
1136
        sa: &SignedAttest,
1137
        accepted: Option<&SignedAttest>,
1138
    ) -> u64 {
1139
        self.issuer_acceptance(signer, sa, accepted)
1140
            .unwrap_or(sa.attest.hlc.wall_ms)
1141
    }
1142
1143
    /// The time of `accepted` when it accepts exactly `sa` and is signed by
1144
    /// the issuer of `signer`'s kept certificate (spec §14.7); else none.
1145
    pub(crate) fn issuer_acceptance(
1146
        &self,
1147
        signer: &KeyId,
1148
        sa: &SignedAttest,
1149
        accepted: Option<&SignedAttest>,
1150
    ) -> Option<u64> {
1151
        let acc = accepted?;
1152
        let c = self.store.certificate_of(signer).ok().flatten()?;
1153
        let cert = self.store.get_certificate(&c).ok()?;
1154
        let AttestBody::Accepted { attest } = &acc.attest.body else {
1155
            return None;
1156
        };
1157
        let sig = acc.sigs.iter().find(|s| s.key == cert.cert.issuer)?;
1158
        let issuer = self.store.get_identity(&cert.cert.issuer).ok()?;
1159
        (*attest == sa.attest.hash()
1160
            && verify_signature(&issuer, sig, Domain::Attest, &acc.attest.hash()).is_ok())
1161
        .then_some(acc.attest.hlc.wall_ms)
1162
    }
1163
1164
    /// Keep an acceptance this repository was given for attestation `r`
1165
    /// (one [`Repo::issuer_acceptance`] took), so it is served again with
1166
    /// what it accepts.
1167
    pub(crate) fn keep_acceptance(&self, r: &ObjectRef, acc: &SignedAttest) -> Result<()> {
1168
        let a = self.store.put_attest(&acc.attest)?;
1169
        for sig in &acc.sigs {
1170
            self.store.put_signature(&a, sig)?;
1171
        }
1172
        self.store.index_add_pub("accepted", r, &a)?;
1173
        Ok(())
1174
    }
1175
1176
    /// A fetched bookmark's attestation by a signer the policy does not
1177
    /// list itself: a delegate or a short-lived key, whose authority
1178
    /// depends on when it signed (spec §14.5, §14.8). Checked at the
1179
    /// attestation's integrated time when the host's log gives one; else
1180
    /// as before, at the host's acceptance for a certified signer, when it
1181
    /// countersigned one, or at the attestation's own time, and said so
1182
    /// when the host keeps a log. Failing at the integrated time but not
1183
    /// as before is a note while logs are new; a large gap between the
1184
    /// claimed and the integrated time is a note.
1185
    #[allow(clippy::too_many_arguments)]
1186
    pub(crate) fn authorize_in_time(
1187
        &self,
1188
        p: &crate::policy::Policy,
1189
        remote_name: &str,
1190
        r: &crate::protocol::RemoteRef,
1191
        sa: &SignedAttest,
1192
        signer: &KeyId,
1193
        log: &super::transparency::LogCheck,
1194
        notes: &mut Vec<String>,
1195
    ) -> Result<()> {
1196
        use super::transparency::{gap_note, judge, Timed};
1197
        let own = sa.attest.hlc.wall_ms;
1198
        let at = self.certified_time(signer, sa, r.accepted.as_ref());
1199
        let time = log.time_of(&r.name);
1200
        let check = |t_delegate: u64, t_cert: u64| {
1201
            self.delegate_authorized(p, signer, &r.name, t_delegate)
1202
                || self.certified_authorized(p, signer, &r.name, Scope::REFS, t_cert)
1203
        };
1204
        let before = if at != own {
1205
            "the host's acceptance"
1206
        } else {
1207
            "the time it claims"
1208
        };
1209
        let what = format!("{remote_name}/{}", r.name);
1210
        match judge(time.as_ref().ok().map(|i| i.ms()), |t| check(t, t), || check(own, at)) {
1211
            Timed::Refused => {
1212
                return fail(format!(
1213
                    "{what}: attested by {}, which the policy does not authorize for this bookmark; a policy owner can grant it with `oxy policy grant`",
1214
                    signer.short(12)
1215
                ))
1216
            }
1217
            Timed::Integrated => {}
1218
            Timed::Disagrees => notes.push(format!(
1219
                "{what}: {}'s authority had lapsed when witness {} first saw its attestation, though not at {before}; accepted while transparency logs are new",
1220
                signer.short(12),
1221
                time.as_ref().map(|i| i.witness.as_str()).unwrap_or("?")
1222
            )),
1223
            Timed::Fallback if log.kept => notes.push(format!(
1224
                "{what}: {} checked at {before}, not at a witnessed time: {}",
1225
                signer.short(12),
1226
                time.as_ref().err().map(String::as_str).unwrap_or("none")
1227
            )),
1228
            Timed::Fallback => {}
1229
        }
1230
        if let Some(n) = time.ok().and_then(|i| gap_note(own, i)) {
1231
            notes.push(format!("{what}'s attestation {n}"));
1232
        }
1233
        Ok(())
1234
    }
1235
1236
    /// A deletion (spec §16.4): `new_state` is zero. The bookmark must exist
1237
    /// at `old_state`, is never the bookmark a non-bare repository has
1238
    /// checked out (a bare one's default is its host's to protect, see
1239
    /// `oxy_server::Backend::default_bookmark`), and under a policy needs
1240
    /// an attestation by a key that may update it.
1241
    /// The deletion's attestation stays the bookmark's latest, so an older
1242
    /// one cannot be replayed to bring it back.
1243
    fn apply_deletion(
1244
        &self,
1245
        view: &mut View,
1246
        u: &RefUpdate,
1247
        policy: Option<&crate::policy::Policy>,
1248
    ) -> std::result::Result<(), String> {
1249
        let Some(current) = view.bookmarks.get(&u.name).copied() else {
1250
            return Err(format!("no bookmark {:?} to delete", u.name));
1251
        };
1252
        if current != u.old_state {
1253
            return Err("stale: the remote bookmark has moved; pull first".into());
1254
        }
1255
        if !self.is_bare() && u.name == self.config.bookmark {
1256
            return Err(format!(
1257
                "{:?} is this repository's default bookmark (checked out there) and cannot be deleted",
1258
                u.name
1259
            ));
1260
        }
1261
        if let Some(p) = policy {
1262
            let Some(sa) = &u.attestation else {
1263
                return Err(format!(
1264
                    "deleting {:?} requires a signed reference attestation under this repository's policy",
1265
                    u.name
1266
                ));
1267
            };
1268
            let signer = sa
1269
                .sigs
1270
                .first()
1271
                .map(|s| s.key)
1272
                .ok_or("attestation carries no signature")?;
1273
            // Received now: the receiver's own clock is the trusted time.
1274
            let now = Hlc::now().wall_ms;
1275
            if !p.authorizes(&signer, &u.name)
1276
                && !self.delegate_authorized(p, &signer, &u.name, sa.attest.hlc.wall_ms)
1277
                && !self.certified_authorized(p, &signer, &u.name, Scope::REFS, now)
1278
            {
1279
                return Err(format!(
1280
                    "key {} may not delete {:?} under this repository's policy",
1281
                    signer.short(12),
1282
                    u.name
1283
                ));
1284
            }
1285
        }
1286
        if let Some(sa) = &u.attestation {
1287
            self.record_update_attest(u, sa)?;
1288
        }
1289
        view.bookmarks.remove(&u.name);
1290
        Ok(())
1291
    }
1292
1293
    fn apply_update(
1294
        &self,
1295
        view: &mut View,
1296
        u: &RefUpdate,
1297
        policy: Option<&crate::policy::Policy>,
1298
    ) -> std::result::Result<(), String> {
1299
        if u.name == "policy" {
1300
            return Err(
1301
                "the policy reference is updated by sending a policy, not a bookmark update".into(),
1302
            );
1303
        }
1304
        let empty = self
1305
            .store
1306
            .put_state(&StateObject::new(vec![]))
1307
            .map_err(|e| e.to_string())?;
1308
        if u.new_state.is_zero() {
1309
            return self.apply_deletion(view, u, policy);
1310
        }
1311
        let current = view.bookmarks.get(&u.name).copied().unwrap_or(empty);
1312
        if current != u.old_state {
1313
            return Err("stale: the remote bookmark has moved; pull first".into());
1314
        }
1315
        let new_state = self
1316
            .store
1317
            .get_state(&u.new_state)
1318
            .map_err(|_| "new state object is missing".to_string())?;
1319
        for h in &new_state.heads {
1320
            if !self.store.has(h) {
1321
                return Err(format!("head {} is missing", h.short(12)));
1322
            }
1323
        }
1324
        if !u.force {
1325
            let old_heads = self
1326
                .store
1327
                .get_state(&current)
1328
                .map_err(|e| e.to_string())?
1329
                .heads;
1330
            let closure = self
1331
                .closure_in_store(&new_state.heads)
1332
                .map_err(|e| e.to_string())?;
1333
            if let Some(h) = old_heads.iter().find(|h| !closure.contains(h)) {
1334
                return Err(format!(
1335
                    "not a fast-forward: current head {} would be dropped (use --force)",
1336
                    h.short(12)
1337
                ));
1338
            }
1339
        }
1340
        if let Some(p) = policy {
1341
            let Some(sa) = &u.attestation else {
1342
                return Err(format!(
1343
                    "{:?} requires a signed reference attestation under this repository's policy",
1344
                    u.name
1345
                ));
1346
            };
1347
            let signer = sa
1348
                .sigs
1349
                .first()
1350
                .map(|s| s.key)
1351
                .ok_or("attestation carries no signature")?;
1352
            // Received now: the receiver's own clock is the trusted time.
1353
            let now = Hlc::now().wall_ms;
1354
            if !p.authorizes(&signer, &u.name)
1355
                && !self.delegate_authorized(p, &signer, &u.name, sa.attest.hlc.wall_ms)
1356
                && !self.certified_authorized(p, &signer, &u.name, Scope::REFS, now)
1357
            {
1358
                return Err(format!(
1359
                    "key {} may not update {:?} under this repository's policy",
1360
                    signer.short(12),
1361
                    u.name
1362
                ));
1363
            }
1364
            let old_heads = self
1365
                .store
1366
                .get_state(&current)
1367
                .map_err(|e| e.to_string())?
1368
                .heads;
1369
            self.state_allowed(&old_heads, &new_state.heads, p, &u.name)?;
1370
        }
1371
        if let Some(sa) = &u.attestation {
1372
            self.record_update_attest(u, sa)?;
1373
        }
1374
        view.bookmarks.insert(u.name.clone(), u.new_state);
1375
        Ok(())
1376
    }
1377
1378
    // ------------------------------------------------------------ client side
1379
1380
    fn remote_url(&self, name: &str) -> Result<String> {
1381
        self.config.remotes.get(name).cloned().ok_or_else(|| {
1382
            let names: Vec<&str> = self.config.remotes.keys().map(String::as_str).collect();
1383
            RepoError::Msg(match names.as_slice() {
1384
                [] => format!("no remote named {name:?}; add one with `oxy remote add`"),
1385
                _ if name == "origin" && names.len() > 1 && self.config.default_remote.is_none() => format!(
1386
                    "this repository has no remote named origin, and several others ({}): name one (`oxy pull {}`), or make one the default with `oxy config core.remote {}`",
1387
                    names.join(", "),
1388
                    names[0],
1389
                    names[0]
1390
                ),
1391
                _ => format!(
1392
                    "no remote named {name:?}; this repository has {}",
1393
                    names.join(", ")
1394
                ),
1395
            })
1396
        })
1397
    }
1398
1399
    /// Refuse a remote that is another repository. An **unbound** remote
1400
    /// (id all zero: a hosted repository nobody has pushed to) is accepted
1401
    /// for pushes, which bind it to this repository's id (spec §16.1).
1402
    fn check_remote_id(
1403
        &self,
1404
        remote: &dyn crate::protocol::Remote,
1405
        remote_name: &str,
1406
        allow_unbound: bool,
1407
    ) -> Result<()> {
1408
        let theirs = remote.repo_id()?;
1409
        if theirs == self.id()? {
1410
            return Ok(());
1411
        }
1412
        if theirs == crate::protocol::UNBOUND_ID {
1413
            if allow_unbound {
1414
                return Ok(());
1415
            }
1416
            return fail(format!(
1417
                "remote {remote_name:?} is empty and unbound: push to it first"
1418
            ));
1419
        }
1420
        fail(format!(
1421
            "remote {remote_name:?} is a different repository (repository IDs differ); check its URL with `oxy remote list`"
1422
        ))
1423
    }
1424
1425
    /// Credentials and trust for a remote: `remote.<name>.token` and
1426
    /// `remote.<name>.ca`, else the defaults for its URL
1427
    /// ([`Repo::fill_credentials`]).
1428
    pub(crate) fn remote_options(&self, name: &str) -> RemoteOptions {
1429
        let url = self.config.remotes.get(name).cloned().unwrap_or_default();
1430
        let mut options = RemoteOptions {
1431
            repo_id: self.id().ok(),
1432
            token: self.config.remote_tokens.get(name).cloned(),
1433
            ca: self.config.remote_cas.get(name).map(PathBuf::from),
1434
            cache_dir: Some(self.oxy.join("remotes").join(name).join("strata")),
1435
            ..RemoteOptions::default()
1436
        };
1437
        self.fill_credentials(&url, &mut options);
1438
        options
1439
    }
1440
1441
    /// The user's credential store (`credentials.toml` beside the user
1442
    /// configuration file).
1443
    pub fn credentials(&self) -> crate::credentials::Credentials {
1444
        crate::credentials::Credentials::at(
1445
            self.user_config
1446
                .parent()
1447
                .map(Path::to_path_buf)
1448
                .unwrap_or_else(default_home),
1449
        )
1450
    }
1451
1452
    /// Fill in what `options` lacks for `url`: `$OXY_TOKEN` and `$OXY_CA`,
1453
    /// then the user's credential store for its hub, then a remote on the
1454
    /// same hub in an enclosing checkout (a subrepo borrows its parent's).
1455
    /// When the token is not this remote's own or the environment's, the
1456
    /// other tokens enclosing checkouts hold for the hub are kept to try
1457
    /// if it is refused: a stale stored login must not hide a parent's
1458
    /// good token (oxygen#112).
1459
    fn fill_credentials(&self, url: &str, options: &mut RemoteOptions) {
1460
        if options.token.is_some() && options.ca.is_some() {
1461
            return;
1462
        }
1463
        let explicit =
1464
            options.token.is_some() || std::env::var("OXY_TOKEN").is_ok_and(|t| !t.is_empty());
1465
        let stored = self.credentials().options_for(url);
1466
        options.token = options.token.take().or(stored.token);
1467
        options.ca = options.ca.take().or(stored.ca);
1468
        let enclosing = self.enclosing_credentials(url);
1469
        if options.token.is_none() {
1470
            options.token = enclosing.iter().find_map(|(t, _)| t.clone());
1471
        }
1472
        if options.ca.is_none() {
1473
            options.ca = enclosing.iter().find_map(|(_, c)| c.clone());
1474
        }
1475
        if !explicit {
1476
            for t in enclosing.into_iter().filter_map(|(t, _)| t) {
1477
                if options.token.as_ref() != Some(&t) && !options.fallback_tokens.contains(&t) {
1478
                    options.fallback_tokens.push(t);
1479
                }
1480
            }
1481
        }
1482
    }
1483
1484
    /// The tokens and CAs of remotes on `url`'s hub in enclosing
1485
    /// checkouts, nearest first.
1486
    fn enclosing_credentials(&self, url: &str) -> Vec<(Option<String>, Option<PathBuf>)> {
1487
        let mut out = Vec::new();
1488
        let Some(hub) = crate::credentials::hub_of(url) else {
1489
            return out;
1490
        };
1491
        let mut at = self.root.parent();
1492
        while let Some(dir) = at {
1493
            let Ok(cfg) = read_config_file(&dir.join(OXY_DIR).join("config")) else {
1494
                break;
1495
            };
1496
            for (key, remote_url) in &cfg {
1497
                let Some(name) = key
1498
                    .strip_prefix("remote.")
1499
                    .and_then(|k| k.strip_suffix(".url"))
1500
                else {
1501
                    continue;
1502
                };
1503
                if crate::credentials::hub_of(remote_url).as_deref() != Some(hub.as_str()) {
1504
                    continue;
1505
                }
1506
                out.push((
1507
                    cfg.get(&format!("remote.{name}.token")).cloned(),
1508
                    cfg.get(&format!("remote.{name}.ca")).map(PathBuf::from),
1509
                ));
1510
            }
1511
            at = dir.parent();
1512
        }
1513
        out
1514
    }
1515
1516
    /// Push seal attestations on their own (hub spec §7.4): a bundle with
1517
    /// no changes and no reference updates, which the receiver stores.
1518
    pub fn push_seals(&self, remote_name: &str, seals: &[ObjectRef]) -> Result<()> {
1519
        let remote = self.open_named_remote(remote_name)?;
1520
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
1521
        let mut bundle = Bundle::default();
1522
        for r in seals {
1523
            let attest = self.store.get_attest(r)?;
1524
            let sigs = self.store.signatures(r)?;
1525
            bundle
1526
                .attests
1527
                .push(crate::protocol::SignedAttest { attest, sigs });
1528
        }
1529
        let results = remote.push(bundle, vec![], None, vec![])?;
1530
        if let Some(r) = results.iter().find(|r| !r.ok) {
1531
            return fail(format!("{}: {}", r.name, r.reason));
1532
        }
1533
        Ok(())
1534
    }
1535
1536
    /// The remote whose URL starts with `base` (a hub), if configured.
1537
    pub fn remote_for_url(&self, base: &str) -> Option<String> {
1538
        let base = base.trim_end_matches('/');
1539
        self.config
1540
            .remotes
1541
            .iter()
1542
            .find(|(_, url)| url.trim_end_matches('/') == base)
1543
            .map(|(name, _)| name.clone())
1544
    }
1545
1546
    /// Open a remote by name or, failing that, by URL with the default
1547
    /// credentials (see [`Repo::credentials_for`]).
1548
    pub fn open_remote_at(&self, url: &str) -> Result<(String, Box<dyn crate::protocol::Remote>)> {
1549
        if let Some(name) = self.remote_for_url(url) {
1550
            return Ok((name.clone(), self.open_named_remote(&name)?));
1551
        }
1552
        let options = RemoteOptions {
1553
            repo_id: self.id().ok(),
1554
            ..self.credentials_for(url)
1555
        };
1556
        Ok((url.to_string(), open_remote(url, &options)?))
1557
    }
1558
1559
    /// Credentials for a URL: those of the configured remote with that
1560
    /// URL, else the environment's.
1561
    pub fn options_for_url(&self, url: &str) -> RemoteOptions {
1562
        match self.remote_for_url(url) {
1563
            Some(name) => self.remote_options(&name),
1564
            None => RemoteOptions {
1565
                repo_id: self.id().ok(),
1566
                ..self.credentials_for(url)
1567
            },
1568
        }
1569
    }
1570
1571
    fn open_named_remote(&self, name: &str) -> Result<Box<dyn crate::protocol::Remote>> {
1572
        open_remote(&self.remote_url(name)?, &self.remote_options(name))
1573
    }
1574
1575
    fn serial_path(&self, remote: &str, bookmark: &str) -> PathBuf {
1576
        self.oxy
1577
            .join("remotes")
1578
            .join(remote)
1579
            .join(format!("{bookmark}.serial"))
1580
    }
1581
1582
    fn last_seen_serial(&self, remote: &str, bookmark: &str) -> Option<u64> {
1583
        fs::read_to_string(self.serial_path(remote, bookmark))
1584
            .ok()
1585
            .and_then(|s| s.trim().parse().ok())
1586
    }
1587
1588
    fn record_serial(&self, remote: &str, bookmark: &str, serial: u64) -> Result<()> {
1589
        let p = self.serial_path(remote, bookmark);
1590
        fs::create_dir_all(p.parent().unwrap())?;
1591
        write_atomic(&p, serial.to_string().as_bytes())
1592
    }
1593
1594
    fn policy_serial_path(&self, remote: &str) -> PathBuf {
1595
        self.oxy.join("remotes").join(remote).join("policy.serial")
1596
    }
1597
1598
    /// The highest policy serial `remote` has ever presented.
1599
    fn remote_policy_serial(&self, remote: &str) -> Option<u64> {
1600
        fs::read_to_string(self.policy_serial_path(remote))
1601
            .ok()
1602
            .and_then(|s| s.trim().parse().ok())
1603
    }
1604
1605
    fn record_policy_serial(&self, remote: &str, serial: u64) -> Result<()> {
1606
        let p = self.policy_serial_path(remote);
1607
        fs::create_dir_all(p.parent().unwrap())?;
1608
        write_atomic(&p, serial.to_string().as_bytes())
1609
    }
1610
1611
    fn lazy_dir(&self) -> PathBuf {
1612
        self.oxy.join("lazy")
1613
    }
1614
1615
    /// Remember that `blob` was withheld by `remote` (spec §13.5).
1616
    fn register_lazy(&self, blob: &ObjectRef, remote: &str) -> Result<()> {
1617
        fs::create_dir_all(self.lazy_dir())?;
1618
        write_atomic(&self.lazy_dir().join(blob.to_hex()), remote.as_bytes())
1619
    }
1620
1621
    /// Which remote promised an absent blob, if any.
1622
    pub fn lazy_remote(&self, blob: &ObjectRef) -> Option<String> {
1623
        fs::read_to_string(self.lazy_dir().join(blob.to_hex()))
1624
            .ok()
1625
            .map(|s| s.trim().to_string())
1626
    }
1627
1628
    /// Fetch an absent blob (and its chunks) from the remote that promised it.
1629
    pub(crate) fn fetch_lazy_blob(&self, blob: &ObjectRef) -> Result<()> {
1630
        let Some(remote_name) = self.lazy_remote(blob) else {
1631
            return Err(RepoError::Store(StoreError::NotFound(*blob)));
1632
        };
1633
        let remote = self.open_named_remote(&remote_name)?;
1634
        let objects = remote.objects(&[*blob])?;
1635
        for (ty, body) in objects {
1636
            if matches!(ty, ObjectType::Blob | ObjectType::Chunk) {
1637
                self.store.put(ty, &body)?;
1638
            }
1639
        }
1640
        if !self.store.has(blob) {
1641
            return fail(format!(
1642
                "{remote_name} did not send blob {}",
1643
                blob.short(12)
1644
            ));
1645
        }
1646
        let _ = fs::remove_file(self.lazy_dir().join(blob.to_hex()));
1647
        Ok(())
1648
    }
1649
1650
    /// After importing from `remote`, record every blob the changes reference
1651
    /// that the store lacks as lazily available from that remote.
1652
    fn register_missing_blobs(&self, changes: &[ChangeHash], remote: &str) -> Result<usize> {
1653
        let mut n = 0;
1654
        for h in changes {
1655
            let c = self.store.get_change(h)?;
1656
            for e in &c.edits {
1657
                if let Edit::Replace { old, new, .. } = e {
1658
                    for b in [old, new].into_iter().flatten() {
1659
                        if !self.store.has(b) {
1660
                            self.register_lazy(b, remote)?;
1661
                            n += 1;
1662
                        }
1663
                    }
1664
                }
1665
            }
1666
        }
1667
        Ok(n)
1668
    }
1669
1670
    /// Every head this repository knows: bookmarks, remote tracking, working copy.
1671
    pub fn known_heads(&self, view: &View) -> Result<Vec<ChangeHash>> {
1672
        let mut have: BTreeSet<ChangeHash> = BTreeSet::new();
1673
        for st in view
1674
            .bookmarks
1675
            .values()
1676
            .chain(view.remotes.values().flat_map(|b| b.values()))
1677
        {
1678
            if let Ok(s) = self.store.get_state(st) {
1679
                have.extend(s.heads);
1680
            }
1681
        }
1682
        have.extend(view.workspaces.values().copied());
1683
        Ok(have.into_iter().collect())
1684
    }
1685
1686
    /// Fetch every bookmark of a remote into `remotes/<name>/bookmarks/*` (spec §10.2).
1687
    pub fn fetch(&self, remote_name: &str) -> Result<FetchReport> {
1688
        let remote = self.open_named_remote(remote_name)?;
1689
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
1690
        let resp = remote.refs()?;
1691
        // The host's transparency log (spec §14.8): a fork fails here.
1692
        let log = self.check_log(
1693
            &self.remote_url(remote_name)?,
1694
            self.config.remote_cas.get(remote_name).map(Path::new),
1695
            &resp,
1696
        )?;
1697
        let mut log_notes = log.notes.clone();
1698
        // Trust policy (spec §14.5). Adopt the remote's policy when it is newer
1699
        // and signed by roots we trust, or on first contact. A remote that
1700
        // once presented a policy may never present none or an older one:
1701
        // that is a downgrade. A remote that never presented one is
1702
        // unverified, and this fetch is checked only as far as possible.
1703
        let local_policy = self.current_policy()?.map(|(_, sp)| sp.policy);
1704
        let seen = self.remote_policy_serial(remote_name);
1705
        let mut unverified = false;
1706
        match &resp.policy {
1707
            Some(sp) => {
1708
                if seen.is_some_and(|s| sp.policy.serial < s) {
1709
                    return fail(format!("{remote_name} presents policy serial {} but presented {} before: refusing the downgrade", sp.policy.serial, seen.unwrap()));
1710
                }
1711
                match &local_policy {
1712
                    Some(l) if sp.policy.serial < l.serial => {}
1713
                    Some(l) if sp.policy.serial == l.serial => {
1714
                        if sp.policy.hash() != l.hash() {
1715
                            return fail(format!(
1716
                                "{remote_name} presents a different policy with the same serial {}",
1717
                                l.serial
1718
                            ));
1719
                        }
1720
                    }
1721
                    _ => {
1722
                        self.adopt_policy_chain(
1723
                            &resp.history,
1724
                            sp,
1725
                            &format!("oxy fetch {remote_name}"),
1726
                        )
1727
                        .map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?;
1728
                    }
1729
                }
1730
                self.record_policy_serial(remote_name, sp.policy.serial)?;
1731
            }
1732
            None => {
1733
                if seen.is_some() {
1734
                    return fail(format!("{remote_name} presented a policy before but has none now: refusing the downgrade"));
1735
                }
1736
                unverified = true;
1737
            }
1738
        }
1739
        let policy = if unverified {
1740
            None
1741
        } else {
1742
            self.current_policy()?.map(|(_, sp)| sp.policy)
1743
        };
1744
        let (op_ref, op) = self.head_op()?;
1745
        let mut view = op.view.clone();
1746
        let have = self.known_heads(&view)?;
1747
        let mut report = Vec::new();
1748
        let mut skipped = Vec::new();
1749
        let max_blob = self.config.max_blob;
1750
        for r in resp.refs {
1751
            let needed = {
1752
                let _t = crate::timing::phase("fetch: negotiate");
1753
                remote.negotiate(&have, &r.heads)?
1754
            };
1755
            let bundle = {
1756
                let _t = crate::timing::phase("fetch: bundle");
1757
                remote.fetch(&needed, max_blob)?
1758
            };
1759
            // Changes are not individually re-verified here: the bookmark's
1760
            // attestation (checked below) binds its whole state, and the
1761
            // receiver of the push verified the added changes at that time.
1762
            // A shallow repository takes changes whose dependencies it never
1763
            // held (below what it cloned), as its clone did (#137).
1764
            let shallow = self.is_shallow();
1765
            let imported = {
1766
                let _t = crate::timing::phase("fetch: import");
1767
                if shallow {
1768
                    self.import_bundle_shallow(&bundle, false)?
1769
                } else {
1770
                    self.import_bundle(&bundle, false)?
1771
                }
1772
            };
1773
            if shallow {
1774
                self.refresh_boundary(&imported)?;
1775
            }
1776
            if max_blob.is_some() {
1777
                self.register_missing_blobs(&imported, remote_name)?;
1778
            }
1779
            let boundary = if shallow {
1780
                self.boundary()
1781
            } else {
1782
                BTreeSet::new()
1783
            };
1784
            for h in &r.heads {
1785
                if !self.store.has(h) && !boundary.contains(h) {
1786
                    return fail(format!("remote did not send head {}", h.short(12)));
1787
                }
1788
            }
1789
            let state = self.store.put_state(&StateObject::new(r.heads.clone()))?;
1790
            if state != r.state {
1791
                return fail(format!(
1792
                    "remote state {} does not match its heads",
1793
                    r.state.short(12)
1794
                ));
1795
            }
1796
            if let Some(p) = &policy {
1797
                let Some(sa) = &r.attestation else {
1798
                    return fail(format!(
1799
                        "{remote_name}/{} has no attestation, which its policy requires",
1800
                        r.name
1801
                    ));
1802
                };
1803
                let signer = sa
1804
                    .sigs
1805
                    .first()
1806
                    .map(|s| s.key)
1807
                    .ok_or_else(|| RepoError::Msg("attestation carries no signature".into()))?;
1808
                if !p.authorizes(&signer, &r.name) {
1809
                    self.authorize_in_time(p, remote_name, &r, sa, &signer, &log, &mut log_notes)?;
1810
                }
1811
            }
1812
            if let Some(sa) = &r.attestation {
1813
                let AttestBody::Ref {
1814
                    old_state,
1815
                    new_state,
1816
                    ..
1817
                } = &sa.attest.body
1818
                else {
1819
                    return fail("remote sent a non-ref attestation for a bookmark");
1820
                };
1821
                let (old_state, new_state) = (*old_state, *new_state);
1822
                let serial = self
1823
                    .check_ref_attest_body(
1824
                        sa,
1825
                        &r.name,
1826
                        &old_state,
1827
                        &new_state,
1828
                        self.last_seen_serial(remote_name, &r.name),
1829
                        true,
1830
                    )
1831
                    .map_err(|e| {
1832
                        RepoError::Msg(format!("attestation for {}/{}: {e}", remote_name, r.name))
1833
                    })?;
1834
                if new_state != r.state {
1835
                    return fail(format!(
1836
                        "attestation for {}/{} does not cover its current state",
1837
                        remote_name, r.name
1838
                    ));
1839
                }
1840
                // A signature this client cannot verify (its signer unknown
1841
                // here, as from a host that did not send the identity, or
1842
                // bad) fails that bookmark alone: it stays as it was and
1843
                // the rest are fetched (oxygenhub#266). The current
1844
                // bookmark, which a pull would take, still fails the fetch.
1845
                // A policy's requirements were checked above and still fail.
1846
                if let Err(e) = self.verify_attest_signature(sa) {
1847
                    let why = format!("attestation for {remote_name}/{}: {e}", r.name);
1848
                    if r.name == self.config.bookmark {
1849
                        return fail(why);
1850
                    }
1851
                    skipped.push((r.name, why));
1852
                    continue;
1853
                }
1854
                let ar = self.store.put_attest(&sa.attest)?;
1855
                for sig in &sa.sigs {
1856
                    self.store.put_signature(&ar, sig)?;
1857
                }
1858
                self.record_serial(remote_name, &r.name, serial)?;
1859
            }
1860
            view.remotes
1861
                .entry(remote_name.to_string())
1862
                .or_default()
1863
                .insert(r.name.clone(), state);
1864
            report.push((r.name, r.heads, imported.len()));
1865
        }
1866
        let mut new_tags = 0;
1867
        for t in &resp.tags {
1868
            if view.tags.contains_key(&t.name) {
1869
                continue;
1870
            }
1871
            // Withdrawn here: the name is not used again.
1872
            if self.tag_withdrawal(&t.name)?.is_some() {
1873
                continue;
1874
            }
1875
            // The tag's changes must be here: fetch them if the bookmarks did not bring them.
1876
            let missing: Vec<ChangeHash> = t
1877
                .heads
1878
                .iter()
1879
                .filter(|h| !self.store.has(h))
1880
                .copied()
1881
                .collect();
1882
            if !missing.is_empty() {
1883
                let needed = remote.negotiate(&self.known_heads(&view)?, &missing)?;
1884
                let bundle = remote.fetch(&needed, max_blob)?;
1885
                self.import_bundle(&bundle, false)?;
1886
            }
1887
            self.store.put_state(&StateObject::new(t.heads.clone()))?;
1888
            let mut check = TagCheck::Fetched {
1889
                remote: remote_name,
1890
                log: &log,
1891
                notes: &mut log_notes,
1892
            };
1893
            match self.adopt_tag(&mut view, t, policy.as_ref(), &mut check) {
1894
                Ok(()) => new_tags += 1,
1895
                // Nothing says when a short-lived key signed it: the tag is
1896
                // left out, said so, and the rest is fetched (oxygen#129).
1897
                Err(e) if e.untimed => {
1898
                    skipped.push((format!("tags/{}", t.name), e.reason));
1899
                }
1900
                Err(e) => return fail(format!("{remote_name} {}: {}", t.name, e.reason)),
1901
            }
1902
        }
1903
        // The remote withdrew a tag this repository has: so does this one,
1904
        // once the withdrawal is checked as a push's would be; one that is
1905
        // not is left as it is, said so (oxygenhub#301).
1906
        let mut withdrawn_tags = Vec::new();
1907
        for t in &resp.withdrawn {
1908
            if !view.tags.contains_key(&t.name) {
1909
                continue;
1910
            }
1911
            let mut check = TagCheck::Fetched {
1912
                remote: remote_name,
1913
                log: &log,
1914
                notes: &mut log_notes,
1915
            };
1916
            match self.adopt_withdrawal(&mut view, t, policy.as_ref(), &mut check) {
1917
                Ok(true) => withdrawn_tags.push(t.name.clone()),
1918
                Ok(false) => {}
1919
                Err(e) => skipped.push((
1920
                    format!("tags/{}", t.name),
1921
                    format!("withdrawn on {remote_name}, kept here: {}", e.reason),
1922
                )),
1923
            }
1924
        }
1925
        if view != op.view {
1926
            self.record_op(&[op_ref], &format!("oxy fetch {remote_name}"), view)?;
1927
        }
1928
        let _ = self.run_hook(
1929
            "post-fetch",
1930
            &format!(
1931
                "{{\"event\":\"post-fetch\",\"remote\":{},\"bookmarks\":{}}}",
1932
                json_str(remote_name),
1933
                json_array(report.iter().map(|(n, heads, _)| format!(
1934
                    "{{\"name\":{},\"heads\":{}}}",
1935
                    json_str(n),
1936
                    json_array(heads.iter().map(|h| json_str(&h.to_hex())))
1937
                )))
1938
            ),
1939
        );
1940
        Ok(FetchReport {
1941
            refs: report,
1942
            unverified,
1943
            new_tags,
1944
            withdrawn_tags,
1945
            log_notes,
1946
            skipped,
1947
        })
1948
    }
1949
1950
    /// Shallow fetch of the remote's bookmark: its checkpoint as our base,
1951
    /// the changes within `depth` steps of its heads, and the boundary below.
1952
    pub fn fetch_shallow(&self, remote_name: &str, depth: u32) -> Result<usize> {
1953
        let remote = self.open_named_remote(remote_name)?;
1954
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
1955
        let resp = remote.refs()?;
1956
        let bookmark = self.config.bookmark.clone();
1957
        if let Some(sp) = &resp.policy {
1958
            self.adopt_policy_chain(&resp.history, sp, "oxy clone --depth")
1959
                .map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?;
1960
            self.record_policy_serial(remote_name, sp.policy.serial)?;
1961
        }
1962
        let Some(r) = resp.refs.into_iter().find(|r| r.name == bookmark) else {
1963
            return fail(format!("{remote_name} has no bookmark {bookmark:?}"));
1964
        };
1965
        if r.heads.is_empty() {
1966
            return Ok(0);
1967
        }
1968
        // Base: the remote's checkpoint of exactly these heads.
1969
        let cp_bundle = {
1970
            let _t = crate::timing::phase("shallow: checkpoint request");
1971
            remote.checkpoint(&r.heads)?
1972
        };
1973
        // In one batch: a big state's checkpoint is tens of thousands of
1974
        // graph blobs and chunks, which stored one by one were each written
1975
        // loose and flushed (a VS Code clone ran at 27 objects a second).
1976
        let objects: Vec<(ObjectType, &[u8])> = cp_bundle
1977
            .objects
1978
            .iter()
1979
            .map(|(ty, body)| (*ty, body.as_slice()))
1980
            .collect();
1981
        let t_store = crate::timing::phase("shallow: store the checkpoint");
1982
        let batch = self.store.begin_batch()?;
1983
        let stored = self.store.put_many(&objects);
1984
        batch.finish()?;
1985
        drop(t_store);
1986
        let cp_ref = objects
1987
            .iter()
1988
            .zip(stored?)
1989
            .find(|((ty, _), _)| *ty == ObjectType::Checkpoint)
1990
            .map(|(_, r)| r);
1991
        let Some(cp_ref) = cp_ref else {
1992
            return fail(format!("{remote_name} sent no checkpoint"));
1993
        };
1994
        self.install_checkpoint(&bookmark, &cp_ref)?;
1995
        let _t_blobs = crate::timing::phase("shallow: the binary files' blobs");
1996
        // The blobs of the state's opaque files come with no change when the
1997
        // window holds none of the changes that set them (#137): fetch them
1998
        // now, or under `filter.max-blob` leave them to be fetched on read.
1999
        let cp = crate::objects::Checkpoint::decode(
2000
            &self.store.get_typed(&cp_ref, ObjectType::Checkpoint)?,
2001
        )?;
2002
        let base = State::from_checkpoint_history(&self.read_blob(&cp.payload)?)?;
2003
        let mut missing: Vec<ObjectRef> = base
2004
            .opaque_nodes()
2005
            .iter()
2006
            .flat_map(|n| base.live_blobs(n))
2007
            .filter(|b| !self.store.has(b))
2008
            .collect();
2009
        missing.sort();
2010
        missing.dedup();
2011
        if self.config.max_blob.is_some() {
2012
            for b in &missing {
2013
                self.register_lazy(b, remote_name)?;
2014
            }
2015
        } else {
2016
            for part in missing.chunks(512) {
2017
                let objects = remote.objects(part)?;
2018
                let objects: Vec<(ObjectType, &[u8])> = objects
2019
                    .iter()
2020
                    .filter(|(ty, _)| matches!(ty, ObjectType::Blob | ObjectType::Chunk))
2021
                    .map(|(ty, body)| (*ty, body.as_slice()))
2022
                    .collect();
2023
                let batch = self.store.begin_batch()?;
2024
                let stored = self.store.put_many(&objects);
2025
                batch.finish()?;
2026
                stored?;
2027
            }
2028
            if let Some(b) = missing.iter().find(|b| !self.store.has(b)) {
2029
                return fail(format!("{remote_name} did not send blob {}", b.short(12)));
2030
            }
2031
        }
2032
        // History window. Depth 0 takes none (#137): the checkpoint holds
2033
        // every change's metadata, and its heads become the boundary, so a
2034
        // state of many heads (33,835 in the VS Code import) costs nothing
2035
        // to fetch.
2036
        let imported = if depth == 0 {
2037
            let mut boundary = self.boundary();
2038
            boundary.extend(r.heads.iter().filter(|h| !self.store.has(h)).copied());
2039
            self.write_boundary(&boundary)?;
2040
            Vec::new()
2041
        } else {
2042
            let wanted = remote.shallow(&r.heads, depth)?;
2043
            let bundle = remote.fetch(&wanted, self.config.max_blob)?;
2044
            self.import_bundle_shallow(&bundle, false)?
2045
        };
2046
        if self.config.max_blob.is_some() {
2047
            self.register_missing_blobs(&imported, remote_name)?;
2048
        }
2049
        self.refresh_boundary(&imported)?;
2050
        if let Some(sa) = &r.attestation {
2051
            let ar = self.store.put_attest(&sa.attest)?;
2052
            for sig in &sa.sigs {
2053
                self.store.put_signature(&ar, sig)?;
2054
            }
2055
            if let AttestBody::Ref { serial, .. } = sa.attest.body {
2056
                self.record_serial(remote_name, &r.name, serial)?;
2057
            }
2058
        }
2059
        let state = self.store.put_state(&StateObject::new(r.heads.clone()))?;
2060
        let (op_ref, op) = self.head_op()?;
2061
        let mut view = op.view.clone();
2062
        view.remotes
2063
            .entry(remote_name.to_string())
2064
            .or_default()
2065
            .insert(r.name.clone(), state);
2066
        self.record_op(
2067
            &[op_ref],
2068
            &format!("oxy fetch {remote_name} --depth {depth}"),
2069
            view,
2070
        )?;
2071
        Ok(imported.len())
2072
    }
2073
2074
    /// Fetch, then take the union of the local bookmark's state with the
2075
    /// remote's: no merge change, conflicts become reified (spec §9.4).
2076
    pub fn pull(&self, remote_name: &str) -> Result<PullReport> {
2077
        let _wc = self.lock_working_copy()?;
2078
        let skipped = self.fetch(remote_name)?.skipped;
2079
        let (op_ref, op) = self.head_op()?;
2080
        let current = self.config.bookmark.clone();
2081
        let mut local_current = self.heads_of(&op.view, &current)?;
2082
        local_current.sort();
2083
        let mut view = op.view.clone();
2084
        let mut current_heads = local_current.clone();
2085
        let mut changed = false;
2086
        let mut others = Vec::new();
2087
        let tracked = op
2088
            .view
2089
            .remotes
2090
            .get(remote_name)
2091
            .cloned()
2092
            .unwrap_or_default();
2093
        for (name, remote_state) in tracked {
2094
            // Every local bookmark tracks the remote's of the same name;
2095
            // the current one may be new here.
2096
            let local_state = op.view.bookmarks.get(&name).copied();
2097
            if local_state.is_none() && name != current {
2098
                continue;
2099
            }
2100
            if local_state == Some(remote_state) {
2101
                continue;
2102
            }
2103
            let local = self.heads_of(&op.view, &name)?;
2104
            let remote_heads = self.store.get_state(&remote_state)?.heads;
2105
            if remote_heads.iter().all(|h| local.contains(h)) {
2106
                continue;
2107
            }
2108
            let mut all = local.clone();
2109
            all.extend(remote_heads);
2110
            let st = self.load_state_history(&all)?;
2111
            let mut heads = st.maximal(&all);
2112
            heads.sort();
2113
            let mut sorted_local = local;
2114
            sorted_local.sort();
2115
            if heads == sorted_local {
2116
                continue;
2117
            }
2118
            view.bookmarks.insert(
2119
                name.clone(),
2120
                self.store.put_state(&StateObject::new(heads.clone()))?,
2121
            );
2122
            if name == current {
2123
                changed = true;
2124
                current_heads = heads;
2125
            } else {
2126
                others.push(name);
2127
            }
2128
        }
2129
        if changed || !others.is_empty() {
2130
            self.record_op(&[op_ref], &format!("oxy pull {remote_name}"), view)?;
2131
        }
2132
        // The working copy follows its bookmark whenever nothing would be
2133
        // lost, also when this pull brought nothing new but it had been
2134
        // left behind (oxygen#89).
2135
        let checked_out = self.follow_bookmark(&current_heads, &current, "oxy pull")?;
2136
        Ok(PullReport {
2137
            heads: if changed {
2138
                current_heads
2139
            } else {
2140
                local_current
2141
            },
2142
            changed,
2143
            checked_out,
2144
            others,
2145
            skipped,
2146
        })
2147
    }
2148
2149
    /// How many changes the working copy's bookmark has that its base
2150
    /// lacks, when the base is part of the bookmark's state (it fell behind,
2151
    /// and `oxy checkout` or `oxy pull` brings it up); `None` when it is on
2152
    /// the bookmark or holds changes the bookmark lacks. History is read
2153
    /// only when the base and the bookmark's heads differ (oxygen#89).
2154
    pub fn working_copy_behind(&self) -> Result<Option<usize>> {
2155
        let (_, wc) = self.working_copy()?;
2156
        let mut base = wc.deps.clone();
2157
        base.sort();
2158
        let mut heads = self.bookmark_heads()?;
2159
        heads.sort();
2160
        if base == heads || heads.is_empty() {
2161
            return Ok(None);
2162
        }
2163
        let state = self.load_state_history(&heads)?;
2164
        if !base.iter().all(|h| state.contains(h)) {
2165
            return Ok(None);
2166
        }
2167
        let had = if base.is_empty() {
2168
            0
2169
        } else {
2170
            self.load_state_history(&base)?.len()
2171
        };
2172
        Ok(Some(state.len().saturating_sub(had)).filter(|n| *n > 0))
2173
    }
2174
2175
    /// Bring the working copy to `heads`, the state of its bookmark
2176
    /// `bookmark`, when nothing would be lost: its base is part of that
2177
    /// state, so the bookmark only adds to it. Clean, it is checked out;
2178
    /// with uncommitted work, the work comes along when the new changes
2179
    /// leave its paths alone (as `git pull` does), else it stays. Before,
2180
    /// it followed only from exactly the bookmark's old heads, so a working
2181
    /// copy already behind (the bookmark moved by `bookmark set`, a landing
2182
    /// or a pull from another workspace) never caught up, and nothing said
2183
    /// so (oxygen#74, #89). Returns whether it moved.
2184
    pub fn follow_bookmark(
2185
        &self,
2186
        heads: &[ChangeHash],
2187
        bookmark: &str,
2188
        command: &str,
2189
    ) -> Result<bool> {
2190
        let snap = self.snapshot(command)?;
2191
        let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty();
2192
        let mut base = snap.base.maximal(&snap.wc.deps);
2193
        base.sort();
2194
        let mut target = heads.to_vec();
2195
        target.sort();
2196
        if base == target {
2197
            return Ok(false);
2198
        }
2199
        let state = self.load_state_history(heads)?;
2200
        if !base.iter().all(|h| state.contains(h)) {
2201
            // The working copy holds changes the bookmark lacks: not ours to move.
2202
            return Ok(false);
2203
        }
2204
        if clean {
2205
            self.checkout(heads, false, &format!("{command} (checkout)"))?;
2206
            Ok(true)
2207
        } else {
2208
            Ok(self
2209
                .checkout_carry(heads, bookmark, &format!("{command} (checkout --carry)"))
2210
                .is_ok())
2211
        }
2212
    }
2213
2214
    /// Withdraw tag `tag` on `remote_name` (`oxy push --delete tags/NAME`,
2215
    /// oxygenhub#301): withdrawn here first when it still is a tag here
2216
    /// (`oxy tag delete`), then the withdrawal alone is sent, with its
2217
    /// signer's identity and certificate.
2218
    pub fn push_tag_withdrawal(&self, remote_name: &str, tag: &str) -> Result<Vec<RefResult>> {
2219
        let remote = self.open_named_remote(remote_name)?;
2220
        self.check_remote_id(remote.as_ref(), remote_name, true)?;
2221
        let resp = remote.refs()?;
2222
        let name = format!("tags/{tag}");
2223
        if resp.withdrawn.iter().any(|t| t.name == tag) {
2224
            return Ok(vec![RefResult {
2225
                name,
2226
                ok: true,
2227
                reason: "already withdrawn".into(),
2228
            }]);
2229
        }
2230
        let Some(there) = resp.tags.iter().find(|t| t.name == tag) else {
2231
            return fail(format!("{remote_name} has no tag {tag}"));
2232
        };
2233
        if resp.policy.is_some() && self.secret_key()?.is_none() {
2234
            return fail(format!("{remote_name} enforces a trust policy; withdrawing a tag needs an identity (`oxy identity create`)"));
2235
        }
2236
        if self.tag_withdrawal(tag)?.is_none() {
2237
            match self.view()?.tags.get(tag) {
2238
                Some(st) if *st == there.state => {
2239
                    self.tag_delete(tag, "")?;
2240
                }
2241
                Some(_) => {
2242
                    return fail(format!(
2243
                        "tag {tag} names another state here than on {remote_name}"
2244
                    ))
2245
                }
2246
                None => {
2247
                    return fail(format!(
2248
                        "there is no tag {tag} here to withdraw: `oxy fetch {remote_name}` first"
2249
                    ))
2250
                }
2251
            }
2252
        }
2253
        let Some(w) = self
2254
            .withdrawn_refs()?
2255
            .into_iter()
2256
            .find(|w| w.name == tag && w.state == there.state)
2257
        else {
2258
            return fail(format!(
2259
                "tag {tag} was withdrawn here at another state than {remote_name}'s"
2260
            ));
2261
        };
2262
        let mut bundle = Bundle::default();
2263
        let signers: Vec<KeyId> = w
2264
            .attestation
2265
            .iter()
2266
            .flat_map(|a| a.sigs.iter().map(|s| s.key))
2267
            .collect();
2268
        for k in &signers {
2269
            if let Ok(id) = self.identity(k) {
2270
                bundle.objects.push((ObjectType::Identity, id.encode()));
2271
            }
2272
        }
2273
        self.attach_certificates_for(&mut bundle, &signers)?;
2274
        remote.push(bundle, vec![], None, vec![w])
2275
    }
2276
2277
    /// Push the current bookmark (spec §16.3), sealing this author's unsigned
2278
    /// changes and attesting the reference when a key is available.
2279
    /// Delete `bookmark` on a remote (spec §16.4): an update to the zero
2280
    /// state, attested when we have an identity. The local bookmark, if
2281
    /// any, is kept; only the remote-tracking entry goes.
2282
    pub fn push_delete(&self, remote_name: &str, bookmark: &str) -> Result<Vec<RefResult>> {
2283
        if let Some(tag) = bookmark.strip_prefix("tags/") {
2284
            return self.push_tag_withdrawal(remote_name, tag);
2285
        }
2286
        let remote = self.open_named_remote(remote_name)?;
2287
        self.check_remote_id(remote.as_ref(), remote_name, true)?;
2288
        let resp = remote.refs()?;
2289
        let Some(r) = resp.refs.iter().find(|r| r.name == bookmark) else {
2290
            return fail(format!("{remote_name} has no bookmark {bookmark}"));
2291
        };
2292
        if resp.policy.is_some() && self.secret_key()?.is_none() {
2293
            return fail(format!("{remote_name} enforces a trust policy; deleting needs an identity (`oxy identity create`)"));
2294
        }
2295
        let attestation = match self.secret_key()? {
2296
            Some(_) => Some(self.sign_attest(&Attest {
2297
                repo: self.id()?,
2298
                hlc: Hlc::now(),
2299
                body: AttestBody::Ref {
2300
                    name: bookmark.to_string(),
2301
                    old_state: r.state,
2302
                    new_state: ObjectRef::ZERO,
2303
                    serial: r.last_serial.unwrap_or(0) + 1,
2304
                },
2305
            })?),
2306
            None => None,
2307
        };
2308
        let results = remote.push(
2309
            Bundle::default(),
2310
            vec![RefUpdate {
2311
                name: bookmark.to_string(),
2312
                old_state: r.state,
2313
                new_state: ObjectRef::ZERO,
2314
                force: true,
2315
                attestation,
2316
            }],
2317
            None,
2318
            vec![],
2319
        )?;
2320
        if results.iter().any(|x| x.ok && x.name == bookmark) {
2321
            let (op_ref, op) = self.head_op()?;
2322
            let mut view = op.view.clone();
2323
            if let Some(m) = view.remotes.get_mut(remote_name) {
2324
                m.remove(bookmark);
2325
            }
2326
            self.record_op(
2327
                &[op_ref],
2328
                &format!("oxy push {remote_name} --delete {bookmark}"),
2329
                view,
2330
            )?;
2331
        }
2332
        Ok(results)
2333
    }
2334
2335
    /// Remove remote `name` (oxygen#86): every `remote.NAME.*` key of this
2336
    /// repository's config (its URL, token, CA and store settings), its
2337
    /// tracking bookmarks in the view, and a store remote's mirror under
2338
    /// `.oxy/remotes/NAME`. Nothing stays to be read as if it were current.
2339
    pub fn remove_remote(&mut self, name: &str) -> Result<()> {
2340
        if !self.config.remotes.contains_key(name) {
2341
            return fail(format!("no remote {name}: `oxy remote list` shows them"));
2342
        }
2343
        let prefix = format!("remote.{name}.");
2344
        let keys: Vec<String> = self
2345
            .config
2346
            .repo_layer()
2347
            .keys()
2348
            .filter(|k| k.starts_with(&prefix))
2349
            .cloned()
2350
            .collect();
2351
        for k in keys {
2352
            self.config.set(&k, "")?;
2353
        }
2354
        self.config.remotes.remove(name);
2355
        self.save_config()?;
2356
        let (op_ref, op) = self.head_op()?;
2357
        if op.view.remotes.contains_key(name) {
2358
            let mut view = op.view.clone();
2359
            view.remotes.remove(name);
2360
            self.record_op(&[op_ref], &format!("oxy remote remove {name}"), view)?;
2361
        }
2362
        let mirror = self.oxy.join("remotes").join(name);
2363
        if mirror.is_dir() {
2364
            fs::remove_dir_all(&mirror)?;
2365
        }
2366
        Ok(())
2367
    }
2368
2369
    /// Send `bundle`'s blobs and chunks ahead of it, in parts of about
2370
    /// `push.batch-bytes` of objects alone, when the whole would be larger
2371
    /// (oxygen#133): one change can bring more blobs than one request may
2372
    /// carry, and a proxy in front of a hub refuses a request past its limit
2373
    /// (Cloudflare's is 100 MB). What is left, the changes with their
2374
    /// identities and states, is returned to send as before. A receiver
2375
    /// stores objects as they come, and checks a change's blobs are stored.
2376
    fn send_objects_ahead(
2377
        &self,
2378
        remote: &dyn crate::protocol::Remote,
2379
        remote_name: &str,
2380
        mut bundle: Bundle,
2381
        progress: crate::store::Progress,
2382
    ) -> Result<Bundle> {
2383
        let limit = self.config.push_batch_bytes;
2384
        let changes: u64 = bundle
2385
            .changes
2386
            .iter()
2387
            .map(|(c, _)| c.encode().len() as u64)
2388
            .sum();
2389
        let objects: u64 = bundle.objects.iter().map(|(_, b)| b.len() as u64).sum();
2390
        if changes + objects <= limit {
2391
            return Ok(bundle);
2392
        }
2393
        let (mut kept, mut parts, mut part, mut bytes) = (Vec::new(), Vec::new(), Vec::new(), 0u64);
2394
        for (ty, body) in std::mem::take(&mut bundle.objects) {
2395
            if !matches!(ty, ObjectType::Blob | ObjectType::Chunk) {
2396
                kept.push((ty, body));
2397
                continue;
2398
            }
2399
            if !part.is_empty() && bytes + body.len() as u64 > limit {
2400
                parts.push(std::mem::take(&mut part));
2401
                bytes = 0;
2402
            }
2403
            bytes += body.len() as u64;
2404
            part.push((ty, body));
2405
        }
2406
        if !part.is_empty() {
2407
            parts.push(part);
2408
        }
2409
        let n = parts.len();
2410
        for (i, objects) in parts.into_iter().enumerate() {
2411
            progress(&format!(
2412
                "objects {} of {n}: {} blobs and chunks",
2413
                i + 1,
2414
                objects.len()
2415
            ));
2416
            let results = remote.push(
2417
                Bundle {
2418
                    objects,
2419
                    ..Default::default()
2420
                },
2421
                vec![],
2422
                None,
2423
                vec![],
2424
            )?;
2425
            match results.iter().find(|r| r.name == OBJECTS_RESULT) {
2426
                Some(r) if r.ok => {}
2427
                Some(r) => {
2428
                    return fail(format!(
2429
                        "{remote_name} refused objects part {} of {n}: {}",
2430
                        i + 1,
2431
                        r.reason
2432
                    ))
2433
                }
2434
                None => {
2435
                    return fail(format!(
2436
                        "{remote_name} does not take a push in parts, and this one is too large for one: update the server's oxy"
2437
                    ))
2438
                }
2439
            }
2440
        }
2441
        bundle.objects = kept;
2442
        Ok(bundle)
2443
    }
2444
2445
    /// `changes`, in dependency order, cut into batches of about
2446
    /// `push.batch-bytes` by what each change and its texts and blobs weigh.
2447
    /// There is always one, perhaps empty.
2448
    fn push_batches(&self, changes: &[ChangeHash]) -> Result<Vec<Vec<ChangeHash>>> {
2449
        let blob_len = |r: &ObjectRef| -> u64 {
2450
            self.store
2451
                .get_typed(r, ObjectType::Blob)
2452
                .ok()
2453
                .and_then(|b| Blob::decode(&b).ok())
2454
                .map_or(0, |b| b.length)
2455
        };
2456
        let (mut out, mut batch, mut bytes) = (Vec::new(), Vec::new(), 0u64);
2457
        for h in changes {
2458
            let raw = self.store.get_typed(h, ObjectType::Change)?;
2459
            let (c, texts) = Change::decode_skeleton(&raw, *h)?;
2460
            let mut weight = raw.len() as u64;
2461
            for t in &texts {
2462
                if let crate::objects::TextRef::Blob(r, _, _) = t {
2463
                    weight += blob_len(r);
2464
                }
2465
            }
2466
            for e in &c.edits {
2467
                if let Edit::Replace { new: Some(b), .. } = e {
2468
                    weight += blob_len(b);
2469
                }
2470
            }
2471
            if !batch.is_empty() && bytes + weight > self.config.push_batch_bytes {
2472
                out.push(std::mem::take(&mut batch));
2473
                bytes = 0;
2474
            }
2475
            batch.push(*h);
2476
            bytes += weight;
2477
        }
2478
        out.push(batch);
2479
        Ok(out)
2480
    }
2481
2482
    pub fn push(&self, remote_name: &str, force: bool) -> Result<Vec<RefResult>> {
2483
        let bookmark = self.config.bookmark.clone();
2484
        self.push_bookmark(remote_name, &bookmark, force)
2485
    }
2486
2487
    /// `push` for a named bookmark (`oxy push -b NAME`).
2488
    pub fn push_bookmark(
2489
        &self,
2490
        remote_name: &str,
2491
        bookmark: &str,
2492
        force: bool,
2493
    ) -> Result<Vec<RefResult>> {
2494
        self.push_bookmark_with(remote_name, bookmark, force, &mut |_| {})
2495
    }
2496
2497
    /// `push_bookmark`, saying how a long push is going: one line per
2498
    /// batch sent (oxygen#95).
2499
    pub fn push_bookmark_with(
2500
        &self,
2501
        remote_name: &str,
2502
        bookmark: &str,
2503
        force: bool,
2504
        progress: crate::store::Progress,
2505
    ) -> Result<Vec<RefResult>> {
2506
        let (op_ref, op) = self.head_op()?;
2507
        if !op.view.bookmarks.contains_key(bookmark) {
2508
            return fail(format!("no bookmark {bookmark}"));
2509
        }
2510
        let bookmark = bookmark.to_string();
2511
        let local_heads = self.heads_of(&op.view, &bookmark)?;
2512
        // pre-push hook: may veto before the remote is contacted.
2513
        self.run_hook(
2514
            "pre-push",
2515
            &format!(
2516
                "{{\"event\":\"pre-push\",\"remote\":{},\"bookmark\":{},\"heads\":{}}}",
2517
                json_str(remote_name),
2518
                json_str(&bookmark),
2519
                json_array(local_heads.iter().map(|h| json_str(&h.to_hex())))
2520
            ),
2521
        )?;
2522
        let remote = self.open_named_remote(remote_name)?;
2523
        self.check_remote_id(remote.as_ref(), remote_name, true)?;
2524
        let resp = remote.refs()?;
2525
        let local_policy = self.current_policy()?.map(|(_, sp)| sp);
2526
        let resp_policy_serial = resp.policy.as_ref().map(|p| p.policy.serial);
2527
        let policy_to_send = match (&local_policy, &resp.policy) {
2528
            (Some(l), Some(r)) if l.policy.serial > r.policy.serial => Some(l.clone()),
2529
            (Some(l), None) => Some(l.clone()),
2530
            (Some(l), Some(r)) if l.policy.serial < r.policy.serial => {
2531
                return fail(format!(
2532
                    "{remote_name} has a newer policy (serial {}); fetch first",
2533
                    r.policy.serial
2534
                ));
2535
            }
2536
            _ => None,
2537
        };
2538
        let remote_has_policy = resp.policy.is_some() || policy_to_send.is_some();
2539
        if remote_has_policy && self.secret_key()?.is_none() {
2540
            return fail(format!("{remote_name} enforces a trust policy; pushing needs an identity (`oxy identity create`)"));
2541
        }
2542
        let remote_tags: BTreeSet<String> = resp
2543
            .tags
2544
            .iter()
2545
            .chain(&resp.withdrawn)
2546
            .map(|t| t.name.clone())
2547
            .collect();
2548
        let tags_to_send: Vec<crate::protocol::TagRef> = self
2549
            .tag_refs()?
2550
            .into_iter()
2551
            .filter(|t| !remote_tags.contains(&t.name))
2552
            .collect();
2553
        // Tags withdrawn here that the remote still has, at the same state
2554
        // (oxygenhub#301).
2555
        let withdrawals_to_send: Vec<crate::protocol::TagRef> = self
2556
            .withdrawn_refs()?
2557
            .into_iter()
2558
            .filter(|w| {
2559
                resp.tags
2560
                    .iter()
2561
                    .any(|t| t.name == w.name && t.state == w.state)
2562
            })
2563
            .collect();
2564
        let refs = resp.refs;
2565
        let empty = self.store.put_state(&StateObject::new(vec![]))?;
2566
        let (remote_state, remote_heads, last_serial) =
2567
            match refs.iter().find(|r| r.name == bookmark) {
2568
                Some(r) => (r.state, r.heads.clone(), r.last_serial),
2569
                None => (empty, vec![], None),
2570
            };
2571
        let new_state = self
2572
            .store
2573
            .put_state(&StateObject::new(local_heads.clone()))?;
2574
        // A push that carries a policy re-attests the bookmark even when its
2575
        // state is unchanged, so the current attestation is always by a key
2576
        // the current policy authorizes (a revoked key's attestation would
2577
        // otherwise be refused by every later fetch).
2578
        if new_state == remote_state
2579
            && policy_to_send.is_none()
2580
            && tags_to_send.is_empty()
2581
            && withdrawals_to_send.is_empty()
2582
        {
2583
            return Ok(vec![RefResult {
2584
                name: bookmark,
2585
                ok: true,
2586
                reason: "already up to date".into(),
2587
            }]);
2588
        }
2589
        // A shallow repository knows the changes on its boundary without
2590
        // holding them: a depth-0 clone's are the heads it cloned (#137).
2591
        let boundary = self.boundary();
2592
        for h in &remote_heads {
2593
            if !self.store.has(h) && !boundary.contains(h) {
2594
                return fail(format!(
2595
                    "the remote has change {} which you do not; pull first (`oxy pull {remote_name}`), then push again",
2596
                    h.short(12)
2597
                ));
2598
            }
2599
        }
2600
        if !force {
2601
            let closure = self.closure_in_store(&local_heads)?;
2602
            if let Some(h) = remote_heads
2603
                .iter()
2604
                .find(|h| !closure.contains(h) && !boundary.contains(h))
2605
            {
2606
                return fail(format!("not a fast-forward: remote head {} is not in your state; pull first or use --force", h.short(12)));
2607
            }
2608
        }
2609
        let to_send = self.negotiate(&remote_heads, &local_heads)?;
2610
        let key = self.secret_key()?.map(|(k, _)| k);
2611
        if let Some(key) = key {
2612
            // Seal this author's changes that carry no valid authenticator,
2613
            // and adopt anonymous ones (zero author, spec §14.4). The seals
2614
            // are stored, so each batch's bundle carries those it needs.
2615
            let mut unsigned = Vec::new();
2616
            for h in &to_send {
2617
                let c = self.store.get_change_meta(h)?;
2618
                if self.seals_for_author(&c.author, &key)
2619
                    && !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. })
2620
                {
2621
                    unsigned.push(*h);
2622
                }
2623
            }
2624
            for chunk in unsigned.chunks(crate::attest::MAX_SEAL_HASHES) {
2625
                self.sign_attest(&Attest::seal(self.id()?, chunk.to_vec()))?;
2626
            }
2627
        }
2628
        // A long history goes in batches, in dependency order, so neither
2629
        // side holds it whole and no request passes the remote's limit:
2630
        // all but the last carry objects alone; the last moves the bookmark
2631
        // (oxygen#95).
2632
        let batches = self.push_batches(&to_send)?;
2633
        let (last, earlier) = batches.split_last().expect("at least one batch");
2634
        for (i, batch) in earlier.iter().enumerate() {
2635
            progress(&format!(
2636
                "part {} of {}: {} changes",
2637
                i + 1,
2638
                batches.len(),
2639
                batch.len()
2640
            ));
2641
            let bundle = self.send_objects_ahead(
2642
                remote.as_ref(),
2643
                remote_name,
2644
                self.bundle_for(batch)?,
2645
                &mut *progress,
2646
            )?;
2647
            let results = remote.push(bundle, vec![], None, vec![])?;
2648
            match results.iter().find(|r| r.name == OBJECTS_RESULT) {
2649
                Some(r) if r.ok => {}
2650
                Some(r) => {
2651
                    return fail(format!(
2652
                        "{remote_name} refused part {} of the push: {}",
2653
                        i + 1,
2654
                        r.reason
2655
                    ))
2656
                }
2657
                None => {
2658
                    return fail(format!(
2659
                        "{remote_name} does not take a push in parts, and this one is too long for one: update the server's oxy"
2660
                    ))
2661
                }
2662
            }
2663
        }
2664
        if batches.len() > 1 {
2665
            progress(&format!(
2666
                "part {} of {}: {} changes",
2667
                batches.len(),
2668
                batches.len(),
2669
                last.len()
2670
            ));
2671
        }
2672
        let mut bundle = self.send_objects_ahead(
2673
            remote.as_ref(),
2674
            remote_name,
2675
            self.bundle_for(last)?,
2676
            &mut *progress,
2677
        )?;
2678
        bundle.objects.push((
2679
            ObjectType::State,
2680
            StateObject::new(local_heads.clone()).encode(),
2681
        ));
2682
        let attestation = match key {
2683
            Some(_) => Some(self.sign_attest(&Attest {
2684
                repo: self.id()?,
2685
                hlc: Hlc::now(),
2686
                body: AttestBody::Ref {
2687
                    name: bookmark.clone(),
2688
                    old_state: remote_state,
2689
                    new_state,
2690
                    serial: next_serial(last_serial),
2691
                },
2692
            })?),
2693
            None => None,
2694
        };
2695
        // Whoever signed the move, and whom a delegate acts for, travel with
2696
        // it: a receiver that has never seen a change by this delegate could
2697
        // not otherwise check its signature (oxygenhub#129).
2698
        if attestation.is_some() {
2699
            for k in [self.signing_key(), self.config.author_key]
2700
                .into_iter()
2701
                .flatten()
2702
            {
2703
                if let Ok(id) = self.identity(&k) {
2704
                    let body = id.encode();
2705
                    if !bundle
2706
                        .objects
2707
                        .iter()
2708
                        .any(|(t, b)| *t == ObjectType::Identity && *b == body)
2709
                    {
2710
                        bundle.objects.push((ObjectType::Identity, body));
2711
                    }
2712
                }
2713
            }
2714
            // A short-lived signing key's certificate goes with it (§14.7).
2715
            let signers: Vec<KeyId> = self.signing_key().into_iter().collect();
2716
            self.attach_certificates_for(&mut bundle, &signers)?;
2717
        }
2718
        let serial = attestation.as_ref().and_then(|a| match a.attest.body {
2719
            AttestBody::Ref { serial, .. } => Some(serial),
2720
            _ => None,
2721
        });
2722
        let sent_serial = policy_to_send.as_ref().map(|p| p.policy.serial);
2723
        // Tag states must be present on the remote: bundle their changes too.
2724
        for t in &tags_to_send {
2725
            let extra = self.negotiate(&remote_heads, &t.heads)?;
2726
            let extra: Vec<ChangeHash> =
2727
                extra.into_iter().filter(|h| !to_send.contains(h)).collect();
2728
            if !extra.is_empty() {
2729
                let more = self.bundle_for(&extra)?;
2730
                bundle.changes.extend(more.changes);
2731
                bundle.hashes.extend(more.hashes);
2732
                bundle.attests.extend(more.attests);
2733
                bundle.objects.extend(more.objects);
2734
            }
2735
            bundle.objects.push((
2736
                ObjectType::State,
2737
                StateObject::new(t.heads.clone()).encode(),
2738
            ));
2739
        }
2740
        // A tag's signer travels with it, as the bookmark's does: a tag made
2741
        // by `oxy tag create` under a short-lived key was signed by a key the
2742
        // receiver has never seen, so its identity and certificate go too.
2743
        let tag_signers: Vec<KeyId> = tags_to_send
2744
            .iter()
2745
            .chain(&withdrawals_to_send)
2746
            .filter_map(|t| t.attestation.as_ref())
2747
            .flat_map(|a| a.sigs.iter().map(|s| s.key))
2748
            .collect();
2749
        for k in &tag_signers {
2750
            if let Ok(id) = self.identity(k) {
2751
                let body = id.encode();
2752
                if !bundle
2753
                    .objects
2754
                    .iter()
2755
                    .any(|(t, b)| *t == ObjectType::Identity && *b == body)
2756
                {
2757
                    bundle.objects.push((ObjectType::Identity, body));
2758
                }
2759
            }
2760
        }
2761
        if !tag_signers.is_empty() {
2762
            self.attach_certificates_for(&mut bundle, &tag_signers)?;
2763
        }
2764
        if let Some(l) = &policy_to_send {
2765
            let remote_serial = resp_policy_serial;
2766
            bundle.policies = self
2767
                .policy_history()?
2768
                .into_iter()
2769
                .filter(|sp| {
2770
                    remote_serial.is_none_or(|r| sp.policy.serial > r)
2771
                        && sp.policy.serial < l.policy.serial
2772
                })
2773
                .collect();
2774
        }
2775
        let results = remote.push(
2776
            bundle,
2777
            vec![RefUpdate {
2778
                name: bookmark.clone(),
2779
                old_state: remote_state,
2780
                new_state,
2781
                force,
2782
                attestation,
2783
            }],
2784
            policy_to_send,
2785
            tags_to_send
2786
                .into_iter()
2787
                .chain(withdrawals_to_send)
2788
                .collect(),
2789
        )?;
2790
        if let (Some(serial), true) = (
2791
            sent_serial,
2792
            results.iter().any(|r| r.ok && r.name == "policy"),
2793
        ) {
2794
            self.record_policy_serial(remote_name, serial)?;
2795
        }
2796
        if results.iter().any(|r| r.ok && r.name == bookmark) {
2797
            let mut view = op.view.clone();
2798
            view.remotes
2799
                .entry(remote_name.to_string())
2800
                .or_default()
2801
                .insert(bookmark.clone(), new_state);
2802
            self.record_op(&[op_ref], &format!("oxy push {remote_name}"), view)?;
2803
            if let Some(s) = serial {
2804
                self.record_serial(remote_name, &bookmark, s)?;
2805
            }
2806
        }
2807
        Ok(results)
2808
    }
2809
2810
    /// Clone a remote into `dest`: same repository ID, `origin` remote,
2811
    /// bookmark set to the remote's, working copy checked out.
2812
    pub fn clone(url: &str, dest: &Path) -> Result<Repo> {
2813
        Self::clone_with(url, dest, None, &[])
2814
    }
2815
2816
    /// Clone, leaving blobs above `max_blob` bytes to be fetched on first
2817
    /// read, and materializing only `sparse` paths (which implies lazy blobs
2818
    /// unless a size is given, so out-of-scope binaries are never fetched).
2819
    pub fn clone_with(
2820
        url: &str,
2821
        dest: &Path,
2822
        max_blob: Option<u64>,
2823
        sparse: &[String],
2824
    ) -> Result<Repo> {
2825
        Self::clone_full(url, dest, max_blob, sparse, None)
2826
    }
2827
2828
    /// Shallow clone (spec §17): a checkpoint of the bookmark's heads plus the
2829
    /// changes within `depth` steps of them; the rest is the boundary.
2830
    pub fn clone_shallow(
2831
        url: &str,
2832
        dest: &Path,
2833
        depth: u32,
2834
        max_blob: Option<u64>,
2835
        sparse: &[String],
2836
    ) -> Result<Repo> {
2837
        Self::clone_full(url, dest, max_blob, sparse, Some(depth))
2838
    }
2839
2840
    fn clone_full(
2841
        url: &str,
2842
        dest: &Path,
2843
        max_blob: Option<u64>,
2844
        sparse: &[String],
2845
        depth: Option<u32>,
2846
    ) -> Result<Repo> {
2847
        Ok(Self::clone_reporting(url, dest, max_blob, sparse, depth)?.0)
2848
    }
2849
2850
    /// Clone as [`Repo::clone_with`] (or [`Repo::clone_shallow`] with a
2851
    /// `depth`), also returning the bookmarks the fetch left out because
2852
    /// their attestation could not be verified ([`FetchReport::skipped`]),
2853
    /// for the caller to warn about. The bookmark checked out is never one
2854
    /// of them: that fails the clone.
2855
    pub fn clone_reporting(
2856
        url: &str,
2857
        dest: &Path,
2858
        max_blob: Option<u64>,
2859
        sparse: &[String],
2860
        depth: Option<u32>,
2861
    ) -> Result<(Repo, Vec<(String, String)>)> {
2862
        let options = crate::credentials::Credentials::user().options_for(url);
2863
        Self::clone_using(url, dest, max_blob, sparse, depth, &options)
2864
    }
2865
2866
    /// Clone a subrepo with the credentials the parent would use for its
2867
    /// hub (see [`Repo::credentials_for`]). Nothing is recorded in the
2868
    /// subrepo: it finds the same ones again through its parent.
2869
    pub(crate) fn clone_as_subrepo(&self, url: &str, dest: &Path) -> Result<Repo> {
2870
        let options = self.credentials_for(url);
2871
        Ok(Self::clone_using(url, dest, None, &[], None, &options)?.0)
2872
    }
2873
2874
    /// The name of a remote of this repository on the same hub (scheme,
2875
    /// host and port) as `url`.
2876
    fn remote_on_same_hub(&self, url: &str) -> Option<String> {
2877
        let origin = |u: &str| -> Option<String> {
2878
            let (scheme, rest) = u.split_once("://")?;
2879
            if !matches!(scheme, "http" | "https") {
2880
                return None;
2881
            }
2882
            let host = rest.split('/').next()?;
2883
            Some(format!(
2884
                "{}://{}",
2885
                scheme.to_ascii_lowercase(),
2886
                host.to_ascii_lowercase()
2887
            ))
2888
        };
2889
        let want = origin(url)?;
2890
        self.config
2891
            .remotes
2892
            .iter()
2893
            .find(|(_, u)| origin(u).as_deref() == Some(want.as_str()))
2894
            .map(|(name, _)| name.clone())
2895
    }
2896
2897
    /// The token and CA to reach `url` with: those of a remote of this
2898
    /// repository on the same hub (scheme, host and port), else the
2899
    /// environment's (`OXY_TOKEN`, `OXY_CA`), the user's credential store
2900
    /// and enclosing checkouts', as a clone would use them.
2901
    pub(crate) fn credentials_for(&self, url: &str) -> RemoteOptions {
2902
        match self.remote_on_same_hub(url) {
2903
            Some(name) => RemoteOptions {
2904
                repo_id: None,
2905
                cache_dir: None,
2906
                ..self.remote_options(&name)
2907
            },
2908
            None => {
2909
                let mut options = RemoteOptions::default();
2910
                self.fill_credentials(url, &mut options);
2911
                options
2912
            }
2913
        }
2914
    }
2915
2916
    fn clone_using(
2917
        url: &str,
2918
        dest: &Path,
2919
        max_blob: Option<u64>,
2920
        sparse: &[String],
2921
        depth: Option<u32>,
2922
        options: &RemoteOptions,
2923
    ) -> Result<(Repo, Vec<(String, String)>)> {
2924
        let remote = open_remote(url, options)?;
2925
        let id = remote.repo_id()?;
2926
        if id == crate::protocol::UNBOUND_ID {
2927
            return fail(format!(
2928
                "{url} is empty and unbound: push a repository to it first"
2929
            ));
2930
        }
2931
        let mut repo = Repo::init_with_id(dest, id)?;
2932
        repo.config
2933
            .set("remote.origin.url", &crate::protocol::stored_url(url))?;
2934
        if let Some(m) = max_blob {
2935
            repo.config.set("filter.max-blob", &m.to_string())?;
2936
        } else if !sparse.is_empty() {
2937
            repo.config.set("filter.max-blob", "0")?;
2938
        }
2939
        if !sparse.is_empty() {
2940
            repo.config.set("filter.paths", &sparse.join(","))?;
2941
        }
2942
        repo.save_config()?;
2943
        let skipped = if let Some(depth) = depth {
2944
            repo.fetch_shallow("origin", depth)?;
2945
            Vec::new()
2946
        } else {
2947
            repo.fetch("origin")?.skipped
2948
        };
2949
        let (op_ref, op) = repo.head_op()?;
2950
        // The remote's default bookmark, else ours (main), else its only
2951
        // one; a bookmark the fetch skipped counts, so the clone fails
2952
        // rather than check out another (oxygenhub#266).
2953
        let mut theirs = op.view.remotes.get("origin").cloned().unwrap_or_default();
2954
        for (name, _) in &skipped {
2955
            theirs.insert(name.clone(), ObjectRef::ZERO);
2956
        }
2957
        // Asked of the remote as now configured, so a store-backed one
2958
        // answers from the mirror the fetch just filled.
2959
        let advertised = repo
2960
            .open_named_remote("origin")
2961
            .ok()
2962
            .and_then(|r| r.refs().ok())
2963
            .and_then(|r| r.default_bookmark);
2964
        let bookmark = advertised
2965
            .filter(|b| theirs.contains_key(b))
2966
            .or_else(|| {
2967
                theirs
2968
                    .contains_key(&repo.config.bookmark)
2969
                    .then(|| repo.config.bookmark.clone())
2970
            })
2971
            .or_else(|| {
2972
                (theirs.len() == 1)
2973
                    .then(|| theirs.keys().next().cloned())
2974
                    .flatten()
2975
            })
2976
            .unwrap_or_else(|| repo.config.bookmark.clone());
2977
        if let Some((_, why)) = skipped.iter().find(|(name, _)| *name == bookmark) {
2978
            return fail(format!("cannot check out {bookmark}: {why}"));
2979
        }
2980
        let initial = repo.config.bookmark.clone();
2981
        if bookmark != repo.config.bookmark {
2982
            fs::create_dir_all(repo.workspace_bookmark_path().parent().unwrap())?;
2983
            write_atomic(&repo.workspace_bookmark_path(), bookmark.as_bytes())?;
2984
            repo.config.bookmark = bookmark.clone();
2985
        }
2986
        if let Some(state) = theirs.get(&bookmark).copied() {
2987
            let heads = repo.store.get_state(&state)?.heads;
2988
            let mut view = op.view.clone();
2989
            // The empty bookmark `init` made goes when another is checked out.
2990
            if initial != bookmark && repo.heads_of(&view, &initial)?.is_empty() {
2991
                view.bookmarks.remove(&initial);
2992
            }
2993
            view.bookmarks.insert(bookmark, state);
2994
            repo.record_op(&[op_ref], &format!("oxy clone {url}"), view)?;
2995
            repo.checkout(&heads, false, "oxy clone (checkout)")?;
2996
        }
2997
        Ok((repo, skipped))
2998
    }
2999
}
3000
3001
/// The serial of a reference's next attestation: one more than the last the
3002
/// remote reports, or, for a bookmark the remote does not have, the clock in
3003
/// milliseconds, so a bookmark created again after a deletion goes past the
3004
/// deletion's serial the remote keeps.
3005
fn next_serial(last: Option<u64>) -> u64 {
3006
    match last {
3007
        Some(l) => l + 1,
3008
        None => Hlc::now().wall_ms.max(1),
3009
    }
3010
}