OXYGENHUB

Coverage / crates/oxy-core/src/repo/sync.rs 90.2%

2147 of 2379 lines run, 337 of 446 functions · run #384 on main · run missed

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
681866×
    fn ref_attest_path(&self, bookmark: &str) -> PathBuf {
691866×
        self.oxy.join("attest").join(name_file(bookmark))
701866×
    }
71
72
    /// The latest reference attestation stored for a bookmark.
731443×
    pub fn latest_ref_attest(&self, bookmark: &str) -> Result<Option<(ObjectRef, SignedAttest)>> {
741443×
        match fs::read_to_string(self.ref_attest_path(bookmark)) {
75345×
            Ok(h) => {
76345×
                let r = ObjectRef::from_hex(h.trim())
77345×
                    .ok_or_else(|| RepoError::Msg("corrupt attestation pointer".into()))?;
78
                Ok(Some((
79345×
                    r,
80
                    SignedAttest {
81345×
                        attest: self.store.get_attest(&r)?,
82345×
                        sigs: self.store.signatures(&r)?,
83
                    },
84
                )))
85
            }
861098×
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
870×
            Err(e) => Err(e.into()),
88
        }
891443×
    }
90
91109×
    pub(crate) fn set_ref_attest(&self, bookmark: &str, r: &ObjectRef) -> Result<()> {
92109×
        fs::create_dir_all(self.oxy.join("attest"))?;
93109×
        write_atomic(&self.ref_attest_path(bookmark), r.to_hex().as_bytes())
94109×
    }
95
961150×
    pub fn remote_refs(&self) -> Result<RefsResponse> {
971150×
        let view = self.view()?;
981150×
        let mut out = Vec::new();
991205×
        for (name, state) in &view.bookmarks {
1001205×
            let latest = self.latest_ref_attest(name)?.map(|(_, a)| a);
1011205×
            let last_serial = latest.as_ref().and_then(|a| match a.attest.body {
102230×
                AttestBody::Ref { serial, .. } => Some(serial),
1030×
                _ => None,
104230×
            });
1051205×
            let attestation = latest.filter(|a| matches!(&a.attest.body, AttestBody::Ref { new_state, .. } if new_state == state));
1061205×
            let accepted = match &attestation {
107219×
                Some(a) => self.acceptance_of(&a.attest.hash())?,
108986×
                None => None,
109
            };
1101205×
            out.push(RemoteRef {
1111205×
                name: name.clone(),
1121205×
                state: *state,
1131205×
                heads: self.store.get_state(state)?.heads,
1141205×
                attestation,
1151205×
                last_serial,
1161205×
                accepted,
1171205×
                logged: None,
1181205×
                integration: vec![],
119
            });
120
        }
1211150×
        let policy = self.current_policy()?.map(|(_, sp)| sp);
1221150×
        let history = self
1231150×
            .policy_history()?
1241150×
            .into_iter()
1251150×
            .filter(|sp| {
126221×
                policy
127221×
                    .as_ref()
128221×
                    .is_some_and(|p| sp.policy.serial < p.policy.serial)
129221×
            })
1301150×
            .collect();
131
        Ok(RefsResponse {
1321150×
            refs: out,
1331150×
            policy,
1341150×
            tags: self.tag_refs()?,
1351150×
            history,
1361150×
            default_bookmark: Some(self.config.bookmark.clone()),
1371150×
            log: None,
1381150×
            witnesses: vec![],
1391150×
            withdrawn: self.withdrawn_refs()?,
140
        })
1411150×
    }
142
143
    /// Every change reachable from `heads` that is in the store.
1441561×
    fn closure_in_store(&self, heads: &[ChangeHash]) -> Result<BTreeSet<ChangeHash>> {
1451561×
        let mut seen = BTreeSet::new();
1461561×
        let mut stack: Vec<ChangeHash> = heads.to_vec();
1475142×
        while let Some(h) = stack.pop() {
1483581×
            if seen.contains(&h) || !self.store.has(&h) {
1491037×
                continue;
1502544×
            }
1512544×
            seen.insert(h);
1522544×
            stack.extend(self.store.get_change_meta(&h)?.deps);
153
        }
1541561×
        Ok(seen)
1551561×
    }
156
157
    /// The changes within `depth` dependency steps of `heads` (spec §17),
158
    /// nearest first; depth 1 is the heads themselves.
1593×
    pub fn shallow_changes(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> {
1603×
        let mut out = Vec::new();
1613×
        let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
1623×
        let mut layer: Vec<ChangeHash> = heads.to_vec();
1633×
        for _ in 0..depth.max(1) {
1648×
            let mut next = Vec::new();
16512×
            for h in layer {
16612×
                if !seen.insert(h) || !self.store.has(&h) {
1672×
                    continue;
16810×
                }
16910×
                let c = self.store.get_change(&h)?;
17010×
                next.extend(c.deps.iter().copied());
17110×
                out.push(h);
172
            }
1738×
            if next.is_empty() {
1741×
                break;
1757×
            }
1767×
            layer = next;
177
        }
1783×
        out.reverse();
1793×
        Ok(out)
1803×
    }
181
1823953×
    fn shallow_path(&self) -> PathBuf {
1833953×
        self.oxy.join("shallow")
1843953×
    }
185
186
    /// The shallow boundary: dependencies this repository does not hold (spec §17).
1873944×
    pub fn boundary(&self) -> BTreeSet<ChangeHash> {
1883944×
        fs::read_to_string(self.shallow_path())
1893944×
            .map(|t| t.lines().filter_map(ObjectRef::from_hex).collect())
1903944×
            .unwrap_or_default()
1913944×
    }
192
1931533×
    pub fn is_shallow(&self) -> bool {
1941533×
        !self.boundary().is_empty()
1951533×
    }
196
1979×
    fn write_boundary(&self, boundary: &BTreeSet<ChangeHash>) -> Result<()> {
1989×
        if boundary.is_empty() {
1991×
            let _ = fs::remove_file(self.shallow_path());
2001×
            return Ok(());
2018×
        }
2028×
        let text: String = boundary
2038×
            .iter()
2049×
            .map(|h| format!("{}\n", h.to_hex()))
2058×
            .collect();
2068×
        write_atomic(&self.shallow_path(), text.as_bytes())
2079×
    }
208
209
    /// Recompute the boundary from what is stored: every dependency of a
210
    /// stored change that is not itself stored.
2117×
    fn refresh_boundary(&self, imported: &[ChangeHash]) -> Result<()> {
2127×
        let mut boundary = self.boundary();
2137×
        boundary.retain(|h| !self.store.has(h));
21412×
        for h in imported {
21515×
            for d in self.store.get_change_meta(h)?.deps {
21615×
                if !self.store.has(&d) {
2173×
                    boundary.insert(d);
21812×
                }
219
            }
220
        }
2217×
        self.write_boundary(&boundary)
2227×
    }
223
224
    /// Fetch `depth` more levels of history below the shallow boundary.
2252×
    pub fn deepen(&self, remote_name: &str, depth: u32) -> Result<usize> {
2262×
        let boundary = self.boundary();
2272×
        if boundary.is_empty() {
2280×
            return Ok(0);
2292×
        }
2302×
        let remote = self.open_named_remote(remote_name)?;
2312×
        let wanted = remote.shallow(&boundary.iter().copied().collect::<Vec<_>>(), depth)?;
2322×
        let bundle = remote.fetch(&wanted, self.config.max_blob)?;
2332×
        let imported = self.import_bundle_shallow(&bundle, false)?;
2342×
        if self.config.max_blob.is_some() {
2350×
            self.register_missing_blobs(&imported, remote_name)?;
2362×
        }
2372×
        self.refresh_boundary(&imported)?;
2382×
        Ok(imported.len())
2392×
    }
240
241
    /// `closure(want) \ closure(have)`, dependencies first (spec §16.2).
242906×
    pub fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> {
243906×
        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.
246906×
        let boundary = self.boundary();
247906×
        let mut out = Vec::new();
248906×
        let mut done: BTreeSet<ChangeHash> = have.clone();
249906×
        done.extend(boundary.iter().copied());
250
        // Iterative post-order DFS.
251906×
        let shallow = self.is_shallow();
2521398×
        let mut stack: Vec<(ChangeHash, bool)> = want.iter().rev().map(|h| (*h, false)).collect();
2533594×
        while let Some((h, expanded)) = stack.pop() {
2542688×
            if done.contains(&h) {
255499×
                continue;
2562189×
            }
2572189×
            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).
2621×
                if shallow {
2631×
                    done.insert(h);
2641×
                    continue;
2650×
                }
2660×
                return fail(format!("change {} is not in this repository", h.short(12)));
2672188×
            }
2682188×
            if expanded {
2691094×
                done.insert(h);
2701094×
                out.push(h);
2711094×
                continue;
2721094×
            }
2731094×
            stack.push((h, true));
2741094×
            for d in self.store.get_change_meta(&h)?.deps {
275687×
                if !done.contains(&d) {
276196×
                    stack.push((d, false));
277491×
                }
278
            }
279
        }
280906×
        Ok(out)
281906×
    }
282
283
    /// Bundle changes (in the given dependency order) with their signatures,
284
    /// seals, identities, blobs, chunks.
285346×
    pub fn bundle_for(&self, changes: &[ChangeHash]) -> Result<Bundle> {
286346×
        self.bundle_of(changes, None, false, false)
287346×
    }
288
289
    /// Objects by reference for the `objects` endpoint; a blob brings its chunks.
2906×
    pub fn objects_for(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> {
2916×
        let mut out = Vec::new();
2926×
        for r in refs {
2936×
            let (ty, body) = match self.store.get(r) {
2946×
                Ok(x) => x,
2950×
                Err(StoreError::NotFound(_)) => continue,
2960×
                Err(e) => return Err(e.into()),
297
            };
2986×
            if ty == ObjectType::Blob {
2997×
                for c in Blob::decode(&body)?.chunks {
3007×
                    if let Ok((cty, cb)) = self.store.get(&c) {
3017×
                        out.push((cty, cb));
3027×
                    }
303
                }
3040×
            }
3056×
            out.push((ty, body));
306
        }
3076×
        Ok(out)
3086×
    }
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.
31656×
    pub fn bundle_for_filtered(
31756×
        &self,
31856×
        changes: &[ChangeHash],
31956×
        max_blob: Option<u64>,
32056×
    ) -> Result<Bundle> {
32156×
        self.bundle_of(changes, max_blob, false, true)
32256×
    }
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.
328385×
    pub fn bundle_raw_for(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> {
329385×
        self.bundle_of(changes, max_blob, true, true)
330385×
    }
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.
343441×
    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);
346441×
        let key = (self.oxy.clone(), self.head_op()?.0);
347441×
        if let Some(keys) = SERVED
348441×
            .lock()
349441×
            .unwrap_or_else(|p| p.into_inner())
350441×
            .as_ref()
351441×
            .and_then(|m| m.get(&key))
352
        {
353138×
            return Ok(keys.clone());
354303×
        }
355303×
        let keys = self.attest_signers_now()?;
356303×
        let mut served = SERVED.lock().unwrap_or_else(|p| p.into_inner());
357303×
        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.
3602548×
        map.retain(|(oxy, _), _| *oxy != key.0);
361303×
        if map.len() >= 1024 {
3620×
            map.clear();
363303×
        }
364303×
        map.insert(key, keys.clone());
365303×
        Ok(keys)
366441×
    }
367
368303×
    fn attest_signers_now(&self) -> Result<BTreeSet<KeyId>> {
369303×
        let view = self.view()?;
370303×
        let pointers = view
371303×
            .bookmarks
372303×
            .keys()
373314×
            .map(|b| self.ref_attest_path(b))
374303×
            .chain(view.tags.keys().map(|t| self.tag_attest_path(t)));
375303×
        let mut keys = BTreeSet::new();
376336×
        for p in pointers {
377336×
            let Ok(text) = fs::read_to_string(&p) else {
378273×
                continue;
379
            };
38063×
            let Some(r) = ObjectRef::from_hex(text.trim()) else {
3810×
                continue;
382
            };
38363×
            keys.extend(self.store.signatures(&r)?.into_iter().map(|s| s.key));
384
        }
385303×
        Ok(keys)
386303×
    }
387
388
    /// `serving`: a fetch's answer, which carries the identities and
389
    /// certificates of [`Repo::served_attest_signers`] too.
390787×
    fn bundle_of(
391787×
        &self,
392787×
        changes: &[ChangeHash],
393787×
        max_blob: Option<u64>,
394787×
        raw: bool,
395787×
        serving: bool,
396787×
    ) -> Result<Bundle> {
397787×
        let mut bundle = Bundle::default();
398787×
        let mut objects: BTreeSet<ObjectRef> = BTreeSet::new();
399787×
        let mut seals: BTreeSet<ObjectRef> = BTreeSet::new();
4001258×
        let add_object = |r: &ObjectRef, objects: &mut BTreeSet<ObjectRef>| {
4011258×
            if !r.is_zero() && self.store.has(r) {
402364×
                objects.insert(*r);
403894×
            }
4041258×
        };
4051010×
        for h in changes {
406
            // Raw: the stored bytes, and a skeleton for the rest.
4071010×
            let (c, encoded) = if raw {
408548×
                let bytes = self.store.get_typed(h, ObjectType::Change)?;
409548×
                let (skel, texts) = Change::decode_skeleton(&bytes, *h)?;
4104980×
                for t in texts {
4114980×
                    if let crate::objects::TextRef::Blob(r, _, _) = t {
4123×
                        let blob = Blob::decode(&self.store.get_typed(&r, ObjectType::Blob)?)?;
4133×
                        objects.insert(r);
4143×
                        objects.extend(blob.chunks);
4154977×
                    }
416
                }
417548×
                (skel, Some(bytes))
418
            } else {
419462×
                (self.store.get_change(h)?, None)
420
            };
4211010×
            let sigs = self.store.signatures(h)?;
4221010×
            add_object(&c.author, &mut objects);
4231010×
            for s in &sigs {
42432×
                add_object(&s.key, &mut objects);
42532×
            }
426
            // A replacement's new content only: its old content is the new
427
            // content of a change it depends on, which the receiver holds or
428
            // which this bundle carries (oxygen#136: removing ~1,100 binary
429
            // files sent them all again).
4305958×
            for e in &c.edits {
43132×
                if let Edit::Replace { new: Some(b), .. } = e {
43232×
                    if self.store.has(b) {
43332×
                        if let Ok(blob) = Blob::decode(&self.store.get_typed(b, ObjectType::Blob)?)
434
                        {
43532×
                            if max_blob.is_some_and(|m| blob.length > m) {
4364×
                                continue;
43728×
                            }
43828×
                            objects.insert(*b);
43928×
                            objects.extend(blob.chunks.iter().copied());
4400×
                        }
4410×
                    }
4425926×
                }
443
            }
4441010×
            for s in self.store.seals_for(h)? {
445105×
                seals.insert(s);
446105×
            }
4471010×
            match encoded {
448548×
                Some(bytes) => bundle.raw_changes.push((bytes, sigs)),
449462×
                None => {
450462×
                    bundle.changes.push((c, sigs));
451462×
                    bundle.hashes.push(*h);
452462×
                }
453
            }
454
        }
455787×
        for r in seals {
456100×
            let attest = self.store.get_attest(&r)?;
457100×
            let sigs = self.store.signatures(&r)?;
458100×
            for s in &sigs {
459100×
                add_object(&s.key, &mut objects);
460100×
            }
461100×
            bundle.attests.push(SignedAttest { attest, sigs });
462
        }
463787×
        let signers: Vec<KeyId> = if serving {
464441×
            let signers = self.served_attest_signers()?;
465441×
            for k in &signers {
466116×
                add_object(k, &mut objects);
467116×
            }
468441×
            signers.into_iter().collect()
469
        } else {
470346×
            Vec::new()
471
        };
472787×
        for r in objects {
473334×
            let (ty, body) = self.store.get(&r)?;
474334×
            bundle.objects.push((ty, body));
475
        }
476787×
        self.attach_certificates_for(&mut bundle, &signers)?;
477787×
        Ok(bundle)
478787×
    }
479
480
    /// Add the certificates of short-lived keys that signed what `bundle`
