OXYGENHUB

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

2110 of 2341 lines run, 332 of 442 functions · run #371 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
681839×
    fn ref_attest_path(&self, bookmark: &str) -> PathBuf {
691839×
        self.oxy.join("attest").join(name_file(bookmark))
701839×
    }
71
72
    /// The latest reference attestation stored for a bookmark.
731422×
    pub fn latest_ref_attest(&self, bookmark: &str) -> Result<Option<(ObjectRef, SignedAttest)>> {
741422×
        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
            }
861077×
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
870×
            Err(e) => Err(e.into()),
88
        }
891422×
    }
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
961133×
    pub fn remote_refs(&self) -> Result<RefsResponse> {
971133×
        let view = self.view()?;
981133×
        let mut out = Vec::new();
991186×
        for (name, state) in &view.bookmarks {
1001186×
            let latest = self.latest_ref_attest(name)?.map(|(_, a)| a);
1011186×
            let last_serial = latest.as_ref().and_then(|a| match a.attest.body {
102230×
                AttestBody::Ref { serial, .. } => Some(serial),
1030×
                _ => None,
104230×
            });
1051186×
            let attestation = latest.filter(|a| matches!(&a.attest.body, AttestBody::Ref { new_state, .. } if new_state == state));
1061186×
            let accepted = match &attestation {
107219×
                Some(a) => self.acceptance_of(&a.attest.hash())?,
108967×
                None => None,
109
            };
1101186×
            out.push(RemoteRef {
1111186×
                name: name.clone(),
1121186×
                state: *state,
1131186×
                heads: self.store.get_state(state)?.heads,
1141186×
                attestation,
1151186×
                last_serial,
1161186×
                accepted,
1171186×
                logged: None,
1181186×
                integration: vec![],
119
            });
120
        }
1211133×
        let policy = self.current_policy()?.map(|(_, sp)| sp);
1221133×
        let history = self
1231133×
            .policy_history()?
1241133×
            .into_iter()
1251133×
            .filter(|sp| {
126221×
                policy
127221×
                    .as_ref()
128221×
                    .is_some_and(|p| sp.policy.serial < p.policy.serial)
129221×
            })
1301133×
            .collect();
131
        Ok(RefsResponse {
1321133×
            refs: out,
1331133×
            policy,
1341133×
            tags: self.tag_refs()?,
1351133×
            history,
1361133×
            default_bookmark: Some(self.config.bookmark.clone()),
1371133×
            log: None,
1381133×
            witnesses: vec![],
1391133×
            withdrawn: self.withdrawn_refs()?,
140
        })
1411133×
    }
142
143
    /// Every change reachable from `heads` that is in the store.
1441538×
    fn closure_in_store(&self, heads: &[ChangeHash]) -> Result<BTreeSet<ChangeHash>> {
1451538×
        let mut seen = BTreeSet::new();
1461538×
        let mut stack: Vec<ChangeHash> = heads.to_vec();
1474992×
        while let Some(h) = stack.pop() {
1483454×
            if seen.contains(&h) || !self.store.has(&h) {
149998×
                continue;
1502456×
            }
1512456×
            seen.insert(h);
1522456×
            stack.extend(self.store.get_change_meta(&h)?.deps);
153
        }
1541538×
        Ok(seen)
1551538×
    }
156
157
    /// The changes within `depth` dependency steps of `heads` (spec §17),
158
    /// nearest first; depth 1 is the heads themselves.
1595×
    pub fn shallow_changes(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> {
1605×
        let mut out = Vec::new();
1615×
        let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
1625×
        let mut layer: Vec<ChangeHash> = heads.to_vec();
1635×
        for _ in 0..depth.max(1) {
16410×
            let mut next = Vec::new();
16514×
            for h in layer {
16614×
                if !seen.insert(h) || !self.store.has(&h) {
1672×
                    continue;
16812×
                }
16912×
                let c = self.store.get_change(&h)?;
17012×
                next.extend(c.deps.iter().copied());
17112×
                out.push(h);
172
            }
17310×
            if next.is_empty() {
1741×
                break;
1759×
            }
1769×
            layer = next;
177
        }
1785×
        out.reverse();
1795×
        Ok(out)
1805×
    }
181
1822179×
    fn shallow_path(&self) -> PathBuf {
1832179×
        self.oxy.join("shallow")
1842179×
    }
185
186
    /// The shallow boundary: dependencies this repository does not hold (spec §17).
1872174×
    pub fn boundary(&self) -> BTreeSet<ChangeHash> {
1882174×
        fs::read_to_string(self.shallow_path())
1892174×
            .map(|t| t.lines().filter_map(ObjectRef::from_hex).collect())
1902174×
            .unwrap_or_default()
1912174×
    }
192
193190×
    pub fn is_shallow(&self) -> bool {
194190×
        !self.boundary().is_empty()
195190×
    }
196
1975×
    fn write_boundary(&self, boundary: &BTreeSet<ChangeHash>) -> Result<()> {
1985×
        if boundary.is_empty() {
1991×
            let _ = fs::remove_file(self.shallow_path());
2001×
            return Ok(());
2014×
        }
2024×
        let text: String = boundary
2034×
            .iter()
2044×
            .map(|h| format!("{}\n", h.to_hex()))
2054×
            .collect();
2064×
        write_atomic(&self.shallow_path(), text.as_bytes())
2075×
    }
208
209
    /// Recompute the boundary from what is stored: every dependency of a
210
    /// stored change that is not itself stored.
2115×
    fn refresh_boundary(&self, imported: &[ChangeHash]) -> Result<()> {
2125×
        let mut boundary = self.boundary();
2135×
        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) {
2174×
                    boundary.insert(d);
21811×
                }
219
            }
220
        }
2215×
        self.write_boundary(&boundary)
2225×
    }
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).
242893×
    pub fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> {
243893×
        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.
246893×
        let boundary = self.boundary();
247893×
        let mut out = Vec::new();
248893×
        let mut done: BTreeSet<ChangeHash> = have.clone();
249893×
        done.extend(boundary.iter().copied());
250
        // Iterative post-order DFS.
2511349×
        let mut stack: Vec<(ChangeHash, bool)> = want.iter().rev().map(|h| (*h, false)).collect();
2523517×
        while let Some((h, expanded)) = stack.pop() {
2532624×
            if done.contains(&h) {
254456×
                continue;
2552168×
            }
2562168×
            if !self.store.has(&h) {
2570×
                return fail(format!("change {} is not in this repository", h.short(12)));
2582168×
            }
2592168×
            if expanded {
2601084×
                done.insert(h);
2611084×
                out.push(h);
2621084×
                continue;
2631084×
            }
2641084×
            stack.push((h, true));
2651084×
            for d in self.store.get_change_meta(&h)?.deps {
266682×
                if !done.contains(&d) {
267191×
                    stack.push((d, false));
268491×
                }
269
            }
270
        }
271893×
        Ok(out)
272893×
    }
273
274
    /// Bundle changes (in the given dependency order) with their signatures,
275
    /// seals, identities, blobs, chunks.
276339×
    pub fn bundle_for(&self, changes: &[ChangeHash]) -> Result<Bundle> {
277339×
        self.bundle_of(changes, None, false, false)
278339×
    }
279
280
    /// Objects by reference for the `objects` endpoint; a blob brings its chunks.
