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