481
    /// carries, and their issuers' identities, so a receiver can tell who
482
    /// they stand for (spec §14.7). `also` names more signers (a reference
483
    /// attestation sent beside the bundle).
484914×
    pub(crate) fn attach_certificates_for(
485914×
        &self,
486914×
        bundle: &mut Bundle,
487914×
        also: &[KeyId],
488914×
    ) -> Result<()> {
489914×
        let mut keys: BTreeSet<KeyId> = also.iter().copied().collect();
490914×
        keys.extend(
491914×
            bundle
492914×
                .changes
493914×
                .iter()
494914×
                .flat_map(|(_, s)| s.iter().map(|s| s.key)),
495
        );
496914×
        keys.extend(
497914×
            bundle
498914×
                .raw_changes
499914×
                .iter()
500914×
                .flat_map(|(_, s)| s.iter().map(|s| s.key)),
501
        );
502914×
        keys.extend(
503914×
            bundle
504914×
                .attests
505914×
                .iter()
506914×
                .flat_map(|a| a.sigs.iter().map(|s| s.key)),
507
        );
508914×
        for k in keys {
509338×
            let Some(r) = self.store.certificate_of(&k)? else {
510289×
                continue;
511
            };
51249×
            let cert = self.store.get_certificate(&r)?;
51349×
            if bundle
51449×
                .certificates
51549×
                .iter()
51649×
                .any(|c| c.cert.hash() == cert.cert.hash())
517
            {
51812×
                continue;
51937×
            }
520
            // An agent's certificate brings its grant's signer too, so the
521
            // receiver can check the grant (oxygen#134).
52237×
            let grantor = cert.cert.grant.as_ref().map(|g| g.grant.grantor);
52381×
            for id in [Some(k), Some(cert.cert.issuer), grantor].iter().flatten() {
52481×
                if let Ok(body) = self.store.get_typed(id, ObjectType::Identity) {
52580×
                    if !bundle
52680×
                        .objects
52780×
                        .iter()
528142×
                        .any(|(t, b)| *t == ObjectType::Identity && *b == body)
52935×
                    {
53035×
                        bundle.objects.push((ObjectType::Identity, body));
53145×
                    }
5321×
                }
533
            }
53437×
            bundle.certificates.push(cert);
535
        }
536914×
        Ok(())
537914×
    }
538
539
    /// Keep the certificates a bundle brings once each verifies with the
540
    /// identity of the issuer it names; one that does not refuses the bundle.
541833×
    fn adopt_certificates(&self, certs: &[crate::cert::SignedCertificate]) -> Result<()> {
542833×
        for c in certs {
54336×
            let issuer = self.store.get_identity(&c.cert.issuer).map_err(|_| {
5440×
                RepoError::Msg(format!(
5450×
                    "a certificate names issuer {}, whose identity did not come with it",
5460×
                    c.cert.issuer.short(12)
5470×
                ))
5480×
            })?;
54936×
            c.verify(&issuer)
55036×
                .map_err(|e| RepoError::Msg(format!("a certificate does not verify: {e}")))?;
55136×
            self.store.put_identity(&c.cert.key)?;
55236×
            self.store.put_certificate(c)?;
553
        }
554833×
        Ok(())
555833×
    }
556
557
    /// A signer authorized for `reference` by what an issuer certified it
558
    /// as (spec §14.7): a short-lived key with a kept certificate that
559
    /// verifies with its issuer, gives `scope`, covers `at_ms` (a time the
560
    /// signer did not choose) and names a subject the policy grants.
56157×
    pub(crate) fn certified_authorized(
56257×
        &self,
56357×
        p: &crate::policy::Policy,
56457×
        signer: &KeyId,
56557×
        reference: &str,
56657×
        scope: Scope,
56757×
        at_ms: u64,
56857×
    ) -> bool {
56957×
        let Ok(Some(r)) = self.store.certificate_of(signer) else {
5705×
            return false;
571
        };
57252×
        let Ok(c) = self.store.get_certificate(&r) else {
5730×
            return false;
574
        };
57552×
        let Some(issuer) = p
57652×
            .identity(&c.cert.issuer)
57752×
            .cloned()
57852×
            .or_else(|| self.store.get_identity(&c.cert.issuer).ok())
579
        else {
5800×
            return false;
581
        };
58252×
        c.verify(&issuer).is_ok()
58352×
            && c.cert.key_id() == *signer
58452×
            && c.cert.covers(scope, at_ms)
58542×
            && p.subject_grant(reference, &c.cert.issuer, &c.cert.subject, scope)
58657×
    }
587
588
    /// Store a bundle's contents. With `strict`, every change must
589
    /// authenticate (signature or seal by its author or a delegate).
590828×
    pub fn import_bundle(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> {
591828×
        self.import_bundle_inner(bundle, strict, false)
592828×
    }
593
594
    /// Like `import_bundle`, accepting changes whose dependencies are absent
595
    /// (they join the shallow boundary).
5965×
    pub fn import_bundle_shallow(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> {
5975×
        self.import_bundle_inner(bundle, strict, true)
5985×
    }
599
600833×
    fn import_bundle_inner(
601833×
        &self,
602833×
        bundle: &Bundle,
603833×
        strict: bool,
604833×
        shallow: bool,
605833×
    ) -> Result<Vec<ChangeHash>> {
606833×
        let batch = self.store.begin_batch()?;
607833×
        let result = self.import_bundle_batched(bundle, strict, shallow);
608833×
        batch.finish()?;
609831×
        result
610833×
    }
611
612833×
    fn import_bundle_batched(
613833×
        &self,
614833×
        bundle: &Bundle,
615833×
        strict: bool,
616833×
        shallow: bool,
617833×
    ) -> Result<Vec<ChangeHash>> {
618833×
        for (ty, _) in &bundle.objects {
6190×
            if !matches!(
620806×
                ty,
621
                ObjectType::Chunk
622
                    | ObjectType::Blob
623
                    | ObjectType::Identity
624
                    | ObjectType::State
625
                    | ObjectType::Checkpoint
626
            ) {
6270×
                return fail(format!("bundle may not carry {} objects", ty.name()));
628806×
            }
629
        }
630
        // All at once: in a large batch they are compressed on every core.
631833×
        let objects: Vec<(ObjectType, &[u8])> = bundle
632833×
            .objects
633833×
            .iter()
634833×
            .map(|(ty, body)| (*ty, body.as_slice()))
635833×
            .collect();
636833×
        self.store.put_many(&objects)?;
637833×
        self.adopt_certificates(&bundle.certificates)?;
638833×
        for sa in &bundle.attests {
639103×
            let r = self.store.put_attest(&sa.attest)?;
640103×
            for sig in &sa.sigs {
641103×
                self.store.put_signature(&r, sig)?;
642
            }
643103×
            if let AttestBody::Seal { hashes } = &sa.attest.body {
644108×
                for h in hashes {
645108×
                    self.store.index_seal(h, &r)?;
646
                }
6470×
            }
648
        }
649
        // Each change's hash: from the bundle when its maker knew it, else
650
        // worked out once (for a change adding a whole tree, hashing is
651
        // chunking all its text).
652833×
        let hashes: Vec<ChangeHash> = if bundle.hashes.len() == bundle.changes.len() {
653824×
            bundle.hashes.clone()
654
        } else {
6559×
            bundle.changes.iter().map(|(c, _)| c.hash()).collect()
656
        };
657833×
        let raw_hashes: Vec<ChangeHash> = bundle
658833×
            .raw_changes
659833×
            .iter()
660833×
            .map(|(b, _)| hash_object(ObjectType::Change, b))
661833×
            .collect();
662833×
        let incoming: BTreeSet<ChangeHash> = hashes.iter().chain(&raw_hashes).copied().collect();
663833×
        let boundary = self.boundary();
664833×
        let mut imported = Vec::new();
665
        // Changes as stored: checked, then stored as they are, their text
666
        // blobs having come with the objects.
667833×
        for ((bytes, sigs), h) in bundle.raw_changes.iter().zip(raw_hashes) {
668549×
            let (skel, texts) = Change::decode_skeleton(bytes, h)?;
669549×
            for d in &skel.deps {
670363×
                if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d)
671
                {
6720×
                    return fail(format!(
673
                        "change {} depends on {}, which is neither stored nor in the bundle",
6740×
                        h.short(12),
6750×
                        d.short(12)
676
                    ));
677363×
                }
678
            }
6794981×
            for t in texts {
6804981×
                if let crate::objects::TextRef::Blob(r, _, _) = t {
6814×
                    if !self.store.has(&r) {
6821×
                        return fail(format!(
683
                            "change {} came without its text blob {}",
6841×
                            h.short(12),
6851×
                            r.short(12)
686
                        ));
6873×
                    }
6884977×
                }
689
            }
690548×
            self.store.put(ObjectType::Change, bytes)?;
691548×
            for sig in sigs {
69213×
                self.store.put_signature(&h, sig)?;
693
            }
694548×
            imported.push(h);
695
        }
696832×
        for ((c, sigs), h) in bundle.changes.iter().zip(hashes) {
697486×
            for d in &c.deps {
698278×
                if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d)
699
                {
7003×
                    return fail(format!(
701
                        "change {} depends on {}, which is neither stored nor in the bundle",
7023×
                        h.short(12),
7033×
                        d.short(12)
704
                    ));
705275×
                }
706
            }
707483×
            let stored = self.store.put_change(c)?;
708483×
            if stored != h {
7090×
                return fail(format!(
710
                    "change {} is not the change the bundle names",
7110×
                    h.short(12)
712
                ));
713483×
            }
714483×
            for sig in sigs {
71528×
                self.store.put_signature(&h, sig)?;
716
            }
717483×
            imported.push(h);
718
        }
719829×
        if strict {
72067×
            for h in &imported {
72136×
                if !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) {
7224×
                    return fail(format!(
723
                        "change {} is not authenticated by its author",
7244×
                        h.short(12)
725
                    ));
72632×
                }
727
            }
728762×
        }
729825×
        Ok(imported)
730833×
    }
731
732
    /// Verify a reference attestation against an update it claims to describe.
733
    /// `allow_equal` lets a client re-read the attestation it already has; a
734
    /// server receiving a push requires a strictly greater serial.
735
    /// A reference attestation signed by a delegate of an identity the
736
    /// rule lists is authorized as that identity (spec §14.5): the identity
737
    /// named the delegate with the `refs` scope, still in force at `at_ms`
738
    /// (the attestation's wall time), and the policy names the identity.
739
    /// Revoked keys authorize nothing either way.
74072×
    pub(crate) fn delegate_authorized(
74172×
        &self,
74272×
        p: &crate::policy::Policy,
74372×
        signer: &KeyId,
74472×
        reference: &str,
74572×
        at_ms: u64,
74672×
    ) -> bool {
74772×
        if p.is_revoked(signer) {
7481×
            return false;
74971×
        }
75071×
        let Some(rule) = p.rule_for(reference) else {
7510×
            return false;
752
        };
75372×
        rule.keys.iter().any(|k| {
75472×
            !p.is_revoked(k)
75572×
                && p.identity(k)
75672×
                    .cloned()
75772×
                    .or_else(|| self.store.get_identity(k).ok())
75872×
                    .is_some_and(|id| id.delegates_to(signer, Scope::REFS, at_ms))
75972×
        })
76072×
    }
761
762
    /// A lasting attestation (a tag) signed by a key the policy has since
763
    /// rotated away from (spec §14.2) is authorized as the key that
764
    /// succeeded it: a key the rule lists reaches the signer through
765
    /// `previous` links, and the attestation is dated (`at_ms`) before the
766
    /// signer's successor was made, so a superseded key makes nothing new.
767
    /// Revoked keys authorize nothing.
76827×
    pub(crate) fn predecessor_authorized(
76927×
        &self,
77027×
        p: &crate::policy::Policy,
77127×
        signer: &KeyId,
77227×
        reference: &str,
77327×
        at_ms: u64,
77427×
    ) -> bool {
77527×
        if p.is_revoked(signer) {
7760×
            return false;
77727×
        }
77827×
        let Some(rule) = p.rule_for(reference) else {
7790×
            return false;
780
        };
78128×
        let identity = |k: &KeyId| {
78228×
            p.identity(k)
78328×
                .cloned()
78428×
                .or_else(|| self.store.get_identity(k).ok())
78528×
        };
78627×
        rule.keys.iter().any(|k| {
78727×
            if p.is_revoked(k) {
7880×
                return false;
78927×
            }
79027×
            let mut cur = identity(k);
79127×
            for _ in 0..32 {
79228×
                let Some(id) = cur else {
7930×
                    return false;
794
                };
7951×
                match id.previous {
7966×
                    Some(prev) if prev == *signer => return at_ms < id.created,
7971×
                    Some(prev) if !p.is_revoked(&prev) => cur = identity(&prev),
79822×
                    _ => return false,
799
                }
800
            }
8010×
            false
80227×
        })
80327×
    }
804
805106×
    fn check_ref_attest(
806106×
        &self,
807106×
        sa: &SignedAttest,
808106×
        name: &str,
809106×
        old_state: &ObjectRef,
810106×
        new_state: &ObjectRef,
811106×
        last_serial: Option<u64>,
812106×
        allow_equal: bool,
813106×
    ) -> std::result::Result<u64, String> {
814104×
        let serial =
815106×
            self.check_ref_attest_body(sa, name, old_state, new_state, last_serial, allow_equal)?;
816104×
        self.verify_attest_signature(sa)?;
817104×
        Ok(serial)
818106×
    }
819
820
    /// What [`Repo::check_ref_attest`] checks short of the signature: the
821
    /// attestation describes this update of this repository with a serial
822
    /// past the last seen.
823179×
    fn check_ref_attest_body(
824179×
        &self,
825179×
        sa: &SignedAttest,
826179×
        name: &str,
827179×
        old_state: &ObjectRef,
828179×
        new_state: &ObjectRef,
829179×
        last_serial: Option<u64>,
830179×
        allow_equal: bool,
831179×
    ) -> std::result::Result<u64, String> {
832
        let AttestBody::Ref {
833179×
            name: n,
834179×
            old_state: o,
835179×
            new_state: nw,
836179×
            serial,
837179×
        } = &sa.attest.body
838
        else {
8390×
            return Err("attestation is not a ref attestation".into());
840
        };
841179×
        if n != name || o != old_state || nw != new_state {
8421×
            return Err("attestation does not describe this update".into());
843178×
        }
844178×
        if sa.attest.repo != self.id().map_err(|e| e.to_string())? {
8450×
            return Err("attestation is for a different repository".into());
846178×
        }
847178×
        if let Some(last) = last_serial {
84893×
            if *serial < last || (*serial == last && !allow_equal) {
8491×
                return Err(format!(
8501×
                    "attestation serial {serial} is not greater than the last seen {last}"
8511×
                ));
85292×
            }
85385×
        }
854177×
        Ok(*serial)
855179×
    }
856
857
    /// An attestation's first signature verifies with its signer's
858
    /// identity, which must be stored here.
859177×
    fn verify_attest_signature(&self, sa: &SignedAttest) -> std::result::Result<(), String> {
860177×
        let r = sa.attest.hash();
861177×
        let sig = sa.sigs.first().ok_or("attestation carries no signature")?;
862177×
        let id = self
863177×
            .store
864177×
            .get_identity(&sig.key)
865177×
            .map_err(|_| format!("unknown key {}", sig.key.short(12)))?;
866171×
        verify_signature(&id, sig, Domain::Attest, &r).map_err(|e| e.to_string())
867177×
    }
868
869
    /// Accept a push (spec §16.4). Without a trust policy every repository
870
    /// is unverified, so unsigned changes and unattested updates are accepted;
871
    /// attestations that are present must still be valid. With a policy,
872
    /// every change and update is checked against it. A newer policy in the
873
    /// push is applied first when it is signed by the current roots.
874321×
    pub fn receive_push(
875321×
        &self,
876321×
        bundle: Bundle,
877321×
        updates: Vec<RefUpdate>,
878321×
        policy: Option<SignedPolicy>,
879321×
        tags: Vec<crate::protocol::TagRef>,
880321×
    ) -> Result<Vec<RefResult>> {
881321×
        Ok(self
882321×
            .receive_push_report(bundle, updates, policy, tags)?
883
            .results)
884321×
    }