2816×
    pub fn objects_for(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> {
2826×
        let mut out = Vec::new();
2836×
        for r in refs {
2846×
            let (ty, body) = match self.store.get(r) {
2856×
                Ok(x) => x,
2860×
                Err(StoreError::NotFound(_)) => continue,
2870×
                Err(e) => return Err(e.into()),
288
            };
2896×
            if ty == ObjectType::Blob {
2907×
                for c in Blob::decode(&body)?.chunks {
2917×
                    if let Ok((cty, cb)) = self.store.get(&c) {
2927×
                        out.push((cty, cb));
2937×
                    }
294
                }
2950×
            }
2966×
            out.push((ty, body));
297
        }
2986×
        Ok(out)
2996×
    }
300
301
    /// Like `bundle_for`, leaving out blobs larger than `max_blob` bytes
302
    /// (and their chunks) so the receiver fetches them lazily (spec §13.5).
303
    ///
304
    /// This is what a server answers a fetch with, so it also carries what
305
    /// verifies the attestations it serves beside it (see
306
    /// [`Repo::served_attest_signers`]), also when `changes` is empty.
30753×
    pub fn bundle_for_filtered(
30853×
        &self,
30953×
        changes: &[ChangeHash],
31053×
        max_blob: Option<u64>,
31153×
    ) -> Result<Bundle> {
31253×
        self.bundle_of(changes, max_blob, false, true)
31353×
    }
314
315
    /// Like [`Repo::bundle_for_filtered`] for a fetch between repositories
316
    /// on one machine: changes as they are stored ([`Bundle::raw_changes`]),
317
    /// with their text blobs and chunks among the objects, so neither side
318
    /// decodes or encodes their text.
319383×
    pub fn bundle_raw_for(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> {
320383×
        self.bundle_of(changes, max_blob, true, true)
321383×
    }
322
323
    /// The keys that signed the attestations this repository serves with
324
    /// its references: each bookmark's latest and each tag's. A fetch
325
    /// bundle carries their identities and certificates (spec §16.2), so a
326
    /// client can check an attestation whose signer signed none of the
327
    /// changes it receives, a delegate or a short-lived key that only
328
    /// moved a bookmark (oxygenhub#266). One pointer and one signature
329
    /// list per reference, computed once per operation head and kept
330
    /// (every bookmark move and tag is an operation): a client sends one
331
    /// fetch per bookmark, and on a bucket store each signature list is a
332
    /// LIST request, so recomputing per fetch cost metalgear2 (145
333
    /// bookmarks) 14 s a fetch.
334436×
    pub fn served_attest_signers(&self) -> Result<BTreeSet<KeyId>> {
335
        type Signers = std::collections::HashMap<(PathBuf, ObjectRef), BTreeSet<KeyId>>;
336
        static SERVED: std::sync::Mutex<Option<Signers>> = std::sync::Mutex::new(None);
337436×
        let key = (self.oxy.clone(), self.head_op()?.0);
338436×
        if let Some(keys) = SERVED
339436×
            .lock()
340436×
            .unwrap_or_else(|p| p.into_inner())
341436×
            .as_ref()
342436×
            .and_then(|m| m.get(&key))
343
        {
344138×
            return Ok(keys.clone());
345298×
        }
346298×
        let keys = self.attest_signers_now()?;
347298×
        let mut served = SERVED.lock().unwrap_or_else(|p| p.into_inner());
348298×
        let map = served.get_or_insert_with(Default::default);
349
        // One entry per repository: an older operation head is never asked
350
        // for again once a newer one is current.
3516336×
        map.retain(|(oxy, _), _| *oxy != key.0);
352298×
        if map.len() >= 1024 {
3530×
            map.clear();
354298×
        }
355298×
        map.insert(key, keys.clone());
356298×
        Ok(keys)
357436×
    }
358
359298×
    fn attest_signers_now(&self) -> Result<BTreeSet<KeyId>> {
360298×
        let view = self.view()?;
361298×
        let pointers = view
362298×
            .bookmarks
363298×
            .keys()
364308×
            .map(|b| self.ref_attest_path(b))
365298×
            .chain(view.tags.keys().map(|t| self.tag_attest_path(t)));
366298×
        let mut keys = BTreeSet::new();
367330×
        for p in pointers {
368330×
            let Ok(text) = fs::read_to_string(&p) else {
369267×
                continue;
370
            };
37163×
            let Some(r) = ObjectRef::from_hex(text.trim()) else {
3720×
                continue;
373
            };
37463×
            keys.extend(self.store.signatures(&r)?.into_iter().map(|s| s.key));
375
        }
376298×
        Ok(keys)
377298×
    }
378
379
    /// `serving`: a fetch's answer, which carries the identities and
380
    /// certificates of [`Repo::served_attest_signers`] too.
381775×
    fn bundle_of(
382775×
        &self,
383775×
        changes: &[ChangeHash],
384775×
        max_blob: Option<u64>,
385775×
        raw: bool,
386775×
        serving: bool,
387775×
    ) -> Result<Bundle> {
388775×
        let mut bundle = Bundle::default();
389775×
        let mut objects: BTreeSet<ObjectRef> = BTreeSet::new();
390775×
        let mut seals: BTreeSet<ObjectRef> = BTreeSet::new();
3911258×
        let add_object = |r: &ObjectRef, objects: &mut BTreeSet<ObjectRef>| {
3921258×
            if !r.is_zero() && self.store.has(r) {
393384×
                objects.insert(*r);
394874×
            }
3951258×
        };
3961000×
        for h in changes {
397
            // Raw: the stored bytes, and a skeleton for the rest.
3981000×
            let (c, encoded) = if raw {
399532×
                let bytes = self.store.get_typed(h, ObjectType::Change)?;
400532×
                let (skel, texts) = Change::decode_skeleton(&bytes, *h)?;
4014982×
                for t in texts {
4024982×
                    if let crate::objects::TextRef::Blob(r, _, _) = t {
4033×
                        let blob = Blob::decode(&self.store.get_typed(&r, ObjectType::Blob)?)?;
4043×
                        objects.insert(r);
4053×
                        objects.extend(blob.chunks);
4064979×
                    }
407
                }
408532×
                (skel, Some(bytes))
409
            } else {
410468×
                (self.store.get_change(h)?, None)
411
            };
4121000×
            let sigs = self.store.signatures(h)?;
4131000×
            add_object(&c.author, &mut objects);
4141000×
            for s in &sigs {
41537×
                add_object(&s.key, &mut objects);
41637×
            }
4175913×
            for e in &c.edits {
4185913×
                if let Edit::Replace { old, new, .. } = e {
41922×
                    for b in [old, new].into_iter().flatten() {
42022×
                        if self.store.has(b) {
42122×
                            if let Ok(blob) =
42222×
                                Blob::decode(&self.store.get_typed(b, ObjectType::Blob)?)
423
                            {
42422×
                                if max_blob.is_some_and(|m| blob.length > m) {
4254×
                                    continue;
42618×
                                }
42718×
                                objects.insert(*b);
42818×
                                objects.extend(blob.chunks.iter().copied());
4290×
                            }
4300×
                        }
431
                    }
4325893×
                }
433
            }
4341000×
            for s in self.store.seals_for(h)? {
435110×
                seals.insert(s);
436110×
            }
4371000×
            match encoded {
438532×
                Some(bytes) => bundle.raw_changes.push((bytes, sigs)),
439468×
                None => {
440468×
                    bundle.changes.push((c, sigs));
441468×
                    bundle.hashes.push(*h);
442468×
                }
443
            }
444
        }
445775×
        for r in seals {
446105×
            let attest = self.store.get_attest(&r)?;
447105×
            let sigs = self.store.signatures(&r)?;
448105×
            for s in &sigs {
449105×
                add_object(&s.key, &mut objects);
450105×
            }
451105×
            bundle.attests.push(SignedAttest { attest, sigs });
452
        }
453775×
        let signers: Vec<KeyId> = if serving {
454436×
            let signers = self.served_attest_signers()?;
455436×
            for k in &signers {
456116×
                add_object(k, &mut objects);
457116×
            }
458436×
            signers.into_iter().collect()
459
        } else {
460339×
            Vec::new()
461
        };
462775×
        for r in objects {
463243×
            let (ty, body) = self.store.get(&r)?;
464243×
            bundle.objects.push((ty, body));
465
        }
466775×
        self.attach_certificates_for(&mut bundle, &signers)?;
467775×
        Ok(bundle)
468775×
    }
469
470
    /// Add the certificates of short-lived keys that signed what `bundle`
471
    /// carries, and their issuers' identities, so a receiver can tell who
472
    /// they stand for (spec §14.7). `also` names more signers (a reference
473
    /// attestation sent beside the bundle).
474902×
    pub(crate) fn attach_certificates_for(
475902×
        &self,
476902×
        bundle: &mut Bundle,
477902×
        also: &[KeyId],
478902×
    ) -> Result<()> {
479902×
        let mut keys: BTreeSet<KeyId> = also.iter().copied().collect();
480902×
        keys.extend(
481902×
            bundle
482902×
                .changes
483902×
                .iter()
484902×
                .flat_map(|(_, s)| s.iter().map(|s| s.key)),
485
        );
486902×
        keys.extend(
487902×
            bundle
488902×
                .raw_changes
489902×
                .iter()
490902×
                .flat_map(|(_, s)| s.iter().map(|s| s.key)),
491
        );
492902×
        keys.extend(
493902×
            bundle
494902×
                .attests
495902×
                .iter()
496902×
                .flat_map(|a| a.sigs.iter().map(|s| s.key)),
497
        );
498902×
        for k in keys {
499350×
            let Some(r) = self.store.certificate_of(&k)? else {
500301×
                continue;
501
            };
50249×
            let cert = self.store.get_certificate(&r)?;
50349×
            if bundle
50449×
                .certificates
50549×
                .iter()
50649×
                .any(|c| c.cert.hash() == cert.cert.hash())
507
            {
50812×
                continue;
50937×
            }
510
            // An agent's certificate brings its grant's signer too, so the
511
            // receiver can check the grant (oxygen#134).
51237×
            let grantor = cert.cert.grant.as_ref().map(|g| g.grant.grantor);
51381×
            for id in [Some(k), Some(cert.cert.issuer), grantor].iter().flatten() {
51481×
                if let Ok(body) = self.store.get_typed(id, ObjectType::Identity) {
51580×
                    if !bundle
51680×
                        .objects
51780×
                        .iter()
518148×
                        .any(|(t, b)| *t == ObjectType::Identity && *b == body)
51935×
                    {
52035×
                        bundle.objects.push((ObjectType::Identity, body));
52145×
                    }
5221×
                }
523
            }
52437×
            bundle.certificates.push(cert);
525
        }
526902×
        Ok(())
527902×
    }
528
529
    /// Keep the certificates a bundle brings once each verifies with the
530
    /// identity of the issuer it names; one that does not refuses the bundle.
531823×
    fn adopt_certificates(&self, certs: &[crate::cert::SignedCertificate]) -> Result<()> {
532823×
        for c in certs {
53336×
            let issuer = self.store.get_identity(&c.cert.issuer).map_err(|_| {
5340×
                RepoError::Msg(format!(
5350×
                    "a certificate names issuer {}, whose identity did not come with it",
5360×
                    c.cert.issuer.short(12)
5370×
                ))
5380×
            })?;
53936×
            c.verify(&issuer)
54036×
                .map_err(|e| RepoError::Msg(format!("a certificate does not verify: {e}")))?;
54136×
            self.store.put_identity(&c.cert.key)?;
54236×
            self.store.put_certificate(c)?;
543
        }
544823×
        Ok(())
545823×
    }
546
547
    /// A signer authorized for `reference` by what an issuer certified it
548
    /// as (spec §14.7): a short-lived key with a kept certificate that
549
    /// verifies with its issuer, gives `scope`, covers `at_ms` (a time the
550
    /// signer did not choose) and names a subject the policy grants.
55157×
    pub(crate) fn certified_authorized(
55257×
        &self,
55357×
        p: &crate::policy::Policy,
55457×
        signer: &KeyId,
55557×
        reference: &str,
55657×
        scope: Scope,
55757×
        at_ms: u64,
55857×
    ) -> bool {
55957×
        let Ok(Some(r)) = self.store.certificate_of(signer) else {
5605×
            return false;
561
        };
56252×
        let Ok(c) = self.store.get_certificate(&r) else {
5630×
            return false;
564
        };
56552×
        let Some(issuer) = p
56652×
            .identity(&c.cert.issuer)
56752×
            .cloned()
56852×
            .or_else(|| self.store.get_identity(&c.cert.issuer).ok())
569
        else {
5700×
            return false;
571
        };
57252×
        c.verify(&issuer).is_ok()
57352×
            && c.cert.key_id() == *signer
57452×
            && c.cert.covers(scope, at_ms)
57542×
            && p.subject_grant(reference, &c.cert.issuer, &c.cert.subject, scope)
57657×
    }
577
578
    /// Store a bundle's contents. With `strict`, every change must
579
    /// authenticate (signature or seal by its author or a delegate).
580818×
    pub fn import_bundle(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> {
581818×
        self.import_bundle_inner(bundle, strict, false)
582818×
    }
583
584
    /// Like `import_bundle`, accepting changes whose dependencies are absent
585
    /// (they join the shallow boundary).
5865×
    pub fn import_bundle_shallow(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> {
5875×
        self.import_bundle_inner(bundle, strict, true)
5885×
    }
589
590823×
    fn import_bundle_inner(
591823×
        &self,
592823×
        bundle: &Bundle,
593823×
        strict: bool,
594823×
        shallow: bool,
595823×
    ) -> Result<Vec<ChangeHash>> {
596823×
        let batch = self.store.begin_batch()?;
597823×
        let result = self.import_bundle_batched(bundle, strict, shallow);
598823×
        batch.finish()?;
599821×
        result
600823×
    }
601
602823×
    fn import_bundle_batched(
603823×
        &self,
604823×
        bundle: &Bundle,
605823×
        strict: bool,
606823×
        shallow: bool,
607823×
    ) -> Result<Vec<ChangeHash>> {
608823×
        for (ty, _) in &bundle.objects {
6090×
            if !matches!(
610712×
                ty,
611
                ObjectType::Chunk
612
                    | ObjectType::Blob
613
                    | ObjectType::Identity
614
                    | ObjectType::State
615
                    | ObjectType::Checkpoint
616
            ) {
6170×
                return fail(format!("bundle may not carry {} objects", ty.name()));
618712×
            }
619
        }
620
        // All at once: in a large batch they are compressed on every core.
621823×
        let objects: Vec<(ObjectType, &[u8])> = bundle
622823×
            .objects
623823×
            .iter()
624823×
            .map(|(ty, body)| (*ty, body.as_slice()))
625823×
            .collect();
626823×
        self.store.put_many(&objects)?;
627823×
        self.adopt_certificates(&bundle.certificates)?;
628823×
        for sa in &bundle.attests {
629108×
            let r = self.store.put_attest(&sa.attest)?;
630108×
            for sig in &sa.sigs {
631108×
                self.store.put_signature(&r, sig)?;
632
            }
633108×
            if let AttestBody::Seal { hashes } = &sa.attest.body {
634113×
                for h in hashes {
635113×
                    self.store.index_seal(h, &r)?;
636
                }
6370×
            }
638
        }
639
        // Each change's hash: from the bundle when its maker knew it, else
640
        // worked out once (for a change adding a whole tree, hashing is
641
        // chunking all its text).
642823×
        let hashes: Vec<ChangeHash> = if bundle.hashes.len() == bundle.changes.len() {
643814×
            bundle.hashes.clone()
644
        } else {
6459×
            bundle.changes.iter().map(|(c, _)| c.hash()).collect()
646
        };
647823×
        let raw_hashes: Vec<ChangeHash> = bundle
648823×
            .raw_changes
649823×
            .iter()
650823×
            .map(|(b, _)| hash_object(ObjectType::Change, b))
651823×
            .collect();
652823×
        let incoming: BTreeSet<ChangeHash> = hashes.iter().chain(&raw_hashes).copied().collect();
653823×
        let boundary = self.boundary();
654823×
        let mut imported = Vec::new();
655
        // Changes as stored: checked, then stored as they are, their text
656
        // blobs having come with the objects.
657823×
        for ((bytes, sigs), h) in bundle.raw_changes.iter().zip(raw_hashes) {
658533×
            let (skel, texts) = Change::decode_skeleton(bytes, h)?;
659533×
            for d in &skel.deps {
660357×
                if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d)
661
                {
6620×
                    return fail(format!(
663
                        "change {} depends on {}, which is neither stored nor in the bundle",
6640×
                        h.short(12),
6650×
                        d.short(12)
666
                    ));
667357×
                }
668
            }
6694983×
            for t in texts {
6704983×
                if let crate::objects::TextRef::Blob(r, _, _) = t {
6714×
                    if !self.store.has(&r) {
6721×
                        return fail(format!(
673
                            "change {} came without its text blob {}",
6741×
                            h.short(12),
6751×
                            r.short(12)
676
                        ));
6773×
                    }
6784979×
                }
679
            }
680532×
            self.store.put(ObjectType::Change, bytes)?;
681532×
            for sig in sigs {
68213×
                self.store.put_signature(&h, sig)?;
683
            }
684532×
            imported.push(h);
685
        }
686822×
        for ((c, sigs), h) in bundle.changes.iter().zip(hashes) {
687495×
            for d in &c.deps {
688281×
                if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d)
689
                {
6903×
                    return fail(format!(
691
                        "change {} depends on {}, which is neither stored nor in the bundle",
6923×
                        h.short(12),
6933×
                        d.short(12)
694
                    ));
695278×
                }
696
            }
697492×
            let stored = self.store.put_change(c)?;
698492×
            if stored != h {
6990×
                return fail(format!(
700
                    "change {} is not the change the bundle names",
7010×
                    h.short(12)
702
                ));
703492×
            }
704492×
            for sig in sigs {
70533×
                self.store.put_signature(&h, sig)?;
706
            }
707492×
            imported.push(h);
708
        }
709819×
        if strict {
71067×
            for h in &imported {
71139×
                if !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) {
7124×
                    return fail(format!(
713
                        "change {} is not authenticated by its author",
7144×
                        h.short(12)
715
                    ));
71635×
                }
717
            }
718752×
        }
719815×
        Ok(imported)
720823×
    }
721
722
    /// Verify a reference attestation against an update it claims to describe.
723
    /// `allow_equal` lets a client re-read the attestation it already has; a
724
    /// server receiving a push requires a strictly greater serial.
725
    /// A reference attestation signed by a delegate of an identity the
726
    /// rule lists is authorized as that identity (spec §14.5): the identity
727
    /// named the delegate with the `refs` scope, still in force at `at_ms`
728
    /// (the attestation's wall time), and the policy names the identity.
729
    /// Revoked keys authorize nothing either way.
73072×
    pub(crate) fn delegate_authorized(
73172×
        &self,
73272×
        p: &crate::policy::Policy,
73372×
        signer: &KeyId,
73472×
        reference: &str,
73572×
        at_ms: u64,
73672×
    ) -> bool {
73772×
        if p.is_revoked(signer) {
7381×
            return false;
73971×
        }
74071×
        let Some(rule) = p.rule_for(reference) else {
7410×
            return false;
742
        };
74372×
        rule.keys.iter().any(|k| {
74472×
            !p.is_revoked(k)
74572×
                && p.identity(k)
74672×
                    .cloned()
74772×
                    .or_else(|| self.store.get_identity(k).ok())
74872×
                    .is_some_and(|id| id.delegates_to(signer, Scope::REFS, at_ms))
74972×
        })
75072×
    }
751
752
    /// A lasting attestation (a tag) signed by a key the policy has since
753
    /// rotated away from (spec §14.2) is authorized as the key that
754
    /// succeeded it: a key the rule lists reaches the signer through
755
    /// `previous` links, and the attestation is dated (`at_ms`) before the
756
    /// signer's successor was made, so a superseded key makes nothing new.
757
    /// Revoked keys authorize nothing.
75827×
    pub(crate) fn predecessor_authorized(
75927×
        &self,
76027×
        p: &crate::policy::Policy,
76127×
        signer: &KeyId,
76227×
        reference: &str,
76327×
        at_ms: u64,
76427×
    ) -> bool {
76527×
        if p.is_revoked(signer) {
7660×
            return false;
76727×
        }
76827×
        let Some(rule) = p.rule_for(reference) else {
7690×
            return false;
770
        };
77128×
        let identity = |k: &KeyId| {
77228×
            p.identity(k)
77328×
                .cloned()
77428×
                .or_else(|| self.store.get_identity(k).ok())
77528×
        };
77627×
        rule.keys.iter().any(|k| {
77727×
            if p.is_revoked(k) {
7780×
                return false;
77927×
            }
78027×
            let mut cur = identity(k);
78127×
            for _ in 0..32 {
78228×
                let Some(id) = cur else {
7830×
                    return false;
784
                };
7851×
                match id.previous {
7866×
                    Some(prev) if prev == *signer => return at_ms < id.created,
7871×
                    Some(prev) if !p.is_revoked(&prev) => cur = identity(&prev),
78822×
                    _ => return false,
789
                }
790
            }
7910×
            false
79227×
        })
79327×
    }
794
795106×
    fn check_ref_attest(
796106×
        &self,
797106×
        sa: &SignedAttest,
798106×
        name: &str,
799106×
        old_state: &ObjectRef,
800106×
        new_state: &ObjectRef,
801106×
        last_serial: Option<u64>,
802106×
        allow_equal: bool,
803106×
    ) -> std::result::Result<u64, String> {
804104×
        let serial =
805106×
            self.check_ref_attest_body(sa, name, old_state, new_state, last_serial, allow_equal)?;
806104×
        self.verify_attest_signature(sa)?;
807104×
        Ok(serial)
808106×
    }
809
810
    /// What [`Repo::check_ref_attest`] checks short of the signature: the
811
    /// attestation describes this update of this repository with a serial
812
    /// past the last seen.
813179×
    fn check_ref_attest_body(
814179×
        &self,
815179×
        sa: &SignedAttest,
816179×
        name: &str,
817179×
        old_state: &ObjectRef,
818179×
        new_state: &ObjectRef,
819179×
        last_serial: Option<u64>,
820179×
        allow_equal: bool,
821179×
    ) -> std::result::Result<u64, String> {
822
        let AttestBody::Ref {
823179×
            name: n,
824179×
            old_state: o,
825179×
            new_state: nw,
826179×
            serial,
827179×
        } = &sa.attest.body
828
        else {
8290×
            return Err("attestation is not a ref attestation".into());
830
        };
831179×
        if n != name || o != old_state || nw != new_state {
8321×
            return Err("attestation does not describe this update".into());
833178×
        }
834178×
        if sa.attest.repo != self.id().map_err(|e| e.to_string())? {
8350×
            return Err("attestation is for a different repository".into());
836178×
        }
837178×
        if let Some(last) = last_serial {
83893×
            if *serial < last || (*serial == last && !allow_equal) {
8391×
                return Err(format!(
8401×
                    "attestation serial {serial} is not greater than the last seen {last}"
8411×
                ));
84292×
            }
84385×
        }
844177×
        Ok(*serial)
845179×
    }
846
847
    /// An attestation's first signature verifies with its signer's
848
    /// identity, which must be stored here.
849177×
    fn verify_attest_signature(&self, sa: &SignedAttest) -> std::result::Result<(), String> {
850177×
        let r = sa.attest.hash();
851177×
        let sig = sa.sigs.first().ok_or("attestation carries no signature")?;
852177×
        let id = self
853177×
            .store
854177×
            .get_identity(&sig.key)
855177×
            .map_err(|_| format!("unknown key {}", sig.key.short(12)))?;
856171×
        verify_signature(&id, sig, Domain::Attest, &r).map_err(|e| e.to_string())
857177×
    }
858
859
    /// Accept a push (spec §16.4). Without a trust policy every repository
860
    /// is unverified, so unsigned changes and unattested updates are accepted;
861
    /// attestations that are present must still be valid. With a policy,
862
    /// every change and update is checked against it. A newer policy in the
863
    /// push is applied first when it is signed by the current roots.
864317×
    pub fn receive_push(
865317×
        &self,
866317×
        bundle: Bundle,
867317×
        updates: Vec<RefUpdate>,
868317×
        policy: Option<SignedPolicy>,
869317×
        tags: Vec<crate::protocol::TagRef>,
870317×
    ) -> Result<Vec<RefResult>> {
871317×
        Ok(self
872317×
            .receive_push_report(bundle, updates, policy, tags)?
873
            .results)
874317×
    }
875
876
    /// `receive_push` with what a host wants to log: each accepted
877
    /// reference's old and new state and heads, and the changes this push
878
    /// stored (hub spec §6.12).
879363×
    pub fn receive_push_report(
880363×
        &self,
881363×
        bundle: Bundle,
882363×
        updates: Vec<RefUpdate>,
883363×
        policy: Option<SignedPolicy>,
884363×
        tags: Vec<crate::protocol::TagRef>,
885363×
    ) -> Result<PushReport> {
886
        // Updates are checked against the head as it is now, not as this
887
        // handle first read it (oxygenhub#249): another push may have
888
        // landed since.
889363×
        self.refresh()?;
890363×
        let mut report = PushReport::default();
891363×
        let results = &mut report.results;
892363×
        if let Some(sp) = policy {
89336×
            match self.adopt_policy_chain(&bundle.policies, &sp, "push received") {
89434×
                Ok(()) => {
89534×
                    results.push(RefResult {
89634×
                        name: "policy".into(),
89734×
                        ok: true,
89834×
                        reason: format!("serial {}", sp.policy.serial),
89934×
                    });
90034×
                }
9012×
                Err(e) => {
9022×
                    results.push(RefResult {
9032×
                        name: "policy".into(),
9042×
                        ok: false,
9052×
                        reason: e,
9062×
                    });
9072×
                    for u in updates {
9080×
                        results.push(RefResult {
9090×
                            name: u.name,
9100×
                            ok: false,
9110×
                            reason: "policy update was rejected".into(),
9120×
                        });
9130×
                    }
9142×
                    return Ok(report);
915
                }
916
            }
917327×
        }
918361×
        let policy = self.current_policy()?.map(|(_, sp)| sp.policy);
919
        // A push of objects alone (one batch of a long history, oxygen#95)
920
        // names no reference: its outcome is reported as `objects`.
921361×
        let objects_only = updates.is_empty() && tags.is_empty();
922361×
        match self.import_bundle(&bundle, policy.is_some()) {
923355×
            Ok(imported) => {
924355×
                if objects_only {
92511×
                    results.push(RefResult {
92611×
                        name: OBJECTS_RESULT.into(),
92711×
                        ok: true,
92811×
                        reason: format!("{} changes stored", imported.len()),
92911×
                    });
930344×
                }
931355×
                report.imported = imported;
932
            }
9336×
            Err(e) => {
9346×
                if objects_only {
9350×
                    results.push(RefResult {
9360×
                        name: OBJECTS_RESULT.into(),
9370×
                        ok: false,
9380×
                        reason: e.to_string(),
9390×
                    });
9406×
                }
9416×
                for u in updates {
9426×
                    results.push(RefResult {
9436×
                        name: u.name,
9446×
                        ok: false,
9456×
                        reason: e.to_string(),
9466×
                    });
9476×
                }
9486×
                return Ok(report);
949
            }
950
        }
951355×
        let (op_ref, op) = self.head_op()?;
952355×
        let mut view = op.view.clone();
953355×
        let mut names = Vec::new();
954355×
        for u in updates {
955339×
            let outcome = self.apply_update(&mut view, &u, policy.as_ref());
956339×
            let (ok, reason) = match outcome {
957309×
                Ok(()) => (true, String::new()),
95830×
                Err(e) => (false, e),
959
            };
960339×
            if ok {
961309×
                names.push(u.name.clone());
962309×
                let heads = self
963309×
                    .store
964309×
                    .get_state(&u.new_state)
965309×
                    .map(|s| s.heads)
966309×
                    .unwrap_or_default();
967309×
                report.updated.push(RefUpdated {
968309×
                    name: u.name.clone(),
969309×
                    old_state: u.old_state,
970309×
                    new_state: u.new_state,
971309×
                    heads,
972309×
                });
97330×
            }
974339×
            results.push(RefResult {
975339×
                name: u.name,
976339×
                ok,
977339×
                reason,
978339×
            });
979
        }
980355×
        for t in &tags {
98122×
            let name = format!("tags/{}", t.name);
98222×
            if t.is_withdrawal() {
9833×
                match self.adopt_withdrawal(&mut view, t, policy.as_ref(), &mut TagCheck::Received)
984
                {
9852×
                    Ok(removed) => {
986
                        // A withdrawal is an update to nothing, as a
987
                        // bookmark's deletion is.
9882×
                        if removed {
9892×
                            report.updated.push(RefUpdated {
9902×
                                name: name.clone(),
9912×
                                old_state: t.state,
9922×
                                new_state: ObjectRef::ZERO,
9932×
                                heads: vec![],
9942×
                            });
9952×
                        }
9962×
                        names.push(name.clone());
9972×
                        results.push(RefResult {
9982×
                            name,
999
                            ok: true,
10002×
                            reason: if removed {
10012×
                                "withdrawn"
1002
                            } else {
10030×
                                "already withdrawn"
1004
                            }
10052×
                            .into(),
1006
                        });
1007
                    }
10081×
                    Err(e) => results.push(RefResult {
10091×
                        name,
10101×
                        ok: false,
10111×
                        reason: e.reason,
10121×
                    }),
1013
                }
10143×
                continue;
101519×
            }
101619×
            let new = !view.tags.contains_key(&t.name);
101719×
            match self.adopt_tag(&mut view, t, policy.as_ref(), &mut TagCheck::Received) {
1018
                Ok(()) => {
1019
                    // A tag new here is an update too, so a hub can start
1020
                    // its pipeline's tag jobs (oxygenflow#24); one it
1021
                    // already had, sent again, is not.
102214×
                    if new {
102313×
                        report.updated.push(RefUpdated {
102413×
                            name: name.clone(),
102513×
                            old_state: ObjectRef::ZERO,
102613×
                            new_state: t.state,
102713×
                            heads: t.heads.clone(),
102813×
                        });
102913×
                    }
103014×
                    names.push(name.clone());
103114×
                    results.push(RefResult {
103214×
                        name,
103314×
                        ok: true,
103414×
                        reason: String::new(),
103514×
                    });
1036
                }
10375×
                Err(e) => results.push(RefResult {
10385×
                    name,
10395×
                    ok: false,
10405×
                    reason: e.reason,
10415×
                }),
1042
            }
1043
        }
1044355×
        if !names.is_empty() {
1045311×
            self.record_op(
1046311×
                &[op_ref],
1047311×
                &format!("push received: {}", names.join(" ")),
1048311×
                view,
10493×
            )?;
105044×
        }
1051352×
        Ok(report)
1052363×
    }
1053
1054
    /// Check an update's attestation against the latest one for its
1055
    /// reference and store it as the new latest. Failures say what was
1056
    /// being done, for which reference, so a refused push can be diagnosed
1057
    /// from its reason alone.
1058106×
    fn record_update_attest(
1059106×
        &self,
1060106×
        u: &RefUpdate,
1061106×
        sa: &SignedAttest,
1062106×
    ) -> std::result::Result<(), String> {
1063106×
        let name = &u.name;
1064106×
        let last = self
1065106×
            .latest_ref_attest(name)
1066106×
            .map_err(|e| format!("reading the last attestation of {name:?}: {e}"))?
1067106×
            .and_then(|(_, a)| match a.attest.body {
106865×
                AttestBody::Ref { serial, .. } => Some(serial),
10690×
                _ => None,
107065×
            });
1071106×
        self.check_ref_attest(sa, name, &u.old_state, &u.new_state, last, false)?;
1072104×
        let store = |e: &dyn std::fmt::Display| format!("storing the attestation of {name:?}: {e}");
1073104×
        let r = self.store.put_attest(&sa.attest).map_err(|e| store(&e))?;
1074103×
        for sig in &sa.sigs {
1075103×
            self.store.put_signature(&r, sig).map_err(|e| store(&e))?;
1076
        }
1077103×
        self.set_ref_attest(name, &r).map_err(|e| store(&e))?;
1078
        // Signed by a short-lived key: a host records when it accepted it.
1079100×
        let certified = sa
1080100×
            .sigs
1081100×
            .first()
1082100×
            .is_some_and(|s| self.store.certificate_of(&s.key).ok().flatten().is_some());
1083100×
        if certified {
10846×
            self.record_acceptance(&r).map_err(|e| store(&e))?;
108594×
        }
1086100×
        Ok(())
1087106×
    }
1088
1089
    /// Record that this host accepted attestation `r` now, signed with its
1090
    /// acceptor key (spec §14.7); nothing without one.
109111×
    pub(crate) fn record_acceptance(&self, r: &ObjectRef) -> Result<()> {
109211×
        let Some(acceptor) = &self.acceptor else {
10937×
            return Ok(());
1094
        };
10954×
        let (id, sk) = (&acceptor.0, &acceptor.1);
10964×
        let attest = Attest {
10974×
            repo: self.id()?,
10984×
            hlc: Hlc::now(),
10994×
            body: AttestBody::Accepted { attest: *r },
1100
        };
11014×
        let sig = sk.sign(Domain::Attest, &id.hash(), &attest.hash());
11024×
        self.store.put_identity(id)?;
11034×
        let a = self.store.put_attest(&attest)?;
11044×
        self.store.put_signature(&a, &sig)?;
11054×
        self.store.index_add_pub("accepted", r, &a)?;
11064×
        Ok(())
110711×
    }
1108
1109
    /// The host's acceptance of attestation `r`, if one was recorded.
1110227×
    pub fn acceptance_of(&self, r: &ObjectRef) -> Result<Option<SignedAttest>> {
1111227×
        let Some(a) = self.store.index_get_pub("accepted", r)?.into_iter().next() else {
1112206×
            return Ok(None);
1113
        };
1114
        Ok(Some(SignedAttest {
111521×
            attest: self.store.get_attest(&a)?,
111621×
            sigs: self.store.signatures(&a)?,
1117
        }))
1118227×
    }
1119
1120
    /// When a certified signer's attestation `sa` is checked on a fetch: the
1121
    /// host's acceptance time, if `accepted` is signed by the issuer of the
1122
    /// signer's certificate and accepts `sa`; else the attestation's own
1123
    /// time, which the signer chose.
112431×
    pub(crate) fn certified_time(
112531×
        &self,
112631×
        signer: &KeyId,
112731×
        sa: &SignedAttest,
112831×
        accepted: Option<&SignedAttest>,
112931×
    ) -> u64 {
113031×
        self.issuer_acceptance(signer, sa, accepted)
113131×
            .unwrap_or(sa.attest.hlc.wall_ms)
113231×
    }
1133
1134
    /// The time of `accepted` when it accepts exactly `sa` and is signed by
1135
    /// the issuer of `signer`'s kept certificate (spec §14.7); else none.
1136101×
    pub(crate) fn issuer_acceptance(
1137101×
        &self,
1138101×
        signer: &KeyId,
1139101×
        sa: &SignedAttest,
1140101×
        accepted: Option<&SignedAttest>,
1141101×
    ) -> Option<u64> {
1142101×
        let acc = accepted?;
114346×
        let c = self.store.certificate_of(signer).ok().flatten()?;
114446×
        let cert = self.store.get_certificate(&c).ok()?;
114546×
        let AttestBody::Accepted { attest } = &acc.attest.body else {
11460×
            return None;
1147
        };
114851×
        let sig = acc.sigs.iter().find(|s| s.key == cert.cert.issuer)?;
114943×
        let issuer = self.store.get_identity(&cert.cert.issuer).ok()?;
115043×
        (*attest == sa.attest.hash()
115142×
            && verify_signature(&issuer, sig, Domain::Attest, &acc.attest.hash()).is_ok())
115243×
        .then_some(acc.attest.hlc.wall_ms)
1153101×
    }
1154
1155
    /// Keep an acceptance this repository was given for attestation `r`
1156
    /// (one [`Repo::issuer_acceptance`] took), so it is served again with
1157
    /// what it accepts.
11589×
    pub(crate) fn keep_acceptance(&self, r: &ObjectRef, acc: &SignedAttest) -> Result<()> {
11599×
        let a = self.store.put_attest(&acc.attest)?;
116011×
        for sig in &acc.sigs {
116111×
            self.store.put_signature(&a, sig)?;
1162
        }
11639×
        self.store.index_add_pub("accepted", r, &a)?;
11649×
        Ok(())
11659×
    }
1166
1167
    /// A fetched bookmark's attestation by a signer the policy does not
1168
    /// list itself: a delegate or a short-lived key, whose authority
1169
    /// depends on when it signed (spec §14.5, §14.8). Checked at the
1170
    /// attestation's integrated time when the host's log gives one; else
1171
    /// as before, at the host's acceptance for a certified signer, when it
1172
    /// countersigned one, or at the attestation's own time, and said so
1173
    /// when the host keeps a log. Failing at the integrated time but not
1174
    /// as before is a note while logs are new; a large gap between the
1175
    /// claimed and the integrated time is a note.
1176
    #[allow(clippy::too_many_arguments)]
117727×
    pub(crate) fn authorize_in_time(
117827×
        &self,
117927×
        p: &crate::policy::Policy,
118027×
        remote_name: &str,
118127×
        r: &crate::protocol::RemoteRef,
118227×
        sa: &SignedAttest,
118327×
        signer: &KeyId,
118427×
        log: &super::transparency::LogCheck,
118527×
        notes: &mut Vec<String>,
118627×
    ) -> Result<()> {
1187
        use super::transparency::{gap_note, judge, Timed};
118827×
        let own = sa.attest.hlc.wall_ms;
118927×
        let at = self.certified_time(signer, sa, r.accepted.as_ref());
119027×
        let time = log.time_of(&r.name);
119129×
        let check = |t_delegate: u64, t_cert: u64| {
119229×
            self.delegate_authorized(p, signer, &r.name, t_delegate)
119327×
                || self.certified_authorized(p, signer, &r.name, Scope::REFS, t_cert)
119429×
        };
119527×
        let before = if at != own {
11967×
            "the host's acceptance"
1197
        } else {
119820×
            "the time it claims"
1199
        };
120027×
        let what = format!("{remote_name}/{}", r.name);
120127×
        match judge(time.as_ref().ok().map(|i| i.ms()), |t| check(t, t), || check(own, at)) {
1202
            Timed::Refused => {
12031×
                return fail(format!(
1204
                    "{what}: attested by {}, which the policy does not authorize for this bookmark; a policy owner can grant it with `oxy policy grant`",
12051×
                    signer.short(12)
1206
                ))
1207
            }
12083×
            Timed::Integrated => {}
12091×
            Timed::Disagrees => notes.push(format!(
1210
                "{what}: {}'s authority had lapsed when witness {} first saw its attestation, though not at {before}; accepted while transparency logs are new",
12111×
                signer.short(12),
12121×
                time.as_ref().map(|i| i.witness.as_str()).unwrap_or("?")
1213
            )),
12141×
            Timed::Fallback if log.kept => notes.push(format!(
1215
                "{what}: {} checked at {before}, not at a witnessed time: {}",
12161×
                signer.short(12),
12171×
                time.as_ref().err().map(String::as_str).unwrap_or("none")
1218
            )),
121921×
            Timed::Fallback => {}
1220
        }
122126×
        if let Some(n) = time.ok().and_then(|i| gap_note(own, i)) {
12222×
            notes.push(format!("{what}'s attestation {n}"));
122324×
        }
122426×
        Ok(())
122527×
    }
1226
1227
    /// A deletion (spec §16.4): `new_state` is zero. The bookmark must exist
1228
    /// at `old_state`, is never the bookmark a non-bare repository has
1229
    /// checked out (a bare one's default is its host's to protect, see
1230
    /// `oxy_server::Backend::default_bookmark`), and under a policy needs
1231
    /// an attestation by a key that may update it.
1232
    /// The deletion's attestation stays the bookmark's latest, so an older
1233
    /// one cannot be replayed to bring it back.
12343×
    fn apply_deletion(
12353×
        &self,
12363×
        view: &mut View,
12373×
        u: &RefUpdate,
12383×
        policy: Option<&crate::policy::Policy>,
12393×
    ) -> std::result::Result<(), String> {
12403×
        let Some(current) = view.bookmarks.get(&u.name).copied() else {
12410×
            return Err(format!("no bookmark {:?} to delete", u.name));
1242
        };
12433×
        if current != u.old_state {
12440×
            return Err("stale: the remote bookmark has moved; pull first".into());
12453×
        }
12463×
        if !self.is_bare() && u.name == self.config.bookmark {
12471×
            return Err(format!(
12481×
                "{:?} is this repository's default bookmark (checked out there) and cannot be deleted",
12491×
                u.name
12501×
            ));
12512×
        }
12522×
        if let Some(p) = policy {
12530×
            let Some(sa) = &u.attestation else {
12540×
                return Err(format!(
12550×
                    "deleting {:?} requires a signed reference attestation under this repository's policy",
12560×
                    u.name
12570×
                ));
1258
            };
12590×
            let signer = sa
12600×
                .sigs
12610×
                .first()
12620×
                .map(|s| s.key)
12630×
                .ok_or("attestation carries no signature")?;
1264
            // Received now: the receiver's own clock is the trusted time.
12650×
            let now = Hlc::now().wall_ms;
12660×
            if !p.authorizes(&signer, &u.name)
12670×
                && !self.delegate_authorized(p, &signer, &u.name, sa.attest.hlc.wall_ms)
12680×
                && !self.certified_authorized(p, &signer, &u.name, Scope::REFS, now)
1269
            {
12700×
                return Err(format!(
12710×
                    "key {} may not delete {:?} under this repository's policy",
12720×
                    signer.short(12),
12730×
                    u.name
12740×
                ));
12750×
            }
12762×
        }
12772×
        if let Some(sa) = &u.attestation {
12781×
            self.record_update_attest(u, sa)?;
12791×
        }
12802×
        view.bookmarks.remove(&u.name);
12812×
        Ok(())
12823×
    }
1283
1284339×
    fn apply_update(
1285339×
        &self,
1286339×
        view: &mut View,
1287339×
        u: &RefUpdate,
1288339×
        policy: Option<&crate::policy::Policy>,
1289339×
    ) -> std::result::Result<(), String> {
1290339×
        if u.name == "policy" {
12910×
            return Err(
12920×
                "the policy reference is updated by sending a policy, not a bookmark update".into(),
12930×
            );
1294339×
        }
1295339×
        let empty = self
1296339×
            .store
1297339×
            .put_state(&StateObject::new(vec![]))
1298339×
            .map_err(|e| e.to_string())?;
1299339×
        if u.new_state.is_zero() {
13003×
            return self.apply_deletion(view, u, policy);
1301336×
        }
1302336×
        let current = view.bookmarks.get(&u.name).copied().unwrap_or(empty);
1303336×
        if current != u.old_state {
130410×
            return Err("stale: the remote bookmark has moved; pull first".into());
1305326×
        }
1306326×
        let new_state = self
1307326×
            .store
1308326×
            .get_state(&u.new_state)
1309326×
            .map_err(|_| "new state object is missing".to_string())?;
1310592×
        for h in &new_state.heads {
1311592×
            if !self.store.has(h) {
13121×
                return Err(format!("head {} is missing", h.short(12)));
1313591×
            }
1314
        }
1315325×
        if !u.force {
1316324×
            let old_heads = self
1317324×
                .store
1318324×
                .get_state(&current)
1319324×
                .map_err(|e| e.to_string())?
1320
                .heads;
1321324×
            let closure = self
1322324×
                .closure_in_store(&new_state.heads)
1323324×
                .map_err(|e| e.to_string())?;
1324324×
            if let Some(h) = old_heads.iter().find(|h| !closure.contains(h)) {
13251×
                return Err(format!(
13261×
                    "not a fast-forward: current head {} would be dropped (use --force)",
13271×
                    h.short(12)
13281×
                ));
1329323×
            }
13301×
        }
1331324×
        if let Some(p) = policy {
133259×
            let Some(sa) = &u.attestation else {
13331×
                return Err(format!(
13341×
                    "{:?} requires a signed reference attestation under this repository's policy",
13351×
                    u.name
13361×
                ));
1337
            };
133858×
            let signer = sa
133958×
                .sigs
134058×
                .first()
134158×
                .map(|s| s.key)
134258×
                .ok_or("attestation carries no signature")?;
1343
            // Received now: the receiver's own clock is the trusted time.
134458×
            let now = Hlc::now().wall_ms;
134558×
            if !p.authorizes(&signer, &u.name)
134619×
                && !self.delegate_authorized(p, &signer, &u.name, sa.attest.hlc.wall_ms)
134716×
                && !self.certified_authorized(p, &signer, &u.name, Scope::REFS, now)
1348
            {
134910×
                return Err(format!(
135010×
                    "key {} may not update {:?} under this repository's policy",
135110×
                    signer.short(12),
135210×
                    u.name
135310×
                ));
135448×
            }
135548×
            let old_heads = self
135648×
                .store
135748×
                .get_state(&current)
135848×
                .map_err(|e| e.to_string())?
1359
                .heads;
136048×
            self.state_allowed(&old_heads, &new_state.heads, p, &u.name)?;
1361265×
        }
1362313×
        if let Some(sa) = &u.attestation {
1363105×
            self.record_update_attest(u, sa)?;
1364208×
        }
1365307×
        view.bookmarks.insert(u.name.clone(), u.new_state);
1366307×
        Ok(())
1367339×
    }
1368
1369
    // ------------------------------------------------------------ client side
1370
13711506×
    fn remote_url(&self, name: &str) -> Result<String> {
13721506×
        self.config.remotes.get(name).cloned().ok_or_else(|| {
13733×
            let names: Vec<&str> = self.config.remotes.keys().map(String::as_str).collect();
13743×
            RepoError::Msg(match names.as_slice() {
13753×
                [] => format!("no remote named {name:?}; add one with `oxy remote add`"),
13761×
                _ if name == "origin" && names.len() > 1 && self.config.default_remote.is_none() => format!(
1377
                    "this repository has no remote named origin, and several others ({}): name one (`oxy pull {}`), or make one the default with `oxy config core.remote {}`",
13781×
                    names.join(", "),
13791×
                    names[0],
13801×
                    names[0]
1381
                ),
13820×
                _ => format!(
1383
                    "no remote named {name:?}; this repository has {}",
13840×
                    names.join(", ")
1385
                ),
1386
            })
13873×
        })
13881506×
    }
1389
1390
    /// Refuse a remote that is another repository. An **unbound** remote
1391
    /// (id all zero: a hosted repository nobody has pushed to) is accepted
1392
    /// for pushes, which bind it to this repository's id (spec §16.1).
1393819×
    fn check_remote_id(
1394819×
        &self,
1395819×
        remote: &dyn crate::protocol::Remote,
1396819×
        remote_name: &str,
1397819×
        allow_unbound: bool,
1398819×
    ) -> Result<()> {
1399819×
        let theirs = remote.repo_id()?;
1400818×
        if theirs == self.id()? {
1401790×
            return Ok(());
140228×
        }
140328×
        if theirs == crate::protocol::UNBOUND_ID {
140425×
            if allow_unbound {
140524×
                return Ok(());
14061×
            }
14071×
            return fail(format!(
1408
                "remote {remote_name:?} is empty and unbound: push to it first"
1409
            ));
14103×
        }
14113×
        fail(format!(
1412
            "remote {remote_name:?} is a different repository (repository IDs differ); check its URL with `oxy remote list`"
1413
        ))
1414819×
    }
1415
1416
    /// Credentials and trust for a remote: `remote.<name>.token` and
1417
    /// `remote.<name>.ca`, else the defaults for its URL
1418
    /// ([`Repo::fill_credentials`]).
14191122×
    pub(crate) fn remote_options(&self, name: &str) -> RemoteOptions {
14201122×
        let url = self.config.remotes.get(name).cloned().unwrap_or_default();
14211122×
        let mut options = RemoteOptions {
14221122×
            repo_id: self.id().ok(),
14231122×
            token: self.config.remote_tokens.get(name).cloned(),
14241122×
            ca: self.config.remote_cas.get(name).map(PathBuf::from),
14251122×
            cache_dir: Some(self.oxy.join("remotes").join(name).join("strata")),
14261122×
            ..RemoteOptions::default()
14271122×
        };
14281122×
        self.fill_credentials(&url, &mut options);
14291122×
        options
14301122×
    }
1431
1432
    /// The user's credential store (`credentials.toml` beside the user
1433
    /// configuration file).
14341143×
    pub fn credentials(&self) -> crate::credentials::Credentials {
14351143×
        crate::credentials::Credentials::at(
14361143×
            self.user_config
14371143×
                .parent()
14381143×
                .map(Path::to_path_buf)
14391143×
                .unwrap_or_else(default_home),
1440
        )
14411143×
    }
1442
1443
    /// Fill in what `options` lacks for `url`: `$OXY_TOKEN` and `$OXY_CA`,
1444
    /// then the user's credential store for its hub, then a remote on the
1445
    /// same hub in an enclosing checkout (a subrepo borrows its parent's).
1446
    /// When the token is not this remote's own or the environment's, the
1447
    /// other tokens enclosing checkouts hold for the hub are kept to try
1448
    /// if it is refused: a stale stored login must not hide a parent's
1449
    /// good token (oxygen#112).
14501144×
    fn fill_credentials(&self, url: &str, options: &mut RemoteOptions) {
14511144×
        if options.token.is_some() && options.ca.is_some() {
14521×
            return;
14531143×
        }
14541143×
        let explicit =
14551143×
            options.token.is_some() || std::env::var("OXY_TOKEN").is_ok_and(|t| !t.is_empty());
14561143×
        let stored = self.credentials().options_for(url);
14571143×
        options.token = options.token.take().or(stored.token);
14581143×
        options.ca = options.ca.take().or(stored.ca);
14591143×
        let enclosing = self.enclosing_credentials(url);
14601143×
        if options.token.is_none() {
14611133×
            options.token = enclosing.iter().find_map(|(t, _)| t.clone());
146210×
        }
14631143×
        if options.ca.is_none() {
14641127×
            options.ca = enclosing.iter().find_map(|(_, c)| c.clone());
146516×
        }
14661143×
        if !explicit {
14671136×
            for t in enclosing.into_iter().filter_map(|(t, _)| t) {
14685×
                if options.token.as_ref() != Some(&t) && !options.fallback_tokens.contains(&t) {
14692×
                    options.fallback_tokens.push(t);
14703×
                }
1471
            }
14727×
        }
14731144×
    }
1474
1475
    /// The tokens and CAs of remotes on `url`'s hub in enclosing
1476
    /// checkouts, nearest first.
14771143×
    fn enclosing_credentials(&self, url: &str) -> Vec<(Option<String>, Option<PathBuf>)> {
14781143×
        let mut out = Vec::new();
14791143×
        let Some(hub) = crate::credentials::hub_of(url) else {
1480993×
            return out;
1481
        };
1482150×
        let mut at = self.root.parent();
1483608×
        while let Some(dir) = at {
1484458×
            let Ok(cfg) = read_config_file(&dir.join(OXY_DIR).join("config")) else {
14850×
                break;
1486
            };
1487458×
            for (key, remote_url) in &cfg {
148815×
                let Some(name) = key
148915×
                    .strip_prefix("remote.")
149015×
                    .and_then(|k| k.strip_suffix(".url"))
1491
                else {
14927×
                    continue;
1493
                };
14948×
                if crate::credentials::hub_of(remote_url).as_deref() != Some(hub.as_str()) {
14951×
                    continue;
14967×
                }
14977×
                out.push((
14987×
                    cfg.get(&format!("remote.{name}.token")).cloned(),
14997×
                    cfg.get(&format!("remote.{name}.ca")).map(PathBuf::from),
15007×
                ));
1501
            }
1502458×
            at = dir.parent();
1503
        }
1504150×
        out
15051143×
    }
1506
1507
    /// Push seal attestations on their own (hub spec §7.4): a bundle with
1508
    /// no changes and no reference updates, which the receiver stores.
15090×
    pub fn push_seals(&self, remote_name: &str, seals: &[ObjectRef]) -> Result<()> {
15100×
        let remote = self.open_named_remote(remote_name)?;
15110×
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
15120×
        let mut bundle = Bundle::default();
15130×
        for r in seals {
15140×
            let attest = self.store.get_attest(r)?;
15150×
            let sigs = self.store.signatures(r)?;
15160×
            bundle
15170×
                .attests
15180×
                .push(crate::protocol::SignedAttest { attest, sigs });
1519
        }
15200×
        let results = remote.push(bundle, vec![], None, vec![])?;
15210×
        if let Some(r) = results.iter().find(|r| !r.ok) {
15220×
            return fail(format!("{}: {}", r.name, r.reason));
15230×
        }
15240×
        Ok(())
15250×
    }
1526
1527
    /// The remote whose URL starts with `base` (a hub), if configured.
15284×
    pub fn remote_for_url(&self, base: &str) -> Option<String> {
15294×
        let base = base.trim_end_matches('/');
15304×
        self.config
15314×
            .remotes
15324×
            .iter()
15334×
            .find(|(_, url)| url.trim_end_matches('/') == base)
15344×
            .map(|(name, _)| name.clone())
15354×
    }
1536
1537
    /// Open a remote by name or, failing that, by URL with the default
1538
    /// credentials (see [`Repo::credentials_for`]).
15390×
    pub fn open_remote_at(&self, url: &str) -> Result<(String, Box<dyn crate::protocol::Remote>)> {
15400×
        if let Some(name) = self.remote_for_url(url) {
15410×
            return Ok((name.clone(), self.open_named_remote(&name)?));
15420×
        }
15430×
        let options = RemoteOptions {
15440×
            repo_id: self.id().ok(),
15450×
            ..self.credentials_for(url)
15460×
        };
15470×
        Ok((url.to_string(), open_remote(url, &options)?))
15480×
    }
1549
1550
    /// Credentials for a URL: those of the configured remote with that
1551
    /// URL, else the environment's.
15524×
    pub fn options_for_url(&self, url: &str) -> RemoteOptions {
15534×
        match self.remote_for_url(url) {
15544×
            Some(name) => self.remote_options(&name),
15550×
            None => RemoteOptions {
15560×
                repo_id: self.id().ok(),
15570×
                ..self.credentials_for(url)
15580×
            },
1559
        }
15604×
    }
1561
15621105×
    fn open_named_remote(&self, name: &str) -> Result<Box<dyn crate::protocol::Remote>> {
15631105×
        open_remote(&self.remote_url(name)?, &self.remote_options(name))
15641105×
    }
1565
1566220×
    fn serial_path(&self, remote: &str, bookmark: &str) -> PathBuf {
1567220×
        self.oxy
1568220×
            .join("remotes")
1569220×
            .join(remote)
1570220×
            .join(format!("{bookmark}.serial"))
1571220×
    }
1572
157373×
    fn last_seen_serial(&self, remote: &str, bookmark: &str) -> Option<u64> {
157473×
        fs::read_to_string(self.serial_path(remote, bookmark))
157573×
            .ok()
157673×
            .and_then(|s| s.trim().parse().ok())
157773×
    }
1578
1579147×
    fn record_serial(&self, remote: &str, bookmark: &str, serial: u64) -> Result<()> {
1580147×
        let p = self.serial_path(remote, bookmark);
1581147×
        fs::create_dir_all(p.parent().unwrap())?;
1582147×
        write_atomic(&p, serial.to_string().as_bytes())
1583147×
    }
1584
1585473×
    fn policy_serial_path(&self, remote: &str) -> PathBuf {
1586473×
        self.oxy.join("remotes").join(remote).join("policy.serial")
1587473×
    }
1588
1589
    /// The highest policy serial `remote` has ever presented.
1590401×
    fn remote_policy_serial(&self, remote: &str) -> Option<u64> {
1591401×
        fs::read_to_string(self.policy_serial_path(remote))
1592401×
            .ok()
1593401×
            .and_then(|s| s.trim().parse().ok())
1594401×
    }
1595
159672×
    fn record_policy_serial(&self, remote: &str, serial: u64) -> Result<()> {
159772×
        let p = self.policy_serial_path(remote);
159872×
        fs::create_dir_all(p.parent().unwrap())?;
159972×
        write_atomic(&p, serial.to_string().as_bytes())
160072×
    }
1601
160232×
    fn lazy_dir(&self) -> PathBuf {
160332×
        self.oxy.join("lazy")
160432×
    }
1605
1606
    /// Remember that `blob` was withheld by `remote` (spec §13.5).
16075×
    fn register_lazy(&self, blob: &ObjectRef, remote: &str) -> Result<()> {
16085×
        fs::create_dir_all(self.lazy_dir())?;
16095×
        write_atomic(&self.lazy_dir().join(blob.to_hex()), remote.as_bytes())
16105×
    }
1611
1612
    /// Which remote promised an absent blob, if any.
161317×
    pub fn lazy_remote(&self, blob: &ObjectRef) -> Option<String> {
161417×
        fs::read_to_string(self.lazy_dir().join(blob.to_hex()))
161517×
            .ok()
161617×
            .map(|s| s.trim().to_string())
161717×
    }
1618
1619
    /// Fetch an absent blob (and its chunks) from the remote that promised it.
16205×
    pub(crate) fn fetch_lazy_blob(&self, blob: &ObjectRef) -> Result<()> {
16215×
        let Some(remote_name) = self.lazy_remote(blob) else {
16220×
            return Err(RepoError::Store(StoreError::NotFound(*blob)));
1623
        };
16245×
        let remote = self.open_named_remote(&remote_name)?;
16255×
        let objects = remote.objects(&[*blob])?;
162611×
        for (ty, body) in objects {
162711×
            if matches!(ty, ObjectType::Blob | ObjectType::Chunk) {
162811×
                self.store.put(ty, &body)?;
16290×
            }
1630
        }
16315×
        if !self.store.has(blob) {
16320×
            return fail(format!(
1633
                "{remote_name} did not send blob {}",
16340×
                blob.short(12)
1635
            ));
16365×
        }
16375×
        let _ = fs::remove_file(self.lazy_dir().join(blob.to_hex()));
16385×
        Ok(())
16395×
    }
1640
1641
    /// After importing from `remote`, record every blob the changes reference
1642
    /// that the store lacks as lazily available from that remote.
16435×
    fn register_missing_blobs(&self, changes: &[ChangeHash], remote: &str) -> Result<usize> {
16445×
        let mut n = 0;
16455×
        for h in changes {
16465×
            let c = self.store.get_change(h)?;
164715×
            for e in &c.edits {
164815×
                if let Edit::Replace { old, new, .. } = e {
16496×
                    for b in [old, new].into_iter().flatten() {
16506×
                        if !self.store.has(b) {
16514×
                            self.register_lazy(b, remote)?;
16524×
                            n += 1;
16532×
                        }
1654
                    }
16559×
                }
1656
            }
1657
        }
16585×
        Ok(n)
16595×
    }
1660
1661
    /// Every head this repository knows: bookmarks, remote tracking, working copy.
1662401×
    pub fn known_heads(&self, view: &View) -> Result<Vec<ChangeHash>> {
1663401×
        let mut have: BTreeSet<ChangeHash> = BTreeSet::new();
1664542×
        for st in view
1665401×
            .bookmarks
1666401×
            .values()
1667401×
            .chain(view.remotes.values().flat_map(|b| b.values()))
1668
        {
1669542×
            if let Ok(s) = self.store.get_state(st) {
1670542×
                have.extend(s.heads);
1671542×
            }
1672
        }
1673401×
        have.extend(view.workspaces.values().copied());
1674401×
        Ok(have.into_iter().collect())
1675401×
    }
1676
1677
    /// Fetch every bookmark of a remote into `remotes/<name>/bookmarks/*` (spec §10.2).
1678407×
    pub fn fetch(&self, remote_name: &str) -> Result<FetchReport> {
1679407×
        let remote = self.open_named_remote(remote_name)?;
1680404×
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
1681402×
        let resp = remote.refs()?;
1682
        // The host's transparency log (spec §14.8): a fork fails here.
1683401×
        let log = self.check_log(
1684401×
            &self.remote_url(remote_name)?,
1685401×
            self.config.remote_cas.get(remote_name).map(Path::new),
1686401×
            &resp,
16870×
        )?;
1688401×
        let mut log_notes = log.notes.clone();
1689
        // Trust policy (spec §14.5). Adopt the remote's policy when it is newer
1690
        // and signed by roots we trust, or on first contact. A remote that
1691
        // once presented a policy may never present none or an older one:
1692
        // that is a downgrade. A remote that never presented one is
1693
        // unverified, and this fetch is checked only as far as possible.
1694401×
        let local_policy = self.current_policy()?.map(|(_, sp)| sp.policy);
1695401×
        let seen = self.remote_policy_serial(remote_name);
1696401×
        let mut unverified = false;
1697401×
        match &resp.policy {
169838×
            Some(sp) => {
169938×
                if seen.is_some_and(|s| sp.policy.serial < s) {
17000×
                    return fail(format!("{remote_name} presents policy serial {} but presented {} before: refusing the downgrade", sp.policy.serial, seen.unwrap()));
170138×
                }
170214×
                match &local_policy {
170314×
                    Some(l) if sp.policy.serial < l.serial => {}
170414×
                    Some(l) if sp.policy.serial == l.serial => {
170510×
                        if sp.policy.hash() != l.hash() {
17060×
                            return fail(format!(
1707
                                "{remote_name} presents a different policy with the same serial {}",
1708
                                l.serial
1709
                            ));
171010×
                        }
1711
                    }
1712
                    _ => {
171328×
                        self.adopt_policy_chain(
171428×
                            &resp.history,
171528×
                            sp,
171628×
                            &format!("oxy fetch {remote_name}"),
1717
                        )
171828×
                        .map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?;
1719
                    }
1720
                }
172138×
                self.record_policy_serial(remote_name, sp.policy.serial)?;
1722
            }
1723
            None => {
1724363×
                if seen.is_some() {
17250×
                    return fail(format!("{remote_name} presented a policy before but has none now: refusing the downgrade"));
1726363×
                }
1727363×
                unverified = true;
1728
            }
1729
        }
1730401×
        let policy = if unverified {
1731363×
            None
1732
        } else {
173338×
            self.current_policy()?.map(|(_, sp)| sp.policy)
1734
        };
1735401×
        let (op_ref, op) = self.head_op()?;
1736401×
        let mut view = op.view.clone();
1737401×
        let have = self.known_heads(&view)?;
1738401×
        let mut report = Vec::new();
1739401×
        let mut skipped = Vec::new();
1740401×
        let max_blob = self.config.max_blob;
1741427×
        for r in resp.refs {
1742427×
            let needed = {
1743427×
                let _t = crate::timing::phase("fetch: negotiate");
1744427×
                remote.negotiate(&have, &r.heads)?
1745
            };
1746427×
            let bundle = {
1747427×
                let _t = crate::timing::phase("fetch: bundle");
1748427×
                remote.fetch(&needed, max_blob)?
1749
            };
1750
            // Changes are not individually re-verified here: the bookmark's
1751
            // attestation (checked below) binds its whole state, and the
1752
            // receiver of the push verified the added changes at that time.
1753427×
            let imported = {
1754427×
                let _t = crate::timing::phase("fetch: import");
1755427×
                self.import_bundle(&bundle, false)?
1756
            };
1757427×
            if max_blob.is_some() {
17584×
                self.register_missing_blobs(&imported, remote_name)?;
1759423×
            }
1760564×
            for h in &r.heads {
1761564×
                if !self.store.has(h) {
17620×
                    return fail(format!("remote did not send head {}", h.short(12)));
1763564×
                }
1764
            }
1765427×
            let state = self.store.put_state(&StateObject::new(r.heads.clone()))?;
1766427×
            if state != r.state {
17670×
                return fail(format!(
1768
                    "remote state {} does not match its heads",
17690×
                    r.state.short(12)
1770
                ));
1771427×
            }
1772427×
            if let Some(p) = &policy {
177341×
                let Some(sa) = &r.attestation else {
17740×
                    return fail(format!(
1775
                        "{remote_name}/{} has no attestation, which its policy requires",
1776
                        r.name
1777
                    ));
1778
                };
177941×
                let signer = sa
178041×
                    .sigs
178141×
                    .first()
178241×
                    .map(|s| s.key)
178341×
                    .ok_or_else(|| RepoError::Msg("attestation carries no signature".into()))?;
178441×
                if !p.authorizes(&signer, &r.name) {
178520×
                    self.authorize_in_time(p, remote_name, &r, sa, &signer, &log, &mut log_notes)?;
178621×
                }
1787386×
            }
1788427×
            if let Some(sa) = &r.attestation {
1789
                let AttestBody::Ref {
179073×
                    old_state,
179173×
                    new_state,
1792
                    ..
179373×
                } = &sa.attest.body
1794
                else {
17950×
                    return fail("remote sent a non-ref attestation for a bookmark");
1796
                };
179773×
                let (old_state, new_state) = (*old_state, *new_state);
179873×
                let serial = self
179973×
                    .check_ref_attest_body(
180073×
                        sa,
180173×
                        &r.name,
180273×
                        &old_state,
180373×
                        &new_state,
180473×
                        self.last_seen_serial(remote_name, &r.name),
1805
                        true,
1806
                    )
180773×
                    .map_err(|e| {
18080×
                        RepoError::Msg(format!("attestation for {}/{}: {e}", remote_name, r.name))
18090×
                    })?;
181073×
                if new_state != r.state {
18110×
                    return fail(format!(
1812
                        "attestation for {}/{} does not cover its current state",
1813
                        remote_name, r.name
1814
                    ));
181573×
                }
1816
                // A signature this client cannot verify (its signer unknown
1817
                // here, as from a host that did not send the identity, or
1818
                // bad) fails that bookmark alone: it stays as it was and
1819
                // the rest are fetched (oxygenhub#266). The current
1820
                // bookmark, which a pull would take, still fails the fetch.
1821
                // A policy's requirements were checked above and still fail.
182273×
                if let Err(e) = self.verify_attest_signature(sa) {
182312×
                    let why = format!("attestation for {remote_name}/{}: {e}", r.name);
182412×
                    if r.name == self.config.bookmark {
18253×
                        return fail(why);
18269×
                    }
18279×
                    skipped.push((r.name, why));
18289×
                    continue;
182961×
                }
183061×
                let ar = self.store.put_attest(&sa.attest)?;
183161×
                for sig in &sa.sigs {
183261×
                    self.store.put_signature(&ar, sig)?;
1833
                }
183461×
                self.record_serial(remote_name, &r.name, serial)?;
1835354×
            }
1836415×
            view.remotes
1837415×
                .entry(remote_name.to_string())
1838415×
                .or_default()
1839415×
                .insert(r.name.clone(), state);
1840415×
            report.push((r.name, r.heads, imported.len()));
1841
        }
1842398×
        let mut new_tags = 0;
1843398×
        for t in &resp.tags {
184431×
            if view.tags.contains_key(&t.name) {
18457×
                continue;
184624×
            }
1847
            // Withdrawn here: the name is not used again.
184824×
            if self.tag_withdrawal(&t.name)?.is_some() {
18490×
                continue;
185024×
            }
1851
            // The tag's changes must be here: fetch them if the bookmarks did not bring them.
185224×
            let missing: Vec<ChangeHash> = t
185324×
                .heads
185424×
                .iter()
185524×
                .filter(|h| !self.store.has(h))
185624×
                .copied()
185724×
                .collect();
185824×
            if !missing.is_empty() {
18590×
                let needed = remote.negotiate(&self.known_heads(&view)?, &missing)?;
18600×
                let bundle = remote.fetch(&needed, max_blob)?;
18610×
                self.import_bundle(&bundle, false)?;
186224×
            }
186324×
            self.store.put_state(&StateObject::new(t.heads.clone()))?;
186424×
            let mut check = TagCheck::Fetched {
186524×
                remote: remote_name,
186624×
                log: &log,
186724×
                notes: &mut log_notes,
186824×
            };
186924×
            match self.adopt_tag(&mut view, t, policy.as_ref(), &mut check) {
187022×
                Ok(()) => new_tags += 1,
1871
                // Nothing says when a short-lived key signed it: the tag is
1872
                // left out, said so, and the rest is fetched (oxygen#129).
18732×
                Err(e) if e.untimed => {
18742×
                    skipped.push((format!("tags/{}", t.name), e.reason));
18752×
                }
18760×
                Err(e) => return fail(format!("{remote_name} {}: {}", t.name, e.reason)),
1877
            }
1878
        }
1879
        // The remote withdrew a tag this repository has: so does this one,
1880
        // once the withdrawal is checked as a push's would be; one that is
1881
        // not is left as it is, said so (oxygenhub#301).
1882398×
        let mut withdrawn_tags = Vec::new();
1883398×
        for t in &resp.withdrawn {
18841×
            if !view.tags.contains_key(&t.name) {
18850×
                continue;
18861×
            }
18871×
            let mut check = TagCheck::Fetched {
18881×
                remote: remote_name,
18891×
                log: &log,
18901×
                notes: &mut log_notes,
18911×
            };
18921×
            match self.adopt_withdrawal(&mut view, t, policy.as_ref(), &mut check) {
18931×
                Ok(true) => withdrawn_tags.push(t.name.clone()),
18940×
                Ok(false) => {}
18950×
                Err(e) => skipped.push((
18960×
                    format!("tags/{}", t.name),
18970×
                    format!("withdrawn on {remote_name}, kept here: {}", e.reason),
18980×
                )),
1899
            }
1900
        }
1901398×
        if view != op.view {
1902382×
            self.record_op(&[op_ref], &format!("oxy fetch {remote_name}"), view)?;
190316×
        }
1904398×
        let _ = self.run_hook(
1905398×
            "post-fetch",
1906398×
            &format!(
1907
                "{{\"event\":\"post-fetch\",\"remote\":{},\"bookmarks\":{}}}",
1908398×
                json_str(remote_name),
1909412×
                json_array(report.iter().map(|(n, heads, _)| format!(
1910
                    "{{\"name\":{},\"heads\":{}}}",
1911412×
                    json_str(n),
1912549×
                    json_array(heads.iter().map(|h| json_str(&h.to_hex())))
1913
                )))
1914
            ),
1915
        );
1916398×
        Ok(FetchReport {
1917398×
            refs: report,
1918398×
            unverified,
1919398×
            new_tags,
1920398×
            withdrawn_tags,
1921398×
            log_notes,
1922398×
            skipped,
1923398×
        })
1924407×
    }
1925
1926
    /// Shallow fetch of the remote's bookmark: its checkpoint as our base,
1927
    /// the changes within `depth` steps of its heads, and the boundary below.
19283×
    pub fn fetch_shallow(&self, remote_name: &str, depth: u32) -> Result<usize> {
19293×
        let remote = self.open_named_remote(remote_name)?;
19303×
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
19313×
        let resp = remote.refs()?;
19323×
        let bookmark = self.config.bookmark.clone();
19333×
        if let Some(sp) = &resp.policy {
19340×
            self.adopt_policy_chain(&resp.history, sp, "oxy clone --depth")
19350×
                .map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?;
19360×
            self.record_policy_serial(remote_name, sp.policy.serial)?;
19373×
        }
19383×
        let Some(r) = resp.refs.into_iter().find(|r| r.name == bookmark) else {
19390×
            return fail(format!("{remote_name} has no bookmark {bookmark:?}"));
1940
        };
19413×
        if r.heads.is_empty() {
19420×
            return Ok(0);
19433×
        }
1944
        // Base: the remote's checkpoint of exactly these heads.
19453×
        let cp_bundle = remote.checkpoint(&r.heads)?;
1946
        // In one batch: a big state's checkpoint is tens of thousands of
1947
        // graph blobs and chunks, which stored one by one were each written
1948
        // loose and flushed (a VS Code clone ran at 27 objects a second).
19493×
        let objects: Vec<(ObjectType, &[u8])> = cp_bundle
19503×
            .objects
19513×
            .iter()
19529×
            .map(|(ty, body)| (*ty, body.as_slice()))
19533×
            .collect();
19543×
        let batch = self.store.begin_batch()?;
19553×
        let stored = self.store.put_many(&objects);
19563×
        batch.finish()?;
19573×
        let cp_ref = objects
19583×
            .iter()
19593×
            .zip(stored?)
19609×
            .find(|((ty, _), _)| *ty == ObjectType::Checkpoint)
19613×
            .map(|(_, r)| r);
19623×
        let Some(cp_ref) = cp_ref else {
19630×
            return fail(format!("{remote_name} sent no checkpoint"));
1964
        };
19653×
        self.install_checkpoint(&bookmark, &cp_ref)?;
1966
        // The blobs of the state's opaque files come with no change when the
1967
        // window holds none of the changes that set them (#137): fetch them
1968
        // now, or under `filter.max-blob` leave them to be fetched on read.
19693×
        let cp = crate::objects::Checkpoint::decode(
19703×
            &self.store.get_typed(&cp_ref, ObjectType::Checkpoint)?,
19710×
        )?;
19723×
        let base = State::from_checkpoint_history(&self.read_blob(&cp.payload)?)?;
19733×
        let mut missing: Vec<ObjectRef> = base
19743×
            .opaque_nodes()
19753×
            .iter()
19763×
            .flat_map(|n| base.live_blobs(n))
19773×
            .filter(|b| !self.store.has(b))
19783×
            .collect();
19793×
        missing.sort();
19803×
        missing.dedup();
19813×
        if self.config.max_blob.is_some() {
19821×
            for b in &missing {
19831×
                self.register_lazy(b, remote_name)?;
1984
            }
1985
        } else {
19862×
            for part in missing.chunks(512) {
19871×
                let objects = remote.objects(part)?;
19881×
                let objects: Vec<(ObjectType, &[u8])> = objects
19891×
                    .iter()
19902×
                    .filter(|(ty, _)| matches!(ty, ObjectType::Blob | ObjectType::Chunk))
19912×
                    .map(|(ty, body)| (*ty, body.as_slice()))
19921×
                    .collect();
19931×
                let batch = self.store.begin_batch()?;
19941×
                let stored = self.store.put_many(&objects);
19951×
                batch.finish()?;
19961×
                stored?;
1997
            }
19982×
            if let Some(b) = missing.iter().find(|b| !self.store.has(b)) {
19990×
                return fail(format!("{remote_name} did not send blob {}", b.short(12)));
20002×
            }
2001
        }
2002
        // History window.
20033×
        let wanted = remote.shallow(&r.heads, depth)?;
20043×
        let bundle = remote.fetch(&wanted, self.config.max_blob)?;
20053×
        let imported = self.import_bundle_shallow(&bundle, false)?;
20063×
        if self.config.max_blob.is_some() {
20071×
            self.register_missing_blobs(&imported, remote_name)?;
20082×
        }
20093×
        self.refresh_boundary(&imported)?;
20103×
        if let Some(sa) = &r.attestation {
20110×
            let ar = self.store.put_attest(&sa.attest)?;
20120×
            for sig in &sa.sigs {
20130×
                self.store.put_signature(&ar, sig)?;
2014
            }
20150×
            if let AttestBody::Ref { serial, .. } = sa.attest.body {
20160×
                self.record_serial(remote_name, &r.name, serial)?;
20170×
            }
20183×
        }
20193×
        let state = self.store.put_state(&StateObject::new(r.heads.clone()))?;
20203×
        let (op_ref, op) = self.head_op()?;
20213×
        let mut view = op.view.clone();
20223×
        view.remotes
20233×
            .entry(remote_name.to_string())
20243×
            .or_default()
20253×
            .insert(r.name.clone(), state);
20263×
        self.record_op(
20273×
            &[op_ref],
20283×
            &format!("oxy fetch {remote_name} --depth {depth}"),
20293×
            view,
20300×
        )?;
20313×
        Ok(imported.len())
20323×
    }
2033
2034
    /// Fetch, then take the union of the local bookmark's state with the
2035
    /// remote's: no merge change, conflicts become reified (spec §9.4).
2036111×
    pub fn pull(&self, remote_name: &str) -> Result<PullReport> {
2037111×
        let _wc = self.lock_working_copy()?;
2038111×
        let skipped = self.fetch(remote_name)?.skipped;
2039107×
        let (op_ref, op) = self.head_op()?;
2040107×
        let current = self.config.bookmark.clone();
2041107×
        let mut local_current = self.heads_of(&op.view, &current)?;
2042107×
        local_current.sort();
2043107×
        let mut view = op.view.clone();
2044107×
        let mut current_heads = local_current.clone();
2045107×
        let mut changed = false;
2046107×
        let mut others = Vec::new();
2047107×
        let tracked = op
2048107×
            .view
2049107×
            .remotes
2050107×
            .get(remote_name)
2051107×
            .cloned()
2052107×
            .unwrap_or_default();
2053112×
        for (name, remote_state) in tracked {
2054
            // Every local bookmark tracks the remote's of the same name;
2055
            // the current one may be new here.
2056112×
            let local_state = op.view.bookmarks.get(&name).copied();
2057112×
            if local_state.is_none() && name != current {
20581×
                continue;
2059111×
            }
2060111×
            if local_state == Some(remote_state) {
206110×
                continue;
2062101×
            }
2063101×
            let local = self.heads_of(&op.view, &name)?;
2064101×
            let remote_heads = self.store.get_state(&remote_state)?.heads;
2065134×
            if remote_heads.iter().all(|h| local.contains(h)) {
20660×
                continue;
2067101×
            }
2068101×
            let mut all = local.clone();
2069101×
            all.extend(remote_heads);
2070101×
            let st = self.load_state_history(&all)?;
2071101×
            let mut heads = st.maximal(&all);
2072101×
            heads.sort();
2073101×
            let mut sorted_local = local;
2074101×
            sorted_local.sort();
2075101×
            if heads == sorted_local {
20761×
                continue;
2077100×
            }
2078100×
            view.bookmarks.insert(
2079100×
                name.clone(),
2080100×
                self.store.put_state(&StateObject::new(heads.clone()))?,
2081
            );
2082100×
            if name == current {
208398×
                changed = true;
208498×
                current_heads = heads;
208598×
            } else {
20862×
                others.push(name);
20872×
            }
2088
        }
2089107×
        if changed || !others.is_empty() {
209099×
            self.record_op(&[op_ref], &format!("oxy pull {remote_name}"), view)?;
20918×
        }
2092
        // The working copy follows its bookmark whenever nothing would be
2093
        // lost, also when this pull brought nothing new but it had been
2094
        // left behind (oxygen#89).
2095107×
        let checked_out = self.follow_bookmark(&current_heads, &current, "oxy pull")?;
2096
        Ok(PullReport {
2097107×
            heads: if changed {
209898×
                current_heads
2099
            } else {
21009×
                local_current
2101
            },
2102107×
            changed,
2103107×
            checked_out,
2104107×
            others,
2105107×
            skipped,
2106
        })
2107111×
    }
2108
2109
    /// How many changes the working copy's bookmark has that its base
2110
    /// lacks, when the base is part of the bookmark's state (it fell behind,
2111
    /// and `oxy checkout` or `oxy pull` brings it up); `None` when it is on
2112
    /// the bookmark or holds changes the bookmark lacks. History is read
2113
    /// only when the base and the bookmark's heads differ (oxygen#89).
21147×
    pub fn working_copy_behind(&self) -> Result<Option<usize>> {
21157×
        let (_, wc) = self.working_copy()?;
21167×
        let mut base = wc.deps.clone();
21177×
        base.sort();
21187×
        let mut heads = self.bookmark_heads()?;
21197×
        heads.sort();
21207×
        if base == heads || heads.is_empty() {
21216×
            return Ok(None);
21221×
        }
21231×
        let state = self.load_state_history(&heads)?;
21241×
        if !base.iter().all(|h| state.contains(h)) {
21250×
            return Ok(None);
21261×
        }
21271×
        let had = if base.is_empty() {
21280×
            0
2129
        } else {
21301×
            self.load_state_history(&base)?.len()
2131
        };
21321×
        Ok(Some(state.len().saturating_sub(had)).filter(|n| *n > 0))
21337×
    }
2134
2135
    /// Bring the working copy to `heads`, the state of its bookmark
2136
    /// `bookmark`, when nothing would be lost: its base is part of that
2137
    /// state, so the bookmark only adds to it. Clean, it is checked out;
2138
    /// with uncommitted work, the work comes along when the new changes
2139
    /// leave its paths alone (as `git pull` does), else it stays. Before,
2140
    /// it followed only from exactly the bookmark's old heads, so a working
2141
    /// copy already behind (the bookmark moved by `bookmark set`, a landing
2142
    /// or a pull from another workspace) never caught up, and nothing said
2143
    /// so (oxygen#74, #89). Returns whether it moved.
2144107×
    pub fn follow_bookmark(
2145107×
        &self,
2146107×
        heads: &[ChangeHash],
2147107×
        bookmark: &str,
2148107×
        command: &str,
2149107×
    ) -> Result<bool> {
2150107×
        let snap = self.snapshot(command)?;
2151107×
        let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty();
2152107×
        let mut base = snap.base.maximal(&snap.wc.deps);
2153107×
        base.sort();
2154107×
        let mut target = heads.to_vec();
2155107×
        target.sort();
2156107×
        if base == target {
21578×
            return Ok(false);
215899×
        }
215999×
        let state = self.load_state_history(heads)?;
2160185×
        if !base.iter().all(|h| state.contains(h)) {
2161
            // The working copy holds changes the bookmark lacks: not ours to move.
21620×
            return Ok(false);
216399×
        }
216499×
        if clean {
216599×
            self.checkout(heads, false, &format!("{command} (checkout)"))?;
216699×
            Ok(true)
2167
        } else {
21680×
            Ok(self
21690×
                .checkout_carry(heads, bookmark, &format!("{command} (checkout --carry)"))
21700×
                .is_ok())
2171
        }
2172107×
    }
2173
2174
    /// Withdraw tag `tag` on `remote_name` (`oxy push --delete tags/NAME`,
2175
    /// oxygenhub#301): withdrawn here first when it still is a tag here
2176
    /// (`oxy tag delete`), then the withdrawal alone is sent, with its
2177
    /// signer's identity and certificate.
21783×
    pub fn push_tag_withdrawal(&self, remote_name: &str, tag: &str) -> Result<Vec<RefResult>> {
21793×
        let remote = self.open_named_remote(remote_name)?;
21803×
        self.check_remote_id(remote.as_ref(), remote_name, true)?;
21813×
        let resp = remote.refs()?;
21823×
        let name = format!("tags/{tag}");
21833×
        if resp.withdrawn.iter().any(|t| t.name == tag) {
21841×
            return Ok(vec![RefResult {
21851×
                name,
21861×
                ok: true,
21871×
                reason: "already withdrawn".into(),
21881×
            }]);
21892×
        }
21902×
        let Some(there) = resp.tags.iter().find(|t| t.name == tag) else {
21910×
            return fail(format!("{remote_name} has no tag {tag}"));
2192
        };
21932×
        if resp.policy.is_some() && self.secret_key()?.is_none() {
21940×
            return fail(format!("{remote_name} enforces a trust policy; withdrawing a tag needs an identity (`oxy identity create`)"));
21952×
        }
21962×
        if self.tag_withdrawal(tag)?.is_none() {
21972×
            match self.view()?.tags.get(tag) {
21982×
                Some(st) if *st == there.state => {
21992×
                    self.tag_delete(tag, "")?;
2200
                }
2201
                Some(_) => {
22020×
                    return fail(format!(
2203
                        "tag {tag} names another state here than on {remote_name}"
2204
                    ))
2205
                }
2206
                None => {
22070×
                    return fail(format!(
2208
                        "there is no tag {tag} here to withdraw: `oxy fetch {remote_name}` first"
2209
                    ))
2210
                }
2211
            }
22120×
        }
22132×
        let Some(w) = self
22142×
            .withdrawn_refs()?
22152×
            .into_iter()
22162×
            .find(|w| w.name == tag && w.state == there.state)
2217
        else {
22180×
            return fail(format!(
2219
                "tag {tag} was withdrawn here at another state than {remote_name}'s"
2220
            ));
2221
        };
22222×
        let mut bundle = Bundle::default();
22232×
        let signers: Vec<KeyId> = w
22242×
            .attestation
22252×
            .iter()
22262×
            .flat_map(|a| a.sigs.iter().map(|s| s.key))
22272×
            .collect();
22282×
        for k in &signers {
22292×
            if let Ok(id) = self.identity(k) {
22302×
                bundle.objects.push((ObjectType::Identity, id.encode()));
22312×
            }
2232
        }
22332×
        self.attach_certificates_for(&mut bundle, &signers)?;
22342×
        remote.push(bundle, vec![], None, vec![w])
22353×
    }
2236
2237
    /// Push the current bookmark (spec §16.3), sealing this author's unsigned
2238
    /// changes and attesting the reference when a key is available.
2239
    /// Delete `bookmark` on a remote (spec §16.4): an update to the zero
2240
    /// state, attested when we have an identity. The local bookmark, if
2241
    /// any, is kept; only the remote-tracking entry goes.
22428×
    pub fn push_delete(&self, remote_name: &str, bookmark: &str) -> Result<Vec<RefResult>> {
22438×
        if let Some(tag) = bookmark.strip_prefix("tags/") {
22443×
            return self.push_tag_withdrawal(remote_name, tag);
22455×
        }
22465×
        let remote = self.open_named_remote(remote_name)?;
22475×
        self.check_remote_id(remote.as_ref(), remote_name, true)?;
22485×
        let resp = remote.refs()?;
22497×
        let Some(r) = resp.refs.iter().find(|r| r.name == bookmark) else {
22501×
            return fail(format!("{remote_name} has no bookmark {bookmark}"));
2251
        };
22524×
        if resp.policy.is_some() && self.secret_key()?.is_none() {
22530×
            return fail(format!("{remote_name} enforces a trust policy; deleting needs an identity (`oxy identity create`)"));
22544×
        }
22554×
        let attestation = match self.secret_key()? {
22562×
            Some(_) => Some(self.sign_attest(&Attest {
22572×
                repo: self.id()?,
22582×
                hlc: Hlc::now(),
22592×
                body: AttestBody::Ref {
22602×
                    name: bookmark.to_string(),
22612×
                    old_state: r.state,
22622×
                    new_state: ObjectRef::ZERO,
22632×
                    serial: r.last_serial.unwrap_or(0) + 1,
22642×
                },
22650×
            })?),
22662×
            None => None,
2267
        };
22684×
        let results = remote.push(
22694×
            Bundle::default(),
22704×
            vec![RefUpdate {
22714×
                name: bookmark.to_string(),
22724×
                old_state: r.state,
22734×
                new_state: ObjectRef::ZERO,
22744×
                force: true,
22754×
                attestation,
22764×
            }],
22774×
            None,
22784×
            vec![],
22790×
        )?;
22804×
        if results.iter().any(|x| x.ok && x.name == bookmark) {
22812×
            let (op_ref, op) = self.head_op()?;
22822×
            let mut view = op.view.clone();
22832×
            if let Some(m) = view.remotes.get_mut(remote_name) {
22842×
                m.remove(bookmark);
22852×
            }
22862×
            self.record_op(
22872×
                &[op_ref],
22882×
                &format!("oxy push {remote_name} --delete {bookmark}"),
22892×
                view,
22900×
            )?;
22912×
        }
22924×
        Ok(results)
22938×
    }
2294
2295
    /// Remove remote `name` (oxygen#86): every `remote.NAME.*` key of this
2296
    /// repository's config (its URL, token, CA and store settings), its
2297
    /// tracking bookmarks in the view, and a store remote's mirror under
2298
    /// `.oxy/remotes/NAME`. Nothing stays to be read as if it were current.
22993×
    pub fn remove_remote(&mut self, name: &str) -> Result<()> {
23003×
        if !self.config.remotes.contains_key(name) {
23011×
            return fail(format!("no remote {name}: `oxy remote list` shows them"));
23022×
        }
23032×
        let prefix = format!("remote.{name}.");
23042×
        let keys: Vec<String> = self
23052×
            .config
23062×
            .repo_layer()
23072×
            .keys()
23084×
            .filter(|k| k.starts_with(&prefix))
23092×
            .cloned()
23102×
            .collect();
23113×
        for k in keys {
23123×
            self.config.set(&k, "")?;
2313
        }
23142×
        self.config.remotes.remove(name);
23152×
        self.save_config()?;
23162×
        let (op_ref, op) = self.head_op()?;
23172×
        if op.view.remotes.contains_key(name) {
23181×
            let mut view = op.view.clone();
23191×
            view.remotes.remove(name);
23201×
            self.record_op(&[op_ref], &format!("oxy remote remove {name}"), view)?;
23211×
        }
23222×
        let mirror = self.oxy.join("remotes").join(name);
23232×
        if mirror.is_dir() {
23240×
            fs::remove_dir_all(&mirror)?;
23252×
        }
23262×
        Ok(())
23273×
    }
2328
2329
    /// Send `bundle`'s blobs and chunks ahead of it, in parts of about
2330
    /// `push.batch-bytes` of objects alone, when the whole would be larger
2331
    /// (oxygen#133): one change can bring more blobs than one request may
2332
    /// carry, and a proxy in front of a hub refuses a request past its limit
2333
    /// (Cloudflare's is 100 MB). What is left, the changes with their
2334
    /// identities and states, is returned to send as before. A receiver
2335
    /// stores objects as they come, and checks a change's blobs are stored.
2336325×
    fn send_objects_ahead(
2337325×
        &self,
2338325×
        remote: &dyn crate::protocol::Remote,
2339325×
        remote_name: &str,
2340325×
        mut bundle: Bundle,
2341325×
        progress: crate::store::Progress,
2342325×
    ) -> Result<Bundle> {
2343325×
        let limit = self.config.push_batch_bytes;
2344325×
        let changes: u64 = bundle
2345325×
            .changes
2346325×
            .iter()
2347413×
            .map(|(c, _)| c.encode().len() as u64)
2348325×
            .sum();
2349325×
        let objects: u64 = bundle.objects.iter().map(|(_, b)| b.len() as u64).sum();
2350325×
        if changes + objects <= limit {
2351318×
            return Ok(bundle);
23527×
        }
23537×
        let (mut kept, mut parts, mut part, mut bytes) = (Vec::new(), Vec::new(), Vec::new(), 0u64);
23549×
        for (ty, body) in std::mem::take(&mut bundle.objects) {
23559×
            if !matches!(ty, ObjectType::Blob | ObjectType::Chunk) {
23560×
                kept.push((ty, body));
23570×
                continue;
23589×
            }
23599×
            if !part.is_empty() && bytes + body.len() as u64 > limit {
23603×
                parts.push(std::mem::take(&mut part));
23613×
                bytes = 0;
23626×
            }
23639×
            bytes += body.len() as u64;
23649×
            part.push((ty, body));
2365
        }
23667×
        if !part.is_empty() {
23671×
            parts.push(part);
23686×
        }
23697×
        let n = parts.len();
23707×
        for (i, objects) in parts.into_iter().enumerate() {
23714×
            progress(&format!(
23724×
                "objects {} of {n}: {} blobs and chunks",
23734×
                i + 1,
23744×
                objects.len()
23754×
            ));
23764×
            let results = remote.push(
23774×
                Bundle {
23784×
                    objects,
23794×
                    ..Default::default()
23804×
                },
23814×
                vec![],
23824×
                None,
23834×
                vec![],
23840×
            )?;
23854×
            match results.iter().find(|r| r.name == OBJECTS_RESULT) {
23864×
                Some(r) if r.ok => {}
23870×
                Some(r) => {
23880×
                    return fail(format!(
2389
                        "{remote_name} refused objects part {} of {n}: {}",
23900×
                        i + 1,
2391
                        r.reason
2392
                    ))
2393
                }
2394
                None => {
23950×
                    return fail(format!(
2396
                        "{remote_name} does not take a push in parts, and this one is too large for one: update the server's oxy"
2397
                    ))
2398
                }
2399
            }
2400
        }
24017×
        bundle.objects = kept;
24027×
        Ok(bundle)
2403325×
    }
2404
2405
    /// `changes`, in dependency order, cut into batches of about
2406
    /// `push.batch-bytes` by what each change and its texts and blobs weigh.
2407
    /// There is always one, perhaps empty.
2408320×
    fn push_batches(&self, changes: &[ChangeHash]) -> Result<Vec<Vec<ChangeHash>>> {
2409320×
        let blob_len = |r: &ObjectRef| -> u64 {
241033×
            self.store
241133×
                .get_typed(r, ObjectType::Blob)
241233×
                .ok()
241333×
                .and_then(|b| Blob::decode(&b).ok())
241433×
                .map_or(0, |b| b.length)
241533×
        };
2416320×
        let (mut out, mut batch, mut bytes) = (Vec::new(), Vec::new(), 0u64);
2417413×
        for h in changes {
2418413×
            let raw = self.store.get_typed(h, ObjectType::Change)?;
2419413×
            let (c, texts) = Change::decode_skeleton(&raw, *h)?;
2420413×
            let mut weight = raw.len() as u64;
2421513×
            for t in &texts {
2422513×
                if let crate::objects::TextRef::Blob(r, _, _) = t {
242326×
                    weight += blob_len(r);
2424487×
                }
2425
            }
2426647×
            for e in &c.edits {
24277×
                if let Edit::Replace { new: Some(b), .. } = e {
24287×
                    weight += blob_len(b);
2429640×
                }
2430
            }
2431413×
            if !batch.is_empty() && bytes + weight > self.config.push_batch_bytes {
24325×
                out.push(std::mem::take(&mut batch));
24335×
                bytes = 0;
2434408×
            }
2435413×
            batch.push(*h);
2436413×
            bytes += weight;
2437
        }
2438320×
        out.push(batch);
2439320×
        Ok(out)
2440320×
    }
2441
2442383×
    pub fn push(&self, remote_name: &str, force: bool) -> Result<Vec<RefResult>> {
2443383×
        let bookmark = self.config.bookmark.clone();
2444383×
        self.push_bookmark(remote_name, &bookmark, force)
2445383×
    }
2446
2447
    /// `push` for a named bookmark (`oxy push -b NAME`).
2448396×
    pub fn push_bookmark(
2449396×
        &self,
2450396×
        remote_name: &str,
2451396×
        bookmark: &str,
2452396×
        force: bool,
2453396×
    ) -> Result<Vec<RefResult>> {
2454396×
        self.push_bookmark_with(remote_name, bookmark, force, &mut |_| {})
2455396×
    }
2456
2457
    /// `push_bookmark`, saying how a long push is going: one line per
2458
    /// batch sent (oxygen#95).
2459405×
    pub fn push_bookmark_with(
2460405×
        &self,
2461405×
        remote_name: &str,
2462405×
        bookmark: &str,
2463405×
        force: bool,
2464405×
        progress: crate::store::Progress,
2465405×
    ) -> Result<Vec<RefResult>> {
2466405×
        let (op_ref, op) = self.head_op()?;
2467405×
        if !op.view.bookmarks.contains_key(bookmark) {
24680×
            return fail(format!("no bookmark {bookmark}"));
2469405×
        }
2470405×
        let bookmark = bookmark.to_string();
2471405×
        let local_heads = self.heads_of(&op.view, &bookmark)?;
2472
        // pre-push hook: may veto before the remote is contacted.
2473405×
        self.run_hook(
2474405×
            "pre-push",
2475405×
            &format!(
2476
                "{{\"event\":\"pre-push\",\"remote\":{},\"bookmark\":{},\"heads\":{}}}",
2477405×
                json_str(remote_name),
2478405×
                json_str(&bookmark),
2479795×
                json_array(local_heads.iter().map(|h| json_str(&h.to_hex())))
2480
            ),
24811×
        )?;
2482404×
        let remote = self.open_named_remote(remote_name)?;
2483404×
        self.check_remote_id(remote.as_ref(), remote_name, true)?;
2484401×
        let resp = remote.refs()?;
2485401×
        let local_policy = self.current_policy()?.map(|(_, sp)| sp);
2486401×
        let resp_policy_serial = resp.policy.as_ref().map(|p| p.policy.serial);
2487401×
        let policy_to_send = match (&local_policy, &resp.policy) {
248844×
            (Some(l), Some(r)) if l.policy.serial > r.policy.serial => Some(l.clone()),
248916×
            (Some(l), None) => Some(l.clone()),
249026×
            (Some(l), Some(r)) if l.policy.serial < r.policy.serial => {
24911×
                return fail(format!(
2492
                    "{remote_name} has a newer policy (serial {}); fetch first",
2493
                    r.policy.serial
2494
                ));
2495
            }
2496366×
            _ => None,
2497
        };
2498400×
        let remote_has_policy = resp.policy.is_some() || policy_to_send.is_some();
2499400×
        if remote_has_policy && self.secret_key()?.is_none() {
25001×
            return fail(format!("{remote_name} enforces a trust policy; pushing needs an identity (`oxy identity create`)"));
2501399×
        }
2502399×
        let remote_tags: BTreeSet<String> = resp
2503399×
            .tags
2504399×
            .iter()
2505399×
            .chain(&resp.withdrawn)
2506399×
            .map(|t| t.name.clone())
2507399×
            .collect();
2508399×
        let tags_to_send: Vec<crate::protocol::TagRef> = self
2509399×
            .tag_refs()?
2510399×
            .into_iter()
2511399×
            .filter(|t| !remote_tags.contains(&t.name))
2512399×
            .collect();
2513
        // Tags withdrawn here that the remote still has, at the same state
2514
        // (oxygenhub#301).
2515399×
        let withdrawals_to_send: Vec<crate::protocol::TagRef> = self
2516399×
            .withdrawn_refs()?
2517399×
            .into_iter()
2518399×
            .filter(|w| {
25192×
                resp.tags
25202×
                    .iter()
25212×
                    .any(|t| t.name == w.name && t.state == w.state)
25222×
            })
2523399×
            .collect();
2524399×
        let refs = resp.refs;
2525399×
        let empty = self.store.put_state(&StateObject::new(vec![]))?;
2526399×
        let (remote_state, remote_heads, last_serial) =
2527400×
            match refs.iter().find(|r| r.name == bookmark) {
2528377×
                Some(r) => (r.state, r.heads.clone(), r.last_serial),
252922×
                None => (empty, vec![], None),
2530
            };
2531399×
        let new_state = self
2532399×
            .store
2533399×
            .put_state(&StateObject::new(local_heads.clone()))?;
2534
        // A push that carries a policy re-attests the bookmark even when its
2535
        // state is unchanged, so the current attestation is always by a key
2536
        // the current policy authorizes (a revoked key's attestation would
2537
        // otherwise be refused by every later fetch).
2538399×
        if new_state == remote_state
253936×
            && policy_to_send.is_none()
254019×
            && tags_to_send.is_empty()
254110×
            && withdrawals_to_send.is_empty()
2542
        {
25439×
            return Ok(vec![RefResult {
25449×
                name: bookmark,
25459×
                ok: true,
25469×
                reason: "already up to date".into(),
25479×
            }]);
2548390×
        }
2549405×
        for h in &remote_heads {
2550405×
            if !self.store.has(h) {
255169×
                return fail(format!(
2552
                    "the remote has change {} which you do not; pull first (`oxy pull {remote_name}`), then push again",
255369×
                    h.short(12)
2554
                ));
2555336×
            }
2556
        }
2557321×
        if !force {
2558321×
            let closure = self.closure_in_store(&local_heads)?;
2559321×
            if let Some(h) = remote_heads.iter().find(|h| !closure.contains(h)) {
25600×
                return fail(format!("not a fast-forward: remote head {} is not in your state; pull first or use --force", h.short(12)));
2561321×
            }
25620×
        }
2563321×
        let to_send = self.negotiate(&remote_heads, &local_heads)?;
2564321×
        let key = self.secret_key()?.map(|(k, _)| k);
2565321×
        if let Some(key) = key {
2566
            // Seal this author's changes that carry no valid authenticator,
2567
            // and adopt anonymous ones (zero author, spec §14.4). The seals
2568
            // are stored, so each batch's bundle carries those it needs.
2569112×
            let mut unsigned = Vec::new();
2570112×
            for h in &to_send {
257192×
                let c = self.store.get_change_meta(h)?;
257292×
                if self.seals_for_author(&c.author, &key)
257387×
                    && !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. })
257453×
                {
257553×
                    unsigned.push(*h);
257653×
                }
2577
            }
2578112×
            for chunk in unsigned.chunks(crate::attest::MAX_SEAL_HASHES) {
257950×
                self.sign_attest(&Attest::seal(self.id()?, chunk.to_vec()))?;
2580
            }
2581209×
        }
