OXYGENHUB

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

1575 of 1766 lines run, 229 of 326 functions · run #301 on main · run missed

1
//! Serving and talking to remotes (spec §10.2, §16).
2
3
use super::*;
4
5
/// What a push did, for hosts (hub spec §6.12).
6
#[derive(Clone, Debug, Default)]
7
pub struct PushReport {
8
    pub results: Vec<RefResult>,
9
    /// Accepted reference updates, in request order.
10
    pub updated: Vec<RefUpdated>,
11
    /// Changes this push stored that were not already present.
12
    pub imported: Vec<ChangeHash>,
13
}
14
15
#[derive(Clone, Debug)]
16
pub struct RefUpdated {
17
    pub name: String,
18
    pub old_state: ObjectRef,
19
    pub new_state: ObjectRef,
20
    pub heads: Vec<ChangeHash>,
21
}
22
23
/// What a fetch brought in.
24
#[derive(Debug, Clone, PartialEq, Eq, Default)]
25
pub struct FetchReport {
26
    /// Bookmark name, its heads, and how many changes were new.
27
    pub refs: Vec<(String, Vec<ChangeHash>, usize)>,
28
    /// Tags adopted from the remote.
29
    pub new_tags: usize,
30
    /// The remote has never presented a policy, so nothing was verified
31
    /// against one (spec §14.5: warn loudly).
32
    pub unverified: bool,
33
}
34
35
#[derive(Debug, Clone, PartialEq, Eq)]
36
pub struct PullReport {
37
    /// The current bookmark's heads after the pull.
38
    pub heads: Vec<ChangeHash>,
39
    /// Whether the current bookmark changed.
40
    pub changed: bool,
41
    pub checked_out: bool,
42
    /// Other bookmarks the pull moved: each local bookmark tracks the
43
    /// remote's of the same name.
44
    pub others: Vec<String>,
45
}
46
47
/// The result a push of objects alone reports (a batch of a long history):
48
/// not a reference.
49
pub const OBJECTS_RESULT: &str = "objects";
50
51
impl Repo {
52
    // ------------------------------------------------------------ serving (spec §16)
53
541282×
    fn ref_attest_path(&self, bookmark: &str) -> PathBuf {
551282×
        self.oxy.join("attest").join(name_file(bookmark))
561282×
    }
57
58
    /// The latest reference attestation stored for a bookmark.
591204×
    pub fn latest_ref_attest(&self, bookmark: &str) -> Result<Option<(ObjectRef, SignedAttest)>> {
601204×
        match fs::read_to_string(self.ref_attest_path(bookmark)) {
61202×
            Ok(h) => {
62202×
                let r = ObjectRef::from_hex(h.trim())
63202×
                    .ok_or_else(|| RepoError::Msg("corrupt attestation pointer".into()))?;
64
                Ok(Some((
65202×
                    r,
66
                    SignedAttest {
67202×
                        attest: self.store.get_attest(&r)?,
68202×
                        sigs: self.store.signatures(&r)?,
69
                    },
70
                )))
71
            }
721002×
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
730×
            Err(e) => Err(e.into()),
74
        }
751204×
    }
76
7778×
    pub(crate) fn set_ref_attest(&self, bookmark: &str, r: &ObjectRef) -> Result<()> {
7878×
        fs::create_dir_all(self.oxy.join("attest"))?;
7978×
        write_atomic(&self.ref_attest_path(bookmark), r.to_hex().as_bytes())
8078×
    }
81
82987×
    pub fn remote_refs(&self) -> Result<RefsResponse> {
83987×
        let view = self.view()?;
84987×
        let mut out = Vec::new();
851009×
        for (name, state) in &view.bookmarks {
861009×
            let latest = self.latest_ref_attest(name)?.map(|(_, a)| a);
871009×
            let last_serial = latest.as_ref().and_then(|a| match a.attest.body {
88109×
                AttestBody::Ref { serial, .. } => Some(serial),
890×
                _ => None,
90109×
            });
911009×
            let attestation = latest.filter(|a| matches!(&a.attest.body, AttestBody::Ref { new_state, .. } if new_state == state));
921009×
            out.push(RemoteRef {
931009×
                name: name.clone(),
941009×
                state: *state,
951009×
                heads: self.store.get_state(state)?.heads,
961009×
                attestation,
971009×
                last_serial,
98
            });
99
        }
100987×
        let policy = self.current_policy()?.map(|(_, sp)| sp);
101987×
        let history = self
102987×
            .policy_history()?
103987×
            .into_iter()
104987×
            .filter(|sp| {
10574×
                policy
10674×
                    .as_ref()
10774×
                    .is_some_and(|p| sp.policy.serial < p.policy.serial)
10874×
            })
109987×
            .collect();
110
        Ok(RefsResponse {
111987×
            refs: out,
112987×
            policy,
113987×
            tags: self.tag_refs()?,
114987×
            history,
115987×
            default_bookmark: Some(self.config.bookmark.clone()),
116
        })
117987×
    }
118
119
    /// Every change reachable from `heads` that is in the store.
1201315×
    fn closure_in_store(&self, heads: &[ChangeHash]) -> Result<BTreeSet<ChangeHash>> {
1211315×
        let mut seen = BTreeSet::new();
1221315×
        let mut stack: Vec<ChangeHash> = heads.to_vec();
1234573×
        while let Some(h) = stack.pop() {
1243258×
            if seen.contains(&h) || !self.store.has(&h) {
125891×
                continue;
1262367×
            }
1272367×
            seen.insert(h);
1282367×
            stack.extend(self.store.get_change_meta(&h)?.deps);
129
        }
1301315×
        Ok(seen)
1311315×
    }
132
133
    /// The changes within `depth` dependency steps of `heads` (spec §17),
134
    /// nearest first; depth 1 is the heads themselves.
1353×
    pub fn shallow_changes(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> {
1363×
        let mut out = Vec::new();
1373×
        let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
1383×
        let mut layer: Vec<ChangeHash> = heads.to_vec();
1393×
        for _ in 0..depth.max(1) {
1408×
            let mut next = Vec::new();
14112×
            for h in layer {
14212×
                if !seen.insert(h) || !self.store.has(&h) {
1432×
                    continue;
14410×
                }
14510×
                let c = self.store.get_change(&h)?;
14610×
                next.extend(c.deps.iter().copied());
14710×
                out.push(h);
148
            }
1498×
            if next.is_empty() {
1501×
                break;
1517×
            }
1527×
            layer = next;
153
        }
1543×
        out.reverse();
1553×
        Ok(out)
1563×
    }
157
1581899×
    fn shallow_path(&self) -> PathBuf {
1591899×
        self.oxy.join("shallow")
1601899×
    }
161
162
    /// The shallow boundary: dependencies this repository does not hold (spec §17).
1631896×
    pub fn boundary(&self) -> BTreeSet<ChangeHash> {
1641896×
        fs::read_to_string(self.shallow_path())
1651896×
            .map(|t| t.lines().filter_map(ObjectRef::from_hex).collect())
1661896×
            .unwrap_or_default()
1671896×
    }
168
169183×
    pub fn is_shallow(&self) -> bool {
170183×
        !self.boundary().is_empty()
171183×
    }
172
1733×
    fn write_boundary(&self, boundary: &BTreeSet<ChangeHash>) -> Result<()> {
1743×
        if boundary.is_empty() {
1751×
            let _ = fs::remove_file(self.shallow_path());
1761×
            return Ok(());
1772×
        }
1782×
        let text: String = boundary
1792×
            .iter()
1802×
            .map(|h| format!("{}\n", h.to_hex()))
1812×
            .collect();
1822×
        write_atomic(&self.shallow_path(), text.as_bytes())
1833×
    }
184
185
    /// Recompute the boundary from what is stored: every dependency of a
186
    /// stored change that is not itself stored.
1873×
    fn refresh_boundary(&self, imported: &[ChangeHash]) -> Result<()> {
1883×
        let mut boundary = self.boundary();
1893×
        boundary.retain(|h| !self.store.has(h));
19010×
        for h in imported {
19113×
            for d in self.store.get_change_meta(h)?.deps {
19213×
                if !self.store.has(&d) {
1932×
                    boundary.insert(d);
19411×
                }
195
            }
196
        }
1973×
        self.write_boundary(&boundary)
1983×
    }
199
200
    /// Fetch `depth` more levels of history below the shallow boundary.
2012×
    pub fn deepen(&self, remote_name: &str, depth: u32) -> Result<usize> {
2022×
        let boundary = self.boundary();
2032×
        if boundary.is_empty() {
2040×
            return Ok(0);
2052×
        }
2062×
        let remote = self.open_named_remote(remote_name)?;
2072×
        let wanted = remote.shallow(&boundary.iter().copied().collect::<Vec<_>>(), depth)?;
2082×
        let bundle = remote.fetch(&wanted, self.config.max_blob)?;
2092×
        let imported = self.import_bundle_shallow(&bundle, false)?;
2102×
        if self.config.max_blob.is_some() {
2110×
            self.register_missing_blobs(&imported, remote_name)?;
2122×
        }
2132×
        self.refresh_boundary(&imported)?;
2142×
        Ok(imported.len())
2152×
    }
216
217
    /// `closure(want) \ closure(have)`, dependencies first (spec §16.2).
218749×
    pub fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> {
219749×
        let have = self.closure_in_store(have)?;
220
        // In a shallow repository the walk stops at the boundary, whose
221
        // changes the remote is assumed to hold.
222749×
        let boundary = self.boundary();
223749×
        let mut out = Vec::new();
224749×
        let mut done: BTreeSet<ChangeHash> = have.clone();
225749×
        done.extend(boundary.iter().copied());
226
        // Iterative post-order DFS.
2271205×
        let mut stack: Vec<(ChangeHash, bool)> = want.iter().rev().map(|h| (*h, false)).collect();
2283129×
        while let Some((h, expanded)) = stack.pop() {
2292380×
            if done.contains(&h) {
230404×
                continue;
2311976×
            }
2321976×
            if !self.store.has(&h) {
2330×
                return fail(format!("change {} is not in this repository", h.short(12)));
2341976×
            }
2351976×
            if expanded {
236988×
                done.insert(h);
237988×
                out.push(h);
238988×
                continue;
239988×
            }
240988×
            stack.push((h, true));
241988×
            for d in self.store.get_change_meta(&h)?.deps {
242653×
                if !done.contains(&d) {
243187×
                    stack.push((d, false));
244466×
                }
245
            }
246
        }
247749×
        Ok(out)
248749×
    }
249
250
    /// Bundle changes (in the given dependency order) with their signatures,
251
    /// seals, identities, blobs, chunks.
252299×
    pub fn bundle_for(&self, changes: &[ChangeHash]) -> Result<Bundle> {
253299×
        self.bundle_for_filtered(changes, None)
254299×
    }
255
256
    /// Objects by reference for the `objects` endpoint; a blob brings its chunks.
2574×
    pub fn objects_for(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> {
2584×
        let mut out = Vec::new();
2594×
        for r in refs {
2604×
            let (ty, body) = match self.store.get(r) {
2614×
                Ok(x) => x,
2620×
                Err(StoreError::NotFound(_)) => continue,
2630×
                Err(e) => return Err(e.into()),
264
            };
2654×
            if ty == ObjectType::Blob {
2665×
                for c in Blob::decode(&body)?.chunks {
2675×
                    if let Ok((cty, cb)) = self.store.get(&c) {
2685×
                        out.push((cty, cb));
2695×
                    }
270
                }
2710×
            }
2724×
            out.push((ty, body));
273
        }
2744×
        Ok(out)
2754×
    }
276
277
    /// Like `bundle_for`, leaving out blobs larger than `max_blob` bytes
278
    /// (and their chunks) so the receiver fetches them lazily (spec §13.5).
279348×
    pub fn bundle_for_filtered(
280348×
        &self,
281348×
        changes: &[ChangeHash],
282348×
        max_blob: Option<u64>,
283348×
    ) -> Result<Bundle> {
284348×
        self.bundle_of(changes, max_blob, false)
285348×
    }
286
287
    /// Like [`Repo::bundle_for_filtered`] for a fetch between repositories
288
    /// on one machine: changes as they are stored ([`Bundle::raw_changes`]),
289
    /// with their text blobs and chunks among the objects, so neither side
290
    /// decodes or encodes their text.
291312×
    pub fn bundle_raw_for(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> {
292312×
        self.bundle_of(changes, max_blob, true)
293312×
    }
294
295660×
    fn bundle_of(
296660×
        &self,
297660×
        changes: &[ChangeHash],
298660×
        max_blob: Option<u64>,
299660×
        raw: bool,
300660×
    ) -> Result<Bundle> {
301660×
        let mut bundle = Bundle::default();
302660×
        let mut objects: BTreeSet<ObjectRef> = BTreeSet::new();
303660×
        let mut seals: BTreeSet<ObjectRef> = BTreeSet::new();
3041001×
        let add_object = |r: &ObjectRef, objects: &mut BTreeSet<ObjectRef>| {
3051001×
            if !r.is_zero() && self.store.has(r) {
306173×
                objects.insert(*r);
307828×
            }
3081001×
        };
309917×
        for h in changes {
310
            // Raw: the stored bytes, and a skeleton for the rest.
311917×
            let (c, encoded) = if raw {
312481×
                let bytes = self.store.get_typed(h, ObjectType::Change)?;
313481×
                let (skel, texts) = Change::decode_skeleton(&bytes, *h)?;
3144941×
                for t in texts {
3154941×
                    if let crate::objects::TextRef::Blob(r, _, _) = t {
3163×
                        let blob = Blob::decode(&self.store.get_typed(&r, ObjectType::Blob)?)?;
3173×
                        objects.insert(r);
3183×
                        objects.extend(blob.chunks);
3194938×
                    }
320
                }
321481×
                (skel, Some(bytes))
322
            } else {
323436×
                (self.store.get_change(h)?, None)
324
            };
325917×
            let sigs = self.store.signatures(h)?;
326917×
            add_object(&c.author, &mut objects);
327917×
            for s in &sigs {
32822×
                add_object(&s.key, &mut objects);
32922×
            }
3305878×
            for e in &c.edits {
3315878×
                if let Edit::Replace { old, new, .. } = e {
33214×
                    for b in [old, new].into_iter().flatten() {
33314×
                        if self.store.has(b) {
33414×
                            if let Ok(blob) =
33514×
                                Blob::decode(&self.store.get_typed(b, ObjectType::Blob)?)
336
                            {
33714×
                                if max_blob.is_some_and(|m| blob.length > m) {
3384×
                                    continue;
33910×
                                }
34010×
                                objects.insert(*b);
34110×
                                objects.extend(blob.chunks.iter().copied());
3420×
                            }
3430×
                        }
344
                    }
3455866×
                }
346
            }
347917×
            for s in self.store.seals_for(h)? {
34867×
                seals.insert(s);
34967×
            }
350917×
            match encoded {
351481×
                Some(bytes) => bundle.raw_changes.push((bytes, sigs)),
352436×
                None => {
353436×
                    bundle.changes.push((c, sigs));
354436×
                    bundle.hashes.push(*h);
355436×
                }
356
            }
357
        }
358660×
        for r in seals {
35962×
            let attest = self.store.get_attest(&r)?;
36062×
            let sigs = self.store.signatures(&r)?;
36162×
            for s in &sigs {
36262×
                add_object(&s.key, &mut objects);
36362×
            }
36462×
            bundle.attests.push(SignedAttest { attest, sigs });
365
        }
366660×
        for r in objects {
367106×
            let (ty, body) = self.store.get(&r)?;
368106×
            bundle.objects.push((ty, body));
369
        }
370660×
        Ok(bundle)
371660×
    }
372
373
    /// Store a bundle's contents. With `strict`, every change must
374
    /// authenticate (signature or seal by its author or a delegate).
375702×
    pub fn import_bundle(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> {
376702×
        self.import_bundle_inner(bundle, strict, false)
377702×
    }
378
379
    /// Like `import_bundle`, accepting changes whose dependencies are absent
380
    /// (they join the shallow boundary).
3813×
    pub fn import_bundle_shallow(&self, bundle: &Bundle, strict: bool) -> Result<Vec<ChangeHash>> {
3823×
        self.import_bundle_inner(bundle, strict, true)
3833×
    }
384
385705×
    fn import_bundle_inner(
386705×
        &self,
387705×
        bundle: &Bundle,
388705×
        strict: bool,
389705×
        shallow: bool,
390705×
    ) -> Result<Vec<ChangeHash>> {
391705×
        let batch = self.store.begin_batch()?;
392705×
        let result = self.import_bundle_batched(bundle, strict, shallow);
393705×
        batch.finish()?;
394703×
        result
395705×
    }
396
397705×
    fn import_bundle_batched(
398705×
        &self,
399705×
        bundle: &Bundle,
400705×
        strict: bool,
401705×
        shallow: bool,
402705×
    ) -> Result<Vec<ChangeHash>> {
403705×
        for (ty, _) in &bundle.objects {
4040×
            if !matches!(
405477×
                ty,
406
                ObjectType::Chunk
407
                    | ObjectType::Blob
408
                    | ObjectType::Identity
409
                    | ObjectType::State
410
                    | ObjectType::Checkpoint
411
            ) {
4120×
                return fail(format!("bundle may not carry {} objects", ty.name()));
413477×
            }
414
        }
415
        // All at once: in a large batch they are compressed on every core.
416705×
        let objects: Vec<(ObjectType, &[u8])> = bundle
417705×
            .objects
418705×
            .iter()
419705×
            .map(|(ty, body)| (*ty, body.as_slice()))
420705×
            .collect();
421705×
        self.store.put_many(&objects)?;
422705×
        for sa in &bundle.attests {
42365×
            let r = self.store.put_attest(&sa.attest)?;
42465×
            for sig in &sa.sigs {
42565×
                self.store.put_signature(&r, sig)?;
426
            }
42765×
            if let AttestBody::Seal { hashes } = &sa.attest.body {
42870×
                for h in hashes {
42970×
                    self.store.index_seal(h, &r)?;
430
                }
4310×
            }
432
        }
433
        // Each change's hash: from the bundle when its maker knew it, else
434
        // worked out once (for a change adding a whole tree, hashing is
435
        // chunking all its text).
436705×
        let hashes: Vec<ChangeHash> = if bundle.hashes.len() == bundle.changes.len() {
437696×
            bundle.hashes.clone()
438
        } else {
4399×
            bundle.changes.iter().map(|(c, _)| c.hash()).collect()
440
        };
441705×
        let raw_hashes: Vec<ChangeHash> = bundle
442705×
            .raw_changes
443705×
            .iter()
444705×
            .map(|(b, _)| hash_object(ObjectType::Change, b))
445705×
            .collect();
446705×
        let incoming: BTreeSet<ChangeHash> = hashes.iter().chain(&raw_hashes).copied().collect();
447705×
        let boundary = self.boundary();
448705×
        let mut imported = Vec::new();
449
        // Changes as stored: checked, then stored as they are, their text
450
        // blobs having come with the objects.
451705×
        for ((bytes, sigs), h) in bundle.raw_changes.iter().zip(raw_hashes) {
452482×
            let (skel, texts) = Change::decode_skeleton(bytes, h)?;
453482×
            for d in &skel.deps {
454341×
                if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d)
455
                {
4560×
                    return fail(format!(
457
                        "change {} depends on {}, which is neither stored nor in the bundle",
4580×
                        h.short(12),
4590×
                        d.short(12)
460
                    ));
461341×
                }
462
            }
4634942×
            for t in texts {
4644942×
                if let crate::objects::TextRef::Blob(r, _, _) = t {
4654×
                    if !self.store.has(&r) {
4661×
                        return fail(format!(
467
                            "change {} came without its text blob {}",
4681×
                            h.short(12),
4691×
                            r.short(12)
470
                        ));
4713×
                    }
4724938×
                }
473
            }
474481×
            self.store.put(ObjectType::Change, bytes)?;
475481×
            for sig in sigs {
4764×
                self.store.put_signature(&h, sig)?;
477
            }
478481×
            imported.push(h);
479
        }
480704×
        for ((c, sigs), h) in bundle.changes.iter().zip(hashes) {
481464×
            for d in &c.deps {
482266×
                if !self.store.has(d) && !incoming.contains(d) && !shallow && !boundary.contains(d)
483
                {
4843×
                    return fail(format!(
485
                        "change {} depends on {}, which is neither stored nor in the bundle",
4863×
                        h.short(12),
4873×
                        d.short(12)
488
                    ));
489263×
                }
490
            }
491461×
            let stored = self.store.put_change(c)?;
492461×
            if stored != h {
4930×
                return fail(format!(
494
                    "change {} is not the change the bundle names",
4950×
                    h.short(12)
496
                ));
497461×
            }
498461×
            for sig in sigs {
49927×
                self.store.put_signature(&h, sig)?;
500
            }
501461×
            imported.push(h);
502
        }
503701×
        if strict {
50432×
            for h in &imported {
50521×
                if !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) {
5061×
                    return fail(format!(
507
                        "change {} is not authenticated by its author",
5081×
                        h.short(12)
509
                    ));
51020×
                }
511
            }
512669×
        }
513700×
        Ok(imported)
514705×
    }
515
516
    /// Verify a reference attestation against an update it claims to describe.
517
    /// `allow_equal` lets a client re-read the attestation it already has; a
518
    /// server receiving a push requires a strictly greater serial.
519
    /// A reference attestation signed by a delegate of an identity the
520
    /// rule lists is authorized as that identity (spec §14.5): the identity
521
    /// named the delegate with the `refs` scope, still in force at `at_ms`
522
    /// (the attestation's wall time), and the policy names the identity.
523
    /// Revoked keys authorize nothing either way.
52410×
    pub(crate) fn delegate_authorized(
52510×
        &self,
52610×
        p: &crate::policy::Policy,
52710×
        signer: &KeyId,
52810×
        reference: &str,
52910×
        at_ms: u64,
53010×
    ) -> bool {
53110×
        if p.is_revoked(signer) {
5321×
            return false;
5339×
        }
5349×
        let Some(rule) = p.rule_for(reference) else {
5350×
            return false;
536
        };
53710×
        rule.keys.iter().any(|k| {
53810×
            !p.is_revoked(k)
53910×
                && p.identity(k)
54010×
                    .cloned()
54110×
                    .or_else(|| self.store.get_identity(k).ok())
54210×
                    .is_some_and(|id| id.delegates_to(signer, Scope::REFS, at_ms))
54310×
        })
54410×
    }
545
546
    /// A lasting attestation (a tag) signed by a key the policy has since
547
    /// rotated away from (spec §14.2) is authorized as the key that
548
    /// succeeded it: a key the rule lists reaches the signer through
549
    /// `previous` links, and the attestation is dated (`at_ms`) before the
550
    /// signer's successor was made, so a superseded key makes nothing new.
551
    /// Revoked keys authorize nothing.
5526×
    pub(crate) fn predecessor_authorized(
5536×
        &self,
5546×
        p: &crate::policy::Policy,
5556×
        signer: &KeyId,
5566×
        reference: &str,
5576×
        at_ms: u64,
5586×
    ) -> bool {
5596×
        if p.is_revoked(signer) {
5600×
            return false;
5616×
        }
5626×
        let Some(rule) = p.rule_for(reference) else {
5630×
            return false;
564
        };
5657×
        let identity = |k: &KeyId| {
5667×
            p.identity(k)
5677×
                .cloned()
5687×
                .or_else(|| self.store.get_identity(k).ok())
5697×
        };
5706×
        rule.keys.iter().any(|k| {
5716×
            if p.is_revoked(k) {
5720×
                return false;
5736×
            }
5746×
            let mut cur = identity(k);
5756×
            for _ in 0..32 {
5767×
                let Some(id) = cur else {
5770×
                    return false;
578
                };
5791×
                match id.previous {
5806×
                    Some(prev) if prev == *signer => return at_ms < id.created,
5811×
                    Some(prev) if !p.is_revoked(&prev) => cur = identity(&prev),
5821×
                    _ => return false,
583
                }
584
            }
5850×
            false
5866×
        })
5876×
    }
588
58998×
    fn check_ref_attest(
59098×
        &self,
59198×
        sa: &SignedAttest,
59298×
        name: &str,
59398×
        old_state: &ObjectRef,
59498×
        new_state: &ObjectRef,
59598×
        last_serial: Option<u64>,
59698×
        allow_equal: bool,
59798×
    ) -> std::result::Result<u64, String> {
598
        let AttestBody::Ref {
59998×
            name: n,
60098×
            old_state: o,
60198×
            new_state: nw,
60298×
            serial,
60398×
        } = &sa.attest.body
604
        else {
6050×
            return Err("attestation is not a ref attestation".into());
606
        };
60798×
        if n != name || o != old_state || nw != new_state {
6081×
            return Err("attestation does not describe this update".into());
60997×
        }
61097×
        if sa.attest.repo != self.id().map_err(|e| e.to_string())? {
6110×
            return Err("attestation is for a different repository".into());
61297×
        }
61397×
        if let Some(last) = last_serial {
61461×
            if *serial < last || (*serial == last && !allow_equal) {
6151×
                return Err(format!(
6161×
                    "attestation serial {serial} is not greater than the last seen {last}"
6171×
                ));
61860×
            }
61936×
        }
62096×
        let r = sa.attest.hash();
62196×
        let sig = sa.sigs.first().ok_or("attestation carries no signature")?;
62296×
        let id = self
62396×
            .store
62496×
            .get_identity(&sig.key)
62596×
            .map_err(|_| format!("unknown key {}", sig.key.short(12)))?;
62696×
        verify_signature(&id, sig, Domain::Attest, &r).map_err(|e| e.to_string())?;
62796×
        Ok(*serial)
62898×
    }
629
630
    /// Accept a push (spec §16.4). Without a trust policy every repository
631
    /// is unverified, so unsigned changes and unattested updates are accepted;
632
    /// attestations that are present must still be valid. With a policy,
633
    /// every change and update is checked against it. A newer policy in the
634
    /// push is applied first when it is signed by the current roots.
635273×
    pub fn receive_push(
636273×
        &self,
637273×
        bundle: Bundle,
638273×
        updates: Vec<RefUpdate>,
639273×
        policy: Option<SignedPolicy>,
640273×
        tags: Vec<crate::protocol::TagRef>,
641273×
    ) -> Result<Vec<RefResult>> {
642273×
        Ok(self
643273×
            .receive_push_report(bundle, updates, policy, tags)?
644
            .results)
645273×
    }
646
647
    /// `receive_push` with what a host wants to log: each accepted
648
    /// reference's old and new state and heads, and the changes this push
649
    /// stored (hub spec §6.12).
650317×
    pub fn receive_push_report(
651317×
        &self,
652317×
        bundle: Bundle,
653317×
        updates: Vec<RefUpdate>,
654317×
        policy: Option<SignedPolicy>,
655317×
        tags: Vec<crate::protocol::TagRef>,
656317×
    ) -> Result<PushReport> {
657317×
        let mut report = PushReport::default();
658317×
        let results = &mut report.results;
659317×
        if let Some(sp) = policy {
66021×
            match self.adopt_policy_chain(&bundle.policies, &sp, "push received") {
66119×
                Ok(()) => {
66219×
                    results.push(RefResult {
66319×
                        name: "policy".into(),
66419×
                        ok: true,
66519×
                        reason: format!("serial {}", sp.policy.serial),
66619×
                    });
66719×
                }
6682×
                Err(e) => {
6692×
                    results.push(RefResult {
6702×
                        name: "policy".into(),
6712×
                        ok: false,
6722×
                        reason: e,
6732×
                    });
6742×
                    for u in updates {
6750×
                        results.push(RefResult {
6760×
                            name: u.name,
6770×
                            ok: false,
6780×
                            reason: "policy update was rejected".into(),
6790×
                        });
6800×
                    }
6812×
                    return Ok(report);
682
                }
683
            }
684296×
        }
685315×
        let policy = self.current_policy()?.map(|(_, sp)| sp.policy);
686
        // A push of objects alone (one batch of a long history, oxygen#95)
687
        // names no reference: its outcome is reported as `objects`.
688315×
        let objects_only = updates.is_empty() && tags.is_empty();
689315×
        match self.import_bundle(&bundle, policy.is_some()) {
690312×
            Ok(imported) => {
691312×
                if objects_only {
6927×
                    results.push(RefResult {
6937×
                        name: OBJECTS_RESULT.into(),
6947×
                        ok: true,
6957×
                        reason: format!("{} changes stored", imported.len()),
6967×
                    });
697305×
                }
698312×
                report.imported = imported;
699
            }
7003×
            Err(e) => {
7013×
                if objects_only {
7020×
                    results.push(RefResult {
7030×
                        name: OBJECTS_RESULT.into(),
7040×
                        ok: false,
7050×
                        reason: e.to_string(),
7060×
                    });
7073×
                }
7083×
                for u in updates {
7093×
                    results.push(RefResult {
7103×
                        name: u.name,
7113×
                        ok: false,
7123×
                        reason: e.to_string(),
7133×
                    });
7143×
                }
7153×
                return Ok(report);
716
            }
717
        }
718312×
        let (op_ref, op) = self.head_op()?;
719312×
        let mut view = op.view.clone();
720312×
        let mut names = Vec::new();
721312×
        for u in updates {
722303×
            let outcome = self.apply_update(&mut view, &u, policy.as_ref());
723303×
            let (ok, reason) = match outcome {
724275×
                Ok(()) => (true, String::new()),
72528×
                Err(e) => (false, e),
726
            };
727303×
            if ok {
728275×
                names.push(u.name.clone());
729275×
                let heads = self
730275×
                    .store
731275×
                    .get_state(&u.new_state)
732275×
                    .map(|s| s.heads)
733275×
                    .unwrap_or_default();
734275×
                report.updated.push(RefUpdated {
735275×
                    name: u.name.clone(),
736275×
                    old_state: u.old_state,
737275×
                    new_state: u.new_state,
738275×
                    heads,
739275×
                });
74028×
            }
741303×
            results.push(RefResult {
742303×
                name: u.name,
743303×
                ok,
744303×
                reason,
745303×
            });
746
        }
747312×
        for t in &tags {
7488×
            let name = format!("tags/{}", t.name);
7498×
            let new = !view.tags.contains_key(&t.name);
7508×
            match self.adopt_tag(&mut view, t, policy.as_ref()) {
751
                Ok(()) => {
752
                    // A tag new here is an update too, so a hub can start
753
                    // its pipeline's tag jobs (oxygenflow#24); one it
754
                    // already had, sent again, is not.
7556×
                    if new {
7566×
                        report.updated.push(RefUpdated {
7576×
                            name: name.clone(),
7586×
                            old_state: ObjectRef::ZERO,
7596×
                            new_state: t.state,
7606×
                            heads: t.heads.clone(),
7616×
                        });
7626×
                    }
7636×
                    names.push(name.clone());
7646×
                    results.push(RefResult {
7656×
                        name,
7666×
                        ok: true,
7676×
                        reason: String::new(),
7686×
                    });
769
                }
7702×
                Err(e) => results.push(RefResult {
7712×
                    name,
7722×
                    ok: false,
7732×
                    reason: e,
7742×
                }),
775
            }
776
        }
777312×
        if !names.is_empty() {
778275×
            self.record_op(
779275×
                &[op_ref],
780275×
                &format!("push received: {}", names.join(" ")),
781275×
                view,
7823×
            )?;
78337×
        }
784309×
        Ok(report)
785317×
    }
786
787
    /// Check an update's attestation against the latest one for its
788
    /// reference and store it as the new latest. Failures say what was
789
    /// being done, for which reference, so a refused push can be diagnosed
790
    /// from its reason alone.
79178×
    fn record_update_attest(
79278×
        &self,
79378×
        u: &RefUpdate,
79478×
        sa: &SignedAttest,
79578×
    ) -> std::result::Result<(), String> {
79678×
        let name = &u.name;
79778×
        let last = self
79878×
            .latest_ref_attest(name)
79978×
            .map_err(|e| format!("reading the last attestation of {name:?}: {e}"))?
80078×
            .and_then(|(_, a)| match a.attest.body {
80151×
                AttestBody::Ref { serial, .. } => Some(serial),
8020×
                _ => None,
80351×
            });
80478×
        self.check_ref_attest(sa, name, &u.old_state, &u.new_state, last, false)?;
80576×
        let store = |e: &dyn std::fmt::Display| format!("storing the attestation of {name:?}: {e}");
80676×
        let r = self.store.put_attest(&sa.attest).map_err(|e| store(&e))?;
80775×
        for sig in &sa.sigs {
80875×
            self.store.put_signature(&r, sig).map_err(|e| store(&e))?;
809
        }
81075×
        self.set_ref_attest(name, &r).map_err(|e| store(&e))
81178×
    }
812
813
    /// A deletion (spec §16.4): `new_state` is zero. The bookmark must exist
814
    /// at `old_state`, is never the bookmark a non-bare repository has
815
    /// checked out (a bare one's default is its host's to protect, see
816
    /// `oxy_server::Backend::default_bookmark`), and under a policy needs
817
    /// an attestation by a key that may update it.
818
    /// The deletion's attestation stays the bookmark's latest, so an older
819
    /// one cannot be replayed to bring it back.
8203×
    fn apply_deletion(
8213×
        &self,
8223×
        view: &mut View,
8233×
        u: &RefUpdate,
8243×
        policy: Option<&crate::policy::Policy>,
8253×
    ) -> std::result::Result<(), String> {
8263×
        let Some(current) = view.bookmarks.get(&u.name).copied() else {
8270×
            return Err(format!("no bookmark {:?} to delete", u.name));
828
        };
8293×
        if current != u.old_state {
8300×
            return Err("stale: the remote bookmark has moved; pull first".into());
8313×
        }
8323×
        if !self.is_bare() && u.name == self.config.bookmark {
8331×
            return Err(format!(
8341×
                "{:?} is this repository's default bookmark (checked out there) and cannot be deleted",
8351×
                u.name
8361×
            ));
8372×
        }
8382×
        if let Some(p) = policy {
8390×
            let Some(sa) = &u.attestation else {
8400×
                return Err(format!(
8410×
                    "deleting {:?} requires a signed reference attestation under this repository's policy",
8420×
                    u.name
8430×
                ));
844
            };
8450×
            let signer = sa
8460×
                .sigs
8470×
                .first()
8480×
                .map(|s| s.key)
8490×
                .ok_or("attestation carries no signature")?;
8500×
            if !p.authorizes(&signer, &u.name)
8510×
                && !self.delegate_authorized(p, &signer, &u.name, sa.attest.hlc.wall_ms)
852
            {
8530×
                return Err(format!(
8540×
                    "key {} may not delete {:?} under this repository's policy",
8550×
                    signer.short(12),
8560×
                    u.name
8570×
                ));
8580×
            }
8592×
        }
8602×
        if let Some(sa) = &u.attestation {
8611×
            self.record_update_attest(u, sa)?;
8621×
        }
8632×
        view.bookmarks.remove(&u.name);
8642×
        Ok(())
8653×
    }
866
867303×
    fn apply_update(
868303×
        &self,
869303×
        view: &mut View,
870303×
        u: &RefUpdate,
871303×
        policy: Option<&crate::policy::Policy>,
872303×
    ) -> std::result::Result<(), String> {
873303×
        if u.name == "policy" {
8740×
            return Err(
8750×
                "the policy reference is updated by sending a policy, not a bookmark update".into(),
8760×
            );
877303×
        }
878303×
        let empty = self
879303×
            .store
880303×
            .put_state(&StateObject::new(vec![]))
881303×
            .map_err(|e| e.to_string())?;
882303×
        if u.new_state.is_zero() {
8833×
            return self.apply_deletion(view, u, policy);
884300×
        }
885300×
        let current = view.bookmarks.get(&u.name).copied().unwrap_or(empty);
886300×
        if current != u.old_state {
88713×
            return Err("stale: the remote bookmark has moved; pull first".into());
888287×
        }
889287×
        let new_state = self
890287×
            .store
891287×
            .get_state(&u.new_state)
892287×
            .map_err(|_| "new state object is missing".to_string())?;
893564×
        for h in &new_state.heads {
894564×
            if !self.store.has(h) {
8951×
                return Err(format!("head {} is missing", h.short(12)));
896563×
            }
897
        }
898286×
        if !u.force {
899285×
            let old_heads = self
900285×
                .store
901285×
                .get_state(&current)
902285×
                .map_err(|e| e.to_string())?
903
                .heads;
904285×
            let closure = self
905285×
                .closure_in_store(&new_state.heads)
906285×
                .map_err(|e| e.to_string())?;
907308×
            if let Some(h) = old_heads.iter().find(|h| !closure.contains(h)) {
9081×
                return Err(format!(
9091×
                    "not a fast-forward: current head {} would be dropped (use --force)",
9101×
                    h.short(12)
9111×
                ));
912284×
            }
9131×
        }
914285×
        if let Some(p) = policy {
91530×
            let Some(sa) = &u.attestation else {
9161×
                return Err(format!(
9171×
                    "{:?} requires a signed reference attestation under this repository's policy",
9181×
                    u.name
9191×
                ));
920
            };
92129×
            let signer = sa
92229×
                .sigs
92329×
                .first()
92429×
                .map(|s| s.key)
92529×
                .ok_or("attestation carries no signature")?;
92629×
            if !p.authorizes(&signer, &u.name)
9277×
                && !self.delegate_authorized(p, &signer, &u.name, sa.attest.hlc.wall_ms)
928
            {
9295×
                return Err(format!(
9305×
                    "key {} may not update {:?} under this repository's policy",
9315×
                    signer.short(12),
9325×
                    u.name
9335×
                ));
93424×
            }
93524×
            let old_heads = self
93624×
                .store
93724×
                .get_state(&current)
93824×
                .map_err(|e| e.to_string())?
939
                .heads;
94024×
            self.state_allowed(&old_heads, &new_state.heads, p, &u.name)?;
941255×
        }
942279×
        if let Some(sa) = &u.attestation {
94377×
            self.record_update_attest(u, sa)?;
944202×
        }
945273×
        view.bookmarks.insert(u.name.clone(), u.new_state);
946273×
        Ok(())
947303×
    }
948
949
    // ------------------------------------------------------------ client side
950
951965×
    fn remote_url(&self, name: &str) -> Result<String> {
952965×
        self.config.remotes.get(name).cloned().ok_or_else(|| {
9533×
            let names: Vec<&str> = self.config.remotes.keys().map(String::as_str).collect();
9543×
            RepoError::Msg(match names.as_slice() {
9553×
                [] => format!("no remote named {name:?}; add one with `oxy remote add`"),
9561×
                _ if name == "origin" && names.len() > 1 && self.config.default_remote.is_none() => format!(
957
                    "this repository has no remote named origin, and several others ({}): name one (`oxy pull {}`), or make one the default with `oxy config core.remote {}`",
9581×
                    names.join(", "),
9591×
                    names[0],
9601×
                    names[0]
961
                ),
9620×
                _ => format!(
963
                    "no remote named {name:?}; this repository has {}",
9640×
                    names.join(", ")
965
                ),
966
            })
9673×
        })
968965×
    }
969
970
    /// Refuse a remote that is another repository. An **unbound** remote
971
    /// (id all zero: a hosted repository nobody has pushed to) is accepted
972
    /// for pushes, which bind it to this repository's id (spec §16.1).
973718×
    fn check_remote_id(
974718×
        &self,
975718×
        remote: &dyn crate::protocol::Remote,
976718×
        remote_name: &str,
977718×
        allow_unbound: bool,
978718×
    ) -> Result<()> {
979718×
        let theirs = remote.repo_id()?;
980717×
        if theirs == self.id()? {
981694×
            return Ok(());
98223×
        }
98323×
        if theirs == crate::protocol::UNBOUND_ID {
98420×
            if allow_unbound {
98519×
                return Ok(());
9861×
            }
9871×
            return fail(format!(
988
                "remote {remote_name:?} is empty and unbound: push to it first"
989
            ));
9903×
        }
9913×
        fail(format!(
992
            "remote {remote_name:?} is a different repository (repository IDs differ); check its URL with `oxy remote list`"
993
        ))
994718×
    }
995
996
    /// Credentials and trust for a remote: `remote.<name>.token` and
997
    /// `remote.<name>.ca`, else the defaults for its URL
998
    /// ([`Repo::fill_credentials`]).
999982×
    pub(crate) fn remote_options(&self, name: &str) -> RemoteOptions {
1000982×
        let url = self.config.remotes.get(name).cloned().unwrap_or_default();
1001982×
        let mut options = RemoteOptions {
1002982×
            repo_id: self.id().ok(),
1003982×
            token: self.config.remote_tokens.get(name).cloned(),
1004982×
            ca: self.config.remote_cas.get(name).map(PathBuf::from),
1005982×
            cache_dir: Some(self.oxy.join("remotes").join(name).join("strata")),
1006982×
            ..RemoteOptions::default()
1007982×
        };
1008982×
        self.fill_credentials(&url, &mut options);
1009982×
        options
1010982×
    }
1011
1012
    /// The user's credential store (`credentials.toml` beside the user
1013
    /// configuration file).
10141001×
    pub fn credentials(&self) -> crate::credentials::Credentials {
10151001×
        crate::credentials::Credentials::at(
10161001×
            self.user_config
10171001×
                .parent()
10181001×
                .map(Path::to_path_buf)
10191001×
                .unwrap_or_else(default_home),
1020
        )
10211001×
    }
1022
1023
    /// Fill in what `options` lacks for `url`: `$OXY_TOKEN` and `$OXY_CA`,
1024
    /// then the user's credential store for its hub, then a remote on the
1025
    /// same hub in an enclosing checkout (a subrepo borrows its parent's).
1026
    /// When the token is not this remote's own or the environment's, the
1027
    /// other tokens enclosing checkouts hold for the hub are kept to try
1028
    /// if it is refused: a stale stored login must not hide a parent's
1029
    /// good token (oxygen#112).
10301002×
    fn fill_credentials(&self, url: &str, options: &mut RemoteOptions) {
10311002×
        if options.token.is_some() && options.ca.is_some() {
10321×
            return;
10331001×
        }
10341001×
        let explicit =
10351001×
            options.token.is_some() || std::env::var("OXY_TOKEN").is_ok_and(|t| !t.is_empty());
10361001×
        let stored = self.credentials().options_for(url);
10371001×
        options.token = options.token.take().or(stored.token);
10381001×
        options.ca = options.ca.take().or(stored.ca);
10391001×
        let enclosing = self.enclosing_credentials(url);
10401001×
        if options.token.is_none() {
1041991×
            options.token = enclosing.iter().find_map(|(t, _)| t.clone());
104210×
        }
10431001×
        if options.ca.is_none() {
1044947×
            options.ca = enclosing.iter().find_map(|(_, c)| c.clone());
104554×
        }
10461001×
        if !explicit {
1047994×
            for t in enclosing.into_iter().filter_map(|(t, _)| t) {
10485×
                if options.token.as_ref() != Some(&t) && !options.fallback_tokens.contains(&t) {
10492×
                    options.fallback_tokens.push(t);
10503×
                }
1051
            }
10527×
        }
10531002×
    }
1054
1055
    /// The tokens and CAs of remotes on `url`'s hub in enclosing
1056
    /// checkouts, nearest first.
10571001×
    fn enclosing_credentials(&self, url: &str) -> Vec<(Option<String>, Option<PathBuf>)> {
10581001×
        let mut out = Vec::new();
10591001×
        let Some(hub) = crate::credentials::hub_of(url) else {
1060861×
            return out;
1061
        };
1062140×
        let mut at = self.root.parent();
1063568×
        while let Some(dir) = at {
1064428×
            let Ok(cfg) = read_config_file(&dir.join(OXY_DIR).join("config")) else {
10650×
                break;
1066
            };
1067428×
            for (key, remote_url) in &cfg {
106815×
                let Some(name) = key
106915×
                    .strip_prefix("remote.")
107015×
                    .and_then(|k| k.strip_suffix(".url"))
1071
                else {
10727×
                    continue;
1073
                };
10748×
                if crate::credentials::hub_of(remote_url).as_deref() != Some(hub.as_str()) {
10751×
                    continue;
10767×
                }
10777×
                out.push((
10787×
                    cfg.get(&format!("remote.{name}.token")).cloned(),
10797×
                    cfg.get(&format!("remote.{name}.ca")).map(PathBuf::from),
10807×
                ));
1081
            }
1082428×
            at = dir.parent();
1083
        }
1084140×
        out
10851001×
    }
1086
1087
    /// Push seal attestations on their own (hub spec §7.4): a bundle with
1088
    /// no changes and no reference updates, which the receiver stores.
10890×
    pub fn push_seals(&self, remote_name: &str, seals: &[ObjectRef]) -> Result<()> {
10900×
        let remote = self.open_named_remote(remote_name)?;
10910×
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
10920×
        let mut bundle = Bundle::default();
10930×
        for r in seals {
10940×
            let attest = self.store.get_attest(r)?;
10950×
            let sigs = self.store.signatures(r)?;
10960×
            bundle
10970×
                .attests
10980×
                .push(crate::protocol::SignedAttest { attest, sigs });
1099
        }
11000×
        let results = remote.push(bundle, vec![], None, vec![])?;
11010×
        if let Some(r) = results.iter().find(|r| !r.ok) {
11020×
            return fail(format!("{}: {}", r.name, r.reason));
11030×
        }
11040×
        Ok(())
11050×
    }
1106
1107
    /// The remote whose URL starts with `base` (a hub), if configured.
11084×
    pub fn remote_for_url(&self, base: &str) -> Option<String> {
11094×
        let base = base.trim_end_matches('/');
11104×
        self.config
11114×
            .remotes
11124×
            .iter()
11134×
            .find(|(_, url)| url.trim_end_matches('/') == base)
11144×
            .map(|(name, _)| name.clone())
11154×
    }
1116
1117
    /// Open a remote by name or, failing that, by URL with the default
1118
    /// credentials (see [`Repo::credentials_for`]).
11190×
    pub fn open_remote_at(&self, url: &str) -> Result<(String, Box<dyn crate::protocol::Remote>)> {
11200×
        if let Some(name) = self.remote_for_url(url) {
11210×
            return Ok((name.clone(), self.open_named_remote(&name)?));
11220×
        }
11230×
        let options = RemoteOptions {
11240×
            repo_id: self.id().ok(),
11250×
            ..self.credentials_for(url)
11260×
        };
11270×
        Ok((url.to_string(), open_remote(url, &options)?))
11280×
    }
1129
1130
    /// Credentials for a URL: those of the configured remote with that
1131
    /// URL, else the environment's.
11324×
    pub fn options_for_url(&self, url: &str) -> RemoteOptions {
11334×
        match self.remote_for_url(url) {
11344×
            Some(name) => self.remote_options(&name),
11350×
            None => RemoteOptions {
11360×
                repo_id: self.id().ok(),
11370×
                ..self.credentials_for(url)
11380×
            },
1139
        }
11404×
    }
1141
1142965×
    fn open_named_remote(&self, name: &str) -> Result<Box<dyn crate::protocol::Remote>> {
1143965×
        open_remote(&self.remote_url(name)?, &self.remote_options(name))
1144965×
    }
1145
114698×
    fn serial_path(&self, remote: &str, bookmark: &str) -> PathBuf {
114798×
        self.oxy
114898×
            .join("remotes")
114998×
            .join(remote)
115098×
            .join(format!("{bookmark}.serial"))
115198×
    }
1152
115320×
    fn last_seen_serial(&self, remote: &str, bookmark: &str) -> Option<u64> {
115420×
        fs::read_to_string(self.serial_path(remote, bookmark))
115520×
            .ok()
115620×
            .and_then(|s| s.trim().parse().ok())
115720×
    }
1158
115978×
    fn record_serial(&self, remote: &str, bookmark: &str, serial: u64) -> Result<()> {
116078×
        let p = self.serial_path(remote, bookmark);
116178×
        fs::create_dir_all(p.parent().unwrap())?;
116278×
        write_atomic(&p, serial.to_string().as_bytes())
116378×
    }
1164
1165378×
    fn policy_serial_path(&self, remote: &str) -> PathBuf {
1166378×
        self.oxy.join("remotes").join(remote).join("policy.serial")
1167378×
    }
1168
1169
    /// The highest policy serial `remote` has ever presented.
1170350×
    fn remote_policy_serial(&self, remote: &str) -> Option<u64> {
1171350×
        fs::read_to_string(self.policy_serial_path(remote))
1172350×
            .ok()
1173350×
            .and_then(|s| s.trim().parse().ok())
1174350×
    }
1175
117628×
    fn record_policy_serial(&self, remote: &str, serial: u64) -> Result<()> {
117728×
        let p = self.policy_serial_path(remote);
117828×
        fs::create_dir_all(p.parent().unwrap())?;
117928×
        write_atomic(&p, serial.to_string().as_bytes())
118028×
    }
1181
118227×
    fn lazy_dir(&self) -> PathBuf {
118327×
        self.oxy.join("lazy")
118427×
    }
1185
1186
    /// Remember that `blob` was withheld by `remote` (spec §13.5).
11874×
    fn register_lazy(&self, blob: &ObjectRef, remote: &str) -> Result<()> {
11884×
        fs::create_dir_all(self.lazy_dir())?;
11894×
        write_atomic(&self.lazy_dir().join(blob.to_hex()), remote.as_bytes())
11904×
    }
1191
1192
    /// Which remote promised an absent blob, if any.
119315×
    pub fn lazy_remote(&self, blob: &ObjectRef) -> Option<String> {
119415×
        fs::read_to_string(self.lazy_dir().join(blob.to_hex()))
119515×
            .ok()
119615×
            .map(|s| s.trim().to_string())
119715×
    }
1198
1199
    /// Fetch an absent blob (and its chunks) from the remote that promised it.
12004×
    pub(crate) fn fetch_lazy_blob(&self, blob: &ObjectRef) -> Result<()> {
12014×
        let Some(remote_name) = self.lazy_remote(blob) else {
12020×
            return Err(RepoError::Store(StoreError::NotFound(*blob)));
1203
        };
12044×
        let remote = self.open_named_remote(&remote_name)?;
12054×
        let objects = remote.objects(&[*blob])?;
12069×
        for (ty, body) in objects {
12079×
            if matches!(ty, ObjectType::Blob | ObjectType::Chunk) {
12089×
                self.store.put(ty, &body)?;
12090×
            }
1210
        }
12114×
        if !self.store.has(blob) {
12120×
            return fail(format!(
1213
                "{remote_name} did not send blob {}",
12140×
                blob.short(12)
1215
            ));
12164×
        }
12174×
        let _ = fs::remove_file(self.lazy_dir().join(blob.to_hex()));
12184×
        Ok(())
12194×
    }
1220
1221
    /// After importing from `remote`, record every blob the changes reference
1222
    /// that the store lacks as lazily available from that remote.
12234×
    fn register_missing_blobs(&self, changes: &[ChangeHash], remote: &str) -> Result<usize> {
12244×
        let mut n = 0;
12254×
        for h in changes {
12264×
            let c = self.store.get_change(h)?;
122713×
            for e in &c.edits {
122813×
                if let Edit::Replace { old, new, .. } = e {
12296×
                    for b in [old, new].into_iter().flatten() {
12306×
                        if !self.store.has(b) {
12314×
                            self.register_lazy(b, remote)?;
12324×
                            n += 1;
12332×
                        }
1234
                    }
12357×
                }
1236
            }
1237
        }
12384×
        Ok(n)
12394×
    }
1240
1241
    /// Every head this repository knows: bookmarks, remote tracking, working copy.
1242350×
    pub fn known_heads(&self, view: &View) -> Result<Vec<ChangeHash>> {
1243350×
        let mut have: BTreeSet<ChangeHash> = BTreeSet::new();
1244472×
        for st in view
1245350×
            .bookmarks
1246350×
            .values()
1247350×
            .chain(view.remotes.values().flat_map(|b| b.values()))
1248
        {
1249472×
            if let Ok(s) = self.store.get_state(st) {
1250472×
                have.extend(s.heads);
1251472×
            }
1252
        }
1253350×
        have.extend(view.workspaces.values().copied());
1254350×
        Ok(have.into_iter().collect())
1255350×
    }
1256
1257
    /// Fetch every bookmark of a remote into `remotes/<name>/bookmarks/*` (spec §10.2).
1258356×
    pub fn fetch(&self, remote_name: &str) -> Result<FetchReport> {
1259356×
        let remote = self.open_named_remote(remote_name)?;
1260353×
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
1261351×
        let resp = remote.refs()?;
1262
        // Trust policy (spec §14.5). Adopt the remote's policy when it is newer
1263
        // and signed by roots we trust, or on first contact. A remote that
1264
        // once presented a policy may never present none or an older one:
1265
        // that is a downgrade. A remote that never presented one is
1266
        // unverified, and this fetch is checked only as far as possible.
1267350×
        let local_policy = self.current_policy()?.map(|(_, sp)| sp.policy);
1268350×
        let seen = self.remote_policy_serial(remote_name);
1269350×
        let mut unverified = false;
1270350×
        match &resp.policy {
12719×
            Some(sp) => {
12729×
                if seen.is_some_and(|s| sp.policy.serial < s) {
12730×
                    return fail(format!("{remote_name} presents policy serial {} but presented {} before: refusing the downgrade", sp.policy.serial, seen.unwrap()));
12749×
                }
12755×
                match &local_policy {
12765×
                    Some(l) if sp.policy.serial < l.serial => {}
12775×
                    Some(l) if sp.policy.serial == l.serial => {
12781×
                        if sp.policy.hash() != l.hash() {
12790×
                            return fail(format!(
1280
                                "{remote_name} presents a different policy with the same serial {}",
1281
                                l.serial
1282
                            ));
12831×
                        }
1284
                    }
1285
                    _ => {
12868×
                        self.adopt_policy_chain(
12878×
                            &resp.history,
12888×
                            sp,
12898×
                            &format!("oxy fetch {remote_name}"),
1290
                        )
12918×
                        .map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?;
1292
                    }
1293
                }
12949×
                self.record_policy_serial(remote_name, sp.policy.serial)?;
1295
            }
1296
            None => {
1297341×
                if seen.is_some() {
12980×
                    return fail(format!("{remote_name} presented a policy before but has none now: refusing the downgrade"));
1299341×
                }
1300341×
                unverified = true;
1301
            }
1302
        }
1303350×
        let policy = if unverified {
1304341×
            None
1305
        } else {
13069×
            self.current_policy()?.map(|(_, sp)| sp.policy)
1307
        };
1308350×
        let (op_ref, op) = self.head_op()?;
1309350×
        let mut view = op.view.clone();
1310350×
        let have = self.known_heads(&view)?;
1311350×
        let mut report = Vec::new();
1312350×
        let max_blob = self.config.max_blob;
1313357×
        for r in resp.refs {
1314357×
            let needed = {
1315357×
                let _t = crate::timing::phase("fetch: negotiate");
1316357×
                remote.negotiate(&have, &r.heads)?
1317
            };
1318357×
            let bundle = {
1319357×
                let _t = crate::timing::phase("fetch: bundle");
1320357×
                remote.fetch(&needed, max_blob)?
1321
            };
1322
            // Changes are not individually re-verified here: the bookmark's
1323
            // attestation (checked below) binds its whole state, and the
1324
            // receiver of the push verified the added changes at that time.
1325357×
            let imported = {
1326357×
                let _t = crate::timing::phase("fetch: import");
1327357×
                self.import_bundle(&bundle, false)?
1328
            };
1329357×
            if max_blob.is_some() {
13304×
                self.register_missing_blobs(&imported, remote_name)?;
1331353×
            }
1332488×
            for h in &r.heads {
1333488×
                if !self.store.has(h) {
13340×
                    return fail(format!("remote did not send head {}", h.short(12)));
1335488×
                }
1336
            }
1337357×
            let state = self.store.put_state(&StateObject::new(r.heads.clone()))?;
1338357×
            if state != r.state {
13390×
                return fail(format!(
1340
                    "remote state {} does not match its heads",
13410×
                    r.state.short(12)
1342
                ));
1343357×
            }
1344357×
            if let Some(p) = &policy {
13459×
                let Some(sa) = &r.attestation else {
13460×
                    return fail(format!(
1347
                        "{remote_name}/{} has no attestation, which its policy requires",
1348
                        r.name
1349
                    ));
1350
                };
13519×
                let signer = sa
13529×
                    .sigs
13539×
                    .first()
13549×
                    .map(|s| s.key)
13559×
                    .ok_or_else(|| RepoError::Msg("attestation carries no signature".into()))?;
13569×
                if !p.authorizes(&signer, &r.name)
13570×
                    && !self.delegate_authorized(p, &signer, &r.name, sa.attest.hlc.wall_ms)
1358
                {
13590×
                    return fail(format!(
1360
                        "{remote_name}/{}: attested by {}, which the policy does not authorize for this bookmark; a policy owner can grant it with `oxy policy grant`",
1361
                        r.name,
13620×
                        signer.short(12)
1363
                    ));
13649×
                }
1365348×
            }
1366357×
            if let Some(sa) = &r.attestation {
1367
                let AttestBody::Ref {
136820×
                    old_state,
136920×
                    new_state,
1370
                    ..
137120×
                } = &sa.attest.body
1372
                else {
13730×
                    return fail("remote sent a non-ref attestation for a bookmark");
1374
                };
137520×
                let (old_state, new_state) = (*old_state, *new_state);
137620×
                let serial = self
137720×
                    .check_ref_attest(
137820×
                        sa,
137920×
                        &r.name,
138020×
                        &old_state,
138120×
                        &new_state,
138220×
                        self.last_seen_serial(remote_name, &r.name),
1383
                        true,
1384
                    )
138520×
                    .map_err(|e| {
13860×
                        RepoError::Msg(format!("attestation for {}/{}: {e}", remote_name, r.name))
13870×
                    })?;
138820×
                if new_state != r.state {
13890×
                    return fail(format!(
1390
                        "attestation for {}/{} does not cover its current state",
1391
                        remote_name, r.name
1392
                    ));
139320×
                }
139420×
                let ar = self.store.put_attest(&sa.attest)?;
139520×
                for sig in &sa.sigs {
139620×
                    self.store.put_signature(&ar, sig)?;
1397
                }
139820×
                self.record_serial(remote_name, &r.name, serial)?;
1399337×
            }
1400357×
            view.remotes
1401357×
                .entry(remote_name.to_string())
1402357×
                .or_default()
1403357×
                .insert(r.name.clone(), state);
1404357×
            report.push((r.name, r.heads, imported.len()));
1405
        }
1406350×
        let mut new_tags = 0;
1407350×
        for t in &resp.tags {
140813×
            if view.tags.contains_key(&t.name) {
14094×
                continue;
14109×
            }
1411
            // The tag's changes must be here: fetch them if the bookmarks did not bring them.
14129×
            let missing: Vec<ChangeHash> = t
14139×
                .heads
14149×
                .iter()
14159×
                .filter(|h| !self.store.has(h))
14169×
                .copied()
14179×
                .collect();
14189×
            if !missing.is_empty() {
14190×
                let needed = remote.negotiate(&self.known_heads(&view)?, &missing)?;
14200×
                let bundle = remote.fetch(&needed, max_blob)?;
14210×
                self.import_bundle(&bundle, false)?;
14229×
            }
14239×
            self.store.put_state(&StateObject::new(t.heads.clone()))?;
14249×
            self.adopt_tag(&mut view, t, policy.as_ref())
14259×
                .map_err(|e| RepoError::Msg(format!("{remote_name} {}: {e}", t.name)))?;
14269×
            new_tags += 1;
1427
        }
1428350×
        if view != op.view {
1429340×
            self.record_op(&[op_ref], &format!("oxy fetch {remote_name}"), view)?;
143010×
        }
1431350×
        let _ = self.run_hook(
1432350×
            "post-fetch",
1433350×
            &format!(
1434
                "{{\"event\":\"post-fetch\",\"remote\":{},\"bookmarks\":{}}}",
1435350×
                json_str(remote_name),
1436357×
                json_array(report.iter().map(|(n, heads, _)| format!(
1437
                    "{{\"name\":{},\"heads\":{}}}",
1438357×
                    json_str(n),
1439488×
                    json_array(heads.iter().map(|h| json_str(&h.to_hex())))
1440
                )))
1441
            ),
1442
        );
1443350×
        Ok(FetchReport {
1444350×
            refs: report,
1445350×
            unverified,
1446350×
            new_tags,
1447350×
        })
1448356×
    }
1449
1450
    /// Shallow fetch of the remote's bookmark: its checkpoint as our base,
1451
    /// the changes within `depth` steps of its heads, and the boundary below.
14521×
    pub fn fetch_shallow(&self, remote_name: &str, depth: u32) -> Result<usize> {
14531×
        let remote = self.open_named_remote(remote_name)?;
14541×
        self.check_remote_id(remote.as_ref(), remote_name, false)?;
14551×
        let resp = remote.refs()?;
14561×
        let bookmark = self.config.bookmark.clone();
14571×
        if let Some(sp) = &resp.policy {
14580×
            self.adopt_policy_chain(&resp.history, sp, "oxy clone --depth")
14590×
                .map_err(|e| RepoError::Msg(format!("policy from {remote_name}: {e}")))?;
14600×
            self.record_policy_serial(remote_name, sp.policy.serial)?;
14611×
        }
14621×
        let Some(r) = resp.refs.into_iter().find(|r| r.name == bookmark) else {
14630×
            return fail(format!("{remote_name} has no bookmark {bookmark:?}"));
1464
        };
14651×
        if r.heads.is_empty() {
14660×
            return Ok(0);
14671×
        }
1468
        // Base: the remote's checkpoint of exactly these heads.
14691×
        let cp_bundle = remote.checkpoint(&r.heads)?;
14701×
        let mut cp_ref = None;
14713×
        for (ty, body) in &cp_bundle.objects {
14723×
            let put = self.store.put(*ty, body)?;
14733×
            if *ty == ObjectType::Checkpoint {
14741×
                cp_ref = Some(put);
14752×
            }
1476
        }
14771×
        let Some(cp_ref) = cp_ref else {
14780×
            return fail(format!("{remote_name} sent no checkpoint"));
1479
        };
14801×
        self.install_checkpoint(&bookmark, &cp_ref)?;
1481
        // History window.
14821×
        let wanted = remote.shallow(&r.heads, depth)?;
14831×
        let bundle = remote.fetch(&wanted, self.config.max_blob)?;
14841×
        let imported = self.import_bundle_shallow(&bundle, false)?;
14851×
        if self.config.max_blob.is_some() {
14860×
            self.register_missing_blobs(&imported, remote_name)?;
14871×
        }
14881×
        self.refresh_boundary(&imported)?;
14891×
        if let Some(sa) = &r.attestation {
14900×
            let ar = self.store.put_attest(&sa.attest)?;
14910×
            for sig in &sa.sigs {
14920×
                self.store.put_signature(&ar, sig)?;
1493
            }
14940×
            if let AttestBody::Ref { serial, .. } = sa.attest.body {
14950×
                self.record_serial(remote_name, &r.name, serial)?;
14960×
            }
14971×
        }
14981×
        let state = self.store.put_state(&StateObject::new(r.heads.clone()))?;
14991×
        let (op_ref, op) = self.head_op()?;
15001×
        let mut view = op.view.clone();
15011×
        view.remotes
15021×
            .entry(remote_name.to_string())
15031×
            .or_default()
15041×
            .insert(r.name.clone(), state);
15051×
        self.record_op(
15061×
            &[op_ref],
15071×
            &format!("oxy fetch {remote_name} --depth {depth}"),
15081×
            view,
15090×
        )?;
15101×
        Ok(imported.len())
15111×
    }
1512
1513
    /// Fetch, then take the union of the local bookmark's state with the
1514
    /// remote's: no merge change, conflicts become reified (spec §9.4).
1515105×
    pub fn pull(&self, remote_name: &str) -> Result<PullReport> {
1516105×
        let _wc = self.lock_working_copy()?;
1517105×
        self.fetch(remote_name)?;
1518102×
        let (op_ref, op) = self.head_op()?;
1519102×
        let current = self.config.bookmark.clone();
1520102×
        let mut local_current = self.heads_of(&op.view, &current)?;
1521102×
        local_current.sort();
1522102×
        let mut view = op.view.clone();
1523102×
        let mut current_heads = local_current.clone();
1524102×
        let mut changed = false;
1525102×
        let mut others = Vec::new();
1526102×
        let tracked = op
1527102×
            .view
1528102×
            .remotes
1529102×
            .get(remote_name)
1530102×
            .cloned()
1531102×
            .unwrap_or_default();
1532106×
        for (name, remote_state) in tracked {
1533
            // Every local bookmark tracks the remote's of the same name;
1534
            // the current one may be new here.
1535106×
            let local_state = op.view.bookmarks.get(&name).copied();
1536106×
            if local_state.is_none() && name != current {
15370×
                continue;
1538106×
            }
1539106×
            if local_state == Some(remote_state) {
15408×
                continue;
154198×
            }
154298×
            let local = self.heads_of(&op.view, &name)?;
154398×
            let remote_heads = self.store.get_state(&remote_state)?.heads;
1544151×
            if remote_heads.iter().all(|h| local.contains(h)) {
15450×
                continue;
154698×
            }
154798×
            let mut all = local.clone();
154898×
            all.extend(remote_heads);
154998×
            let st = self.load_state_history(&all)?;
155098×
            let mut heads = st.maximal(&all);
155198×
            heads.sort();
155298×
            let mut sorted_local = local;
155398×
            sorted_local.sort();
155498×
            if heads == sorted_local {
15551×
                continue;
155697×
            }
155797×
            view.bookmarks.insert(
155897×
                name.clone(),
155997×
                self.store.put_state(&StateObject::new(heads.clone()))?,
1560
            );
156197×
            if name == current {
156295×
                changed = true;
156395×
                current_heads = heads;
156495×
            } else {
15652×
                others.push(name);
15662×
            }
1567
        }
1568102×
        if changed || !others.is_empty() {
156996×
            self.record_op(&[op_ref], &format!("oxy pull {remote_name}"), view)?;
15706×
        }
1571
        // The working copy follows its bookmark whenever nothing would be
1572
        // lost, also when this pull brought nothing new but it had been
1573
        // left behind (oxygen#89).
1574102×
        let checked_out = self.follow_bookmark(&current_heads, &current, "oxy pull")?;
1575
        Ok(PullReport {
1576102×
            heads: if changed {
157795×
                current_heads
1578
            } else {
15797×
                local_current
1580
            },
1581102×
            changed,
1582102×
            checked_out,
1583102×
            others,
1584
        })
1585105×
    }
1586
1587
    /// How many changes the working copy's bookmark has that its base
1588
    /// lacks, when the base is part of the bookmark's state (it fell behind,
1589
    /// and `oxy checkout` or `oxy pull` brings it up); `None` when it is on
1590
    /// the bookmark or holds changes the bookmark lacks. History is read
1591
    /// only when the base and the bookmark's heads differ (oxygen#89).
15927×
    pub fn working_copy_behind(&self) -> Result<Option<usize>> {
15937×
        let (_, wc) = self.working_copy()?;
15947×
        let mut base = wc.deps.clone();
15957×
        base.sort();
15967×
        let mut heads = self.bookmark_heads()?;
15977×
        heads.sort();
15987×
        if base == heads || heads.is_empty() {
15996×
            return Ok(None);
16001×
        }
16011×
        let state = self.load_state_history(&heads)?;
16021×
        if !base.iter().all(|h| state.contains(h)) {
16030×
            return Ok(None);
16041×
        }
16051×
        let had = if base.is_empty() {
16060×
            0
1607
        } else {
16081×
            self.load_state_history(&base)?.len()
1609
        };
16101×
        Ok(Some(state.len().saturating_sub(had)).filter(|n| *n > 0))
16117×
    }
1612
1613
    /// Bring the working copy to `heads`, the state of its bookmark
1614
    /// `bookmark`, when nothing would be lost: its base is part of that
1615
    /// state, so the bookmark only adds to it. Clean, it is checked out;
1616
    /// with uncommitted work, the work comes along when the new changes
1617
    /// leave its paths alone (as `git pull` does), else it stays. Before,
1618
    /// it followed only from exactly the bookmark's old heads, so a working
1619
    /// copy already behind (the bookmark moved by `bookmark set`, a landing
1620
    /// or a pull from another workspace) never caught up, and nothing said
1621
    /// so (oxygen#74, #89). Returns whether it moved.
1622102×
    pub fn follow_bookmark(
1623102×
        &self,
1624102×
        heads: &[ChangeHash],
1625102×
        bookmark: &str,
1626102×
        command: &str,
1627102×
    ) -> Result<bool> {
1628102×
        let snap = self.snapshot(command)?;
1629102×
        let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty();
1630102×
        let mut base = snap.base.maximal(&snap.wc.deps);
1631102×
        base.sort();
1632102×
        let mut target = heads.to_vec();
1633102×
        target.sort();
1634102×
        if base == target {
16356×
            return Ok(false);
163696×
        }
163796×
        let state = self.load_state_history(heads)?;
1638189×
        if !base.iter().all(|h| state.contains(h)) {
1639
            // The working copy holds changes the bookmark lacks: not ours to move.
16400×
            return Ok(false);
164196×
        }
164296×
        if clean {
164396×
            self.checkout(heads, false, &format!("{command} (checkout)"))?;
164496×
            Ok(true)
1645
        } else {
16460×
            Ok(self
16470×
                .checkout_carry(heads, bookmark, &format!("{command} (checkout --carry)"))
16480×
                .is_ok())
1649
        }
1650102×
    }
1651
1652
    /// Push the current bookmark (spec §16.3), sealing this author's unsigned
1653
    /// changes and attesting the reference when a key is available.
1654
    /// Delete `bookmark` on a remote (spec §16.4): an update to the zero
1655
    /// state, attested when we have an identity. The local bookmark, if
1656
    /// any, is kept; only the remote-tracking entry goes.
16575×
    pub fn push_delete(&self, remote_name: &str, bookmark: &str) -> Result<Vec<RefResult>> {
16585×
        let remote = self.open_named_remote(remote_name)?;
16595×
        self.check_remote_id(remote.as_ref(), remote_name, true)?;
16605×
        let resp = remote.refs()?;
16617×
        let Some(r) = resp.refs.iter().find(|r| r.name == bookmark) else {
16621×
            return fail(format!("{remote_name} has no bookmark {bookmark}"));
1663
        };
16644×
        if resp.policy.is_some() && self.secret_key()?.is_none() {
16650×
            return fail(format!("{remote_name} enforces a trust policy; deleting needs an identity (`oxy identity create`)"));
16664×
        }
16674×
        let attestation = match self.secret_key()? {
16682×
            Some(_) => Some(self.sign_attest(&Attest {
16692×
                repo: self.id()?,
16702×
                hlc: Hlc::now(),
16712×
                body: AttestBody::Ref {
16722×
                    name: bookmark.to_string(),
16732×
                    old_state: r.state,
16742×
                    new_state: ObjectRef::ZERO,
16752×
                    serial: r.last_serial.unwrap_or(0) + 1,
16762×
                },
16770×
            })?),
16782×
            None => None,
1679
        };
16804×
        let results = remote.push(
16814×
            Bundle::default(),
16824×
            vec![RefUpdate {
16834×
                name: bookmark.to_string(),
16844×
                old_state: r.state,
16854×
                new_state: ObjectRef::ZERO,
16864×
                force: true,
16874×
                attestation,
16884×
            }],
16894×
            None,
16904×
            vec![],
16910×
        )?;
16924×
        if results.iter().any(|x| x.ok && x.name == bookmark) {
16932×
            let (op_ref, op) = self.head_op()?;
16942×
            let mut view = op.view.clone();
16952×
            if let Some(m) = view.remotes.get_mut(remote_name) {
16962×
                m.remove(bookmark);
16972×
            }
16982×
            self.record_op(
16992×
                &[op_ref],
17002×
                &format!("oxy push {remote_name} --delete {bookmark}"),
17012×
                view,
17020×
            )?;
17032×
        }
17044×
        Ok(results)
17055×
    }
1706
1707
    /// Remove remote `name` (oxygen#86): every `remote.NAME.*` key of this
1708
    /// repository's config (its URL, token, CA and store settings), its
1709
    /// tracking bookmarks in the view, and a store remote's mirror under
1710
    /// `.oxy/remotes/NAME`. Nothing stays to be read as if it were current.
17113×
    pub fn remove_remote(&mut self, name: &str) -> Result<()> {
17123×
        if !self.config.remotes.contains_key(name) {
17131×
            return fail(format!("no remote {name}: `oxy remote list` shows them"));
17142×
        }
17152×
        let prefix = format!("remote.{name}.");
17162×
        let keys: Vec<String> = self
17172×
            .config
17182×
            .repo_layer()
17192×
            .keys()
17204×
            .filter(|k| k.starts_with(&prefix))
17212×
            .cloned()
17222×
            .collect();
17233×
        for k in keys {
17243×
            self.config.set(&k, "")?;
1725
        }
17262×
        self.config.remotes.remove(name);
17272×
        self.save_config()?;
17282×
        let (op_ref, op) = self.head_op()?;
17292×
        if op.view.remotes.contains_key(name) {
17301×
            let mut view = op.view.clone();
17311×
            view.remotes.remove(name);
17321×
            self.record_op(&[op_ref], &format!("oxy remote remove {name}"), view)?;
17331×
        }
17342×
        let mirror = self.oxy.join("remotes").join(name);
17352×
        if mirror.is_dir() {
17360×
            fs::remove_dir_all(&mirror)?;
17372×
        }
17382×
        Ok(())
17393×
    }
1740
1741
    /// `changes`, in dependency order, cut into batches of about
1742
    /// `push.batch-bytes` by what each change and its texts and blobs weigh.
1743
    /// There is always one, perhaps empty.
1744280×
    fn push_batches(&self, changes: &[ChangeHash]) -> Result<Vec<Vec<ChangeHash>>> {
1745280×
        let blob_len = |r: &ObjectRef| -> u64 {
174629×
            self.store
174729×
                .get_typed(r, ObjectType::Blob)
174829×
                .ok()
174929×
                .and_then(|b| Blob::decode(&b).ok())
175029×
                .map_or(0, |b| b.length)
175129×
        };
1752280×
        let (mut out, mut batch, mut bytes) = (Vec::new(), Vec::new(), 0u64);
1753384×
        for h in changes {
1754384×
            let raw = self.store.get_typed(h, ObjectType::Change)?;
1755384×
            let (c, texts) = Change::decode_skeleton(&raw, *h)?;
1756384×
            let mut weight = raw.len() as u64;
1757488×
            for t in &texts {
1758488×
                if let crate::objects::TextRef::Blob(r, _, _) = t {
175926×
                    weight += blob_len(r);
1760462×
                }
1761
            }
1762630×
            for e in &c.edits {
17633×
                if let Edit::Replace { new: Some(b), .. } = e {
17643×
                    weight += blob_len(b);
1765627×
                }
1766
            }
1767384×
            if !batch.is_empty() && bytes + weight > self.config.push_batch_bytes {
17685×
                out.push(std::mem::take(&mut batch));
17695×
                bytes = 0;
1770379×
            }
1771384×
            batch.push(*h);
1772384×
            bytes += weight;
1773
        }
1774280×
        out.push(batch);
1775280×
        Ok(out)
1776280×
    }
1777
1778344×
    pub fn push(&self, remote_name: &str, force: bool) -> Result<Vec<RefResult>> {
1779344×
        let bookmark = self.config.bookmark.clone();
1780344×
        self.push_bookmark(remote_name, &bookmark, force)
1781344×
    }
1782
1783
    /// `push` for a named bookmark (`oxy push -b NAME`).
1784352×
    pub fn push_bookmark(
1785352×
        &self,
1786352×
        remote_name: &str,
1787352×
        bookmark: &str,
1788352×
        force: bool,
1789352×
    ) -> Result<Vec<RefResult>> {
1790352×
        self.push_bookmark_with(remote_name, bookmark, force, &mut |_| {})
1791352×
    }
1792
1793
    /// `push_bookmark`, saying how a long push is going: one line per
1794
    /// batch sent (oxygen#95).
1795360×
    pub fn push_bookmark_with(
1796360×
        &self,
1797360×
        remote_name: &str,
1798360×
        bookmark: &str,
1799360×
        force: bool,
1800360×
        progress: crate::store::Progress,
1801360×
    ) -> Result<Vec<RefResult>> {
1802360×
        let (op_ref, op) = self.head_op()?;
1803360×
        if !op.view.bookmarks.contains_key(bookmark) {
18040×
            return fail(format!("no bookmark {bookmark}"));
1805360×
        }
1806360×
        let bookmark = bookmark.to_string();
1807360×
        let local_heads = self.heads_of(&op.view, &bookmark)?;
1808
        // pre-push hook: may veto before the remote is contacted.
1809360×
        self.run_hook(
1810360×
            "pre-push",
1811360×
            &format!(
1812
                "{{\"event\":\"pre-push\",\"remote\":{},\"bookmark\":{},\"heads\":{}}}",
1813360×
                json_str(remote_name),
1814360×
                json_str(&bookmark),
1815732×
                json_array(local_heads.iter().map(|h| json_str(&h.to_hex())))
1816
            ),
18171×
        )?;
1818359×
        let remote = self.open_named_remote(remote_name)?;
1819359×
        self.check_remote_id(remote.as_ref(), remote_name, true)?;
1820356×
        let resp = remote.refs()?;
1821356×
        let local_policy = self.current_policy()?.map(|(_, sp)| sp);
1822356×
        let resp_policy_serial = resp.policy.as_ref().map(|p| p.policy.serial);
1823356×
        let policy_to_send = match (&local_policy, &resp.policy) {
182420×
            (Some(l), Some(r)) if l.policy.serial > r.policy.serial => Some(l.clone()),
18258×
            (Some(l), None) => Some(l.clone()),
18269×
            (Some(l), Some(r)) if l.policy.serial < r.policy.serial => {
18271×
                return fail(format!(
1828
                    "{remote_name} has a newer policy (serial {}); fetch first",
1829
                    r.policy.serial
1830
                ));
1831
            }
1832336×
            _ => None,
1833
        };
1834355×
        let remote_has_policy = resp.policy.is_some() || policy_to_send.is_some();
1835355×
        if remote_has_policy && self.secret_key()?.is_none() {
18361×
            return fail(format!("{remote_name} enforces a trust policy; pushing needs an identity (`oxy identity create`)"));
1837354×
        }
1838354×
        let remote_tags: BTreeSet<String> = resp.tags.iter().map(|t| t.name.clone()).collect();
1839354×
        let tags_to_send: Vec<crate::protocol::TagRef> = self
1840354×
            .tag_refs()?
1841354×
            .into_iter()
1842354×
            .filter(|t| !remote_tags.contains(&t.name))
1843354×
            .collect();
1844354×
        let refs = resp.refs;
1845354×
        let empty = self.store.put_state(&StateObject::new(vec![]))?;
1846354×
        let (remote_state, remote_heads, last_serial) =
1847354×
            match refs.iter().find(|r| r.name == bookmark) {
1848337×
                Some(r) => (r.state, r.heads.clone(), r.last_serial),
184917×
                None => (empty, vec![], None),
1850
            };
1851354×
        let new_state = self
1852354×
            .store
1853354×
            .put_state(&StateObject::new(local_heads.clone()))?;
1854
        // A push that carries a policy re-attests the bookmark even when its
1855
        // state is unchanged, so the current attestation is always by a key
1856
        // the current policy authorizes (a revoked key's attestation would
1857
        // otherwise be refused by every later fetch).
1858354×
        if new_state == remote_state && policy_to_send.is_none() && tags_to_send.is_empty() {
18598×
            return Ok(vec![RefResult {
18608×
                name: bookmark,
18618×
                ok: true,
18628×
                reason: "already up to date".into(),
18638×
            }]);
1864346×
        }
1865414×
        for h in &remote_heads {
1866414×
            if !self.store.has(h) {
186765×
                return fail(format!(
1868
                    "the remote has change {} which you do not; pull first (`oxy pull {remote_name}`), then push again",
186965×
                    h.short(12)
1870
                ));
1871349×
            }
1872
        }
1873281×
        if !force {
1874281×
            let closure = self.closure_in_store(&local_heads)?;
1875317×
            if let Some(h) = remote_heads.iter().find(|h| !closure.contains(h)) {
18760×
                return fail(format!("not a fast-forward: remote head {} is not in your state; pull first or use --force", h.short(12)));
1877281×
            }
18780×
        }
1879281×
        let to_send = self.negotiate(&remote_heads, &local_heads)?;
1880281×
        let key = self.secret_key()?.map(|(k, _)| k);
1881281×
        if let Some(key) = key {
1882
            // Seal this author's changes that carry no valid authenticator,
1883
            // and adopt anonymous ones (zero author, spec §14.4). The seals
1884
            // are stored, so each batch's bundle carries those it needs.
188576×
            let mut unsigned = Vec::new();
188676×
            for h in &to_send {
188770×
                let c = self.store.get_change_meta(h)?;
188870×
                if (c.author == key || c.author.is_zero())
188966×
                    && !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. })
189038×
                {
189138×
                    unsigned.push(*h);
189238×
                }
1893
            }
189476×
            for chunk in unsigned.chunks(crate::attest::MAX_SEAL_HASHES) {
189535×
                self.sign_attest(&Attest::seal(self.id()?, chunk.to_vec()))?;
1896
            }
1897205×
        }
1898
        // A long history goes in batches, in dependency order, so neither
1899
        // side holds it whole and no request passes the remote's limit:
1900
        // all but the last carry objects alone; the last moves the bookmark
1901
        // (oxygen#95).
1902280×
        let batches = self.push_batches(&to_send)?;
1903280×
        let (last, earlier) = batches.split_last().expect("at least one batch");
1904280×
        for (i, batch) in earlier.iter().enumerate() {
19055×
            progress(&format!(
19065×
                "part {} of {}: {} changes",
19075×
                i + 1,
19085×
                batches.len(),
19095×
                batch.len()
19105×
            ));
19115×
            let results = remote.push(self.bundle_for(batch)?, vec![], None, vec![])?;
19125×
            match results.iter().find(|r| r.name == OBJECTS_RESULT) {
19135×
                Some(r) if r.ok => {}
19140×
                Some(r) => {
19150×
                    return fail(format!(
1916
                        "{remote_name} refused part {} of the push: {}",
19170×
                        i + 1,
1918
                        r.reason
1919
                    ))
1920
                }
1921
                None => {
19220×
                    return fail(format!(
1923
                        "{remote_name} does not take a push in parts, and this one is too long for one: update the server's oxy"
1924
                    ))
1925
                }
1926
            }
1927
        }
1928280×
        if batches.len() > 1 {
19291×
            progress(&format!(
19301×
                "part {} of {}: {} changes",
19311×
                batches.len(),
19321×
                batches.len(),
19331×
                last.len()
19341×
            ));
1935279×
        }
1936280×
        let mut bundle = self.bundle_for(last)?;
1937280×
        bundle.objects.push((
1938280×
            ObjectType::State,
1939280×
            StateObject::new(local_heads.clone()).encode(),
1940280×
        ));
1941280×
        let attestation = match key {
194275×
            Some(_) => Some(self.sign_attest(&Attest {
194375×
                repo: self.id()?,
194475×
                hlc: Hlc::now(),
194575×
                body: AttestBody::Ref {
194675×
                    name: bookmark.clone(),
194775×
                    old_state: remote_state,
194875×
                    new_state,
194975×
                    serial: next_serial(last_serial),
195075×
                },
19511×
            })?),
1952205×
            None => None,
1953
        };
1954
        // Whoever signed the move, and whom a delegate acts for, travel with
1955
        // it: a receiver that has never seen a change by this delegate could
1956
        // not otherwise check its signature (oxygenhub#129).
1957279×
        if attestation.is_some() {
1958148×
            for k in [self.signing_key(), self.config.author_key]
195974×
                .into_iter()
196074×
                .flatten()
1961
            {
1962148×
                if let Ok(id) = self.identity(&k) {
1963148×
                    let body = id.encode();
1964148×
                    if !bundle
1965148×
                        .objects
1966148×
                        .iter()
1967169×
                        .any(|(t, b)| *t == ObjectType::Identity && *b == body)
196815×
                    {
196915×
                        bundle.objects.push((ObjectType::Identity, body));
1970133×
                    }
19710×
                }
1972
            }
1973205×
        }
1974279×
        let serial = attestation.as_ref().and_then(|a| match a.attest.body {
197574×
            AttestBody::Ref { serial, .. } => Some(serial),
19760×
            _ => None,
197774×
        });
1978279×
        let sent_serial = policy_to_send.as_ref().map(|p| p.policy.serial);
1979
        // Tag states must be present on the remote: bundle their changes too.
1980279×
        for t in &tags_to_send {
19816×
            let extra = self.negotiate(&remote_heads, &t.heads)?;
19826×
            let extra: Vec<ChangeHash> =
19836×
                extra.into_iter().filter(|h| !to_send.contains(h)).collect();
19846×
            if !extra.is_empty() {
19850×
                let more = self.bundle_for(&extra)?;
19860×
                bundle.changes.extend(more.changes);
19870×
                bundle.hashes.extend(more.hashes);
19880×
                bundle.attests.extend(more.attests);
19890×
                bundle.objects.extend(more.objects);
19906×
            }
19916×
            bundle.objects.push((
19926×
                ObjectType::State,
19936×
                StateObject::new(t.heads.clone()).encode(),
19946×
            ));
1995
        }
1996279×
        if let Some(l) = &policy_to_send {
199719×
            let remote_serial = resp_policy_serial;
199819×
            bundle.policies = self
199919×
                .policy_history()?
200019×
                .into_iter()
200144×
                .filter(|sp| {
200244×
                    remote_serial.is_none_or(|r| sp.policy.serial > r)
200321×
                        && sp.policy.serial < l.policy.serial
200444×
                })
200519×
                .collect();
2006260×
        }
2007279×
        let results = remote.push(
2008279×
            bundle,
2009279×
            vec![RefUpdate {
2010279×
                name: bookmark.clone(),
2011279×
                old_state: remote_state,
2012279×
                new_state,
2013279×
                force,
2014279×
                attestation,
2015279×
            }],
2016279×
            policy_to_send,
2017279×
            tags_to_send,
20187×
        )?;
201919×
        if let (Some(serial), true) = (
2020272×
            sent_serial,
2021278×
            results.iter().any(|r| r.ok && r.name == "policy"),
2022
        ) {
202319×
            self.record_policy_serial(remote_name, serial)?;
2024253×
        }
2025291×
        if results.iter().any(|r| r.ok && r.name == bookmark) {
2026250×
            let mut view = op.view.clone();
2027250×
            view.remotes
2028250×
                .entry(remote_name.to_string())
2029250×
                .or_default()
2030250×
                .insert(bookmark.clone(), new_state);
2031250×
            self.record_op(&[op_ref], &format!("oxy push {remote_name}"), view)?;
2032247×
            if let Some(s) = serial {
203358×
                self.record_serial(remote_name, &bookmark, s)?;
2034189×
            }
203522×
        }
2036267×
        Ok(results)
2037360×
    }
2038
2039
    /// Clone a remote into `dest`: same repository ID, `origin` remote,
2040
    /// bookmark set to the remote's, working copy checked out.
2041227×
    pub fn clone(url: &str, dest: &Path) -> Result<Repo> {
2042227×
        Self::clone_with(url, dest, None, &[])
2043227×
    }
2044
2045
    /// Clone, leaving blobs above `max_blob` bytes to be fetched on first
2046
    /// read, and materializing only `sparse` paths (which implies lazy blobs
2047
    /// unless a size is given, so out-of-scope binaries are never fetched).
2048235×
    pub fn clone_with(
2049235×
        url: &str,
2050235×
        dest: &Path,
2051235×
        max_blob: Option<u64>,
2052235×
        sparse: &[String],
2053235×
    ) -> Result<Repo> {
2054235×
        Self::clone_full(url, dest, max_blob, sparse, None)
2055235×
    }
2056
2057
    /// Shallow clone (spec §17): a checkpoint of the bookmark's heads plus the
2058
    /// changes within `depth` steps of them; the rest is the boundary.
20591×
    pub fn clone_shallow(
20601×
        url: &str,
20611×
        dest: &Path,
20621×
        depth: u32,
20631×
        max_blob: Option<u64>,
20641×
        sparse: &[String],
20651×
    ) -> Result<Repo> {
20661×
        Self::clone_full(url, dest, max_blob, sparse, Some(depth))
20671×
    }
2068
2069236×
    fn clone_full(
2070236×
        url: &str,
2071236×
        dest: &Path,
2072236×
        max_blob: Option<u64>,
2073236×
        sparse: &[String],
2074236×
        depth: Option<u32>,
2075236×
    ) -> Result<Repo> {
2076236×
        let options = crate::credentials::Credentials::user().options_for(url);
2077236×
        Self::clone_using(url, dest, max_blob, sparse, depth, &options)
2078236×
    }
2079
2080
    /// Clone a subrepo with the credentials the parent would use for its
2081
    /// hub (see [`Repo::credentials_for`]). Nothing is recorded in the
2082
    /// subrepo: it finds the same ones again through its parent.
20839×
    pub(crate) fn clone_as_subrepo(&self, url: &str, dest: &Path) -> Result<Repo> {
20849×
        let options = self.credentials_for(url);
20859×
        Self::clone_using(url, dest, None, &[], None, &options)
20869×
    }
2087
2088
    /// The name of a remote of this repository on the same hub (scheme,
2089
    /// host and port) as `url`.
209021×
    fn remote_on_same_hub(&self, url: &str) -> Option<String> {
209125×
        let origin = |u: &str| -> Option<String> {
209225×
            let (scheme, rest) = u.split_once("://")?;
20938×
            if !matches!(scheme, "http" | "https") {
20940×
                return None;
20958×
            }
20968×
            let host = rest.split('/').next()?;
20978×
            Some(format!(
20988×
                "{}://{}",
20998×
                scheme.to_ascii_lowercase(),
21008×
                host.to_ascii_lowercase()
21018×
            ))
210225×
        };
210321×
        let want = origin(url)?;
21044×
        self.config
21054×
            .remotes
21064×
            .iter()
21074×
            .find(|(_, u)| origin(u).as_deref() == Some(want.as_str()))
21084×
            .map(|(name, _)| name.clone())
210921×
    }
2110
2111
    /// The token and CA to reach `url` with: those of a remote of this
2112
    /// repository on the same hub (scheme, host and port), else the
2113
    /// environment's (`OXY_TOKEN`, `OXY_CA`), the user's credential store
2114
    /// and enclosing checkouts', as a clone would use them.
211521×
    pub(crate) fn credentials_for(&self, url: &str) -> RemoteOptions {
211621×
        match self.remote_on_same_hub(url) {
21171×
            Some(name) => RemoteOptions {
21181×
                repo_id: None,
21191×
                cache_dir: None,
21201×
                ..self.remote_options(&name)
21211×
            },
2122
            None => {
212320×
                let mut options = RemoteOptions::default();
212420×
                self.fill_credentials(url, &mut options);
212520×
                options
2126
            }
2127
        }
212821×
    }
2129
2130245×
    fn clone_using(
2131245×
        url: &str,
2132245×
        dest: &Path,
2133245×
        max_blob: Option<u64>,
2134245×
        sparse: &[String],
2135245×
        depth: Option<u32>,
2136245×
        options: &RemoteOptions,
2137245×
    ) -> Result<Repo> {
2138245×
        let remote = open_remote(url, options)?;
2139245×
        let id = remote.repo_id()?;
2140241×
        if id == crate::protocol::UNBOUND_ID {
21412×
            return fail(format!(
2142
                "{url} is empty and unbound: push a repository to it first"
2143
            ));
2144239×
        }
2145239×
        let mut repo = Repo::init_with_id(dest, id)?;
2146239×
        repo.config
2147239×
            .set("remote.origin.url", &crate::protocol::stored_url(url))?;
2148239×
        if let Some(m) = max_blob {
21492×
            repo.config.set("filter.max-blob", &m.to_string())?;
2150237×
        } else if !sparse.is_empty() {
21511×
            repo.config.set("filter.max-blob", "0")?;
2152236×
        }
2153239×
        if !sparse.is_empty() {
21541×
            repo.config.set("filter.paths", &sparse.join(","))?;
2155238×
        }
2156239×
        repo.save_config()?;
2157239×
        if let Some(depth) = depth {
21581×
            repo.fetch_shallow("origin", depth)?;
2159
        } else {
2160238×
            repo.fetch("origin")?;
2161
        }
2162238×
        let (op_ref, op) = repo.head_op()?;
2163
        // The remote's default bookmark, else ours (main), else its only one.
2164238×
        let theirs = op.view.remotes.get("origin").cloned().unwrap_or_default();
2165
        // Asked of the remote as now configured, so a store-backed one
2166
        // answers from the mirror the fetch just filled.
2167238×
        let advertised = repo
2168238×
            .open_named_remote("origin")
2169238×
            .ok()
2170238×
            .and_then(|r| r.refs().ok())
2171238×
            .and_then(|r| r.default_bookmark);
2172238×
        let bookmark = advertised
2173238×
            .filter(|b| theirs.contains_key(b))
2174238×
            .or_else(|| {
21757×
                theirs
21767×
                    .contains_key(&repo.config.bookmark)
21777×
                    .then(|| repo.config.bookmark.clone())
21787×
            })
2179238×
            .or_else(|| {
21800×
                (theirs.len() == 1)
21810×
                    .then(|| theirs.keys().next().cloned())
21820×
                    .flatten()
21830×
            })
2184238×
            .unwrap_or_else(|| repo.config.bookmark.clone());
2185238×
        let initial = repo.config.bookmark.clone();
2186238×
        if bookmark != repo.config.bookmark {
21871×
            fs::create_dir_all(repo.workspace_bookmark_path().parent().unwrap())?;
21881×
            write_atomic(&repo.workspace_bookmark_path(), bookmark.as_bytes())?;
21891×
            repo.config.bookmark = bookmark.clone();
2190237×
        }
2191238×
        if let Some(state) = theirs.get(&bookmark).copied() {
2192238×
            let heads = repo.store.get_state(&state)?.heads;
2193238×
            let mut view = op.view.clone();
2194
            // The empty bookmark `init` made goes when another is checked out.
2195238×
            if initial != bookmark && repo.heads_of(&view, &initial)?.is_empty() {
21961×
                view.bookmarks.remove(&initial);
2197237×
            }
2198238×
            view.bookmarks.insert(bookmark, state);
2199238×
            repo.record_op(&[op_ref], &format!("oxy clone {url}"), view)?;
2200238×
            repo.checkout(&heads, false, "oxy clone (checkout)")?;
22010×
        }
2202238×
        Ok(repo)
2203245×
    }
2204
}
2205
2206
/// The serial of a reference's next attestation: one more than the last the
2207
/// remote reports, or, for a bookmark the remote does not have, the clock in
2208
/// milliseconds, so a bookmark created again after a deletion goes past the
2209
/// deletion's serial the remote keeps.
221075×
fn next_serial(last: Option<u64>) -> u64 {
221175×
    match last {
221247×
        Some(l) => l + 1,
221328×
        None => Hlc::now().wall_ms.max(1),
2214
    }
221575×
}