885
886
    /// `receive_push` with what a host wants to log: each accepted
887
    /// reference's old and new state and heads, and the changes this push
888
    /// stored (hub spec §6.12).
889368×
    pub fn receive_push_report(
890368×
        &self,
891368×
        bundle: Bundle,
892368×
        updates: Vec<RefUpdate>,
893368×
        policy: Option<SignedPolicy>,
894368×
        tags: Vec<crate::protocol::TagRef>,
895368×
    ) -> Result<PushReport> {
896
        // Updates are checked against the head as it is now, not as this
897
        // handle first read it (oxygenhub#249): another push may have
898
        // landed since.
899368×
        self.refresh()?;
900368×
        let mut report = PushReport::default();
901368×
        let results = &mut report.results;
902368×
        if let Some(sp) = policy {
90336×
            match self.adopt_policy_chain(&bundle.policies, &sp, "push received") {
90434×
                Ok(()) => {
90534×
                    results.push(RefResult {
90634×
                        name: "policy".into(),
90734×
                        ok: true,
90834×
                        reason: format!("serial {}", sp.policy.serial),
90934×
                    });
91034×
                }
9112×
                Err(e) => {
9122×
                    results.push(RefResult {
9132×
                        name: "policy".into(),
9142×
                        ok: false,
9152×
                        reason: e,
9162×
                    });
9172×
                    for u in updates {
9180×
                        results.push(RefResult {
9190×
                            name: u.name,
9200×
                            ok: false,
9210×
                            reason: "policy update was rejected".into(),
9220×
                        });
9230×
                    }
9242×
                    return Ok(report);
925
                }
926
            }
927332×
        }
928366×
        let policy = self.current_policy()?.map(|(_, sp)| sp.policy);
929
        // A push of objects alone (one batch of a long history, oxygen#95)
930
        // names no reference: its outcome is reported as `objects`.
931366×
        let objects_only = updates.is_empty() && tags.is_empty();
932366×
        match self.import_bundle(&bundle, policy.is_some()) {
933360×
            Ok(imported) => {
934360×
                if objects_only {
93511×
                    results.push(RefResult {
93611×
                        name: OBJECTS_RESULT.into(),
93711×
                        ok: true,
93811×
                        reason: format!("{} changes stored", imported.len()),
93911×
                    });
940349×
                }
941360×
                report.imported = imported;
942
            }
9436×
            Err(e) => {
9446×
                if objects_only {
9450×
                    results.push(RefResult {
9460×
                        name: OBJECTS_RESULT.into(),
9470×
                        ok: false,
9480×
                        reason: e.to_string(),
9490×
                    });
9506×
                }
9516×
                for u in updates {
9526×
                    results.push(RefResult {
9536×
                        name: u.name,
9546×
                        ok: false,
9556×
                        reason: e.to_string(),
9566×
                    });
9576×
                }
9586×
                return Ok(report);
959
            }
960
        }
961360×
        let (op_ref, op) = self.head_op()?;
962360×
        let mut view = op.view.clone();
963360×
        let mut names = Vec::new();
964360×
        for u in updates {
965344×
            let outcome = self.apply_update(&mut view, &u, policy.as_ref());
966344×
            let (ok, reason) = match outcome {
967314×
                Ok(()) => (true, String::new()),
96830×
                Err(e) => (false, e),
969
            };
970344×
            if ok {
971314×
                names.push(u.name.clone());
972314×
                let heads = self
973314×
                    .store
974314×
                    .get_state(&u.new_state)
975314×
                    .map(|s| s.heads)
976314×
                    .unwrap_or_default();
977314×
                report.updated.push(RefUpdated {
978314×
                    name: u.name.clone(),
979314×
                    old_state: u.old_state,
980314×
                    new_state: u.new_state,
981314×
                    heads,
982314×
                });
98330×
            }
984344×
            results.push(RefResult {
985344×
                name: u.name,
986344×
                ok,
987344×
                reason,
988344×
            });
989
        }
990360×
        for t in &tags {
99122×
            let name = format!("tags/{}", t.name);
99222×
            if t.is_withdrawal() {
9933×
                match self.adopt_withdrawal(&mut view, t, policy.as_ref(), &mut TagCheck::Received)
994
                {
9952×
                    Ok(removed) => {
996
                        // A withdrawal is an update to nothing, as a
997
                        // bookmark's deletion is.
9982×
                        if removed {
9992×
                            report.updated.push(RefUpdated {
10002×
                                name: name.clone(),
10012×
                                old_state: t.state,
10022×
                                new_state: ObjectRef::ZERO,
10032×
                                heads: vec![],
10042×
                            });
10052×
                        }
10062×
                        names.push(name.clone());
10072×
                        results.push(RefResult {
10082×
                            name,
1009
                            ok: true,
10102×
                            reason: if removed {
10112×
                                "withdrawn"
1012
                            } else {
10130×
                                "already withdrawn"
1014
                            }
10152×
                            .into(),
1016
                        });
1017
                    }
10181×
                    Err(e) => results.push(RefResult {
10191×
                        name,
10201×
                        ok: false,
10211×
                        reason: e.reason,
10221×
                    }),
1023
                }
10243×
                continue;
102519×
            }
102619×
            let new = !view.tags.contains_key(&t.name);
102719×
            match self.adopt_tag(&mut view, t, policy.as_ref(), &mut TagCheck::Received) {
1028
                Ok(()) => {
1029
                    // A tag new here is an update too, so a hub can start
1030
                    // its pipeline's tag jobs (oxygenflow#24); one it
1031
                    // already had, sent again, is not.
103214×
                    if new {
103313×
                        report.updated.push(RefUpdated {
103413×
                            name: name.clone(),
103513×
                            old_state: ObjectRef::ZERO,
103613×
                            new_state: t.state,
103713×
                            heads: t.heads.clone(),
103813×
                        });
103913×
                    }
104014×
                    names.push(name.clone());
104114×
                    results.push(RefResult {
104214×
                        name,
104314×
                        ok: true,
104414×
                        reason: String::new(),
104514×
                    });
1046
                }
10475×
                Err(e) => results.push(RefResult {
10485×
                    name,
10495×
                    ok: false,
10505×
                    reason: e.reason,
10515×
                }),
1052
            }
1053
        }
1054360×
        if !names.is_empty() {
1055316×
            self.record_op(
1056316×
                &[op_ref],
1057316×
                &format!("push received: {}", names.join(" ")),
1058316×
                view,
10593×
            )?;
106044×
        }
1061357×
        Ok(report)
1062368×
    }
1063
1064
    /// Check an update's attestation against the latest one for its
1065
    /// reference and store it as the new latest. Failures say what was
1066
    /// being done, for which reference, so a refused push can be diagnosed
1067
    /// from its reason alone.
1068106×
    fn record_update_attest(
1069106×
        &self,
1070106×
        u: &RefUpdate,
1071106×
        sa: &SignedAttest,
1072106×
    ) -> std::result::Result<(), String> {
1073106×
        let name = &u.name;
1074106×
        let last = self
1075106×
            .latest_ref_attest(name)
1076106×
            .map_err(|e| format!("reading the last attestation of {name:?}: {e}"))?
1077106×
            .and_then(|(_, a)| match a.attest.body {
107865×
                AttestBody::Ref { serial, .. } => Some(serial),
10790×
                _ => None,
108065×
            });
1081106×
        self.check_ref_attest(sa, name, &u.old_state, &u.new_state, last, false)?;
1082104×
        let store = |e: &dyn std::fmt::Display| format!("storing the attestation of {name:?}: {e}");
1083104×
        let r = self.store.put_attest(&sa.attest).map_err(|e| store(&e))?;
1084103×
        for sig in &sa.sigs {
1085103×
            self.store.put_signature(&r, sig).map_err(|e| store(&e))?;
1086
        }
1087103×
        self.set_ref_attest(name, &r).map_err(|e| store(&e))?;
1088
        // Signed by a short-lived key: a host records when it accepted it.
1089100×
        let certified = sa
1090100×
            .sigs
1091100×
            .first()
1092100×
            .is_some_and(|s| self.store.certificate_of(&s.key).ok().flatten().is_some());
1093100×
        if certified {
10946×
            self.record_acceptance(&r).map_err(|e| store(&e))?;
109594×
        }
1096100×
        Ok(())
1097106×
    }
1098
1099
    /// Record that this host accepted attestation `r` now, signed with its
1100
    /// acceptor key (spec §14.7); nothing without one.
110111×
    pub(crate) fn record_acceptance(&self, r: &ObjectRef) -> Result<()> {
110211×
        let Some(acceptor) = &self.acceptor else {
11037×
            return Ok(());
1104
        };
11054×
        let (id, sk) = (&acceptor.0, &acceptor.1);
11064×
        let attest = Attest {
11074×
            repo: self.id()?,
11084×
            hlc: Hlc::now(),
11094×
            body: AttestBody::Accepted { attest: *r },
1110
        };
11114×
        let sig = sk.sign(Domain::Attest, &id.hash(), &attest.hash());
11124×
        self.store.put_identity(id)?;
11134×
        let a = self.store.put_attest(&attest)?;
11144×
        self.store.put_signature(&a, &sig)?;
11154×
        self.store.index_add_pub("accepted", r, &a)?;
11164×
        Ok(())
111711×
    }
1118
1119
    /// The host's acceptance of attestation `r`, if one was recorded.
1120227×
    pub fn acceptance_of(&self, r: &ObjectRef) -> Result<Option<SignedAttest>> {
1121227×
        let Some(a) = self.store.index_get_pub("accepted", r)?.into_iter().next() else {
1122206×
            return Ok(None);
1123
        };
1124
        Ok(Some(SignedAttest {
112521×
            attest: self.store.get_attest(&a)?,
112621×
            sigs: self.store.signatures(&a)?,
1127
        }))
1128227×
    }
1129
1130
    /// When a certified signer's attestation `sa` is checked on a fetch: the
1131
    /// host's acceptance time, if `accepted` is signed by the issuer of the
1132
    /// signer's certificate and accepts `sa`; else the attestation's own
1133
    /// time, which the signer chose.
113431×
    pub(crate) fn certified_time(
113531×
        &self,
113631×
        signer: &KeyId,
113731×
        sa: &SignedAttest,
113831×
        accepted: Option<&SignedAttest>,
113931×
    ) -> u64 {
114031×
        self.issuer_acceptance(signer, sa, accepted)
114131×
            .unwrap_or(sa.attest.hlc.wall_ms)
114231×
    }
1143
1144
    /// The time of `accepted` when it accepts exactly `sa` and is signed by
1145
    /// the issuer of `signer`'s kept certificate (spec §14.7); else none.
1146101×
    pub(crate) fn issuer_acceptance(
1147101×
        &self,
1148101×
        signer: &KeyId,
1149101×
        sa: &SignedAttest,
1150101×
        accepted: Option<&SignedAttest>,
1151101×
    ) -> Option<u64> {
1152101×
        let acc = accepted?;
115346×
        let c = self.store.certificate_of(signer).ok().flatten()?;
115446×
        let cert = self.store.get_certificate(&c).ok()?;
115546×
        let AttestBody::Accepted { attest } = &acc.attest.body else {
11560×
            return None;
1157
        };
115851×
        let sig = acc.sigs.iter().find(|s| s.key == cert.cert.issuer)?;
115943×
        let issuer = self.store.get_identity(&cert.cert.issuer).ok()?;
116043×
        (*attest == sa.attest.hash()
116142×
            && verify_signature(&issuer, sig, Domain::Attest, &acc.attest.hash()).is_ok())
116243×
        .then_some(acc.attest.hlc.wall_ms)
1163101×
    }
1164
1165
    /// Keep an acceptance this repository was given for attestation `r`
1166
    /// (one [`Repo::issuer_acceptance`] took), so it is served again with
1167
    /// what it accepts.
11689×
    pub(crate) fn keep_acceptance(&self, r: &ObjectRef, acc: &SignedAttest) -> Result<()> {
11699×
        let a = self.store.put_attest(&acc.attest)?;
117011×
        for sig in &acc.sigs {
117111×
            self.store.put_signature(&a, sig)?;
1172
        }
11739×
        self.store.index_add_pub("accepted", r, &a)?;
11749×
        Ok(())
11759×
    }
1176
1177
    /// A fetched bookmark's attestation by a signer the policy does not
1178
    /// list itself: a delegate or a short-lived key, whose authority
1179
    /// depends on when it signed (spec §14.5, §14.8). Checked at the
1180
    /// attestation's integrated time when the host's log gives one; else
1181
    /// as before, at the host's acceptance for a certified signer, when it
1182
    /// countersigned one, or at the attestation's own time, and said so
1183
    /// when the host keeps a log. Failing at the integrated time but not
1184
    /// as before is a note while logs are new; a large gap between the
1185
    /// claimed and the integrated time is a note.
1186
    #[allow(clippy::too_many_arguments)]
118727×
    pub(crate) fn authorize_in_time(
118827×
        &self,
118927×
        p: &crate::policy::Policy,
119027×
        remote_name: &str,
119127×
        r: &crate::protocol::RemoteRef,
119227×
        sa: &SignedAttest,
119327×
        signer: &KeyId,
119427×
        log: &super::transparency::LogCheck,
119527×
        notes: &mut Vec<String>,
119627×
    ) -> Result<()> {
1197
        use super::transparency::{gap_note, judge, Timed};
119827×
        let own = sa.attest.hlc.wall_ms;
119927×
        let at = self.certified_time(signer, sa, r.accepted.as_ref());
120027×
        let time = log.time_of(&r.name);
120129×
        let check = |t_delegate: u64, t_cert: u64| {
120229×
            self.delegate_authorized(p, signer, &r.name, t_delegate)
120327×
                || self.certified_authorized(p, signer, &r.name, Scope::REFS, t_cert)
120429×
        };
120527×
        let before = if at != own {
12067×
            "the host's acceptance"
1207
        } else {
120820×
            "the time it claims"
1209
        };
121027×
        let what = format!("{remote_name}/{}", r.name);
121127×
        match judge(time.as_ref().ok().map(|i| i.ms()), |t| check(t, t), || check(own, at)) {
1212
            Timed::Refused => {
12131×
                return fail(format!(
1214
                    "{what}: attested by {}, which the policy does not authorize for this bookmark; a policy owner can grant it with `oxy policy grant`",
12151×
                    signer.short(12)
1216
                ))
1217
            }
12183×
            Timed::Integrated => {}
12191×
            Timed::Disagrees => notes.push(format!(
1220
                "{what}: {}'s authority had lapsed when witness {} first saw its attestation, though not at {before}; accepted while transparency logs are new",
12211×
                signer.short(12),
12221×
                time.as_ref().map(|i| i.witness.as_str()).unwrap_or("?")
1223
            )),
12241×
            Timed::Fallback if log.kept => notes.push(format!(
1225
                "{what}: {} checked at {before}, not at a witnessed time: {}",
12261×
                signer.short(12),
12271×
                time.as_ref().err().map(String::as_str).unwrap_or("none")
1228
            )),
122921×
            Timed::Fallback => {}
1230
        }
123126×
        if let Some(n) = time.ok().and_then(|i| gap_note(own, i)) {
12322×
            notes.push(format!("{what}'s attestation {n}"));
123324×
        }
123426×
        Ok(())
123527×
    }
1236
1237
    /// A deletion (spec §16.4): `new_state` is zero. The bookmark must exist
1238
    /// at `old_state`, is never the bookmark a non-bare repository has
1239
    /// checked out (a bare one's default is its host's to protect, see
1240
    /// `oxy_server::Backend::default_bookmark`), and under a policy needs
1241
    /// an attestation by a key that may update it.
1242
    /// The deletion's attestation stays the bookmark's latest, so an older
1243
    /// one cannot be replayed to bring it back.