2582
        // A long history goes in batches, in dependency order, so neither
2583
        // side holds it whole and no request passes the remote's limit:
2584
        // all but the last carry objects alone; the last moves the bookmark
2585
        // (oxygen#95).
2586320×
        let batches = self.push_batches(&to_send)?;
2587320×
        let (last, earlier) = batches.split_last().expect("at least one batch");
2588320×
        for (i, batch) in earlier.iter().enumerate() {
25895×
            progress(&format!(
25905×
                "part {} of {}: {} changes",
25915×
                i + 1,
25925×
                batches.len(),
25935×
                batch.len()
25945×
            ));
25955×
            let bundle = self.send_objects_ahead(
25965×
                remote.as_ref(),
25975×
                remote_name,
25985×
                self.bundle_for(batch)?,
25995×
                &mut *progress,
26000×
            )?;
26015×
            let results = remote.push(bundle, vec![], None, vec![])?;
26025×
            match results.iter().find(|r| r.name == OBJECTS_RESULT) {
26035×
                Some(r) if r.ok => {}
26040×
                Some(r) => {
26050×
                    return fail(format!(
2606
                        "{remote_name} refused part {} of the push: {}",
26070×
                        i + 1,
2608
                        r.reason
2609
                    ))
2610
                }
2611
                None => {
26120×
                    return fail(format!(
2613
                        "{remote_name} does not take a push in parts, and this one is too long for one: update the server's oxy"
2614
                    ))
2615
                }
2616
            }
2617
        }