12443×
    fn apply_deletion(
12453×
        &self,
12463×
        view: &mut View,
12473×
        u: &RefUpdate,
12483×
        policy: Option<&crate::policy::Policy>,
12493×
    ) -> std::result::Result<(), String> {
12503×
        let Some(current) = view.bookmarks.get(&u.name).copied() else {
12510×
            return Err(format!("no bookmark {:?} to delete", u.name));
1252
        };
12533×
        if current != u.old_state {
12540×
            return Err("stale: the remote bookmark has moved; pull first".into());
12553×
        }
12563×
        if !self.is_bare() && u.name == self.config.bookmark {
12571×
            return Err(format!(
12581×
                "{:?} is this repository's default bookmark (checked out there) and cannot be deleted",
12591×
                u.name
12601×
            ));
12612×
        }
12622×
        if let Some(p) = policy {
12630×
            let Some(sa) = &u.attestation else {
12640×
                return Err(format!(
12650×
                    "deleting {:?} requires a signed reference attestation under this repository's policy",
12660×
                    u.name
12670×
                ));
1268
            };
12690×
            let signer = sa
12700×
                .sigs
12710×
                .first()
12720×
                .map(|s| s.key)
12730×
                .ok_or("attestation carries no signature")?;
1274
            // Received now: the receiver's own clock is the trusted time.
12750×
            let now = Hlc::now().wall_ms;
12760×
            if !p.authorizes(&signer, &u.name)
12770×
                && !self.delegate_authorized(p, &signer, &u.name, sa.attest.hlc.wall_ms)
12780×
                && !self.certified_authorized(p, &signer, &u.name, Scope::REFS, now)
1279
            {
12800×
                return Err(format!(
12810×
                    "key {} may not delete {:?} under this repository's policy",
12820×
                    signer.short(12),
12830×
                    u.name
12840×
                ));
12850×
            }
12862×
        }
12872×
        if let Some(sa) = &u.attestation {
12881×
            self.record_update_attest(u, sa)?;
12891×
        }
12902×
        view.bookmarks.remove(&u.name);
12912×
        Ok(())
12923×
    }
1293
1294344×
    fn apply_update(
1295344×
        &self,
1296344×
        view: &mut View,
1297344×
        u: &RefUpdate,
1298344×
        policy: Option<&crate::policy::Policy>,
1299344×
    ) -> std::result::Result<(), String> {
1300344×
        if u.name == "policy" {
13010×
            return Err(
13020×
                "the policy reference is updated by sending a policy, not a bookmark update".into(),
13030×
            );
1304344×
        }
1305344×
        let empty = self
1306344×
            .store
1307344×
            .put_state(&StateObject::new(vec![]))
1308344×
            .map_err(|e| e.to_string())?;
1309344×
        if u.new_state.is_zero() {
13103×
            return self.apply_deletion(view, u, policy);
1311341×
        }
1312341×
        let current = view.bookmarks.get(&u.name).copied().unwrap_or(empty);
1313341×
        if current != u.old_state {
131410×
            return Err("stale: the remote bookmark has moved; pull first".into());
1315331×
        }
1316331×
        let new_state = self
1317331×
            .store
1318331×
            .get_state(&u.new_state)
1319331×
            .map_err(|_| "new state object is missing".to_string())?;
1320617×
        for h in &new_state.heads {
1321617×
            if !self.store.has(h) {
13221×
                return Err(format!("head {} is missing", h.short(12)));
1323616×
            }
1324
        }
1325330×
        if !u.force {
1326329×
            let old_heads = self
1327329×
                .store
1328329×
                .get_state(&current)
1329329×
                .map_err(|e| e.to_string())?
1330
                .heads;
1331329×
            let closure = self
1332329×
                .closure_in_store(&new_state.heads)
1333329×
                .map_err(|e| e.to_string())?;
1334329×
            if let Some(h) = old_heads.iter().find(|h| !closure.contains(h)) {
13351×
                return Err(format!(
13361×
                    "not a fast-forward: current head {} would be dropped (use --force)",
13371×
                    h.short(12)
13381×
                ));
1339328×
            }
13401×
        }
1341329×
        if let Some(p) = policy {
134259×
            let Some(sa) = &u.attestation else {
13431×
                return Err(format!(
13441×
                    "{:?} requires a signed reference attestation under this repository's policy",
13451×
                    u.name
13461×
                ));
1347
            };
134858×
            let signer = sa
134958×
                .sigs
135058×
                .first()
135158×
                .map(|s| s.key)
135258×
                .ok_or("attestation carries no signature")?;
1353
            // Received now: the receiver's own clock is the trusted time.
135458×
            let now = Hlc::now().wall_ms;
135558×
            if !p.authorizes(&signer, &u.name)
135619×
                && !self.delegate_authorized(p, &signer, &u.name, sa.attest.hlc.wall_ms)
135716×
                && !self.certified_authorized(p, &signer, &u.name, Scope::REFS, now)
1358
            {
135910×
                return Err(format!(
136010×
                    "key {} may not update {:?} under this repository's policy",
136110×
                    signer.short(12),
136210×
                    u.name
136310×
                ));
136448×
            }
136548×
            let old_heads = self
136648×
                .store
136748×
                .get_state(&current)
136848×
                .map_err(|e| e.to_string())?
1369
                .heads;
137048×
            self.state_allowed(&old_heads, &new_state.heads, p, &u.name)?;
1371270×
        }
1372318×
        if let Some(sa) = &u.attestation {
1373105×
            self.record_update_attest(u, sa)?;
1374213×
        }
1375312×
        view.bookmarks.insert(u.name.clone(), u.new_state);
1376312×
        Ok(())
1377344×
    }
1378
1379
    // ------------------------------------------------------------ client side
1380
13811529×
    fn remote_url(&self, name: &str) -> Result<String> {
13821529×
        self.config.remotes.get(name).cloned().ok_or_else(|| {
13833×
            let names: Vec<&str> = self.config.remotes.keys().map(String::as_str).collect();
13843×
            RepoError::Msg(match names.as_slice() {
13853×
                [] => format!("no remote named {name:?}; add one with `oxy remote add`"),
13861×
                _ if name == "origin" && names.len() > 1 && self.config.default_remote.is_none() => format!(
1387
                    "this repository has no remote named origin, and several others ({}): name one (`oxy pull {}`), or make one the default with `oxy config core.remote {}`",
13881×
                    names.join(", "),
13891×
                    names[0],
13901×
                    names[0]
1391
                ),
13920×
                _ => format!(
1393
                    "no remote named {name:?}; this repository has {}",
13940×
                    names.join(", ")
1395
                ),
1396
            })
13973×
        })
13981529×
    }
1399
1400
    /// Refuse a remote that is another repository. An **unbound** remote
1401
    /// (id all zero: a hosted repository nobody has pushed to) is accepted
1402
    /// for pushes, which bind it to this repository's id (spec §16.1).
1403830×
    fn check_remote_id(
1404830×
        &self,
1405830×
        remote: &dyn crate::protocol::Remote,
1406830×
        remote_name: &str,
1407830×
        allow_unbound: bool,
1408830×
    ) -> Result<()> {
1409830×
        let theirs = remote.repo_id()?;
1410829×
        if theirs == self.id()? {
1411800×
            return Ok(());
141229×
        }
141329×
        if theirs == crate::protocol::UNBOUND_ID {
141426×
            if allow_unbound {
141525×
                return Ok(());
14161×
            }
14171×
            return fail(format!(
1418
                "remote {remote_name:?} is empty and unbound: push to it first"
1419
            ));
14203×
        }
14213×
        fail(format!(
1422
            "remote {remote_name:?} is a different repository (repository IDs differ); check its URL with `oxy remote list`"
1423
        ))
1424830×
    }
1425
1426
    /// Credentials and trust for a remote: `remote.<name>.token` and
1427
    /// `remote.<name>.ca`, else the defaults for its URL
1428
    /// ([`Repo::fill_credentials`]).
14291139×
    pub(crate) fn remote_options(&self, name: &str) -> RemoteOptions {
14301139×
        let url = self.config.remotes.get(name).cloned().unwrap_or_default();
14311139×
        let mut options = RemoteOptions {
14321139×
            repo_id: self.id().ok(),
14331139×
            token: self.config.remote_tokens.get(name).cloned(),
14341139×
            ca: self.config.remote_cas.get(name).map(PathBuf::from),
14351139×
            cache_dir: Some(self.oxy.join("remotes").join(name).join("strata")),
14361139×
            ..RemoteOptions::default()
14371139×
        };
14381139×
        self.fill_credentials(&url, &mut options);
14391139×
        options
14401139×
    }
1441
1442
    /// The user's credential store (`credentials.toml` beside the user
1443
    /// configuration file).
14441162×
    pub fn credentials(&self) -> crate::credentials::Credentials {
14451162×
        crate::credentials::Credentials::at(
14461162×
            self.user_config
14471162×
                .parent()
14481162×
                .map(Path::to_path_buf)
14491162×
                .unwrap_or_else(default_home),
1450
        )
14511162×
    }
1452
1453
    /// Fill in what `options` lacks for `url`: `$OXY_TOKEN` and `$OXY_CA`,
1454
    /// then the user's credential store for its hub, then a remote on the
1455
    /// same hub in an enclosing checkout (a subrepo borrows its parent's).
1456
    /// When the token is not this remote's own or the environment's, the
1457
    /// other tokens enclosing checkouts hold for the hub are kept to try
1458
    /// if it is refused: a stale stored login must not hide a parent's
1459
    /// good token (oxygen#112).
14601163×
    fn fill_credentials(&self, url: &str, options: &mut RemoteOptions) {
14611163×
        if options.token.is_some() && options.ca.is_some() {
14621×
            return;
14631162×
        }
14641162×
        let explicit =
14651162×
            options.token.is_some() || std::env::var("OXY_TOKEN").is_ok_and(|t| !t.is_empty());
14661162×
        let stored = self.credentials().options_for(url);
14671162×
        options.token = options.token.take().or(stored.token);
14681162×
        options.ca = options.ca.take().or(stored.ca);
14691162×
        let enclosing = self.enclosing_credentials(url);
14701162×
        if options.token.is_none() {
14711152×
            options.token = enclosing.iter().find_map(|(t, _)| t.clone());
147210×
        }
14731162×
        if options.ca.is_none() {
14741156×
            options.ca = enclosing.iter().find_map(|(_, c)| c.clone());
14756×
        }
14761162×
        if !explicit {
14771155×
            for t in enclosing.into_iter().filter_map(|(t, _)| t) {
14785×
                if options.token.as_ref() != Some(&t) && !options.fallback_tokens.contains(&t) {
14792×
                    options.fallback_tokens.push(t);
14803×
                }
1481
            }
14827×
        }
14831163×
    }
1484
1485
    /// The tokens and CAs of remotes on `url`'s hub in enclosing
1486
    /// checkouts, nearest first.
14871162×
    fn enclosing_credentials(&self, url: &str) -> Vec<(Option<String>, Option<PathBuf>)> {
14881162×
        let mut out = Vec::new();
14891162×
        let Some(hub) = crate::credentials::hub_of(url) else {
14901005×
            return out;
1491
        };
1492157×
        let mut at = self.root.parent();
1493636×
        while let Some(dir) = at {
1494479×
            let Ok(cfg) = read_config_file(&dir.join(OXY_DIR).join("config")) else {
14950×
                break;
1496
            };
1497479×
            for (key, remote_url) in &cfg {
149815×
                let Some(name) = key
149915×
                    .strip_prefix("remote.")
150015×
                    .and_then(|k| k.strip_suffix(".url"))
1501
                else {
15027×
                    continue;
1503
                };
15048×
                if crate::credentials::hub_of(remote_url).as_deref() != Some(hub.as_str()) {
15051×
                    continue;
15067×
                }
15077×
                out.push((
15087×
                    cfg.get(&format!("remote.{name}.token")).cloned(),
15097×
                    cfg.get(&format!("remote.{name}.ca")).map(PathBuf::from),
15107×
                ));
1511
            }
1512479×
            at = dir.parent();
1513
        }
1514157×
        out
15151162×
    }
1516
1517
    /// Push seal attestations on their own (hub spec §7.4): a bundle with
1518
    /// no changes and no reference updates, which the receiver stores.
15190×
    pub fn push_seals(&self, remote_name: &str, seals: &[ObjectRef]) -> Result<()> {
15200×
        let remote = self.open_named_remote(remote_name)?;
15210×
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
15220×
        let mut bundle = Bundle::default();
15230×
        for r in seals {
15240×
            let attest = self.store.get_attest(r)?;
15250×
            let sigs = self.store.signatures(r)?;
15260×
            bundle
15270×
                .attests
15280×
                .push(crate::protocol::SignedAttest { attest, sigs });
1529
        }
15300×
        let results = remote.push(bundle, vec![], None, vec![])?;
15310×
        if let Some(r) = results.iter().find(|r| !r.ok) {
15320×
            return fail(format!("{}: {}", r.name, r.reason));
15330×
        }
15340×
        Ok(())
15350×
    }
1536
1537
    /// The remote whose URL starts with `base` (a hub), if configured.
15384×
    pub fn remote_for_url(&self, base: &str) -> Option<String> {
15394×
        let base = base.trim_end_matches('/');
15404×
        self.config
15414×
            .remotes
15424×
            .iter()
15434×
            .find(|(_, url)| url.trim_end_matches('/') == base)
15444×
            .map(|(name, _)| name.clone())
15454×
    }
1546
1547
    /// Open a remote by name or, failing that, by URL with the default
1548
    /// credentials (see [`Repo::credentials_for`]).
15490×
    pub fn open_remote_at(&self, url: &str) -> Result<(String, Box<dyn crate::protocol::Remote>)> {
15500×
        if let Some(name) = self.remote_for_url(url) {
15510×
            return Ok((name.clone(), self.open_named_remote(&name)?));
15520×
        }
15530×
        let options = RemoteOptions {
15540×
            repo_id: self.id().ok(),
15550×
            ..self.credentials_for(url)
15560×
        };
15570×
        Ok((url.to_string(), open_remote(url, &options)?))
15580×
    }
1559
1560
    /// Credentials for a URL: those of the configured remote with that
1561
    /// URL, else the environment's.
15624×
    pub fn options_for_url(&self, url: &str) -> RemoteOptions {
15634×
        match self.remote_for_url(url) {
15644×
            Some(name) => self.remote_options(&name),
15650×
            None => RemoteOptions {
15660×
                repo_id: self.id().ok(),
15670×
                ..self.credentials_for(url)
15680×
            },
1569
        }
15704×
    }
1571
15721122×
    pub(crate) fn open_named_remote(&self, name: &str) -> Result<Box<dyn crate::protocol::Remote>> {
15731122×
        open_remote(&self.remote_url(name)?, &self.remote_options(name))
15741122×
    }
1575
1576220×
    fn serial_path(&self, remote: &str, bookmark: &str) -> PathBuf {
1577220×
        self.oxy
1578220×
            .join("remotes")
1579220×
            .join(remote)
1580220×
            .join(format!("{bookmark}.serial"))
1581220×
    }
1582
158373×
    fn last_seen_serial(&self, remote: &str, bookmark: &str) -> Option<u64> {
158473×
        fs::read_to_string(self.serial_path(remote, bookmark))
158573×
            .ok()
158673×
            .and_then(|s| s.trim().parse().ok())
158773×
    }
1588
1589147×
    fn record_serial(&self, remote: &str, bookmark: &str, serial: u64) -> Result<()> {
1590147×
        let p = self.serial_path(remote, bookmark);
1591147×
        fs::create_dir_all(p.parent().unwrap())?;
1592147×
        write_atomic(&p, serial.to_string().as_bytes())
1593147×
    }
1594
1595479×
    fn policy_serial_path(&self, remote: &str) -> PathBuf {
1596479×
        self.oxy.join("remotes").join(remote).join("policy.serial")
1597479×
    }
1598
1599
    /// The highest policy serial `remote` has ever presented.
1600407×
    fn remote_policy_serial(&self, remote: &str) -> Option<u64> {
1601407×
        fs::read_to_string(self.policy_serial_path(remote))
1602407×
            .ok()
1603407×
            .and_then(|s| s.trim().parse().ok())
1604407×
    }
1605
160672×
    fn record_policy_serial(&self, remote: &str, serial: u64) -> Result<()> {
160772×
        let p = self.policy_serial_path(remote);
160872×
        fs::create_dir_all(p.parent().unwrap())?;
160972×
        write_atomic(&p, serial.to_string().as_bytes())
161072×
    }