2618320×
        if batches.len() > 1 {
26191×
            progress(&format!(
26201×
                "part {} of {}: {} changes",
26211×
                batches.len(),
26221×
                batches.len(),
26231×
                last.len()
26241×
            ));
2625319×
        }
2626320×
        let mut bundle = self.send_objects_ahead(
2627320×
            remote.as_ref(),
2628320×
            remote_name,
2629320×
            self.bundle_for(last)?,
2630320×
            &mut *progress,
26310×
        )?;
2632320×
        bundle.objects.push((
2633320×
            ObjectType::State,
2634320×
            StateObject::new(local_heads.clone()).encode(),
2635320×
        ));
2636320×
        let attestation = match key {
2637111×
            Some(_) => Some(self.sign_attest(&Attest {
2638111×
                repo: self.id()?,
2639111×
                hlc: Hlc::now(),
2640111×
                body: AttestBody::Ref {
2641111×
                    name: bookmark.clone(),
2642111×
                    old_state: remote_state,
2643111×
                    new_state,
2644111×
                    serial: next_serial(last_serial),
2645111×
                },
26461×
            })?),
2647209×
            None => None,
2648
        };
2649
        // Whoever signed the move, and whom a delegate acts for, travel with
2650
        // it: a receiver that has never seen a change by this delegate could