1611
161232×
    fn lazy_dir(&self) -> PathBuf {
161332×
        self.oxy.join("lazy")
161432×
    }
1615
1616
    /// Remember that `blob` was withheld by `remote` (spec §13.5).
16175×
    fn register_lazy(&self, blob: &ObjectRef, remote: &str) -> Result<()> {
16185×
        fs::create_dir_all(self.lazy_dir())?;
16195×
        write_atomic(&self.lazy_dir().join(blob.to_hex()), remote.as_bytes())
16205×
    }
1621
1622
    /// Which remote promised an absent blob, if any.
162317×
    pub fn lazy_remote(&self, blob: &ObjectRef) -> Option<String> {
162417×
        fs::read_to_string(self.lazy_dir().join(blob.to_hex()))
162517×
            .ok()
162617×
            .map(|s| s.trim().to_string())
162717×
    }
1628
1629
    /// Fetch an absent blob (and its chunks) from the remote that promised it.
16305×
    pub(crate) fn fetch_lazy_blob(&self, blob: &ObjectRef) -> Result<()> {
16315×
        let Some(remote_name) = self.lazy_remote(blob) else {
16320×
            return Err(RepoError::Store(StoreError::NotFound(*blob)));
1633
        };
16345×
        let remote = self.open_named_remote(&remote_name)?;
16355×
        let objects = remote.objects(&[*blob])?;
163611×
        for (ty, body) in objects {
163711×
            if matches!(ty, ObjectType::Blob | ObjectType::Chunk) {
163811×
                self.store.put(ty, &body)?;
16390×
            }
1640
        }
16415×
        if !self.store.has(blob) {
16420×
            return fail(format!(
1643
                "{remote_name} did not send blob {}",
16440×
                blob.short(12)
1645
            ));
16465×
        }
16475×
        let _ = fs::remove_file(self.lazy_dir().join(blob.to_hex()));
16485×
        Ok(())
16495×
    }
1650
1651
    /// After importing from `remote`, record every blob the changes reference
1652
    /// that the store lacks as lazily available from that remote.
16535×
    fn register_missing_blobs(&self, changes: &[ChangeHash], remote: &str) -> Result<usize> {
16545×
        let mut n = 0;
16555×
        for h in changes {
16564×
            let c = self.store.get_change(h)?;
165713×
            for e in &c.edits {
165813×
                if let Edit::Replace { old, new, .. } = e {
16596×
                    for b in [old, new].into_iter().flatten() {
16606×
                        if !self.store.has(b) {
16614×
                            self.register_lazy(b, remote)?;
16624×
                            n += 1;
16632×
                        }
1664
                    }
16657×
                }
1666
            }
1667
        }
16685×
        Ok(n)
16695×
    }
1670
1671
    /// Every head this repository knows: bookmarks, remote tracking, working copy.
1672407×
    pub fn known_heads(&self, view: &View) -> Result<Vec<ChangeHash>> {
1673407×
        let mut have: BTreeSet<ChangeHash> = BTreeSet::new();
1674549×
        for st in view
1675407×
            .bookmarks
1676407×
            .values()
1677407×
            .chain(view.remotes.values().flat_map(|b| b.values()))
1678
        {
1679549×
            if let Ok(s) = self.store.get_state(st) {
1680549×
                have.extend(s.heads);
1681549×
            }
1682
        }
1683407×
        have.extend(view.workspaces.values().copied());
1684407×
        Ok(have.into_iter().collect())
1685407×
    }
1686
1687
    /// Fetch every bookmark of a remote into `remotes/<name>/bookmarks/*` (spec §10.2).
1688413×
    pub fn fetch(&self, remote_name: &str) -> Result<FetchReport> {
1689413×
        let remote = self.open_named_remote(remote_name)?;
1690410×
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
1691408×
        let resp = remote.refs()?;
1692
        // The host's transparency log (spec §14.8): a fork fails here.
1693407×
        let log = self.check_log(
1694407×
            &self.remote_url(remote_name)?,
1695407×
            self.config.remote_cas.get(remote_name).map(Path::new),
1696407×
            &resp,
16970×
        )?;
1698407×
        let mut log_notes = log.notes.clone();
1699
        // Trust policy (spec §14.5). Adopt the remote's policy when it is newer
1700
        // and signed by roots we trust, or on first contact. A remote that
1701
        // once presented a policy may never present none or an older one:
1702
        // that is a downgrade. A remote that never presented one is
1703
        // unverified, and this fetch is checked only as far as possible.
1704407×
        let local_policy = self.current_policy()?.map(|(_, sp)| sp.policy);
1705407×
        let seen = self.remote_policy_serial(remote_name);
1706407×
        let mut unverified = false;
1707407×
        match &resp.policy {
170838×
            Some(sp) => {
170938×
                if seen.is_some_and(|s| sp.policy.serial < s) {
17100×
                    return fail(format!("{remote_name} presents policy serial {} but presented {} before: refusing the downgrade", sp.policy.serial, seen.unwrap()));
171138×
                }
171214×
                match &local_policy {
171314×
                    Some(l) if sp.policy.serial < l.serial => {}
171414×
                    Some(l) if sp.policy.serial == l.serial => {
171510×
                        if sp.policy.hash() != l.hash() {
17160×
                            return fail(format!(
1717
                                "{remote_name} presents a different policy with the same serial {}",
1718
                                l.serial
1719
                            ));
172010×
                        }
1721
                    }
1722
                    _ => {
172328×
                        self.adopt_policy_chain(
172428×
                            &resp.history,
172528×
                            sp,
172628×
                            &format!("oxy fetch {remote_name}"),
1727
                        )
172828×
                        .map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?;
1729
                    }
1730
                }
173138×
                self.record_policy_serial(remote_name, sp.policy.serial)?;
1732
            }
1733
            None => {
1734369×
                if seen.is_some() {
17350×
                    return fail(format!("{remote_name} presented a policy before but has none now: refusing the downgrade"));
1736369×
                }
1737369×
                unverified = true;
1738
            }
1739
        }
1740407×
        let policy = if unverified {
1741369×
            None
1742
        } else {
174338×
            self.current_policy()?.map(|(_, sp)| sp.policy)
1744
        };
1745407×
        let (op_ref, op) = self.head_op()?;
1746407×
        let mut view = op.view.clone();
1747407×
        let have = self.known_heads(&view)?;
1748407×
        let mut report = Vec::new();
1749407×
        let mut skipped = Vec::new();
1750407×
        let max_blob = self.config.max_blob;
1751434×
        for r in resp.refs {
1752434×
            let needed = {
1753434×
                let _t = crate::timing::phase("fetch: negotiate");
1754434×
                remote.negotiate(&have, &r.heads)?
1755
            };
1756434×
            let bundle = {
1757434×
                let _t = crate::timing::phase("fetch: bundle");
1758434×
                remote.fetch(&needed, max_blob)?
1759
            };
1760
            // Changes are not individually re-verified here: the bookmark's
1761
            // attestation (checked below) binds its whole state, and the
1762
            // receiver of the push verified the added changes at that time.
1763
            // A shallow repository takes changes whose dependencies it never
1764
            // held (below what it cloned), as its clone did (#137).
1765434×
            let shallow = self.is_shallow();
1766434×
            let imported = {
1767434×
                let _t = crate::timing::phase("fetch: import");
1768434×
                if shallow {
17692×
                    self.import_bundle_shallow(&bundle, false)?
1770
                } else {
1771432×
                    self.import_bundle(&bundle, false)?
1772
                }
1773
            };
1774434×
            if shallow {
17752×
                self.refresh_boundary(&imported)?;
1776432×
            }
1777434×
            if max_blob.is_some() {
17784×
                self.register_missing_blobs(&imported, remote_name)?;
1779430×
            }
1780434×
            let boundary = if shallow {
17812×
                self.boundary()
1782
            } else {
1783432×
                BTreeSet::new()
1784
            };
1785585×
            for h in &r.heads {
1786585×
                if !self.store.has(h) && !boundary.contains(h) {
17870×
                    return fail(format!("remote did not send head {}", h.short(12)));
1788585×
                }
1789
            }
1790434×
            let state = self.store.put_state(&StateObject::new(r.heads.clone()))?;
1791434×
            if state != r.state {
17920×
                return fail(format!(
1793
                    "remote state {} does not match its heads",
17940×
                    r.state.short(12)
1795
                ));
1796434×
            }
1797434×
            if let Some(p) = &policy {
179841×
                let Some(sa) = &r.attestation else {
17990×
                    return fail(format!(
1800
                        "{remote_name}/{} has no attestation, which its policy requires",
1801
                        r.name
1802
                    ));
1803
                };
180441×
                let signer = sa
180541×
                    .sigs
180641×
                    .first()
180741×
                    .map(|s| s.key)
180841×
                    .ok_or_else(|| RepoError::Msg("attestation carries no signature".into()))?;
180941×
                if !p.authorizes(&signer, &r.name) {
181020×
                    self.authorize_in_time(p, remote_name, &r, sa, &signer, &log, &mut log_notes)?;
181121×
                }
1812393×
            }
1813434×
            if let Some(sa) = &r.attestation {
1814
                let AttestBody::Ref {
181573×
                    old_state,
181673×
                    new_state,
1817
                    ..
181873×
                } = &sa.attest.body
1819
                else {
18200×
                    return fail("remote sent a non-ref attestation for a bookmark");
1821
                };
182273×
                let (old_state, new_state) = (*old_state, *new_state);
182373×
                let serial = self
182473×
                    .check_ref_attest_body(
182573×
                        sa,
182673×
                        &r.name,
182773×
                        &old_state,
182873×
                        &new_state,
182973×
                        self.last_seen_serial(remote_name, &r.name),
1830
                        true,
1831
                    )
183273×
                    .map_err(|e| {
18330×
                        RepoError::Msg(format!("attestation for {}/{}: {e}", remote_name, r.name))
18340×
                    })?;
183573×
                if new_state != r.state {
18360×
                    return fail(format!(
1837
                        "attestation for {}/{} does not cover its current state",
1838
                        remote_name, r.name
1839
                    ));
184073×
                }
1841
                // A signature this client cannot verify (its signer unknown
1842
                // here, as from a host that did not send the identity, or
1843
                // bad) fails that bookmark alone: it stays as it was and
1844
                // the rest are fetched (oxygenhub#266). The current
1845
                // bookmark, which a pull would take, still fails the fetch.
1846
                // A policy's requirements were checked above and still fail.
184773×
                if let Err(e) = self.verify_attest_signature(sa) {
184812×
                    let why = format!("attestation for {remote_name}/{}: {e}", r.name);
184912×
                    if r.name == self.config.bookmark {
18503×
                        return fail(why);
18519×
                    }
18529×
                    skipped.push((r.name, why));
18539×
                    continue;
185461×
                }
185561×
                let ar = self.store.put_attest(&sa.attest)?;
185661×
                for sig in &sa.sigs {
185761×
                    self.store.put_signature(&ar, sig)?;
1858
                }
185961×
                self.record_serial(remote_name, &r.name, serial)?;
1860361×
            }
1861422×
            view.remotes
1862422×
                .entry(remote_name.to_string())
1863422×
                .or_default()
1864422×
                .insert(r.name.clone(), state);
1865422×
            report.push((r.name, r.heads, imported.len()));
1866
        }
1867404×
        let mut new_tags = 0;
1868404×
        for t in &resp.tags {
186931×
            if view.tags.contains_key(&t.name) {
18707×
                continue;
187124×
            }
1872
            // Withdrawn here: the name is not used again.
187324×
            if self.tag_withdrawal(&t.name)?.is_some() {
18740×
                continue;
187524×
            }
1876
            // The tag's changes must be here: fetch them if the bookmarks did not bring them.
187724×
            let missing: Vec<ChangeHash> = t
187824×
                .heads
187924×
                .iter()
188024×
                .filter(|h| !self.store.has(h))
188124×
                .copied()
188224×
                .collect();
188324×
            if !missing.is_empty() {
18840×
                let needed = remote.negotiate(&self.known_heads(&view)?, &missing)?;
18850×
                let bundle = remote.fetch(&needed, max_blob)?;
18860×
                self.import_bundle(&bundle, false)?;
188724×
            }
188824×
            self.store.put_state(&StateObject::new(t.heads.clone()))?;
188924×
            let mut check = TagCheck::Fetched {
189024×
                remote: remote_name,
189124×
                log: &log,
189224×
                notes: &mut log_notes,
189324×
            };
189424×
            match self.adopt_tag(&mut view, t, policy.as_ref(), &mut check) {
189522×
                Ok(()) => new_tags += 1,
1896
                // Nothing says when a short-lived key signed it: the tag is
1897
                // left out, said so, and the rest is fetched (oxygen#129).
18982×
                Err(e) if e.untimed => {
18992×
                    skipped.push((format!("tags/{}", t.name), e.reason));
19002×
                }
19010×
                Err(e) => return fail(format!("{remote_name} {}: {}", t.name, e.reason)),
1902
            }
1903
        }
1904
        // The remote withdrew a tag this repository has: so does this one,
1905
        // once the withdrawal is checked as a push's would be; one that is
1906
        // not is left as it is, said so (oxygenhub#301).
1907404×
        let mut withdrawn_tags = Vec::new();
1908404×
        for t in &resp.withdrawn {
19091×
            if !view.tags.contains_key(&t.name) {
19100×
                continue;
19111×
            }
19121×
            let mut check = TagCheck::Fetched {
19131×
                remote: remote_name,
19141×
                log: &log,
19151×
                notes: &mut log_notes,
19161×
            };
19171×
            match self.adopt_withdrawal(&mut view, t, policy.as_ref(), &mut check) {
19181×
                Ok(true) => withdrawn_tags.push(t.name.clone()),
19190×
                Ok(false) => {}
19200×
                Err(e) => skipped.push((
19210×
                    format!("tags/{}", t.name),
19220×
                    format!("withdrawn on {remote_name}, kept here: {}", e.reason),
19230×
                )),
1924
            }
1925
        }
1926404×
        if view != op.view {
1927388×
            self.record_op(&[op_ref], &format!("oxy fetch {remote_name}"), view)?;
192816×
        }
1929404×
        let _ = self.run_hook(
1930404×
            "post-fetch",
1931404×
            &format!(
1932
                "{{\"event\":\"post-fetch\",\"remote\":{},\"bookmarks\":{}}}",
1933404×
                json_str(remote_name),
1934419×
                json_array(report.iter().map(|(n, heads, _)| format!(
1935
                    "{{\"name\":{},\"heads\":{}}}",
1936419×
                    json_str(n),
1937570×
                    json_array(heads.iter().map(|h| json_str(&h.to_hex())))
1938
                )))
1939
            ),
1940
        );
1941404×
        Ok(FetchReport {
1942404×
            refs: report,
1943404×
            unverified,
1944404×
            new_tags,
1945404×
            withdrawn_tags,
1946404×
            log_notes,
1947404×
            skipped,
1948404×
        })
1949413×
    }
1950
1951
    /// Shallow fetch of the remote's bookmark: its checkpoint as our base,
1952
    /// the changes within `depth` steps of its heads, and the boundary below.