2651
        // not otherwise check its signature (oxygenhub#129).
2652319×
        if attestation.is_some() {
2653220×
            for k in [self.signing_key(), self.config.author_key]
2654110×
                .into_iter()
2655110×
                .flatten()
2656
            {
2657220×
                if let Ok(id) = self.identity(&k) {
2658220×
                    let body = id.encode();
2659220×
                    if !bundle
2660220×
                        .objects
2661220×
                        .iter()
2662265×
                        .any(|(t, b)| *t == ObjectType::Identity && *b == body)
266332×
                    {
266432×
                        bundle.objects.push((ObjectType::Identity, body));
2665188×
                    }
26660×
                }
2667
            }
2668
            // A short-lived signing key's certificate goes with it (§14.7).
2669110×
            let signers: Vec<KeyId> = self.signing_key().into_iter().collect();
2670110×
            self.attach_certificates_for(&mut bundle, &signers)?;
2671209×
        }
2672319×
        let serial = attestation.as_ref().and_then(|a| match a.attest.body {
2673110×
            AttestBody::Ref { serial, .. } => Some(serial),
26740×
            _ => None,
2675110×
        });
2676319×
        let sent_serial = policy_to_send.as_ref().map(|p| p.policy.serial);
2677
        // Tag states must be present on the remote: bundle their changes too.