19533×
    pub fn fetch_shallow(&self, remote_name: &str, depth: u32) -> Result<usize> {
19543×
        let remote = self.open_named_remote(remote_name)?;
19553×
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
19563×
        let resp = remote.refs()?;
19573×
        let bookmark = self.config.bookmark.clone();
19583×
        if let Some(sp) = &resp.policy {
19590×
            self.adopt_policy_chain(&resp.history, sp, "oxy clone --depth")
19600×
                .map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?;
19610×
            self.record_policy_serial(remote_name, sp.policy.serial)?;
19623×
        }
19633×
        let Some(r) = resp.refs.into_iter().find(|r| r.name == bookmark) else {
19640×
            return fail(format!("{remote_name} has no bookmark {bookmark:?}"));
1965
        };
19663×
        if r.heads.is_empty() {
19670×
            return Ok(0);
19683×
        }
1969
        // Base: the remote's checkpoint of exactly these heads.
19703×
        let cp_bundle = {
19713×
            let _t = crate::timing::phase("shallow: checkpoint request");
19723×
            remote.checkpoint(&r.heads)?
1973
        };
1974
        // In one batch: a big state's checkpoint is tens of thousands of
1975
        // graph blobs and chunks, which stored one by one were each written
1976
        // loose and flushed (a VS Code clone ran at 27 objects a second).
19773×
        let objects: Vec<(ObjectType, &[u8])> = cp_bundle
19783×
            .objects
19793×
            .iter()
19809×
            .map(|(ty, body)| (*ty, body.as_slice()))
19813×
            .collect();
19823×
        let t_store = crate::timing::phase("shallow: store the checkpoint");
19833×
        let batch = self.store.begin_batch()?;
19843×
        let stored = self.store.put_many(&objects);
19853×
        batch.finish()?;
19863×
        drop(t_store);
19873×
        let cp_ref = objects
19883×
            .iter()
19893×
            .zip(stored?)
19909×
            .find(|((ty, _), _)| *ty == ObjectType::Checkpoint)
19913×
            .map(|(_, r)| r);
19923×
        let Some(cp_ref) = cp_ref else {
19930×
            return fail(format!("{remote_name} sent no checkpoint"));
1994
        };
19953×
        self.install_checkpoint(&bookmark, &cp_ref)?;
19963×
        let _t_blobs = crate::timing::phase("shallow: the binary files' blobs");
1997
        // The blobs of the state's opaque files come with no change when the
1998
        // window holds none of the changes that set them (#137): fetch them
1999
        // now, or under `filter.max-blob` leave them to be fetched on read.
20003×
        let cp = crate::objects::Checkpoint::decode(
20013×
            &self.store.get_typed(&cp_ref, ObjectType::Checkpoint)?,
20020×
        )?;
20033×
        let base = State::from_checkpoint_history(&self.read_blob(&cp.payload)?)?;
20043×
        let mut missing: Vec<ObjectRef> = base
20053×
            .opaque_nodes()
20063×
            .iter()
20073×
            .flat_map(|n| base.live_blobs(n))
20083×
            .filter(|b| !self.store.has(b))
20093×
            .collect();
20103×
        missing.sort();
20113×
        missing.dedup();
20123×
        if self.config.max_blob.is_some() {
20131×
            for b in &missing {
20141×
                self.register_lazy(b, remote_name)?;
2015
            }
2016
        } else {
20172×
            for part in missing.chunks(512) {
20181×
                let objects = remote.objects(part)?;
20191×
                let objects: Vec<(ObjectType, &[u8])> = objects
20201×
                    .iter()
20212×
                    .filter(|(ty, _)| matches!(ty, ObjectType::Blob | ObjectType::Chunk))
20222×
                    .map(|(ty, body)| (*ty, body.as_slice()))
20231×
                    .collect();
20241×
                let batch = self.store.begin_batch()?;
20251×
                let stored = self.store.put_many(&objects);
20261×
                batch.finish()?;
20271×
                stored?;
2028
            }
20292×
            if let Some(b) = missing.iter().find(|b| !self.store.has(b)) {
20300×
                return fail(format!("{remote_name} did not send blob {}", b.short(12)));
20312×
            }
2032
        }
2033
        // History window. Depth 0 takes none (#137): the checkpoint holds
2034
        // every change's metadata, and its heads become the boundary, so a
2035
        // state of many heads (33,835 in the VS Code import) costs nothing
2036
        // to fetch.
20373×
        let imported = if depth == 0 {
20382×
            let mut boundary = self.boundary();
20392×
            boundary.extend(r.heads.iter().filter(|h| !self.store.has(h)).copied());
20402×
            self.write_boundary(&boundary)?;
20412×
            Vec::new()
2042
        } else {
20431×
            let wanted = remote.shallow(&r.heads, depth)?;
20441×
            let bundle = remote.fetch(&wanted, self.config.max_blob)?;
20451×
            self.import_bundle_shallow(&bundle, false)?
2046
        };
20473×
        if self.config.max_blob.is_some() {
20481×
            self.register_missing_blobs(&imported, remote_name)?;
20492×
        }
20503×
        self.refresh_boundary(&imported)?;
20513×
        if let Some(sa) = &r.attestation {
20520×
            let ar = self.store.put_attest(&sa.attest)?;
20530×
            for sig in &sa.sigs {
20540×
                self.store.put_signature(&ar, sig)?;
2055
            }
20560×
            if let AttestBody::Ref { serial, .. } = sa.attest.body {
20570×
                self.record_serial(remote_name, &r.name, serial)?;
20580×
            }
20593×
        }
20603×
        let state = self.store.put_state(&StateObject::new(r.heads.clone()))?;
20613×
        let (op_ref, op) = self.head_op()?;
20623×
        let mut view = op.view.clone();
20633×
        view.remotes
20643×
            .entry(remote_name.to_string())
20653×
            .or_default()
20663×
            .insert(r.name.clone(), state);
20673×
        self.record_op(
20683×
            &[op_ref],
20693×
            &format!("oxy fetch {remote_name} --depth {depth}"),
20703×
            view,
20710×
        )?;
20723×
        Ok(imported.len())
20733×
    }
2074
2075
    /// Fetch, then take the union of the local bookmark's state with the
2076
    /// remote's: no merge change, conflicts become reified (spec §9.4).
2077112×
    pub fn pull(&self, remote_name: &str) -> Result<PullReport> {
2078112×
        let _wc = self.lock_working_copy()?;
2079112×
        let skipped = self.fetch(remote_name)?.skipped;
2080108×
        let (op_ref, op) = self.head_op()?;
2081108×
        let current = self.config.bookmark.clone();
2082108×
        let mut local_current = self.heads_of(&op.view, &current)?;
2083108×
        local_current.sort();
2084108×
        let mut view = op.view.clone();
2085108×
        let mut current_heads = local_current.clone();
2086108×
        let mut changed = false;
2087108×
        let mut others = Vec::new();
2088108×
        let tracked = op
2089108×
            .view
2090108×
            .remotes
2091108×
            .get(remote_name)
2092108×
            .cloned()
2093108×
            .unwrap_or_default();
2094113×
        for (name, remote_state) in tracked {
2095
            // Every local bookmark tracks the remote's of the same name;
2096
            // the current one may be new here.
2097113×
            let local_state = op.view.bookmarks.get(&name).copied();
2098113×
            if local_state.is_none() && name != current {
20991×
                continue;
2100112×
            }
2101112×
            if local_state == Some(remote_state) {
210210×
                continue;
2103102×
            }
2104102×
            let local = self.heads_of(&op.view, &name)?;
2105102×
            let remote_heads = self.store.get_state(&remote_state)?.heads;
2106123×
            if remote_heads.iter().all(|h| local.contains(h)) {
21070×
                continue;
2108102×
            }
2109102×
            let mut all = local.clone();
2110102×
            all.extend(remote_heads);
2111102×
            let st = self.load_state_history(&all)?;
2112102×
            let mut heads = st.maximal(&all);
2113102×
            heads.sort();
2114102×
            let mut sorted_local = local;
2115102×
            sorted_local.sort();
2116102×
            if heads == sorted_local {
21171×
                continue;
2118101×
            }
2119101×
            view.bookmarks.insert(
2120101×
                name.clone(),
2121101×
                self.store.put_state(&StateObject::new(heads.clone()))?,
2122
            );
2123101×
            if name == current {
212499×
                changed = true;
212599×
                current_heads = heads;
212699×
            } else {
21272×
                others.push(name);
21282×
            }
2129
        }
2130108×
        if changed || !others.is_empty() {
2131100×
            self.record_op(&[op_ref], &format!("oxy pull {remote_name}"), view)?;
21328×
        }
2133
        // The working copy follows its bookmark whenever nothing would be
2134
        // lost, also when this pull brought nothing new but it had been
2135
        // left behind (oxygen#89).
2136108×
        let checked_out = self.follow_bookmark(&current_heads, &current, "oxy pull")?;
2137
        Ok(PullReport {
2138108×
            heads: if changed {
213999×
                current_heads
2140
            } else {
21419×
                local_current
2142
            },
2143108×
            changed,
2144108×
            checked_out,
2145108×
            others,
2146108×
            skipped,
2147
        })
2148112×
    }
2149
2150
    /// How many changes the working copy's bookmark has that its base
2151
    /// lacks, when the base is part of the bookmark's state (it fell behind,
2152
    /// and `oxy checkout` or `oxy pull` brings it up); `None` when it is on
2153
    /// the bookmark or holds changes the bookmark lacks. History is read
2154
    /// only when the base and the bookmark's heads differ (oxygen#89).
21557×
    pub fn working_copy_behind(&self) -> Result<Option<usize>> {
21567×
        let (_, wc) = self.working_copy()?;
21577×
        let mut base = wc.deps.clone();
21587×
        base.sort();
21597×
        let mut heads = self.bookmark_heads()?;
21607×
        heads.sort();
21617×
        if base == heads || heads.is_empty() {
21626×
            return Ok(None);
21631×
        }
21641×
        let state = self.load_state_history(&heads)?;
21651×
        if !base.iter().all(|h| state.contains(h)) {
21660×
            return Ok(None);
21671×
        }
21681×
        let had = if base.is_empty() {
21690×
            0
2170
        } else {
21711×
            self.load_state_history(&base)?.len()
2172
        };
21731×
        Ok(Some(state.len().saturating_sub(had)).filter(|n| *n > 0))
21747×
    }
2175
2176
    /// Bring the working copy to `heads`, the state of its bookmark
2177
    /// `bookmark`, when nothing would be lost: its base is part of that
2178
    /// state, so the bookmark only adds to it. Clean, it is checked out;
2179
    /// with uncommitted work, the work comes along when the new changes
2180
    /// leave its paths alone (as `git pull` does), else it stays. Before,
2181
    /// it followed only from exactly the bookmark's old heads, so a working
2182
    /// copy already behind (the bookmark moved by `bookmark set`, a landing
2183
    /// or a pull from another workspace) never caught up, and nothing said
2184
    /// so (oxygen#74, #89). Returns whether it moved.
2185108×
    pub fn follow_bookmark(
2186108×
        &self,
2187108×
        heads: &[ChangeHash],
2188108×
        bookmark: &str,
2189108×
        command: &str,
2190108×
    ) -> Result<bool> {
2191108×
        let snap = self.snapshot(command)?;
2192108×
        let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty();
2193108×
        let mut base = snap.base.maximal(&snap.wc.deps);
2194108×
        base.sort();
2195108×
        let mut target = heads.to_vec();
2196108×
        target.sort();
2197108×
        if base == target {
21988×
            return Ok(false);
2199100×
        }
2200100×
        let state = self.load_state_history(heads)?;
2201180×
        if !base.iter().all(|h| state.contains(h)) {
2202
            // The working copy holds changes the bookmark lacks: not ours to move.
22030×
            return Ok(false);
2204100×
        }
2205100×
        if clean {
2206100×
            self.checkout(heads, false, &format!("{command} (checkout)"))?;
2207100×
            Ok(true)
2208
        } else {
22090×
            Ok(self
22100×
                .checkout_carry(heads, bookmark, &format!("{command} (checkout --carry)"))
22110×
                .is_ok())
2212
        }
2213108×
    }
2214
2215
    /// Withdraw tag `tag` on `remote_name` (`oxy push --delete tags/NAME`,
2216
    /// oxygenhub#301): withdrawn here first when it still is a tag here
2217
    /// (`oxy tag delete`), then the withdrawal alone is sent, with its
2218
    /// signer's identity and certificate.
22193×
    pub fn push_tag_withdrawal(&self, remote_name: &str, tag: &str) -> Result<Vec<RefResult>> {
22203×
        let remote = self.open_named_remote(remote_name)?;
22213×
        self.check_remote_id(remote.as_ref(), remote_name, true)?;
22223×
        let resp = remote.refs()?;
22233×
        let name = format!("tags/{tag}");
22243×
        if resp.withdrawn.iter().any(|t| t.name == tag) {
22251×
            return Ok(vec![RefResult {
22261×
                name,
22271×
                ok: true,
22281×
                reason: "already withdrawn".into(),
22291×
            }]);
22302×
        }
22312×
        let Some(there) = resp.tags.iter().find(|t| t.name == tag) else {
22320×
            return fail(format!("{remote_name} has no tag {tag}"));
2233
        };
22342×
        if resp.policy.is_some() && self.secret_key()?.is_none() {
22350×
            return fail(format!("{remote_name} enforces a trust policy; withdrawing a tag needs an identity (`oxy identity create`)"));
22362×
        }
22372×
        if self.tag_withdrawal(tag)?.is_none() {
22382×
            match self.view()?.tags.get(tag) {
22392×
                Some(st) if *st == there.state => {
22402×
                    self.tag_delete(tag, "")?;
2241
                }
2242
                Some(_) => {
22430×
                    return fail(format!(
2244
                        "tag {tag} names another state here than on {remote_name}"
2245
                    ))
2246
                }
2247
                None => {
22480×
                    return fail(format!(
2249
                        "there is no tag {tag} here to withdraw: `oxy fetch {remote_name}` first"
2250
                    ))
2251
                }
2252
            }
22530×
        }
22542×
        let Some(w) = self
22552×
            .withdrawn_refs()?
22562×
            .into_iter()
22572×
            .find(|w| w.name == tag && w.state == there.state)
2258
        else {
22590×
            return fail(format!(
2260
                "tag {tag} was withdrawn here at another state than {remote_name}'s"
2261
            ));
2262
        };
22632×
        let mut bundle = Bundle::default();
22642×
        let signers: Vec<KeyId> = w
22652×
            .attestation
22662×
            .iter()
22672×
            .flat_map(|a| a.sigs.iter().map(|s| s.key))
22682×
            .collect();
22692×
        for k in &signers {
22702×
            if let Ok(id) = self.identity(k) {
22712×
                bundle.objects.push((ObjectType::Identity, id.encode()));
22722×
            }
2273
        }
22742×
        self.attach_certificates_for(&mut bundle, &signers)?;
22752×
        remote.push(bundle, vec![], None, vec![w])
22763×
    }
2277
2278
    /// Push the current bookmark (spec §16.3), sealing this author's unsigned
2279
    /// changes and attesting the reference when a key is available.
2280
    /// Delete `bookmark` on a remote (spec §16.4): an update to the zero
2281
    /// state, attested when we have an identity. The local bookmark, if
2282
    /// any, is kept; only the remote-tracking entry goes.
22838×
    pub fn push_delete(&self, remote_name: &str, bookmark: &str) -> Result<Vec<RefResult>> {
22848×
        if let Some(tag) = bookmark.strip_prefix("tags/") {
22853×
            return self.push_tag_withdrawal(remote_name, tag);
22865×
        }
22875×
        let remote = self.open_named_remote(remote_name)?;
22885×
        self.check_remote_id(remote.as_ref(), remote_name, true)?;
22895×
        let resp = remote.refs()?;
22907×
        let Some(r) = resp.refs.iter().find(|r| r.name == bookmark) else {
22911×
            return fail(format!("{remote_name} has no bookmark {bookmark}"));
2292
        };
22934×
        if resp.policy.is_some() && self.secret_key()?.is_none() {
22940×
            return fail(format!("{remote_name} enforces a trust policy; deleting needs an identity (`oxy identity create`)"));
22954×
        }
22964×
        let attestation = match self.secret_key()? {
22972×
            Some(_) => Some(self.sign_attest(&Attest {
22982×
                repo: self.id()?,
22992×
                hlc: Hlc::now(),
23002×
                body: AttestBody::Ref {
23012×
                    name: bookmark.to_string(),
23022×
                    old_state: r.state,
23032×
                    new_state: ObjectRef::ZERO,
23042×
                    serial: r.last_serial.unwrap_or(0) + 1,
23052×
                },
23060×
            })?),
23072×
            None => None,
2308
        };
23094×
        let results = remote.push(
23104×
            Bundle::default(),
23114×
            vec![RefUpdate {
23124×
                name: bookmark.to_string(),
23134×
                old_state: r.state,
23144×
                new_state: ObjectRef::ZERO,
23154×
                force: true,
23164×
                attestation,
23174×
            }],
23184×
            None,
23194×
            vec![],
23200×
        )?;
23214×
        if results.iter().any(|x| x.ok && x.name == bookmark) {
23222×
            let (op_ref, op) = self.head_op()?;
23232×
            let mut view = op.view.clone();
23242×
            if let Some(m) = view.remotes.get_mut(remote_name) {
23252×
                m.remove(bookmark);
23262×
            }
23272×
            self.record_op(
23282×
                &[op_ref],
23292×
                &format!("oxy push {remote_name} --delete {bookmark}"),
23302×
                view,
23310×
            )?;
23322×
        }
23334×
        Ok(results)
23348×
    }