2678319×
        for t in &tags_to_send {
267916×
            let extra = self.negotiate(&remote_heads, &t.heads)?;
268016×
            let extra: Vec<ChangeHash> =
268116×
                extra.into_iter().filter(|h| !to_send.contains(h)).collect();
268216×
            if !extra.is_empty() {
26830×
                let more = self.bundle_for(&extra)?;
26840×
                bundle.changes.extend(more.changes);
26850×
                bundle.hashes.extend(more.hashes);
26860×
                bundle.attests.extend(more.attests);
26870×
                bundle.objects.extend(more.objects);
268816×
            }
268916×
            bundle.objects.push((
269016×
                ObjectType::State,
269116×
                StateObject::new(t.heads.clone()).encode(),
269216×
            ));
2693
        }
2694
        // A tag's signer travels with it, as the bookmark's does: a tag made
2695
        // by `oxy tag create` under a short-lived key was signed by a key the
2696
        // receiver has never seen, so its identity and certificate go too.
2697319×
        let tag_signers: Vec<KeyId> = tags_to_send
2698319×
            .iter()
2699319×
            .chain(&withdrawals_to_send)
2700319×
            .filter_map(|t| t.attestation.as_ref())
2701319×
            .flat_map(|a| a.sigs.iter().map(|s| s.key))