2335
2336
    /// Remove remote `name` (oxygen#86): every `remote.NAME.*` key of this
2337
    /// repository's config (its URL, token, CA and store settings), its
2338
    /// tracking bookmarks in the view, and a store remote's mirror under
2339
    /// `.oxy/remotes/NAME`. Nothing stays to be read as if it were current.
23403×
    pub fn remove_remote(&mut self, name: &str) -> Result<()> {
23413×
        if !self.config.remotes.contains_key(name) {
23421×
            return fail(format!("no remote {name}: `oxy remote list` shows them"));
23432×
        }
23442×
        let prefix = format!("remote.{name}.");
23452×
        let keys: Vec<String> = self
23462×
            .config
23472×
            .repo_layer()
23482×
            .keys()
23494×
            .filter(|k| k.starts_with(&prefix))
23502×
            .cloned()
23512×
            .collect();
23523×
        for k in keys {
23533×
            self.config.set(&k, "")?;
2354
        }
23552×
        self.config.remotes.remove(name);
23562×
        self.save_config()?;
23572×
        let (op_ref, op) = self.head_op()?;
23582×
        if op.view.remotes.contains_key(name) {
23591×
            let mut view = op.view.clone();
23601×
            view.remotes.remove(name);
23611×
            self.record_op(&[op_ref], &format!("oxy remote remove {name}"), view)?;
23621×
        }
23632×
        let mirror = self.oxy.join("remotes").join(name);
23642×
        if mirror.is_dir() {
23650×
            fs::remove_dir_all(&mirror)?;
23662×
        }
23672×
        Ok(())
23683×
    }
2369
2370
    /// Send `bundle`'s blobs and chunks ahead of it, in parts of about
2371
    /// `push.batch-bytes` of objects alone, when the whole would be larger
2372
    /// (oxygen#133): one change can bring more blobs than one request may
2373
    /// carry, and a proxy in front of a hub refuses a request past its limit
2374
    /// (Cloudflare's is 100 MB). What is left, the changes with their
2375
    /// identities and states, is returned to send as before. A receiver
2376
    /// stores objects as they come, and checks a change's blobs are stored.
2377330×
    fn send_objects_ahead(
2378330×
        &self,
2379330×
        remote: &dyn crate::protocol::Remote,
2380330×
        remote_name: &str,
2381330×
        mut bundle: Bundle,
2382330×
        progress: crate::store::Progress,
2383330×
    ) -> Result<Bundle> {
2384330×
        let limit = self.config.push_batch_bytes;
2385330×
        let changes: u64 = bundle
2386330×
            .changes
2387330×
            .iter()
2388403×
            .map(|(c, _)| c.encode().len() as u64)
2389330×
            .sum();
2390330×
        let objects: u64 = bundle.objects.iter().map(|(_, b)| b.len() as u64).sum();
2391330×
        if changes + objects <= limit {
2392323×
            return Ok(bundle);
23937×
        }
23947×
        let (mut kept, mut parts, mut part, mut bytes) = (Vec::new(), Vec::new(), Vec::new(), 0u64);
23959×
        for (ty, body) in std::mem::take(&mut bundle.objects) {
23969×
            if !matches!(ty, ObjectType::Blob | ObjectType::Chunk) {
23970×
                kept.push((ty, body));
23980×
                continue;
23999×
            }
24009×
            if !part.is_empty() && bytes + body.len() as u64 > limit {
24013×
                parts.push(std::mem::take(&mut part));
24023×
                bytes = 0;
24036×
            }
24049×
            bytes += body.len() as u64;
24059×
            part.push((ty, body));
2406
        }
24077×
        if !part.is_empty() {
24081×
            parts.push(part);
24096×
        }
24107×
        let n = parts.len();
24117×
        for (i, objects) in parts.into_iter().enumerate() {
24124×
            progress(&format!(
24134×
                "objects {} of {n}: {} blobs and chunks",
24144×
                i + 1,
24154×
                objects.len()
24164×
            ));
24174×
            let results = remote.push(
24184×
                Bundle {
24194×
                    objects,
24204×
                    ..Default::default()
24214×
                },
24224×
                vec![],
24234×
                None,
24244×
                vec![],
24250×
            )?;
24264×
            match results.iter().find(|r| r.name == OBJECTS_RESULT) {
24274×
                Some(r) if r.ok => {}
24280×
                Some(r) => {
24290×
                    return fail(format!(
2430
                        "{remote_name} refused objects part {} of {n}: {}",
24310×
                        i + 1,
2432
                        r.reason
2433
                    ))
2434
                }
2435
                None => {
24360×
                    return fail(format!(
2437
                        "{remote_name} does not take a push in parts, and this one is too large for one: update the server's oxy"
2438
                    ))
2439
                }
2440
            }
2441
        }
24427×
        bundle.objects = kept;
24437×
        Ok(bundle)
2444330×
    }
2445
2446
    /// `changes`, in dependency order, cut into batches of about
2447
    /// `push.batch-bytes` by what each change and its texts and blobs weigh.
2448
    /// There is always one, perhaps empty.
2449325×
    fn push_batches(&self, changes: &[ChangeHash]) -> Result<Vec<Vec<ChangeHash>>> {
2450325×
        let blob_len = |r: &ObjectRef| -> u64 {
245137×
            self.store
245237×
                .get_typed(r, ObjectType::Blob)
245337×
                .ok()
245437×
                .and_then(|b| Blob::decode(&b).ok())
245537×
                .map_or(0, |b| b.length)
245637×
        };
2457325×
        let (mut out, mut batch, mut bytes) = (Vec::new(), Vec::new(), 0u64);
2458403×
        for h in changes {
2459403×
            let raw = self.store.get_typed(h, ObjectType::Change)?;
2460403×
            let (c, texts) = Change::decode_skeleton(&raw, *h)?;
2461403×
            let mut weight = raw.len() as u64;
2462498×
            for t in &texts {
2463498×
                if let crate::objects::TextRef::Blob(r, _, _) = t {
246426×
                    weight += blob_len(r);
2465472×
                }
2466
            }
2467647×
            for e in &c.edits {
246811×
                if let Edit::Replace { new: Some(b), .. } = e {
246911×
                    weight += blob_len(b);
2470636×
                }
2471
            }
2472403×
            if !batch.is_empty() && bytes + weight > self.config.push_batch_bytes {
24735×
                out.push(std::mem::take(&mut batch));
24745×
                bytes = 0;
2475398×
            }
2476403×
            batch.push(*h);
2477403×
            bytes += weight;
2478
        }
2479325×
        out.push(batch);
2480325×
        Ok(out)
2481325×
    }
2482
2483387×
    pub fn push(&self, remote_name: &str, force: bool) -> Result<Vec<RefResult>> {
2484387×
        let bookmark = self.config.bookmark.clone();
2485387×
        self.push_bookmark(remote_name, &bookmark, force)
2486387×
    }
2487
2488
    /// `push` for a named bookmark (`oxy push -b NAME`).
2489401×
    pub fn push_bookmark(
2490401×
        &self,
2491401×
        remote_name: &str,
2492401×
        bookmark: &str,
2493401×
        force: bool,
2494401×
    ) -> Result<Vec<RefResult>> {
2495401×
        self.push_bookmark_with(remote_name, bookmark, force, &mut |_| {})
2496401×
    }
2497
2498
    /// `push_bookmark`, saying how a long push is going: one line per
2499
    /// batch sent (oxygen#95).
2500
    /// What a push may take the remote to hold already: the heads of the
2501
    /// bookmark pushed, and of every other bookmark it lists that are held
2502
    /// here, so a new bookmark made from main sends only what main lacks
2503
    /// (oxygen#142), not its whole history again.
2504327×
    pub(crate) fn remote_holds(
2505327×
        &self,
2506327×
        refs: &[crate::protocol::RemoteRef],
2507327×
        bookmark_heads: &[ChangeHash],
2508327×
    ) -> Vec<ChangeHash> {
2509327×
        let mut have: Vec<ChangeHash> = bookmark_heads.to_vec();
2510366×
        for h in refs.iter().flat_map(|r| &r.heads) {
2511366×
            if !have.contains(h) && self.store.has(h) {
251220×
                have.push(*h);
2513346×
            }
2514
        }
2515327×
        have
2516327×
    }
2517
2518410×
    pub fn push_bookmark_with(
2519410×
        &self,
2520410×
        remote_name: &str,
2521410×
        bookmark: &str,
2522410×
        force: bool,
2523410×
        progress: crate::store::Progress,
2524410×
    ) -> Result<Vec<RefResult>> {
2525410×
        let (op_ref, op) = self.head_op()?;
2526410×
        if !op.view.bookmarks.contains_key(bookmark) {
25270×
            return fail(format!("no bookmark {bookmark}"));
2528410×
        }
2529410×
        let bookmark = bookmark.to_string();
2530410×
        let local_heads = self.heads_of(&op.view, &bookmark)?;
2531
        // pre-push hook: may veto before the remote is contacted.
2532410×
        self.run_hook(
2533410×
            "pre-push",
2534410×
            &format!(
2535
                "{{\"event\":\"pre-push\",\"remote\":{},\"bookmark\":{},\"heads\":{}}}",
2536410×
                json_str(remote_name),
2537410×
                json_str(&bookmark),
2538815×
                json_array(local_heads.iter().map(|h| json_str(&h.to_hex())))
2539
            ),
25401×
        )?;
2541409×
        let remote = self.open_named_remote(remote_name)?;
2542409×
        self.check_remote_id(remote.as_ref(), remote_name, true)?;
2543406×
        let resp = remote.refs()?;
2544406×
        let local_policy = self.current_policy()?.map(|(_, sp)| sp);
2545406×
        let resp_policy_serial = resp.policy.as_ref().map(|p| p.policy.serial);
2546406×
        let policy_to_send = match (&local_policy, &resp.policy) {
254744×
            (Some(l), Some(r)) if l.policy.serial > r.policy.serial => Some(l.clone()),
254816×
            (Some(l), None) => Some(l.clone()),
254926×
            (Some(l), Some(r)) if l.policy.serial < r.policy.serial => {
25501×
                return fail(format!(
2551
                    "{remote_name} has a newer policy (serial {}); fetch first",
2552
                    r.policy.serial
2553
                ));
2554
            }
2555371×
            _ => None,
2556
        };
2557405×
        let remote_has_policy = resp.policy.is_some() || policy_to_send.is_some();
2558405×
        if remote_has_policy && self.secret_key()?.is_none() {
25591×
            return fail(format!("{remote_name} enforces a trust policy; pushing needs an identity (`oxy identity create`)"));
2560404×
        }
2561404×
        let remote_tags: BTreeSet<String> = resp
2562404×
            .tags
2563404×
            .iter()
2564404×
            .chain(&resp.withdrawn)
2565404×
            .map(|t| t.name.clone())
2566404×
            .collect();
2567404×
        let tags_to_send: Vec<crate::protocol::TagRef> = self
2568404×
            .tag_refs()?
2569404×
            .into_iter()
2570404×
            .filter(|t| !remote_tags.contains(&t.name))
2571404×
            .collect();
2572
        // Tags withdrawn here that the remote still has, at the same state
2573
        // (oxygenhub#301).
2574404×
        let withdrawals_to_send: Vec<crate::protocol::TagRef> = self
2575404×
            .withdrawn_refs()?
2576404×
            .into_iter()
2577404×
            .filter(|w| {
25782×
                resp.tags
25792×
                    .iter()
25802×
                    .any(|t| t.name == w.name && t.state == w.state)
25812×
            })
2582404×
            .collect();
2583404×
        let refs = resp.refs;
2584404×
        let empty = self.store.put_state(&StateObject::new(vec![]))?;
2585404×
        let (remote_state, remote_heads, last_serial) =
2586405×
            match refs.iter().find(|r| r.name == bookmark) {
2587381×
                Some(r) => (r.state, r.heads.clone(), r.last_serial),
258823×
                None => (empty, vec![], None),
2589
            };
2590404×
        let new_state = self
2591404×
            .store
2592404×
            .put_state(&StateObject::new(local_heads.clone()))?;
2593
        // A push that carries a policy re-attests the bookmark even when its
2594
        // state is unchanged, so the current attestation is always by a key
2595
        // the current policy authorizes (a revoked key's attestation would
2596
        // otherwise be refused by every later fetch).
2597404×
        if new_state == remote_state
259836×
            && policy_to_send.is_none()
259919×
            && tags_to_send.is_empty()
260010×
            && withdrawals_to_send.is_empty()
2601
        {
26029×
            return Ok(vec![RefResult {
26039×
                name: bookmark,
26049×
                ok: true,
26059×
                reason: "already up to date".into(),
26069×
            }]);
2607395×
        }
2608
        // A shallow repository knows the changes on its boundary without
2609
        // holding them: a depth-0 clone's are the heads it cloned (#137).
2610395×
        let boundary = self.boundary();
2611418×
        for h in &remote_heads {
2612418×
            if !self.store.has(h) && !boundary.contains(h) {
261369×
                return fail(format!(
2614
                    "the remote has change {} which you do not; pull first (`oxy pull {remote_name}`), then push again",
261569×
                    h.short(12)
2616
                ));
2617349×
            }
2618
        }
2619326×
        if !force {
2620326×
            let closure = self.closure_in_store(&local_heads)?;
2621326×
            if let Some(h) = remote_heads
2622326×
                .iter()
2623341×
                .find(|h| !closure.contains(h) && !boundary.contains(h))
2624
            {
26250×
                return fail(format!("not a fast-forward: remote head {} is not in your state; pull first or use --force", h.short(12)));
2626326×
            }
26270×
        }
2628326×
        let to_send = self.negotiate(&self.remote_holds(&refs, &remote_heads), &local_heads)?;
2629326×
        let key = self.secret_key()?.map(|(k, _)| k);
2630326×
        if let Some(key) = key {
2631
            // Seal this author's changes that carry no valid authenticator,
2632
            // and adopt anonymous ones (zero author, spec §14.4). The seals
2633
            // are stored, so each batch's bundle carries those it needs.
2634112×
            let mut unsigned = Vec::new();
2635112×
            for h in &to_send {
263682×
                let c = self.store.get_change_meta(h)?;
263782×
                if self.seals_for_author(&c.author, &key)
263880×
                    && !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. })
263953×
                {
264053×
                    unsigned.push(*h);
264153×
                }
2642
            }
2643112×
            for chunk in unsigned.chunks(crate::attest::MAX_SEAL_HASHES) {
264450×
                self.sign_attest(&Attest::seal(self.id()?, chunk.to_vec()))?;
2645
            }
2646214×
        }
2647
        // A long history goes in batches, in dependency order, so neither
2648
        // side holds it whole and no request passes the remote's limit:
2649
        // all but the last carry objects alone; the last moves the bookmark
2650
        // (oxygen#95).
2651325×
        let batches = self.push_batches(&to_send)?;
2652325×
        let (last, earlier) = batches.split_last().expect("at least one batch");
2653325×
        for (i, batch) in earlier.iter().enumerate() {
26545×
            progress(&format!(
26555×
                "part {} of {}: {} changes",
26565×
                i + 1,
26575×
                batches.len(),
26585×
                batch.len()
26595×
            ));
26605×
            let bundle = self.send_objects_ahead(
26615×
                remote.as_ref(),
26625×
                remote_name,
26635×
                self.bundle_for(batch)?,
26645×
                &mut *progress,
26650×
            )?;
26665×
            let results = remote.push(bundle, vec![], None, vec![])?;
26675×
            match results.iter().find(|r| r.name == OBJECTS_RESULT) {
26685×
                Some(r) if r.ok => {}
26690×
                Some(r) => {
26700×
                    return fail(format!(
2671
                        "{remote_name} refused part {} of the push: {}",
26720×
                        i + 1,
2673
                        r.reason
2674
                    ))
2675
                }
2676
                None => {
26770×
                    return fail(format!(
2678
                        "{remote_name} does not take a push in parts, and this one is too long for one: update the server's oxy"
2679
                    ))
2680
                }
2681
            }
2682
        }