2702319×
            .collect();
2703319×
        for k in &tag_signers {
270416×
            if let Ok(id) = self.identity(k) {
270516×
                let body = id.encode();
270616×
                if !bundle
270716×
                    .objects
270816×
                    .iter()
270930×
                    .any(|(t, b)| *t == ObjectType::Identity && *b == body)
27102×
                {
27112×
                    bundle.objects.push((ObjectType::Identity, body));
271214×
                }
27130×
            }
2714
        }
2715319×
        if !tag_signers.is_empty() {
271615×
            self.attach_certificates_for(&mut bundle, &tag_signers)?;
2717304×
        }
2718319×
        if let Some(l) = &policy_to_send {
271934×
            let remote_serial = resp_policy_serial;
272034×
            bundle.policies = self
272134×
                .policy_history()?
272234×
                .into_iter()
272366×
                .filter(|sp| {
272466×
                    remote_serial.is_none_or(|r| sp.policy.serial > r)
272536×
                        && sp.policy.serial < l.policy.serial
272666×
                })
272734×
                .collect();
2728285×
        }
2729319×
        let results = remote.push(
2730319×
            bundle,
2731319×
            vec![RefUpdate {
2732319×
                name: bookmark.clone(),
2733319×
                old_state: remote_state,
2734319×
                new_state,
2735319×
                force,
2736319×
                attestation,
2737319×
            }],
2738319×
            policy_to_send,
2739319×
            tags_to_send
2740319×
                .into_iter()
2741319×
                .chain(withdrawals_to_send)
2742319×
                .collect(),
27438×
        )?;
274434×
        if let (Some(serial), true) = (
2745311×
            sent_serial,
2746325×
            results.iter().any(|r| r.ok && r.name == "policy"),
2747
        ) {
274834×
            self.record_policy_serial(remote_name, serial)?;
2749277×
        }
2750348×
        if results.iter().any(|r| r.ok && r.name == bookmark) {
2751284×
            let mut view = op.view.clone();
2752284×
            view.remotes
2753284×
                .entry(remote_name.to_string())
2754284×
                .or_default()
2755284×
                .insert(bookmark.clone(), new_state);
2756284×
            self.record_op(&[op_ref], &format!("oxy push {remote_name}"), view)?;
2757281×
            if let Some(s) = serial {
275886×
                self.record_serial(remote_name, &bookmark, s)?;
2759195×
            }
276027×
        }
2761306×
        Ok(results)
2762405×
    }
2763
2764
    /// Clone a remote into `dest`: same repository ID, `origin` remote,
2765
    /// bookmark set to the remote's, working copy checked out.
2766262×
    pub fn clone(url: &str, dest: &Path) -> Result<Repo> {
2767262×
        Self::clone_with(url, dest, None, &[])
2768262×
    }
2769
2770
    /// Clone, leaving blobs above `max_blob` bytes to be fetched on first
2771
    /// read, and materializing only `sparse` paths (which implies lazy blobs
2772
    /// unless a size is given, so out-of-scope binaries are never fetched).
2773265×
    pub fn clone_with(
2774265×
        url: &str,
2775265×
        dest: &Path,
2776265×
        max_blob: Option<u64>,
2777265×
        sparse: &[String],
2778265×
    ) -> Result<Repo> {
2779265×
        Self::clone_full(url, dest, max_blob, sparse, None)
2780265×
    }
2781
2782
    /// Shallow clone (spec §17): a checkpoint of the bookmark's heads plus the
2783
    /// changes within `depth` steps of them; the rest is the boundary.
27843×
    pub fn clone_shallow(
27853×
        url: &str,
27863×
        dest: &Path,
27873×
        depth: u32,
27883×
        max_blob: Option<u64>,
27893×
        sparse: &[String],
27903×
    ) -> Result<Repo> {
27913×
        Self::clone_full(url, dest, max_blob, sparse, Some(depth))
27923×
    }
2793
2794268×
    fn clone_full(
2795268×
        url: &str,
2796268×
        dest: &Path,
2797268×
        max_blob: Option<u64>,
2798268×
        sparse: &[String],
2799268×
        depth: Option<u32>,
2800268×
    ) -> Result<Repo> {
2801268×
        Ok(Self::clone_reporting(url, dest, max_blob, sparse, depth)?.0)
2802268×
    }
2803
2804
    /// Clone as [`Repo::clone_with`] (or [`Repo::clone_shallow`] with a
2805
    /// `depth`), also returning the bookmarks the fetch left out because
2806
    /// their attestation could not be verified ([`FetchReport::skipped`]),
2807
    /// for the caller to warn about. The bookmark checked out is never one
2808
    /// of them: that fails the clone.
2809274×
    pub fn clone_reporting(
2810274×
        url: &str,
2811274×
        dest: &Path,
2812274×
        max_blob: Option<u64>,
2813274×
        sparse: &[String],
2814274×
        depth: Option<u32>,
2815274×
    ) -> Result<(Repo, Vec<(String, String)>)> {
2816274×
        let options = crate::credentials::Credentials::user().options_for(url);
2817274×
        Self::clone_using(url, dest, max_blob, sparse, depth, &options)
2818274×
    }
2819
2820
    /// Clone a subrepo with the credentials the parent would use for its
2821
    /// hub (see [`Repo::credentials_for`]). Nothing is recorded in the
2822
    /// subrepo: it finds the same ones again through its parent.
282310×
    pub(crate) fn clone_as_subrepo(&self, url: &str, dest: &Path) -> Result<Repo> {
282410×
        let options = self.credentials_for(url);
282510×
        Ok(Self::clone_using(url, dest, None, &[], None, &options)?.0)
282610×
    }
2827
2828
    /// The name of a remote of this repository on the same hub (scheme,
2829
    /// host and port) as `url`.
283023×
    fn remote_on_same_hub(&self, url: &str) -> Option<String> {
283127×
        let origin = |u: &str| -> Option<String> {
283227×
            let (scheme, rest) = u.split_once("://")?;
28338×
            if !matches!(scheme, "http" | "https") {
28340×
                return None;
28358×
            }
28368×
            let host = rest.split('/').next()?;
28378×
            Some(format!(
28388×
                "{}://{}",
28398×
                scheme.to_ascii_lowercase(),
28408×
                host.to_ascii_lowercase()
28418×
            ))
284227×
        };
284323×
        let want = origin(url)?;
28444×
        self.config
28454×
            .remotes
28464×
            .iter()
28474×
            .find(|(_, u)| origin(u).as_deref() == Some(want.as_str()))
28484×
            .map(|(name, _)| name.clone())
284923×
    }
2850
2851
    /// The token and CA to reach `url` with: those of a remote of this
2852
    /// repository on the same hub (scheme, host and port), else the
2853
    /// environment's (`OXY_TOKEN`, `OXY_CA`), the user's credential store
2854
    /// and enclosing checkouts', as a clone would use them.
285523×
    pub(crate) fn credentials_for(&self, url: &str) -> RemoteOptions {
285623×
        match self.remote_on_same_hub(url) {
28571×
            Some(name) => RemoteOptions {
28581×
                repo_id: None,
28591×
                cache_dir: None,
28601×
                ..self.remote_options(&name)
28611×
            },
2862
            None => {
286322×
                let mut options = RemoteOptions::default();
286422×
                self.fill_credentials(url, &mut options);
286522×
                options
2866
            }
2867
        }
286823×
    }
2869
2870284×
    fn clone_using(
2871284×
        url: &str,
2872284×
        dest: &Path,
2873284×
        max_blob: Option<u64>,
2874284×
        sparse: &[String],
2875284×
        depth: Option<u32>,
2876284×
        options: &RemoteOptions,
2877284×
    ) -> Result<(Repo, Vec<(String, String)>)> {
2878284×
        let remote = open_remote(url, options)?;
2879284×
        let id = remote.repo_id()?;
2880280×
        if id == crate::protocol::UNBOUND_ID {
28812×
            return fail(format!(
2882
                "{url} is empty and unbound: push a repository to it first"
2883
            ));
2884278×
        }
2885278×
        let mut repo = Repo::init_with_id(dest, id)?;
2886278×
        repo.config
2887278×
            .set("remote.origin.url", &crate::protocol::stored_url(url))?;
2888278×
        if let Some(m) = max_blob {
28893×
            repo.config.set("filter.max-blob", &m.to_string())?;
2890275×
        } else if !sparse.is_empty() {
28911×
            repo.config.set("filter.max-blob", "0")?;
2892274×
        }
2893278×
        if !sparse.is_empty() {
28941×
            repo.config.set("filter.paths", &sparse.join(","))?;
2895277×
        }
2896278×
        repo.save_config()?;
2897278×
        let skipped = if let Some(depth) = depth {
28983×
            repo.fetch_shallow("origin", depth)?;
28993×
            Vec::new()
2900
        } else {
2901275×
            repo.fetch("origin")?.skipped
2902
        };
2903276×
        let (op_ref, op) = repo.head_op()?;
2904
        // The remote's default bookmark, else ours (main), else its only
2905
        // one; a bookmark the fetch skipped counts, so the clone fails
2906
        // rather than check out another (oxygenhub#266).
2907276×
        let mut theirs = op.view.remotes.get("origin").cloned().unwrap_or_default();
2908276×
        for (name, _) in &skipped {
29093×
            theirs.insert(name.clone(), ObjectRef::ZERO);
29103×
        }
2911
        // Asked of the remote as now configured, so a store-backed one
2912
        // answers from the mirror the fetch just filled.
2913276×
        let advertised = repo
2914276×
            .open_named_remote("origin")
2915276×
            .ok()
2916276×
            .and_then(|r| r.refs().ok())
2917276×
            .and_then(|r| r.default_bookmark);
2918276×
        let bookmark = advertised
2919276×
            .filter(|b| theirs.contains_key(b))
2920276×
            .or_else(|| {
29217×
                theirs
29227×
                    .contains_key(&repo.config.bookmark)
29237×
                    .then(|| repo.config.bookmark.clone())
29247×
            })
2925276×
            .or_else(|| {
29260×
                (theirs.len() == 1)
29270×
                    .then(|| theirs.keys().next().cloned())
29280×
                    .flatten()
29290×
            })
2930276×
            .unwrap_or_else(|| repo.config.bookmark.clone());
2931276×
        if let Some((_, why)) = skipped.iter().find(|(name, _)| *name == bookmark) {
29320×
            return fail(format!("cannot check out {bookmark}: {why}"));
2933276×
        }
2934276×
        let initial = repo.config.bookmark.clone();
2935276×
        if bookmark != repo.config.bookmark {
29361×
            fs::create_dir_all(repo.workspace_bookmark_path().parent().unwrap())?;
29371×
            write_atomic(&repo.workspace_bookmark_path(), bookmark.as_bytes())?;
29381×
            repo.config.bookmark = bookmark.clone();
2939275×
        }
2940276×
        if let Some(state) = theirs.get(&bookmark).copied() {
2941276×
            let heads = repo.store.get_state(&state)?.heads;
2942276×
            let mut view = op.view.clone();
2943
            // The empty bookmark `init` made goes when another is checked out.
2944276×
            if initial != bookmark && repo.heads_of(&view, &initial)?.is_empty() {
29451×
                view.bookmarks.remove(&initial);
2946275×
            }
2947276×
            view.bookmarks.insert(bookmark, state);
2948276×
            repo.record_op(&[op_ref], &format!("oxy clone {url}"), view)?;
2949276×
            repo.checkout(&heads, false, "oxy clone (checkout)")?;
29500×
        }
2951276×
        Ok((repo, skipped))
2952284×
    }
2953
}
2954
2955
/// The serial of a reference's next attestation: one more than the last the
2956
/// remote reports, or, for a bookmark the remote does not have, the clock in
2957
/// milliseconds, so a bookmark created again after a deletion goes past the
2958
/// deletion's serial the remote keeps.
2959111×
fn next_serial(last: Option<u64>) -> u64 {
2960111×
    match last {
296169×
        Some(l) => l + 1,
296242×
        None => Hlc::now().wall_ms.max(1),
2963
    }
2964111×
}