2683325×
        if batches.len() > 1 {
26841×
            progress(&format!(
26851×
                "part {} of {}: {} changes",
26861×
                batches.len(),
26871×
                batches.len(),
26881×
                last.len()
26891×
            ));
2690324×
        }
2691325×
        let mut bundle = self.send_objects_ahead(
2692325×
            remote.as_ref(),
2693325×
            remote_name,
2694325×
            self.bundle_for(last)?,
2695325×
            &mut *progress,
26960×
        )?;
2697325×
        bundle.objects.push((
2698325×
            ObjectType::State,
2699325×
            StateObject::new(local_heads.clone()).encode(),
2700325×
        ));
2701325×
        let attestation = match key {
2702111×
            Some(_) => Some(self.sign_attest(&Attest {
2703111×
                repo: self.id()?,
2704111×
                hlc: Hlc::now(),
2705111×
                body: AttestBody::Ref {
2706111×
                    name: bookmark.clone(),
2707111×
                    old_state: remote_state,
2708111×
                    new_state,
2709111×
                    serial: next_serial(last_serial),
2710111×
                },
27111×
            })?),
2712214×
            None => None,
2713
        };
2714
        // Whoever signed the move, and whom a delegate acts for, travel with
2715
        // it: a receiver that has never seen a change by this delegate could
2716
        // not otherwise check its signature (oxygenhub#129).
2717324×
        if attestation.is_some() {
2718220×
            for k in [self.signing_key(), self.config.author_key]
2719110×
                .into_iter()
2720110×
                .flatten()
2721
            {
2722220×
                if let Ok(id) = self.identity(&k) {
2723220×
                    let body = id.encode();
2724220×
                    if !bundle
2725220×
                        .objects
2726220×
                        .iter()
2727267×
                        .any(|(t, b)| *t == ObjectType::Identity && *b == body)
272838×
                    {
272938×
                        bundle.objects.push((ObjectType::Identity, body));
2730182×
                    }
27310×
                }
2732
            }
2733
            // A short-lived signing key's certificate goes with it (§14.7).
2734110×
            let signers: Vec<KeyId> = self.signing_key().into_iter().collect();
2735110×
            self.attach_certificates_for(&mut bundle, &signers)?;
2736214×
        }
2737324×
        let serial = attestation.as_ref().and_then(|a| match a.attest.body {
2738110×
            AttestBody::Ref { serial, .. } => Some(serial),
27390×
            _ => None,
2740110×
        });
2741324×
        let sent_serial = policy_to_send.as_ref().map(|p| p.policy.serial);
2742
        // Tag states must be present on the remote: bundle their changes too.
2743324×
        for t in &tags_to_send {
274416×
            let extra = self.negotiate(&remote_heads, &t.heads)?;
274516×
            let extra: Vec<ChangeHash> =
274616×
                extra.into_iter().filter(|h| !to_send.contains(h)).collect();
274716×
            if !extra.is_empty() {
27480×
                let more = self.bundle_for(&extra)?;
27490×
                bundle.changes.extend(more.changes);
27500×
                bundle.hashes.extend(more.hashes);
27510×
                bundle.attests.extend(more.attests);
27520×
                bundle.objects.extend(more.objects);
275316×
            }
275416×
            bundle.objects.push((
275516×
                ObjectType::State,
275616×
                StateObject::new(t.heads.clone()).encode(),
275716×
            ));
2758
        }
2759
        // A tag's signer travels with it, as the bookmark's does: a tag made
2760
        // by `oxy tag create` under a short-lived key was signed by a key the
2761
        // receiver has never seen, so its identity and certificate go too.
2762324×
        let tag_signers: Vec<KeyId> = tags_to_send
2763324×
            .iter()
2764324×
            .chain(&withdrawals_to_send)
2765324×
            .filter_map(|t| t.attestation.as_ref())
2766324×
            .flat_map(|a| a.sigs.iter().map(|s| s.key))
2767324×
            .collect();
2768324×
        for k in &tag_signers {
276916×
            if let Ok(id) = self.identity(k) {
277016×
                let body = id.encode();
277116×
                if !bundle
277216×
                    .objects
277316×
                    .iter()
277430×
                    .any(|(t, b)| *t == ObjectType::Identity && *b == body)
27752×
                {
27762×
                    bundle.objects.push((ObjectType::Identity, body));
277714×
                }
27780×
            }
2779
        }
2780324×
        if !tag_signers.is_empty() {
278115×
            self.attach_certificates_for(&mut bundle, &tag_signers)?;
2782309×
        }
2783324×
        if let Some(l) = &policy_to_send {
278434×
            let remote_serial = resp_policy_serial;
278534×
            bundle.policies = self
278634×
                .policy_history()?
278734×
                .into_iter()
278866×
                .filter(|sp| {
278966×
                    remote_serial.is_none_or(|r| sp.policy.serial > r)
279036×
                        && sp.policy.serial < l.policy.serial
279166×
                })
279234×
                .collect();
2793290×
        }
2794324×
        let results = remote.push(
2795324×
            bundle,
2796324×
            vec![RefUpdate {
2797324×
                name: bookmark.clone(),
2798324×
                old_state: remote_state,
2799324×
                new_state,
2800324×
                force,
2801324×
                attestation,
2802324×
            }],
2803324×
            policy_to_send,
2804324×
            tags_to_send
2805324×
                .into_iter()
2806324×
                .chain(withdrawals_to_send)
2807324×
                .collect(),
28088×
        )?;
280934×
        if let (Some(serial), true) = (
2810316×
            sent_serial,
2811330×
            results.iter().any(|r| r.ok && r.name == "policy"),
2812
        ) {
281334×
            self.record_policy_serial(remote_name, serial)?;
2814282×
        }
2815353×
        if results.iter().any(|r| r.ok && r.name == bookmark) {
2816289×
            let mut view = op.view.clone();
2817289×
            view.remotes
2818289×
                .entry(remote_name.to_string())
2819289×
                .or_default()
2820289×
                .insert(bookmark.clone(), new_state);
2821289×
            self.record_op(&[op_ref], &format!("oxy push {remote_name}"), view)?;
2822286×
            if let Some(s) = serial {
282386×
                self.record_serial(remote_name, &bookmark, s)?;
2824200×
            }
282527×
        }
2826311×
        Ok(results)
2827410×
    }
2828
2829
    /// Clone a remote into `dest`: same repository ID, `origin` remote,
2830
    /// bookmark set to the remote's, working copy checked out.
2831266×
    pub fn clone(url: &str, dest: &Path) -> Result<Repo> {
2832266×
        Self::clone_with(url, dest, None, &[])
2833266×
    }
2834
2835
    /// Clone, leaving blobs above `max_blob` bytes to be fetched on first
2836
    /// read, and materializing only `sparse` paths (which implies lazy blobs
2837
    /// unless a size is given, so out-of-scope binaries are never fetched).
2838269×
    pub fn clone_with(
2839269×
        url: &str,
2840269×
        dest: &Path,
2841269×
        max_blob: Option<u64>,
2842269×
        sparse: &[String],
2843269×
    ) -> Result<Repo> {
2844269×
        Self::clone_full(url, dest, max_blob, sparse, None)
2845269×
    }
2846
2847
    /// Shallow clone (spec §17): a checkpoint of the bookmark's heads plus the
2848
    /// changes within `depth` steps of them; the rest is the boundary.
28493×
    pub fn clone_shallow(
28503×
        url: &str,
28513×
        dest: &Path,
28523×
        depth: u32,
28533×
        max_blob: Option<u64>,
28543×
        sparse: &[String],
28553×
    ) -> Result<Repo> {
28563×
        Self::clone_full(url, dest, max_blob, sparse, Some(depth))
28573×
    }
2858
2859272×
    fn clone_full(
2860272×
        url: &str,
2861272×
        dest: &Path,
2862272×
        max_blob: Option<u64>,
2863272×
        sparse: &[String],
2864272×
        depth: Option<u32>,
2865272×
    ) -> Result<Repo> {
2866272×
        Ok(Self::clone_reporting(url, dest, max_blob, sparse, depth)?.0)
2867272×
    }
2868
2869
    /// Clone as [`Repo::clone_with`] (or [`Repo::clone_shallow`] with a
2870
    /// `depth`), also returning the bookmarks the fetch left out because
2871
    /// their attestation could not be verified ([`FetchReport::skipped`]),
2872
    /// for the caller to warn about. The bookmark checked out is never one
2873
    /// of them: that fails the clone.
2874278×
    pub fn clone_reporting(
2875278×
        url: &str,
2876278×
        dest: &Path,
2877278×
        max_blob: Option<u64>,
2878278×
        sparse: &[String],
2879278×
        depth: Option<u32>,
2880278×
    ) -> Result<(Repo, Vec<(String, String)>)> {
2881278×
        let options = crate::credentials::Credentials::user().options_for(url);
2882278×
        Self::clone_using(url, dest, max_blob, sparse, depth, &options)
2883278×
    }
2884
2885
    /// Clone a subrepo with the credentials the parent would use for its
2886
    /// hub (see [`Repo::credentials_for`]). Nothing is recorded in the
2887
    /// subrepo: it finds the same ones again through its parent.
288811×
    pub(crate) fn clone_as_subrepo(&self, url: &str, dest: &Path) -> Result<Repo> {
288911×
        let options = self.credentials_for(url);
289011×
        Ok(Self::clone_using(url, dest, None, &[], None, &options)?.0)
289111×
    }
2892
2893
    /// The name of a remote of this repository on the same hub (scheme,
2894
    /// host and port) as `url`.
289525×
    fn remote_on_same_hub(&self, url: &str) -> Option<String> {
289629×
        let origin = |u: &str| -> Option<String> {
289729×
            let (scheme, rest) = u.split_once("://")?;
28988×
            if !matches!(scheme, "http" | "https") {
28990×
                return None;
29008×
            }
29018×
            let host = rest.split('/').next()?;
29028×
            Some(format!(
29038×
                "{}://{}",
29048×
                scheme.to_ascii_lowercase(),
29058×
                host.to_ascii_lowercase()
29068×
            ))
290729×
        };
290825×
        let want = origin(url)?;
29094×
        self.config
29104×
            .remotes
29114×
            .iter()
29124×
            .find(|(_, u)| origin(u).as_deref() == Some(want.as_str()))
29134×
            .map(|(name, _)| name.clone())
291425×
    }
2915
2916
    /// The token and CA to reach `url` with: those of a remote of this
2917
    /// repository on the same hub (scheme, host and port), else the
2918
    /// environment's (`OXY_TOKEN`, `OXY_CA`), the user's credential store
2919
    /// and enclosing checkouts', as a clone would use them.
292025×
    pub(crate) fn credentials_for(&self, url: &str) -> RemoteOptions {
292125×
        match self.remote_on_same_hub(url) {
29221×
            Some(name) => RemoteOptions {
29231×
                repo_id: None,
29241×
                cache_dir: None,
29251×
                ..self.remote_options(&name)
29261×
            },
2927
            None => {
292824×
                let mut options = RemoteOptions::default();
292924×
                self.fill_credentials(url, &mut options);
293024×
                options
2931
            }
2932
        }
293325×
    }
2934
2935289×
    fn clone_using(
2936289×
        url: &str,
2937289×
        dest: &Path,
2938289×
        max_blob: Option<u64>,
2939289×
        sparse: &[String],
2940289×
        depth: Option<u32>,
2941289×
        options: &RemoteOptions,
2942289×
    ) -> Result<(Repo, Vec<(String, String)>)> {
2943289×
        let remote = open_remote(url, options)?;
2944289×
        let id = remote.repo_id()?;
2945285×
        if id == crate::protocol::UNBOUND_ID {
29462×
            return fail(format!(
2947
                "{url} is empty and unbound: push a repository to it first"
2948
            ));
2949283×
        }
2950283×
        let mut repo = Repo::init_with_id(dest, id)?;
2951283×
        repo.config
2952283×
            .set("remote.origin.url", &crate::protocol::stored_url(url))?;
2953283×
        if let Some(m) = max_blob {
29543×
            repo.config.set("filter.max-blob", &m.to_string())?;
2955280×
        } else if !sparse.is_empty() {
29561×
            repo.config.set("filter.max-blob", "0")?;
2957279×
        }
2958283×
        if !sparse.is_empty() {
29591×
            repo.config.set("filter.paths", &sparse.join(","))?;
2960282×
        }
2961283×
        repo.save_config()?;
2962283×
        let skipped = if let Some(depth) = depth {
29633×
            repo.fetch_shallow("origin", depth)?;
29643×
            Vec::new()
2965
        } else {
2966280×
            repo.fetch("origin")?.skipped
2967
        };
2968281×
        let (op_ref, op) = repo.head_op()?;
2969
        // The remote's default bookmark, else ours (main), else its only
2970
        // one; a bookmark the fetch skipped counts, so the clone fails
2971
        // rather than check out another (oxygenhub#266).
2972281×
        let mut theirs = op.view.remotes.get("origin").cloned().unwrap_or_default();
2973281×
        for (name, _) in &skipped {
29743×
            theirs.insert(name.clone(), ObjectRef::ZERO);
29753×
        }
2976
        // Asked of the remote as now configured, so a store-backed one
2977
        // answers from the mirror the fetch just filled.
2978281×
        let advertised = repo
2979281×
            .open_named_remote("origin")
2980281×
            .ok()
2981281×
            .and_then(|r| r.refs().ok())
2982281×
            .and_then(|r| r.default_bookmark);
2983281×
        let bookmark = advertised
2984281×
            .filter(|b| theirs.contains_key(b))
2985281×
            .or_else(|| {
29867×
                theirs
29877×
                    .contains_key(&repo.config.bookmark)
29887×
                    .then(|| repo.config.bookmark.clone())
29897×
            })
2990281×
            .or_else(|| {
29910×
                (theirs.len() == 1)
29920×
                    .then(|| theirs.keys().next().cloned())
29930×
                    .flatten()
29940×
            })
2995281×
            .unwrap_or_else(|| repo.config.bookmark.clone());
2996281×
        if let Some((_, why)) = skipped.iter().find(|(name, _)| *name == bookmark) {
29970×
            return fail(format!("cannot check out {bookmark}: {why}"));
2998281×
        }
2999281×
        let initial = repo.config.bookmark.clone();
3000281×
        if bookmark != repo.config.bookmark {
30011×
            fs::create_dir_all(repo.workspace_bookmark_path().parent().unwrap())?;
30021×
            write_atomic(&repo.workspace_bookmark_path(), bookmark.as_bytes())?;
30031×
            repo.config.bookmark = bookmark.clone();
3004280×
        }
3005281×
        if let Some(state) = theirs.get(&bookmark).copied() {
3006281×
            let heads = repo.store.get_state(&state)?.heads;
3007281×
            let mut view = op.view.clone();
3008
            // The empty bookmark `init` made goes when another is checked out.
3009281×
            if initial != bookmark && repo.heads_of(&view, &initial)?.is_empty() {
30101×
                view.bookmarks.remove(&initial);
3011280×
            }
3012281×
            view.bookmarks.insert(bookmark, state);
3013281×
            repo.record_op(&[op_ref], &format!("oxy clone {url}"), view)?;
3014281×
            repo.checkout(&heads, false, "oxy clone (checkout)")?;
30150×
        }
3016281×
        Ok((repo, skipped))
3017289×
    }
3018
}
3019
3020
/// The serial of a reference's next attestation: one more than the last the
3021
/// remote reports, or, for a bookmark the remote does not have, the clock in
3022
/// milliseconds, so a bookmark created again after a deletion goes past the
3023
/// deletion's serial the remote keeps.
3024111×
fn next_serial(last: Option<u64>) -> u64 {
3025111×
    match last {
302669×
        Some(l) => l + 1,
302742×
        None => Hlc::now().wall_ms.max(1),
3028
    }
3029111×
}