98286406 Andy McCall 1 month ago ↶
1
2
3
use super :: * ;
4
7aa2dc93 Andy McCall 1 month ago ↶
5
6
# [ derive ( Clone, Debug, Default ) ]
7
pub struct PushReport {
8
pub results : Vec< RefResult> ,
9
10
pub updated : Vec< RefUpdated> ,
11
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
83e0677b Andy McCall 1 month ago ↶
23
24
# [ derive ( Debug, Clone, PartialEq, Eq, Default ) ]
25
pub struct FetchReport {
26
27
pub refs : Vec< ( String, Vec< ChangeHash> , usize ) > ,
132b884b Andy McCall 1 month ago ↶
28
29
pub new_tags : usize ,
83e0677b Andy McCall 1 month ago ↶
30
31
32
pub unverified : bool ,
4b9d0677 Andy McCall 19 days ago ↶
33
34
35
36
pub log_notes : Vec< String> ,
83e0677b Andy McCall 1 month ago ↶
37
}
38
98286406 Andy McCall 1 month ago ↶
39
# [ derive ( Debug, Clone, PartialEq, Eq ) ]
40
pub struct PullReport {
14edbaaf Andy McCall 1 month ago ↶
41
98286406 Andy McCall 1 month ago ↶
42
pub heads : Vec< ChangeHash> ,
14edbaaf Andy McCall 1 month ago ↶
43
98286406 Andy McCall 1 month ago ↶
44
pub changed : bool ,
45
pub checked_out : bool ,
14edbaaf Andy McCall 1 month ago ↶
46
47
48
pub others : Vec< String> ,
98286406 Andy McCall 1 month ago ↶
49
}
50
cb8b8082 Andy McCall 27 days ago ↶
51
52
53
pub const OBJECTS_RESULT : & str = " objects" ;
54
98286406 Andy McCall 1 month ago ↶
55
impl Repo {
56
57
58
fn ref_attest_path ( & self , bookmark : & str ) -> PathBuf {
54cd08a1 Andy McCall 1 month ago ↶
59
self .oxy.join ( " attest" ) .join ( name_file ( bookmark) )
98286406 Andy McCall 1 month ago ↶
60
}
61
62
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) => {
75167672 Andy McCall 1 month ago
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
) ) )
98286406 Andy McCall 1 month ago ↶
75
}
76
Err ( e) if e.kind ( ) = = std:: io:: ErrorKind:: NotFound => Ok ( None ) ,
77
Err ( e) => Err ( e.into ( ) ) ,
78
}
79
}
80
9f9e72b9 Andy McCall 1 month ago ↶
81
pub ( crate ) fn set_ref_attest ( & self , bookmark : & str , r : & ObjectRef) -> Result< ( ) > {
98286406 Andy McCall 1 month ago ↶
82
fs:: create_dir_all( self .oxy.join ( " attest" ) ) ? ;
83
write_atomic ( & self .ref_attest_path ( bookmark) , r.to_hex ( ) .as_bytes ( ) )
84
}
85
83e0677b Andy McCall 1 month ago ↶
86
pub fn remote_refs ( & self ) -> Result< RefsResponse> {
98286406 Andy McCall 1 month ago ↶
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) ) ;
f53cbe7a Andy McCall 20 days ago ↶
96
let accepted = match & attestation {
97
Some ( a) => self .acceptance_of ( & a.attest.hash ( ) ) ? ,
98
None => None ,
99
} ;
75167672 Andy McCall 1 month ago
100
out.push ( RemoteRef {
101
name: name.clone ( ) ,
102
state: * state,
103
heads: self .store.get_state ( state) ? .heads,
104
attestation,
105
last_serial,
f53cbe7a Andy McCall 20 days ago ↶
106
accepted,
4b9d0677 Andy McCall 19 days ago ↶
107
logged: None ,
75167672 Andy McCall 1 month ago
108
} ) ;
98286406 Andy McCall 1 month ago ↶
109
}
28fc23d7 Andy McCall 1 month ago ↶
110
let policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp ) ;
75167672 Andy McCall 1 month ago
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,
14edbaaf Andy McCall 1 month ago ↶
125
default_bookmark: Some ( self .config.bookmark.clone ( ) ) ,
4b9d0677 Andy McCall 19 days ago ↶
126
log: None ,
75167672 Andy McCall 1 month ago
127
} )
98286406 Andy McCall 1 month ago ↶
128
}
129
130
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) ;
69816d68 Andy McCall 1 month ago ↶
139
stack.extend ( self .store.get_change_meta ( & h) ? .deps) ;
98286406 Andy McCall 1 month ago ↶
140
}
141
Ok ( seen)
142
}
143
2863456a Andy McCall 1 month ago ↶
144
145
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
174
pub fn boundary ( & self ) -> BTreeSet< ChangeHash> {
75167672 Andy McCall 1 month ago
175
fs:: read_to_string( self .shallow_path ( ) )
176
.map ( | t | t.lines ( ) .filter_map ( ObjectRef:: from_hex) .collect ( ) )
177
.unwrap_or_default ( )
2863456a Andy McCall 1 month ago ↶
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
}
75167672 Andy McCall 1 month ago
189
let text: String = boundary
190
.iter ( )
191
.map ( | h | format! ( " {} \n " , h.to_hex ( ) ) )
192
.collect ( ) ;
2863456a Andy McCall 1 month ago ↶
193
write_atomic ( & self .shallow_path ( ) , text.as_bytes ( ) )
194
}
195
196
197
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 {
69816d68 Andy McCall 1 month ago ↶
202
for d in self .store.get_change_meta ( h) ? .deps {
2863456a Andy McCall 1 month ago ↶
203
if ! self .store.has ( & d) {
204
boundary.insert ( d) ;
205
}
206
}
207
}
208
self .write_boundary ( & boundary)
209
}
210
211
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
98286406 Andy McCall 1 month ago ↶
228
229
pub fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > {
230
let have = self .closure_in_store ( have) ? ;
2863456a Andy McCall 1 month ago ↶
231
232
233
let boundary = self .boundary ( ) ;
98286406 Andy McCall 1 month ago ↶
234
let mut out = Vec :: new( ) ;
235
let mut done: BTreeSet< ChangeHash> = have.clone ( ) ;
2863456a Andy McCall 1 month ago ↶
236
done.extend ( boundary.iter ( ) .copied ( ) ) ;
98286406 Andy McCall 1 month ago ↶
237
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 ) ) ;
69816d68 Andy McCall 1 month ago ↶
252
for d in self .store.get_change_meta ( & h) ? .deps {
98286406 Andy McCall 1 month ago ↶
253
if ! done.contains ( & d) {
254
stack.push ( ( d, false ) ) ;
255
}
256
}
257
}
258
Ok ( out)
259
}
260
261
262
263
pub fn bundle_for ( & self , changes : & [ChangeHash]) -> Result< Bundle> {
b3c5a59a Andy McCall 1 month ago ↶
264
self .bundle_for_filtered ( changes, None )
265
}
266
267
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
289
75167672 Andy McCall 1 month ago
290
pub fn bundle_for_filtered (
291
& self ,
292
changes : & [ChangeHash],
293
max_blob : Option< u64 > ,
294
) -> Result< Bundle> {
2c75672f Andy McCall 1 month ago ↶
295
self .bundle_of ( changes, max_blob, false )
296
}
297
298
299
300
301
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> {
98286406 Andy McCall 1 month ago ↶
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 {
2c75672f Andy McCall 1 month ago ↶
321
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 {
0f93f49c Andy McCall 1 month ago ↶
326
if let crate :: objects:: TextRef:: Blob( r, _ , _ ) = t {
2c75672f Andy McCall 1 month ago ↶
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
} ;
98286406 Andy McCall 1 month ago ↶
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) {
75167672 Andy McCall 1 month ago
345
if let Ok ( blob) =
346
Blob:: decode( & self .store.get_typed ( b, ObjectType:: Blob) ? )
347
{
b3c5a59a Andy McCall 1 month ago ↶
348
if max_blob.is_some_and ( | m | blob.length > m ) {
349
continue ;
350
}
351
objects.insert ( * b) ;
98286406 Andy McCall 1 month ago ↶
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) ;
2c75672f Andy McCall 1 month ago ↶
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
}
98286406 Andy McCall 1 month ago ↶
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
}
f0a517d2 Andy McCall 20 days ago ↶
381
self .attach_certificates ( & mut bundle) ? ;
98286406 Andy McCall 1 month ago ↶
382
Ok ( bundle)
383
}
384
f0a517d2 Andy McCall 20 days ago ↶
385
386
387
388
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
446
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
464
465
466
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
98286406 Andy McCall 1 month ago ↶
494
495
496
pub fn import_bundle ( & self , bundle : & Bundle, strict : bool ) -> Result< Vec< ChangeHash> > {
2863456a Andy McCall 1 month ago ↶
497
self .import_bundle_inner ( bundle, strict, false )
498
}
499
500
501
502
pub fn import_bundle_shallow ( & self , bundle : & Bundle, strict : bool ) -> Result< Vec< ChangeHash> > {
503
self .import_bundle_inner ( bundle, strict, true )
504
}
505
75167672 Andy McCall 1 month ago
506
fn import_bundle_inner (
507
& self ,
508
bundle : & Bundle,
509
strict : bool ,
510
shallow : bool ,
511
) -> Result< Vec< ChangeHash> > {
8f10a07a Andy McCall 1 month ago ↶
512
let batch = self .store.begin_batch ( ) ? ;
513
let result = self .import_bundle_batched ( bundle, strict, shallow) ;
514
batch.finish ( ) ? ;
515
result
516
}
517
75167672 Andy McCall 1 month ago
518
fn import_bundle_batched (
519
& self ,
520
bundle : & Bundle,
521
strict : bool ,
522
shallow : bool ,
523
) -> Result< Vec< ChangeHash> > {
2c75672f Andy McCall 1 month ago ↶
524
for ( ty, _ ) in & bundle.objects {
525
if ! matches! (
526
ty,
75167672 Andy McCall 1 month ago
527
ObjectType:: Chunk
2c75672f Andy McCall 1 month ago ↶
528
| ObjectType:: Blob
529
| ObjectType:: Identity
530
| ObjectType:: State
531
| ObjectType:: Checkpoint
532
) {
533
return fail ( format! ( " bundle may not carry {} objects" , ty.name ( ) ) ) ;
98286406 Andy McCall 1 month ago ↶
534
}
535
}
2c75672f Andy McCall 1 month ago ↶
536
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) ? ;
f0a517d2 Andy McCall 20 days ago ↶
543
self .adopt_certificates ( & bundle.certificates) ? ;
98286406 Andy McCall 1 month ago ↶
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
}
598009a7 Andy McCall 1 month ago ↶
555
556
557
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
} ;
2c75672f Andy McCall 1 month ago ↶
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 ( ) ;
2863456a Andy McCall 1 month ago ↶
569
let boundary = self .boundary ( ) ;
98286406 Andy McCall 1 month ago ↶
570
let mut imported = Vec :: new( ) ;
2c75672f Andy McCall 1 month ago ↶
571
572
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 {
0f93f49c Andy McCall 1 month ago ↶
586
if let crate :: objects:: TextRef:: Blob( r, _ , _ ) = t {
2c75672f Andy McCall 1 month ago ↶
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
}
598009a7 Andy McCall 1 month ago ↶
602
for ( ( c, sigs) , h) in bundle.changes.iter ( ) .zip ( hashes) {
98286406 Andy McCall 1 month ago ↶
603
for d in & c.deps {
75167672 Andy McCall 1 month ago
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
) ) ;
98286406 Andy McCall 1 month ago ↶
611
}
612
}
598009a7 Andy McCall 1 month ago ↶
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
}
98286406 Andy McCall 1 month ago ↶
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 { .. } ) {
75167672 Andy McCall 1 month ago
628
return fail ( format! (
629
" change {} is not authenticated by its author" ,
630
h.short ( 12 )
631
) ) ;
98286406 Andy McCall 1 month ago ↶
632
}
633
}
634
}
635
Ok ( imported)
636
}
637
638
639
640
84b9e9ac Andy McCall 1 month ago ↶
641
642
7ba155f2 Andy McCall 1 month ago ↶
643
644
645
eae59556 Andy McCall 1 month ago ↶
646
pub ( crate ) fn delegate_authorized (
647
& self ,
648
p : & crate:: policy:: Policy,
649
signer : & KeyId,
650
reference : & str ,
7ba155f2 Andy McCall 1 month ago ↶
651
at_ms : u64 ,
eae59556 Andy McCall 1 month ago ↶
652
) -> bool {
84b9e9ac Andy McCall 1 month ago ↶
653
if p.is_revoked ( signer) {
654
return false ;
655
}
eae59556 Andy McCall 1 month ago ↶
656
let Some ( rule) = p.rule_for ( reference) else {
657
return false ;
658
} ;
84b9e9ac Andy McCall 1 month ago ↶
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 ( ) )
7ba155f2 Andy McCall 1 month ago ↶
664
.is_some_and ( | id | id.delegates_to ( signer, Scope:: REFS , at_ms) )
84b9e9ac Andy McCall 1 month ago ↶
665
} )
666
}
667
75167672 Andy McCall 1 month ago
668
fn check_ref_attest (
669
& self ,
670
sa : & SignedAttest,
671
name : & str ,
672
old_state : & ObjectRef,
673
new_state : & ObjectRef,
674
last_serial : Option< u64 > ,
675
allow_equal : bool ,
676
) -> std:: result:: Result< u64 , String> {
677
let AttestBody:: Ref {
678
name: n,
679
old_state: o,
680
new_state: nw,
681
serial,
682
} = & sa.attest.body
683
else {
98286406 Andy McCall 1 month ago ↶
684
return Err ( " attestation is not a ref attestation" .into ( ) ) ;
685
} ;
686
if n ! = name | | o ! = old_state | | nw ! = new_state {
687
return Err ( " attestation does not describe this update" .into ( ) ) ;
688
}
689
if sa.attest.repo ! = self .id ( ) .map_err ( | e | e.to_string ( ) ) ? {
690
return Err ( " attestation is for a different repository" .into ( ) ) ;
691
}
692
if let Some ( last) = last_serial {
693
if * serial < last | | ( * serial = = last & & ! allow_equal) {
75167672 Andy McCall 1 month ago
694
return Err ( format! (
695
" attestation serial {serial} is not greater than the last seen {last} "
696
) ) ;
98286406 Andy McCall 1 month ago ↶
697
}
698
}
699
let r = sa.attest.hash ( ) ;
700
let sig = sa.sigs.first ( ) .ok_or ( " attestation carries no signature" ) ? ;
75167672 Andy McCall 1 month ago
701
let id = self
702
.store
703
.get_identity ( & sig.key)
704
.map_err ( | _| format! ( " unknown key {} " , sig.key.short ( 12 ) ) ) ? ;
98286406 Andy McCall 1 month ago ↶
705
verify_signature ( & id, sig, Domain:: Attest, & r) .map_err ( | e | e.to_string ( ) ) ? ;
706
Ok ( * serial)
707
}
708
83e0677b Andy McCall 1 month ago ↶
709
98286406 Andy McCall 1 month ago ↶
710
83e0677b Andy McCall 1 month ago ↶
711
712
713
75167672 Andy McCall 1 month ago
714
pub fn receive_push (
715
& self ,
716
bundle : Bundle,
717
updates : Vec< RefUpdate> ,
718
policy : Option< SignedPolicy> ,
719
tags : Vec< crate:: protocol:: TagRef> ,
720
) -> Result< Vec< RefResult> > {
119bbf50 Andy McCall 1 month ago ↶
721
Ok ( self
722
.receive_push_report ( bundle, updates, policy, tags) ?
723
.results )
7aa2dc93 Andy McCall 1 month ago ↶
724
}
725
726
727
728
729
pub fn receive_push_report (
730
& self ,
731
bundle : Bundle,
732
updates : Vec< RefUpdate> ,
733
policy : Option< SignedPolicy> ,
734
tags : Vec< crate:: protocol:: TagRef> ,
735
) -> Result< PushReport> {
736
let mut report = PushReport:: default( ) ;
737
let results = & mut report.results;
83e0677b Andy McCall 1 month ago ↶
738
if let Some ( sp) = policy {
28fc23d7 Andy McCall 1 month ago ↶
739
match self .adopt_policy_chain ( & bundle.policies, & sp, " push received" ) {
83e0677b Andy McCall 1 month ago ↶
740
Ok ( ( ) ) => {
75167672 Andy McCall 1 month ago
741
results.push ( RefResult {
742
name: " policy" .into ( ) ,
743
ok: true ,
744
reason: format! ( " serial {} " , sp.policy.serial) ,
745
} ) ;
83e0677b Andy McCall 1 month ago ↶
746
}
747
Err ( e) => {
75167672 Andy McCall 1 month ago
748
results.push ( RefResult {
749
name: " policy" .into ( ) ,
750
ok: false ,
751
reason: e,
752
} ) ;
83e0677b Andy McCall 1 month ago ↶
753
for u in updates {
75167672 Andy McCall 1 month ago
754
results.push ( RefResult {
755
name: u.name,
756
ok: false ,
757
reason: " policy update was rejected" .into ( ) ,
758
} ) ;
83e0677b Andy McCall 1 month ago ↶
759
}
7aa2dc93 Andy McCall 1 month ago ↶
760
return Ok ( report) ;
83e0677b Andy McCall 1 month ago ↶
761
}
762
}
763
}
764
let policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp.policy ) ;
cb8b8082 Andy McCall 27 days ago ↶
765
766
767
let objects_only = updates.is_empty ( ) & & tags.is_empty ( ) ;
7aa2dc93 Andy McCall 1 month ago ↶
768
match self .import_bundle ( & bundle, policy.is_some ( ) ) {
cb8b8082 Andy McCall 27 days ago ↶
769
Ok ( imported) => {
770
if objects_only {
771
results.push ( RefResult {
772
name: OBJECTS_RESULT .into ( ) ,
773
ok: true ,
774
reason: format! ( " {} changes stored" , imported.len ( ) ) ,
775
} ) ;
776
}
777
report.imported = imported;
778
}
7aa2dc93 Andy McCall 1 month ago ↶
779
Err ( e) => {
cb8b8082 Andy McCall 27 days ago ↶
780
if objects_only {
781
results.push ( RefResult {
782
name: OBJECTS_RESULT .into ( ) ,
783
ok: false ,
784
reason: e.to_string ( ) ,
785
} ) ;
786
}
7aa2dc93 Andy McCall 1 month ago ↶
787
for u in updates {
788
results.push ( RefResult {
789
name: u.name,
790
ok: false ,
791
reason: e.to_string ( ) ,
792
} ) ;
793
}
794
return Ok ( report) ;
83e0677b Andy McCall 1 month ago ↶
795
}
796
}
98286406 Andy McCall 1 month ago ↶
797
let ( op_ref, op) = self .head_op ( ) ? ;
798
let mut view = op.view.clone ( ) ;
799
let mut names = Vec :: new( ) ;
800
for u in updates {
83e0677b Andy McCall 1 month ago ↶
801
let outcome = self .apply_update ( & mut view, & u, policy.as_ref ( ) ) ;
98286406 Andy McCall 1 month ago ↶
802
let ( ok, reason) = match outcome {
803
Ok ( ( ) ) => ( true , String :: new( ) ) ,
804
Err ( e) => ( false , e) ,
805
} ;
806
if ok {
807
names.push ( u.name.clone ( ) ) ;
119bbf50 Andy McCall 1 month ago ↶
808
let heads = self
809
.store
810
.get_state ( & u.new_state)
811
.map ( | s | s.heads )
812
.unwrap_or_default ( ) ;
813
report.updated.push ( RefUpdated {
814
name: u.name.clone ( ) ,
815
old_state: u.old_state,
816
new_state: u.new_state,
817
heads,
818
} ) ;
98286406 Andy McCall 1 month ago ↶
819
}
75167672 Andy McCall 1 month ago
820
results.push ( RefResult {
821
name: u.name,
822
ok,
823
reason,
824
} ) ;
98286406 Andy McCall 1 month ago ↶
825
}
132b884b Andy McCall 1 month ago ↶
826
for t in & tags {
827
let name = format! ( " tags/{} " , t.name) ;
828
match self .adopt_tag ( & mut view, t, policy.as_ref ( ) ) {
829
Ok ( ( ) ) => {
830
names.push ( name.clone ( ) ) ;
75167672 Andy McCall 1 month ago
831
results.push ( RefResult {
832
name,
833
ok: true ,
834
reason: String :: new( ) ,
835
} ) ;
132b884b Andy McCall 1 month ago ↶
836
}
75167672 Andy McCall 1 month ago
837
Err ( e) => results.push ( RefResult {
838
name,
839
ok: false ,
840
reason: e,
841
} ) ,
132b884b Andy McCall 1 month ago ↶
842
}
843
}
98286406 Andy McCall 1 month ago ↶
844
if ! names.is_empty ( ) {
75167672 Andy McCall 1 month ago
845
self .record_op (
846
& [ op_ref] ,
847
& format! ( " push received: {} " , names.join ( " " ) ) ,
848
view,
849
) ? ;
98286406 Andy McCall 1 month ago ↶
850
}
7aa2dc93 Andy McCall 1 month ago ↶
851
Ok ( report)
98286406 Andy McCall 1 month ago ↶
852
}
853
be451356 Andy McCall 1 month ago ↶
854
855
856
857
858
fn record_update_attest (
859
& self ,
860
u : & RefUpdate,
861
sa : & SignedAttest,
862
) -> std:: result:: Result< ( ) , String> {
863
let name = & u.name;
864
let last = self
865
.latest_ref_attest ( name)
866
.map_err ( | e | format! ( " reading the last attestation of {name:?} : {e} " ) ) ?
867
.and_then ( | ( _, a ) | match a.attest.body {
868
AttestBody:: Ref { serial, .. } => Some ( serial) ,
869
_ => None ,
870
} ) ;
871
self .check_ref_attest ( sa, name, & u.old_state, & u.new_state, last, false ) ? ;
872
let store = | e : & dyn std:: fmt:: Display| format! ( " storing the attestation of {name:?} : {e} " ) ;
873
let r = self .store.put_attest ( & sa.attest) .map_err ( | e | store ( & e) ) ? ;
874
for sig in & sa.sigs {
875
self .store.put_signature ( & r, sig) .map_err ( | e | store ( & e) ) ? ;
f53cbe7a Andy McCall 20 days ago ↶
876
}
877
self .set_ref_attest ( name, & r) .map_err ( | e | store ( & e) ) ? ;
878
879
let certified = sa
880
.sigs
881
.first ( )
882
.is_some_and ( | s | self .store.certificate_of ( & s.key) .ok ( ) .flatten ( ) .is_some ( ) ) ;
883
if certified {
884
self .record_acceptance ( & r) .map_err ( | e | store ( & e) ) ? ;
be451356 Andy McCall 1 month ago ↶
885
}
f53cbe7a Andy McCall 20 days ago ↶
886
Ok ( ( ) )
be451356 Andy McCall 1 month ago ↶
887
}
888
f53cbe7a Andy McCall 20 days ago ↶
889
890
891
pub ( crate ) fn record_acceptance ( & self , r : & ObjectRef) -> Result< ( ) > {
892
let Some ( acceptor) = & self .acceptor else {
893
return Ok ( ( ) ) ;
894
} ;
895
let ( id, sk) = ( & acceptor.0 , & acceptor.1 ) ;
896
let attest = Attest {
897
repo: self .id ( ) ? ,
898
hlc: Hlc:: now( ) ,
899
body: AttestBody:: Accepted { attest: * r } ,
900
} ;
901
let sig = sk.sign ( Domain:: Attest, & id.hash ( ) , & attest.hash ( ) ) ;
902
self .store.put_identity ( id) ? ;
903
let a = self .store.put_attest ( & attest) ? ;
904
self .store.put_signature ( & a, & sig) ? ;
905
self .store.index_add_pub ( " accepted" , r, & a) ? ;
906
Ok ( ( ) )
907
}
908
909
910
pub fn acceptance_of ( & self , r : & ObjectRef) -> Result< Option< SignedAttest> > {
911
let Some ( a) = self .store.index_get_pub ( " accepted" , r) ? .into_iter ( ) .next ( ) else {
912
return Ok ( None ) ;
913
} ;
914
Ok ( Some ( SignedAttest {
915
attest: self .store.get_attest ( & a) ? ,
916
sigs: self .store.signatures ( & a) ? ,
917
} ) )
918
}
919
920
921
922
923
924
pub ( crate ) fn certified_time (
925
& self ,
926
signer : & KeyId,
927
sa : & SignedAttest,
928
accepted : Option< & SignedAttest> ,
929
) -> u64 {
930
let own = sa.attest.hlc.wall_ms;
931
let Some ( acc) = accepted else {
932
return own;
933
} ;
934
let Ok ( Some ( c) ) = self .store.certificate_of ( signer) else {
935
return own;
936
} ;
937
let Ok ( cert) = self .store.get_certificate ( & c) else {
938
return own;
939
} ;
940
let AttestBody:: Accepted { attest } = & acc.attest.body else {
941
return own;
942
} ;
943
let Some ( sig) = acc.sigs.iter ( ) .find ( | s | s.key = = cert.cert.issuer ) else {
944
return own;
945
} ;
946
let Ok ( issuer) = self .store.get_identity ( & cert.cert.issuer) else {
947
return own;
948
} ;
949
if * attest = = sa.attest.hash ( )
950
& & verify_signature ( & issuer, sig, Domain:: Attest, & acc.attest.hash ( ) ) .is_ok ( )
951
{
952
acc.attest.hlc.wall_ms
953
} else {
954
own
955
}
956
}
957
f8c69576 Andy McCall 1 month ago ↶
958
8c30d814 Andy McCall 1 month ago ↶
959
960
961
962
f8c69576 Andy McCall 1 month ago ↶
963
964
965
fn apply_deletion (
966
& self ,
967
view : & mut View,
968
u : & RefUpdate,
969
policy : Option< & crate:: policy:: Policy> ,
970
) -> std:: result:: Result< ( ) , String> {
971
let Some ( current) = view.bookmarks.get ( & u.name) .copied ( ) else {
972
return Err ( format! ( " no bookmark {:?} to delete" , u.name) ) ;
973
} ;
974
if current ! = u.old_state {
975
return Err ( " stale: the remote bookmark has moved; pull first" .into ( ) ) ;
976
}
8c30d814 Andy McCall 1 month ago ↶
977
if ! self .is_bare ( ) & & u.name = = self .config.bookmark {
f8c69576 Andy McCall 1 month ago ↶
978
return Err ( format! (
8c30d814 Andy McCall 1 month ago ↶
979
" {:?} is this repository's default bookmark (checked out there) and cannot be deleted" ,
f8c69576 Andy McCall 1 month ago ↶
980
u.name
981
) ) ;
982
}
983
if let Some ( p) = policy {
984
let Some ( sa) = & u.attestation else {
985
return Err ( format! (
986
" deleting {:?} requires a signed reference attestation under this repository's policy" ,
987
u.name
988
) ) ;
989
} ;
990
let signer = sa
991
.sigs
992
.first ( )
993
.map ( | s | s.key )
994
.ok_or ( " attestation carries no signature" ) ? ;
f0a517d2 Andy McCall 20 days ago ↶
995
996
let now = Hlc:: now( ) .wall_ms;
7ba155f2 Andy McCall 1 month ago ↶
997
if ! p.authorizes ( & signer, & u.name)
998
& & ! self .delegate_authorized ( p, & signer, & u.name, sa.attest.hlc.wall_ms)
f0a517d2 Andy McCall 20 days ago ↶
999
& & ! self .certified_authorized ( p, & signer, & u.name, Scope:: REFS , now)
7ba155f2 Andy McCall 1 month ago ↶
1000
{
f8c69576 Andy McCall 1 month ago ↶
1001
return Err ( format! (
1002
" key {} may not delete {:?} under this repository's policy" ,
1003
signer.short ( 12 ) ,
1004
u.name
1005
) ) ;
1006
}
1007
}
1008
if let Some ( sa) = & u.attestation {
be451356 Andy McCall 1 month ago ↶
1009
self .record_update_attest ( u, sa) ? ;
f8c69576 Andy McCall 1 month ago ↶
1010
}
1011
view.bookmarks.remove ( & u.name) ;
1012
Ok ( ( ) )
1013
}
1014
75167672 Andy McCall 1 month ago
1015
fn apply_update (
1016
& self ,
1017
view : & mut View,
1018
u : & RefUpdate,
1019
policy : Option< & crate:: policy:: Policy> ,
1020
) -> std:: result:: Result< ( ) , String> {
83e0677b Andy McCall 1 month ago ↶
1021
if u.name = = " policy" {
75167672 Andy McCall 1 month ago
1022
return Err (
1023
" the policy reference is updated by sending a policy, not a bookmark update" .into ( ) ,
1024
) ;
83e0677b Andy McCall 1 month ago ↶
1025
}
75167672 Andy McCall 1 month ago
1026
let empty = self
1027
.store
1028
.put_state ( & StateObject:: new( vec! [ ] ) )
1029
.map_err ( | e | e.to_string ( ) ) ? ;
f8c69576 Andy McCall 1 month ago ↶
1030
if u.new_state.is_zero ( ) {
1031
return self .apply_deletion ( view, u, policy) ;
1032
}
98286406 Andy McCall 1 month ago ↶
1033
let current = view.bookmarks.get ( & u.name) .copied ( ) .unwrap_or ( empty) ;
1034
if current ! = u.old_state {
1035
return Err ( " stale: the remote bookmark has moved; pull first" .into ( ) ) ;
1036
}
75167672 Andy McCall 1 month ago
1037
let new_state = self
1038
.store
1039
.get_state ( & u.new_state)
1040
.map_err ( | _| " new state object is missing" .to_string ( ) ) ? ;
98286406 Andy McCall 1 month ago ↶
1041
for h in & new_state.heads {
1042
if ! self .store.has ( h) {
1043
return Err ( format! ( " head {} is missing" , h.short ( 12 ) ) ) ;
1044
}
1045
}
1046
if ! u.force {
75167672 Andy McCall 1 month ago
1047
let old_heads = self
1048
.store
1049
.get_state ( & current)
1050
.map_err ( | e | e.to_string ( ) ) ?
1051
.heads;
1052
let closure = self
1053
.closure_in_store ( & new_state.heads)
1054
.map_err ( | e | e.to_string ( ) ) ? ;
98286406 Andy McCall 1 month ago ↶
1055
if let Some ( h) = old_heads.iter ( ) .find ( | h | ! closure.contains ( h) ) {
75167672 Andy McCall 1 month ago
1056
return Err ( format! (
1057
" not a fast-forward: current head {} would be dropped (use --force)" ,
1058
h.short ( 12 )
1059
) ) ;
98286406 Andy McCall 1 month ago ↶
1060
}
1061
}
83e0677b Andy McCall 1 month ago ↶
1062
if let Some ( p) = policy {
1063
let Some ( sa) = & u.attestation else {
75167672 Andy McCall 1 month ago
1064
return Err ( format! (
1065
" {:?} requires a signed reference attestation under this repository's policy" ,
1066
u.name
1067
) ) ;
83e0677b Andy McCall 1 month ago ↶
1068
} ;
75167672 Andy McCall 1 month ago
1069
let signer = sa
1070
.sigs
1071
.first ( )
1072
.map ( | s | s.key )
1073
.ok_or ( " attestation carries no signature" ) ? ;
f0a517d2 Andy McCall 20 days ago ↶
1074
1075
let now = Hlc:: now( ) .wall_ms;
7ba155f2 Andy McCall 1 month ago ↶
1076
if ! p.authorizes ( & signer, & u.name)
1077
& & ! self .delegate_authorized ( p, & signer, & u.name, sa.attest.hlc.wall_ms)
f0a517d2 Andy McCall 20 days ago ↶
1078
& & ! self .certified_authorized ( p, & signer, & u.name, Scope:: REFS , now)
7ba155f2 Andy McCall 1 month ago ↶
1079
{
75167672 Andy McCall 1 month ago
1080
return Err ( format! (
1081
" key {} may not update {:?} under this repository's policy" ,
1082
signer.short ( 12 ) ,
1083
u.name
1084
) ) ;
83e0677b Andy McCall 1 month ago ↶
1085
}
75167672 Andy McCall 1 month ago
1086
let old_heads = self
1087
.store
1088
.get_state ( & current)
1089
.map_err ( | e | e.to_string ( ) ) ?
1090
.heads;
83e0677b Andy McCall 1 month ago ↶
1091
self .state_allowed ( & old_heads, & new_state.heads, p, & u.name) ? ;
1092
}
98286406 Andy McCall 1 month ago ↶
1093
if let Some ( sa) = & u.attestation {
be451356 Andy McCall 1 month ago ↶
1094
self .record_update_attest ( u, sa) ? ;
98286406 Andy McCall 1 month ago ↶
1095
}
1096
view.bookmarks.insert ( u.name.clone ( ) , u.new_state) ;
1097
Ok ( ( ) )
1098
}
1099
1100
1101
1102
fn remote_url ( & self , name : & str ) -> Result< String> {
75167672 Andy McCall 1 month ago
1103
self .config.remotes.get ( name) .cloned ( ) .ok_or_else ( | | {
42ccf504 Andy McCall 28 days ago ↶
1104
let names: Vec< & str > = self .config.remotes.keys ( ) .map ( String :: as_str) .collect ( ) ;
1105
RepoError:: Msg( match names.as_slice ( ) {
1106
[ ] => format! ( " no remote named {name:?} ; add one with `oxy remote add`" ) ,
1107
_ if name = = " origin" & & names.len ( ) > 1 & & self .config.default_remote.is_none ( ) => format! (
1108
" this repository has no remote named origin, and several others ({} ): name one (`oxy pull {} `), or make one the default with `oxy config core.remote {} `" ,
1109
names.join ( " , " ) ,
1110
names[ 0 ] ,
1111
names[ 0 ]
1112
) ,
1113
_ => format! (
1114
" no remote named {name:?} ; this repository has {} " ,
1115
names.join ( " , " )
1116
) ,
1117
} )
75167672 Andy McCall 1 month ago
1118
} )
98286406 Andy McCall 1 month ago ↶
1119
}
1120
75167672 Andy McCall 1 month ago
1121
1122
1123
05e71ed6 Andy McCall 1 month ago ↶
1124
fn check_remote_id (
1125
& self ,
1126
remote : & dyn crate:: protocol:: Remote,
1127
remote_name : & str ,
1128
allow_unbound : bool ,
1129
) -> Result< ( ) > {
75167672 Andy McCall 1 month ago
1130
let theirs = remote.repo_id ( ) ? ;
1131
if theirs = = self .id ( ) ? {
1132
return Ok ( ( ) ) ;
1133
}
1134
if theirs = = crate :: protocol:: UNBOUND_ID {
1135
if allow_unbound {
1136
return Ok ( ( ) ) ;
1137
}
05e71ed6 Andy McCall 1 month ago ↶
1138
return fail ( format! (
1139
" remote {remote_name:?} is empty and unbound: push to it first"
1140
) ) ;
75167672 Andy McCall 1 month ago
1141
}
05e71ed6 Andy McCall 1 month ago ↶
1142
fail ( format! (
3e5772cc Andy McCall 1 month ago ↶
1143
" remote {remote_name:?} is a different repository (repository IDs differ); check its URL with `oxy remote list`"
05e71ed6 Andy McCall 1 month ago ↶
1144
) )
75167672 Andy McCall 1 month ago
1145
}
1146
ff1f62fe Andy McCall 1 month ago ↶
1147
1148
1149
3e5772cc Andy McCall 1 month ago ↶
1150
pub ( crate ) fn remote_options ( & self , name : & str ) -> RemoteOptions {
ff1f62fe Andy McCall 1 month ago ↶
1151
let url = self .config.remotes.get ( name) .cloned ( ) .unwrap_or_default ( ) ;
1152
let mut options = RemoteOptions {
75167672 Andy McCall 1 month ago
1153
repo_id: self .id ( ) .ok ( ) ,
ff1f62fe Andy McCall 1 month ago ↶
1154
token: self .config.remote_tokens.get ( name) .cloned ( ) ,
1155
ca: self .config.remote_cas.get ( name) .map ( PathBuf:: from) ,
9f9e72b9 Andy McCall 1 month ago ↶
1156
cache_dir: Some ( self .oxy.join ( " remotes" ) .join ( name) .join ( " strata" ) ) ,
68a92484 Andy McCall 23 days ago ↶
1157
.. RemoteOptions:: default( )
ff1f62fe Andy McCall 1 month ago ↶
1158
} ;
1159
self .fill_credentials ( & url, & mut options) ;
1160
options
1161
}
1162
1163
1164
1165
pub fn credentials ( & self ) -> crate:: credentials:: Credentials {
1166
crate :: credentials:: Credentials:: at(
1167
self .user_config
1168
.parent ( )
1169
.map ( Path:: to_path_buf)
1170
.unwrap_or_else ( default_home) ,
1171
)
1172
}
1173
1174
1175
1176
68a92484 Andy McCall 23 days ago ↶
1177
1178
1179
1180
ff1f62fe Andy McCall 1 month ago ↶
1181
fn fill_credentials ( & self , url : & str , options : & mut RemoteOptions) {
1182
if options.token.is_some ( ) & & options.ca.is_some ( ) {
1183
return ;
1184
}
68a92484 Andy McCall 23 days ago ↶
1185
let explicit =
1186
options.token.is_some ( ) | | std:: env:: var( " OXY_TOKEN" ) .is_ok_and ( | t | ! t.is_empty ( ) ) ;
ff1f62fe Andy McCall 1 month ago ↶
1187
let stored = self .credentials ( ) .options_for ( url) ;
1188
options.token = options.token.take ( ) .or ( stored.token) ;
1189
options.ca = options.ca.take ( ) .or ( stored.ca) ;
68a92484 Andy McCall 23 days ago ↶
1190
let enclosing = self .enclosing_credentials ( url) ;
1191
if options.token.is_none ( ) {
1192
options.token = enclosing.iter ( ) .find_map ( | ( t , _) | t.clone ( ) ) ;
1193
}
1194
if options.ca.is_none ( ) {
1195
options.ca = enclosing.iter ( ) .find_map ( | ( _, c ) | c.clone ( ) ) ;
1196
}
1197
if ! explicit {
1198
for t in enclosing.into_iter ( ) .filter_map ( | ( t , _) | t ) {
1199
if options.token.as_ref ( ) ! = Some ( & t) & & ! options.fallback_tokens.contains ( & t) {
1200
options.fallback_tokens.push ( t) ;
1201
}
1202
}
2b5ee371 Andy McCall 1 month ago ↶
1203
}
68a92484 Andy McCall 23 days ago ↶
1204
}
1205
1206
1207
1208
fn enclosing_credentials ( & self , url : & str ) -> Vec< ( Option< String> , Option< PathBuf> ) > {
1209
let mut out = Vec :: new( ) ;
ff1f62fe Andy McCall 1 month ago ↶
1210
let Some ( hub) = crate :: credentials:: hub_of( url) else {
68a92484 Andy McCall 23 days ago ↶
1211
return out;
ff1f62fe Andy McCall 1 month ago ↶
1212
} ;
1213
let mut at = self .root.parent ( ) ;
1214
while let Some ( dir) = at {
1215
let Ok ( cfg) = read_config_file ( & dir.join ( OXY_DIR ) .join ( " config" ) ) else {
1216
break ;
1217
} ;
1218
for ( key, remote_url) in & cfg {
1219
let Some ( name) = key
1220
.strip_prefix ( " remote." )
1221
.and_then ( | k | k.strip_suffix ( " .url" ) )
1222
else {
1223
continue ;
1224
} ;
1225
if crate :: credentials:: hub_of( remote_url) .as_deref ( ) ! = Some ( hub.as_str ( ) ) {
1226
continue ;
1227
}
68a92484 Andy McCall 23 days ago ↶
1228
out.push ( (
1229
cfg.get ( & format! ( " remote.{name} .token" ) ) .cloned ( ) ,
1230
cfg.get ( & format! ( " remote.{name} .ca" ) ) .map ( PathBuf:: from) ,
1231
) ) ;
ff1f62fe Andy McCall 1 month ago ↶
1232
}
1233
at = dir.parent ( ) ;
1234
}
68a92484 Andy McCall 23 days ago ↶
1235
out
98286406 Andy McCall 1 month ago ↶
1236
}
1237
d82e4409 Andy McCall 1 month ago ↶
1238
1239
1240
pub fn push_seals ( & self , remote_name : & str , seals : & [ObjectRef]) -> Result< ( ) > {
1241
let remote = self .open_named_remote ( remote_name) ? ;
1242
self .check_remote_id ( remote.as_ref ( ) , remote_name, false ) ? ;
1243
let mut bundle = Bundle:: default( ) ;
1244
for r in seals {
1245
let attest = self .store.get_attest ( r) ? ;
1246
let sigs = self .store.signatures ( r) ? ;
4213c41d Andy McCall 1 month ago ↶
1247
bundle
1248
.attests
1249
.push ( crate :: protocol:: SignedAttest { attest, sigs } ) ;
d82e4409 Andy McCall 1 month ago ↶
1250
}
1251
let results = remote.push ( bundle, vec! [ ] , None , vec! [ ] ) ? ;
1252
if let Some ( r) = results.iter ( ) .find ( | r | ! r.ok) {
1253
return fail ( format! ( " {} : {} " , r.name, r.reason) ) ;
1254
}
1255
Ok ( ( ) )
1256
}
1257
1258
1259
pub fn remote_for_url ( & self , base : & str ) -> Option< String> {
1260
let base = base.trim_end_matches ( ' /' ) ;
4213c41d Andy McCall 1 month ago ↶
1261
self .config
1262
.remotes
1263
.iter ( )
1264
.find ( | ( _, url ) | url.trim_end_matches ( ' /' ) = = base )
1265
.map ( | ( name , _) | name.clone ( ) )
d82e4409 Andy McCall 1 month ago ↶
1266
}
1267
1268
ff1f62fe Andy McCall 1 month ago ↶
1269
d82e4409 Andy McCall 1 month ago ↶
1270
pub fn open_remote_at ( & self , url : & str ) -> Result< ( String, Box< dyn crate:: protocol:: Remote> ) > {
1271
if let Some ( name) = self .remote_for_url ( url) {
1272
return Ok ( ( name.clone ( ) , self .open_named_remote ( & name) ? ) ) ;
1273
}
1274
let options = RemoteOptions {
1275
repo_id: self .id ( ) .ok ( ) ,
ff1f62fe Andy McCall 1 month ago ↶
1276
.. self .credentials_for ( url)
d82e4409 Andy McCall 1 month ago ↶
1277
} ;
1278
Ok ( ( url.to_string ( ) , open_remote ( url, & options) ? ) )
1279
}
1280
1281
1282
1283
pub fn options_for_url ( & self , url : & str ) -> RemoteOptions {
1284
match self .remote_for_url ( url) {
1285
Some ( name) => self .remote_options ( & name) ,
1286
None => RemoteOptions {
1287
repo_id: self .id ( ) .ok ( ) ,
ff1f62fe Andy McCall 1 month ago ↶
1288
.. self .credentials_for ( url)
d82e4409 Andy McCall 1 month ago ↶
1289
} ,
1290
}
1291
}
1292
98286406 Andy McCall 1 month ago ↶
1293
fn open_named_remote ( & self , name : & str ) -> Result< Box< dyn crate:: protocol:: Remote> > {
2b5ee371 Andy McCall 1 month ago ↶
1294
open_remote ( & self .remote_url ( name) ? , & self .remote_options ( name) )
98286406 Andy McCall 1 month ago ↶
1295
}
1296
1297
fn serial_path ( & self , remote : & str , bookmark : & str ) -> PathBuf {
75167672 Andy McCall 1 month ago
1298
self .oxy
1299
.join ( " remotes" )
1300
.join ( remote)
1301
.join ( format! ( " {bookmark} .serial" ) )
98286406 Andy McCall 1 month ago ↶
1302
}
1303
1304
fn last_seen_serial ( & self , remote : & str , bookmark : & str ) -> Option< u64 > {
75167672 Andy McCall 1 month ago
1305
fs:: read_to_string( self .serial_path ( remote, bookmark) )
1306
.ok ( )
1307
.and_then ( | s | s.trim ( ) .parse ( ) .ok ( ) )
98286406 Andy McCall 1 month ago ↶
1308
}
1309
1310
fn record_serial ( & self , remote : & str , bookmark : & str , serial : u64 ) -> Result< ( ) > {
1311
let p = self .serial_path ( remote, bookmark) ;
1312
fs:: create_dir_all( p.parent ( ) .unwrap ( ) ) ? ;
1313
write_atomic ( & p, serial.to_string ( ) .as_bytes ( ) )
1314
}
1315
83e0677b Andy McCall 1 month ago ↶
1316
fn policy_serial_path ( & self , remote : & str ) -> PathBuf {
1317
self .oxy.join ( " remotes" ) .join ( remote) .join ( " policy.serial" )
1318
}
1319
1320
1321
fn remote_policy_serial ( & self , remote : & str ) -> Option< u64 > {
75167672 Andy McCall 1 month ago
1322
fs:: read_to_string( self .policy_serial_path ( remote) )
1323
.ok ( )
1324
.and_then ( | s | s.trim ( ) .parse ( ) .ok ( ) )
83e0677b Andy McCall 1 month ago ↶
1325
}
1326
1327
fn record_policy_serial ( & self , remote : & str , serial : u64 ) -> Result< ( ) > {
1328
let p = self .policy_serial_path ( remote) ;
1329
fs:: create_dir_all( p.parent ( ) .unwrap ( ) ) ? ;
1330
write_atomic ( & p, serial.to_string ( ) .as_bytes ( ) )
1331
}
1332
b3c5a59a Andy McCall 1 month ago ↶
1333
fn lazy_dir ( & self ) -> PathBuf {
1334
self .oxy.join ( " lazy" )
1335
}
1336
1337
1338
fn register_lazy ( & self , blob : & ObjectRef, remote : & str ) -> Result< ( ) > {
1339
fs:: create_dir_all( self .lazy_dir ( ) ) ? ;
1340
write_atomic ( & self .lazy_dir ( ) .join ( blob.to_hex ( ) ) , remote.as_bytes ( ) )
1341
}
1342
1343
1344
pub fn lazy_remote ( & self , blob : & ObjectRef) -> Option< String> {
75167672 Andy McCall 1 month ago
1345
fs:: read_to_string( self .lazy_dir ( ) .join ( blob.to_hex ( ) ) )
1346
.ok ( )
1347
.map ( | s | s.trim ( ) .to_string ( ) )
b3c5a59a Andy McCall 1 month ago ↶
1348
}
1349
1350
1351
pub ( crate ) fn fetch_lazy_blob ( & self , blob : & ObjectRef) -> Result< ( ) > {
1352
let Some ( remote_name) = self .lazy_remote ( blob) else {
1353
return Err ( RepoError:: Store( StoreError:: NotFound( * blob) ) ) ;
1354
} ;
1355
let remote = self .open_named_remote ( & remote_name) ? ;
1356
let objects = remote.objects ( & [ * blob] ) ? ;
1357
for ( ty, body) in objects {
1358
if matches! ( ty, ObjectType:: Blob | ObjectType:: Chunk) {
1359
self .store.put ( ty, & body) ? ;
1360
}
1361
}
1362
if ! self .store.has ( blob) {
75167672 Andy McCall 1 month ago
1363
return fail ( format! (
1364
" {remote_name} did not send blob {} " ,
1365
blob.short ( 12 )
1366
) ) ;
b3c5a59a Andy McCall 1 month ago ↶
1367
}
1368
let _ = fs:: remove_file( self .lazy_dir ( ) .join ( blob.to_hex ( ) ) ) ;
1369
Ok ( ( ) )
1370
}
1371
1372
1373
1374
fn register_missing_blobs ( & self , changes : & [ChangeHash], remote : & str ) -> Result< usize > {
1375
let mut n = 0 ;
1376
for h in changes {
1377
let c = self .store.get_change ( h) ? ;
1378
for e in & c.edits {
1379
if let Edit:: Replace { old, new, .. } = e {
1380
for b in [ old, new] .into_iter ( ) .flatten ( ) {
1381
if ! self .store.has ( b) {
1382
self .register_lazy ( b, remote) ? ;
1383
n + = 1 ;
1384
}
1385
}
1386
}
1387
}
1388
}
1389
Ok ( n)
1390
}
1391
98286406 Andy McCall 1 month ago ↶
1392
d82e4409 Andy McCall 1 month ago ↶
1393
pub fn known_heads ( & self , view : & View) -> Result< Vec< ChangeHash> > {
98286406 Andy McCall 1 month ago ↶
1394
let mut have: BTreeSet< ChangeHash> = BTreeSet:: new( ) ;
75167672 Andy McCall 1 month ago
1395
for st in view
1396
.bookmarks
1397
.values ( )
1398
.chain ( view.remotes.values ( ) .flat_map ( | b | b.values ( ) ) )
1399
{
98286406 Andy McCall 1 month ago ↶
1400
if let Ok ( s) = self .store.get_state ( st) {
1401
have.extend ( s.heads) ;
1402
}
1403
}
1404
have.extend ( view.workspaces.values ( ) .copied ( ) ) ;
1405
Ok ( have.into_iter ( ) .collect ( ) )
1406
}
1407
1408
83e0677b Andy McCall 1 month ago ↶
1409
pub fn fetch ( & self , remote_name : & str ) -> Result< FetchReport> {
98286406 Andy McCall 1 month ago ↶
1410
let remote = self .open_named_remote ( remote_name) ? ;
75167672 Andy McCall 1 month ago
1411
self .check_remote_id ( remote.as_ref ( ) , remote_name, false ) ? ;
83e0677b Andy McCall 1 month ago ↶
1412
let resp = remote.refs ( ) ? ;
4b9d0677 Andy McCall 19 days ago ↶
1413
1414
let log_notes = self .check_log (
1415
& self .remote_url ( remote_name) ? ,
1416
self .config.remote_cas.get ( remote_name) .map ( Path:: new) ,
1417
& resp,
1418
) ? ;
83e0677b Andy McCall 1 month ago ↶
1419
1420
1421
1422
1423
1424
let local_policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp.policy ) ;
1425
let seen = self .remote_policy_serial ( remote_name) ;
1426
let mut unverified = false ;
1427
match & resp.policy {
1428
Some ( sp) => {
1429
if seen.is_some_and ( | s | sp.policy.serial < s ) {
1430
return fail ( format! ( " {remote_name} presents policy serial {} but presented {} before: refusing the downgrade" , sp.policy.serial, seen.unwrap ( ) ) ) ;
1431
}
1432
match & local_policy {
1433
Some ( l) if sp.policy.serial < l.serial => { }
1434
Some ( l) if sp.policy.serial = = l.serial => {
1435
if sp.policy.hash ( ) ! = l.hash ( ) {
75167672 Andy McCall 1 month ago
1436
return fail ( format! (
1437
" {remote_name} presents a different policy with the same serial {} " ,
1438
l.serial
1439
) ) ;
83e0677b Andy McCall 1 month ago ↶
1440
}
1441
}
28fc23d7 Andy McCall 1 month ago ↶
1442
_ => {
75167672 Andy McCall 1 month ago
1443
self .adopt_policy_chain (
1444
& resp.history,
1445
sp,
1446
& format! ( " oxy fetch {remote_name} " ) ,
1447
)
1448
.map_err ( | e | RepoError:: Msg( format! ( " policy from {remote_name} : {e} " ) ) ) ? ;
83e0677b Andy McCall 1 month ago ↶
1449
}
1450
}
1451
self .record_policy_serial ( remote_name, sp.policy.serial) ? ;
1452
}
1453
None => {
1454
if seen.is_some ( ) {
1455
return fail ( format! ( " {remote_name} presented a policy before but has none now: refusing the downgrade" ) ) ;
1456
}
1457
unverified = true ;
1458
}
1459
}
75167672 Andy McCall 1 month ago
1460
let policy = if unverified {
1461
None
1462
} else {
1463
self .current_policy ( ) ? .map ( | ( _, sp ) | sp.policy )
1464
} ;
98286406 Andy McCall 1 month ago ↶
1465
let ( op_ref, op) = self .head_op ( ) ? ;
1466
let mut view = op.view.clone ( ) ;
1467
let have = self .known_heads ( & view) ? ;
1468
let mut report = Vec :: new( ) ;
b3c5a59a Andy McCall 1 month ago ↶
1469
let max_blob = self .config.max_blob;
83e0677b Andy McCall 1 month ago ↶
1470
for r in resp.refs {
091fef80 Andy McCall 1 month ago ↶
1471
let needed = {
1472
let _t = crate :: timing:: phase( " fetch: negotiate" ) ;
1473
remote.negotiate ( & have, & r.heads) ?
1474
} ;
1475
let bundle = {
1476
let _t = crate :: timing:: phase( " fetch: bundle" ) ;
1477
remote.fetch ( & needed, max_blob) ?
1478
} ;
83e0677b Andy McCall 1 month ago ↶
1479
1480
1481
091fef80 Andy McCall 1 month ago ↶
1482
let imported = {
1483
let _t = crate :: timing:: phase( " fetch: import" ) ;
1484
self .import_bundle ( & bundle, false ) ?
1485
} ;
b3c5a59a Andy McCall 1 month ago ↶
1486
if max_blob.is_some ( ) {
1487
self .register_missing_blobs ( & imported, remote_name) ? ;
1488
}
98286406 Andy McCall 1 month ago ↶
1489
for h in & r.heads {
1490
if ! self .store.has ( h) {
1491
return fail ( format! ( " remote did not send head {} " , h.short ( 12 ) ) ) ;
1492
}
1493
}
1494
let state = self .store.put_state ( & StateObject:: new( r.heads.clone ( ) ) ) ? ;
1495
if state ! = r.state {
75167672 Andy McCall 1 month ago
1496
return fail ( format! (
1497
" remote state {} does not match its heads" ,
1498
r.state.short ( 12 )
1499
) ) ;
98286406 Andy McCall 1 month ago ↶
1500
}
83e0677b Andy McCall 1 month ago ↶
1501
if let Some ( p) = & policy {
1502
let Some ( sa) = & r.attestation else {
75167672 Andy McCall 1 month ago
1503
return fail ( format! (
1504
" {remote_name} /{} has no attestation, which its policy requires" ,
1505
r.name
1506
) ) ;
83e0677b Andy McCall 1 month ago ↶
1507
} ;
75167672 Andy McCall 1 month ago
1508
let signer = sa
1509
.sigs
1510
.first ( )
1511
.map ( | s | s.key )
1512
.ok_or_else ( | | RepoError:: Msg( " attestation carries no signature" .into ( ) ) ) ? ;
f53cbe7a Andy McCall 20 days ago ↶
1513
1514
1515
let at = self .certified_time ( & signer, sa, r.accepted.as_ref ( ) ) ;
7ba155f2 Andy McCall 1 month ago ↶
1516
if ! p.authorizes ( & signer, & r.name)
1517
& & ! self .delegate_authorized ( p, & signer, & r.name, sa.attest.hlc.wall_ms)
f53cbe7a Andy McCall 20 days ago ↶
1518
& & ! self .certified_authorized ( p, & signer, & r.name, Scope:: REFS , at)
3526d339 Andy McCall 1 month ago ↶
1519
{
75167672 Andy McCall 1 month ago
1520
return fail ( format! (
3e5772cc Andy McCall 1 month ago ↶
1521
" {remote_name} /{} : attested by {} , which the policy does not authorize for this bookmark; a policy owner can grant it with `oxy policy grant`" ,
75167672 Andy McCall 1 month ago
1522
r.name,
1523
signer.short ( 12 )
1524
) ) ;
83e0677b Andy McCall 1 month ago ↶
1525
}
1526
}
98286406 Andy McCall 1 month ago ↶
1527
if let Some ( sa) = & r.attestation {
75167672 Andy McCall 1 month ago
1528
let AttestBody:: Ref {
1529
old_state,
1530
new_state,
1531
..
1532
} = & sa.attest.body
1533
else {
98286406 Andy McCall 1 month ago ↶
1534
return fail ( " remote sent a non-ref attestation for a bookmark" ) ;
1535
} ;
1536
let ( old_state, new_state) = ( * old_state, * new_state) ;
1537
let serial = self
75167672 Andy McCall 1 month ago
1538
.check_ref_attest (
1539
sa,
1540
& r.name,
1541
& old_state,
1542
& new_state,
1543
self .last_seen_serial ( remote_name, & r.name) ,
1544
true ,
1545
)
1546
.map_err ( | e | {
1547
RepoError:: Msg( format! ( " attestation for {} /{} : {e} " , remote_name, r.name) )
1548
} ) ? ;
98286406 Andy McCall 1 month ago ↶
1549
if new_state ! = r.state {
75167672 Andy McCall 1 month ago
1550
return fail ( format! (
1551
" attestation for {} /{} does not cover its current state" ,
1552
remote_name, r.name
1553
) ) ;
98286406 Andy McCall 1 month ago ↶
1554
}
1555
let ar = self .store.put_attest ( & sa.attest) ? ;
1556
for sig in & sa.sigs {
1557
self .store.put_signature ( & ar, sig) ? ;
1558
}
1559
self .record_serial ( remote_name, & r.name, serial) ? ;
1560
}
75167672 Andy McCall 1 month ago
1561
view.remotes
1562
.entry ( remote_name.to_string ( ) )
1563
.or_default ( )
1564
.insert ( r.name.clone ( ) , state) ;
98286406 Andy McCall 1 month ago ↶
1565
report.push ( ( r.name, r.heads, imported.len ( ) ) ) ;
1566
}
132b884b Andy McCall 1 month ago ↶
1567
let mut new_tags = 0 ;
1568
for t in & resp.tags {
1569
if view.tags.contains_key ( & t.name) {
1570
continue ;
1571
}
1572
75167672 Andy McCall 1 month ago
1573
let missing: Vec< ChangeHash> = t
1574
.heads
1575
.iter ( )
1576
.filter ( | h | ! self .store.has ( h) )
1577
.copied ( )
1578
.collect ( ) ;
132b884b Andy McCall 1 month ago ↶
1579
if ! missing.is_empty ( ) {
1580
let needed = remote.negotiate ( & self .known_heads ( & view) ? , & missing) ? ;
1581
let bundle = remote.fetch ( & needed, max_blob) ? ;
1582
self .import_bundle ( & bundle, false ) ? ;
1583
}
1584
self .store.put_state ( & StateObject:: new( t.heads.clone ( ) ) ) ? ;
75167672 Andy McCall 1 month ago
1585
self .adopt_tag ( & mut view, t, policy.as_ref ( ) )
1586
.map_err ( | e | RepoError:: Msg( format! ( " {remote_name} {} : {e} " , t.name) ) ) ? ;
132b884b Andy McCall 1 month ago ↶
1587
new_tags + = 1 ;
1588
}
98286406 Andy McCall 1 month ago ↶
1589
if view ! = op.view {
1590
self .record_op ( & [ op_ref] , & format! ( " oxy fetch {remote_name} " ) , view) ? ;
1591
}
6d3eefbc Andy McCall 1 month ago ↶
1592
let _ = self .run_hook (
1593
" post-fetch" ,
1594
& format! (
1595
" {{ \" event\" :\" post-fetch\" ,\" remote\" :{} ,\" bookmarks\" :{} }} " ,
1596
json_str ( remote_name) ,
75167672 Andy McCall 1 month ago
1597
json_array ( report.iter ( ) .map ( | ( n , heads , _) | format! (
1598
" {{ \" name\" :{} ,\" heads\" :{} }} " ,
1599
json_str ( n) ,
1600
json_array ( heads.iter ( ) .map ( | h | json_str ( & h.to_hex ( ) ) ) )
1601
) ) )
6d3eefbc Andy McCall 1 month ago ↶
1602
) ,
1603
) ;
75167672 Andy McCall 1 month ago
1604
Ok ( FetchReport {
1605
refs: report,
1606
unverified,
1607
new_tags,
4b9d0677 Andy McCall 19 days ago ↶
1608
log_notes,
75167672 Andy McCall 1 month ago
1609
} )
98286406 Andy McCall 1 month ago ↶
1610
}
1611
2863456a Andy McCall 1 month ago ↶
1612
1613
1614
pub fn fetch_shallow ( & self , remote_name : & str , depth : u32 ) -> Result< usize > {
1615
let remote = self .open_named_remote ( remote_name) ? ;
75167672 Andy McCall 1 month ago
1616
self .check_remote_id ( remote.as_ref ( ) , remote_name, false ) ? ;
2863456a Andy McCall 1 month ago ↶
1617
let resp = remote.refs ( ) ? ;
1618
let bookmark = self .config.bookmark.clone ( ) ;
1619
if let Some ( sp) = & resp.policy {
75167672 Andy McCall 1 month ago
1620
self .adopt_policy_chain ( & resp.history, sp, " oxy clone --depth" )
1621
.map_err ( | e | RepoError:: Msg( format! ( " policy from {remote_name} : {e} " ) ) ) ? ;
2863456a Andy McCall 1 month ago ↶
1622
self .record_policy_serial ( remote_name, sp.policy.serial) ? ;
1623
}
1624
let Some ( r) = resp.refs.into_iter ( ) .find ( | r | r.name = = bookmark ) else {
1625
return fail ( format! ( " {remote_name} has no bookmark {bookmark:?} " ) ) ;
1626
} ;
1627
if r.heads.is_empty ( ) {
1628
return Ok ( 0 ) ;
1629
}
1630
1631
let cp_bundle = remote.checkpoint ( & r.heads) ? ;
1632
let mut cp_ref = None ;
1633
for ( ty, body) in & cp_bundle.objects {
1634
let put = self .store.put ( * ty, body) ? ;
1635
if * ty = = ObjectType:: Checkpoint {
1636
cp_ref = Some ( put) ;
1637
}
1638
}
75167672 Andy McCall 1 month ago
1639
let Some ( cp_ref) = cp_ref else {
1640
return fail ( format! ( " {remote_name} sent no checkpoint" ) ) ;
1641
} ;
2863456a Andy McCall 1 month ago ↶
1642
self .install_checkpoint ( & bookmark, & cp_ref) ? ;
1643
1644
let wanted = remote.shallow ( & r.heads, depth) ? ;
1645
let bundle = remote.fetch ( & wanted, self .config.max_blob) ? ;
1646
let imported = self .import_bundle_shallow ( & bundle, false ) ? ;
1647
if self .config.max_blob.is_some ( ) {
1648
self .register_missing_blobs ( & imported, remote_name) ? ;
1649
}
1650
self .refresh_boundary ( & imported) ? ;
1651
if let Some ( sa) = & r.attestation {
1652
let ar = self .store.put_attest ( & sa.attest) ? ;
1653
for sig in & sa.sigs {
1654
self .store.put_signature ( & ar, sig) ? ;
1655
}
1656
if let AttestBody:: Ref { serial, .. } = sa.attest.body {
1657
self .record_serial ( remote_name, & r.name, serial) ? ;
1658
}
1659
}
1660
let state = self .store.put_state ( & StateObject:: new( r.heads.clone ( ) ) ) ? ;
1661
let ( op_ref, op) = self .head_op ( ) ? ;
1662
let mut view = op.view.clone ( ) ;
75167672 Andy McCall 1 month ago
1663
view.remotes
1664
.entry ( remote_name.to_string ( ) )
1665
.or_default ( )
1666
.insert ( r.name.clone ( ) , state) ;
1667
self .record_op (
1668
& [ op_ref] ,
1669
& format! ( " oxy fetch {remote_name} --depth {depth} " ) ,
1670
view,
1671
) ? ;
2863456a Andy McCall 1 month ago ↶
1672
Ok ( imported.len ( ) )
1673
}
1674
98286406 Andy McCall 1 month ago ↶
1675
1676
1677
pub fn pull ( & self , remote_name : & str ) -> Result< PullReport> {
6caacfff Andy McCall 1 month ago ↶
1678
let _wc = self .lock_working_copy ( ) ? ;
98286406 Andy McCall 1 month ago ↶
1679
self .fetch ( remote_name) ? ;
1680
let ( op_ref, op) = self .head_op ( ) ? ;
14edbaaf Andy McCall 1 month ago ↶
1681
let current = self .config.bookmark.clone ( ) ;
1682
let mut local_current = self .heads_of ( & op.view, & current) ? ;
1683
local_current.sort ( ) ;
1684
let mut view = op.view.clone ( ) ;
1685
let mut current_heads = local_current.clone ( ) ;
1686
let mut changed = false ;
1687
let mut others = Vec :: new( ) ;
1688
let tracked = op
75167672 Andy McCall 1 month ago
1689
.view
1690
.remotes
1691
.get ( remote_name)
14edbaaf Andy McCall 1 month ago ↶
1692
.cloned ( )
1693
.unwrap_or_default ( ) ;
1694
for ( name, remote_state) in tracked {
1695
1696
1697
let local_state = op.view.bookmarks.get ( & name) .copied ( ) ;
1698
if local_state.is_none ( ) & & name ! = current {
1699
continue ;
1700
}
1701
if local_state = = Some ( remote_state) {
1702
continue ;
1703
}
1704
let local = self .heads_of ( & op.view, & name) ? ;
1705
let remote_heads = self .store.get_state ( & remote_state) ? .heads;
1706
if remote_heads.iter ( ) .all ( | h | local.contains ( h) ) {
1707
continue ;
1708
}
1709
let mut all = local.clone ( ) ;
1710
all.extend ( remote_heads) ;
1711
let st = self .load_state_history ( & all) ? ;
1712
let mut heads = st.maximal ( & all) ;
1713
heads.sort ( ) ;
1714
let mut sorted_local = local;
1715
sorted_local.sort ( ) ;
1716
if heads = = sorted_local {
1717
continue ;
1718
}
1719
view.bookmarks.insert (
1720
name.clone ( ) ,
1721
self .store.put_state ( & StateObject:: new( heads.clone ( ) ) ) ? ,
1722
) ;
1723
if name = = current {
1724
changed = true ;
1725
current_heads = heads;
1726
} else {
1727
others.push ( name) ;
1728
}
1729
}
f6dccc45 Andy McCall 28 days ago ↶
1730
if changed | | ! others.is_empty ( ) {
1731
self .record_op ( & [ op_ref] , & format! ( " oxy pull {remote_name} " ) , view) ? ;
98286406 Andy McCall 1 month ago ↶
1732
}
f6dccc45 Andy McCall 28 days ago ↶
1733
1734
1735
1736
let checked_out = self .follow_bookmark ( & current_heads, & current, " oxy pull" ) ? ;
75167672 Andy McCall 1 month ago
1737
Ok ( PullReport {
f6dccc45 Andy McCall 28 days ago ↶
1738
heads: if changed {
1739
current_heads
1740
} else {
1741
local_current
1742
} ,
14edbaaf Andy McCall 1 month ago ↶
1743
changed,
75167672 Andy McCall 1 month ago
1744
checked_out,
14edbaaf Andy McCall 1 month ago ↶
1745
others,
75167672 Andy McCall 1 month ago
1746
} )
f6dccc45 Andy McCall 28 days ago ↶
1747
}
1748
1749
1750
1751
1752
1753
1754
pub fn working_copy_behind ( & self ) -> Result< Option< usize > > {
1755
let ( _ , wc) = self .working_copy ( ) ? ;
1756
let mut base = wc.deps.clone ( ) ;
1757
base.sort ( ) ;
1758
let mut heads = self .bookmark_heads ( ) ? ;
1759
heads.sort ( ) ;
1760
if base = = heads | | heads.is_empty ( ) {
1761
return Ok ( None ) ;
1762
}
1763
let state = self .load_state_history ( & heads) ? ;
1764
if ! base.iter ( ) .all ( | h | state.contains ( h) ) {
1765
return Ok ( None ) ;
1766
}
1767
let had = if base.is_empty ( ) {
1768
0
1769
} else {
1770
self .load_state_history ( & base) ? .len ( )
1771
} ;
1772
Ok ( Some ( state.len ( ) .saturating_sub ( had) ) .filter ( | n | * n > 0 ) )
1773
}
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
pub fn follow_bookmark (
1785
& self ,
1786
heads : & [ChangeHash],
1787
bookmark : & str ,
1788
command : & str ,
1789
) -> Result< bool > {
1790
let snap = self .snapshot ( command) ? ;
1791
let clean = snap.wc.tree_ops.is_empty ( ) & & snap.wc.edits.is_empty ( ) ;
1792
let mut base = snap.base.maximal ( & snap.wc.deps) ;
1793
base.sort ( ) ;
1794
let mut target = heads.to_vec ( ) ;
1795
target.sort ( ) ;
1796
if base = = target {
1797
return Ok ( false ) ;
1798
}
1799
let state = self .load_state_history ( heads) ? ;
1800
if ! base.iter ( ) .all ( | h | state.contains ( h) ) {
1801
1802
return Ok ( false ) ;
1803
}
1804
if clean {
1805
self .checkout ( heads, false , & format! ( " {command} (checkout)" ) ) ? ;
1806
Ok ( true )
1807
} else {
1808
Ok ( self
1809
.checkout_carry ( heads, bookmark, & format! ( " {command} (checkout --carry)" ) )
1810
.is_ok ( ) )
1811
}
98286406 Andy McCall 1 month ago ↶
1812
}
1813
1814
1815
f8c69576 Andy McCall 1 month ago ↶
1816
1817
1818
1819
pub fn push_delete ( & self , remote_name : & str , bookmark : & str ) -> Result< Vec< RefResult> > {
1820
let remote = self .open_named_remote ( remote_name) ? ;
1821
self .check_remote_id ( remote.as_ref ( ) , remote_name, true ) ? ;
1822
let resp = remote.refs ( ) ? ;
1823
let Some ( r) = resp.refs.iter ( ) .find ( | r | r.name = = bookmark ) else {
1824
return fail ( format! ( " {remote_name} has no bookmark {bookmark} " ) ) ;
1825
} ;
1826
if resp.policy.is_some ( ) & & self .secret_key ( ) ? .is_none ( ) {
1827
return fail ( format! ( " {remote_name} enforces a trust policy; deleting needs an identity (`oxy identity create`)" ) ) ;
1828
}
1829
let attestation = match self .secret_key ( ) ? {
1830
Some ( _ ) => Some ( self .sign_attest ( & Attest {
1831
repo: self .id ( ) ? ,
1832
hlc: Hlc:: now( ) ,
1833
body: AttestBody:: Ref {
1834
name: bookmark.to_string ( ) ,
1835
old_state: r.state,
1836
new_state: ObjectRef:: ZERO ,
1837
serial: r.last_serial.unwrap_or ( 0 ) + 1 ,
1838
} ,
1839
} ) ? ) ,
1840
None => None ,
1841
} ;
1842
let results = remote.push (
1843
Bundle:: default( ) ,
1844
vec! [ RefUpdate {
1845
name: bookmark.to_string ( ) ,
1846
old_state: r.state,
1847
new_state: ObjectRef:: ZERO ,
1848
force: true ,
1849
attestation,
1850
} ] ,
1851
None ,
1852
vec! [ ] ,
1853
) ? ;
1854
if results.iter ( ) .any ( | x | x.ok & & x.name = = bookmark ) {
1855
let ( op_ref, op) = self .head_op ( ) ? ;
1856
let mut view = op.view.clone ( ) ;
1857
if let Some ( m) = view.remotes.get_mut ( remote_name) {
1858
m.remove ( bookmark) ;
1859
}
1860
self .record_op (
1861
& [ op_ref] ,
1862
& format! ( " oxy push {remote_name} --delete {bookmark} " ) ,
1863
view,
1864
) ? ;
1865
}
1866
Ok ( results)
1867
}
1868
34fad4a9 Andy McCall 26 days ago ↶
1869
1870
1871
1872
1873
pub fn remove_remote ( & mut self , name : & str ) -> Result< ( ) > {
1874
if ! self .config.remotes.contains_key ( name) {
1875
return fail ( format! ( " no remote {name} : `oxy remote list` shows them" ) ) ;
1876
}
1877
let prefix = format! ( " remote.{name} ." ) ;
1878
let keys: Vec< String> = self
1879
.config
1880
.repo_layer ( )
1881
.keys ( )
1882
.filter ( | k | k.starts_with ( & prefix) )
1883
.cloned ( )
1884
.collect ( ) ;
1885
for k in keys {
1886
self .config.set ( & k, " " ) ? ;
1887
}
1888
self .config.remotes.remove ( name) ;
1889
self .save_config ( ) ? ;
1890
let ( op_ref, op) = self .head_op ( ) ? ;
1891
if op.view.remotes.contains_key ( name) {
1892
let mut view = op.view.clone ( ) ;
1893
view.remotes.remove ( name) ;
1894
self .record_op ( & [ op_ref] , & format! ( " oxy remote remove {name} " ) , view) ? ;
1895
}
1896
let mirror = self .oxy.join ( " remotes" ) .join ( name) ;
1897
if mirror.is_dir ( ) {
1898
fs:: remove_dir_all( & mirror) ? ;
1899
}
1900
Ok ( ( ) )
1901
}
1902
cb8b8082 Andy McCall 27 days ago ↶
1903
1904
1905
1906
fn push_batches ( & self , changes : & [ChangeHash]) -> Result< Vec< Vec< ChangeHash> > > {
1907
let blob_len = | r : & ObjectRef| -> u64 {
1908
self .store
1909
.get_typed ( r, ObjectType:: Blob)
1910
.ok ( )
1911
.and_then ( | b | Blob:: decode( & b) .ok ( ) )
1912
.map_or ( 0 , | b | b.length )
1913
} ;
1914
let ( mut out, mut batch, mut bytes) = ( Vec :: new( ) , Vec :: new( ) , 0 u64 ) ;
1915
for h in changes {
1916
let raw = self .store.get_typed ( h, ObjectType:: Change) ? ;
1917
let ( c, texts) = Change:: decode_skeleton( & raw, * h) ? ;
1918
let mut weight = raw.len ( ) as u64 ;
1919
for t in & texts {
1920
if let crate :: objects:: TextRef:: Blob( r, _ , _ ) = t {
1921
weight + = blob_len ( r) ;
1922
}
1923
}
1924
for e in & c.edits {
1925
if let Edit:: Replace { new: Some ( b) , .. } = e {
1926
weight + = blob_len ( b) ;
1927
}
1928
}
1929
if ! batch.is_empty ( ) & & bytes + weight > self .config.push_batch_bytes {
1930
out.push ( std:: mem:: take( & mut batch) ) ;
1931
bytes = 0 ;
1932
}
1933
batch.push ( * h) ;
1934
bytes + = weight;
1935
}
1936
out.push ( batch) ;
1937
Ok ( out)
1938
}
1939
98286406 Andy McCall 1 month ago ↶
1940
pub fn push ( & self , remote_name : & str , force : bool ) -> Result< Vec< RefResult> > {
6d3eefbc Andy McCall 1 month ago ↶
1941
let bookmark = self .config.bookmark.clone ( ) ;
7b9809e6 Andy McCall 1 month ago ↶
1942
self .push_bookmark ( remote_name, & bookmark, force)
1943
}
1944
1945
75167672 Andy McCall 1 month ago
1946
pub fn push_bookmark (
cb8b8082 Andy McCall 27 days ago ↶
1947
& self ,
1948
remote_name : & str ,
1949
bookmark : & str ,
1950
force : bool ,
1951
) -> Result< Vec< RefResult> > {
1952
self .push_bookmark_with ( remote_name, bookmark, force, & mut | _ | { } )
1953
}
1954
1955
1956
1957
pub fn push_bookmark_with (
75167672 Andy McCall 1 month ago
1958
& self ,
1959
remote_name : & str ,
1960
bookmark : & str ,
1961
force : bool ,
cb8b8082 Andy McCall 27 days ago ↶
1962
progress : crate:: store:: Progress,
75167672 Andy McCall 1 month ago
1963
) -> Result< Vec< RefResult> > {
7b9809e6 Andy McCall 1 month ago ↶
1964
let ( op_ref, op) = self .head_op ( ) ? ;
1965
if ! op.view.bookmarks.contains_key ( bookmark) {
1966
return fail ( format! ( " no bookmark {bookmark} " ) ) ;
1967
}
1968
let bookmark = bookmark.to_string ( ) ;
6d3eefbc Andy McCall 1 month ago ↶
1969
let local_heads = self .heads_of ( & op.view, & bookmark) ? ;
1970
1971
self .run_hook (
1972
" pre-push" ,
75167672 Andy McCall 1 month ago
1973
& format! (
1974
" {{ \" event\" :\" pre-push\" ,\" remote\" :{} ,\" bookmark\" :{} ,\" heads\" :{} }} " ,
1975
json_str ( remote_name) ,
1976
json_str ( & bookmark) ,
1977
json_array ( local_heads.iter ( ) .map ( | h | json_str ( & h.to_hex ( ) ) ) )
1978
) ,
6d3eefbc Andy McCall 1 month ago ↶
1979
) ? ;
98286406 Andy McCall 1 month ago ↶
1980
let remote = self .open_named_remote ( remote_name) ? ;
75167672 Andy McCall 1 month ago
1981
self .check_remote_id ( remote.as_ref ( ) , remote_name, true ) ? ;
83e0677b Andy McCall 1 month ago ↶
1982
let resp = remote.refs ( ) ? ;
1983
let local_policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp ) ;
28fc23d7 Andy McCall 1 month ago ↶
1984
let resp_policy_serial = resp.policy.as_ref ( ) .map ( | p | p.policy.serial ) ;
83e0677b Andy McCall 1 month ago ↶
1985
let policy_to_send = match ( & local_policy, & resp.policy) {
1986
( Some ( l) , Some ( r) ) if l.policy.serial > r.policy.serial => Some ( l.clone ( ) ) ,
1987
( Some ( l) , None ) => Some ( l.clone ( ) ) ,
1988
( Some ( l) , Some ( r) ) if l.policy.serial < r.policy.serial => {
75167672 Andy McCall 1 month ago
1989
return fail ( format! (
1990
" {remote_name} has a newer policy (serial {} ); fetch first" ,
1991
r.policy.serial
1992
) ) ;
83e0677b Andy McCall 1 month ago ↶
1993
}
1994
_ => None ,
1995
} ;
1996
let remote_has_policy = resp.policy.is_some ( ) | | policy_to_send.is_some ( ) ;
1997
if remote_has_policy & & self .secret_key ( ) ? .is_none ( ) {
1998
return fail ( format! ( " {remote_name} enforces a trust policy; pushing needs an identity (`oxy identity create`)" ) ) ;
1999
}
132b884b Andy McCall 1 month ago ↶
2000
let remote_tags: BTreeSet< String> = resp.tags.iter ( ) .map ( | t | t.name.clone ( ) ) .collect ( ) ;
75167672 Andy McCall 1 month ago
2001
let tags_to_send: Vec< crate:: protocol:: TagRef> = self
2002
.tag_refs ( ) ?
2003
.into_iter ( )
2004
.filter ( | t | ! remote_tags.contains ( & t.name) )
2005
.collect ( ) ;
83e0677b Andy McCall 1 month ago ↶
2006
let refs = resp.refs;
98286406 Andy McCall 1 month ago ↶
2007
let empty = self .store.put_state ( & StateObject:: new( vec! [ ] ) ) ? ;
75167672 Andy McCall 1 month ago
2008
let ( remote_state, remote_heads, last_serial) =
2009
match refs.iter ( ) .find ( | r | r.name = = bookmark ) {
2010
Some ( r) => ( r.state, r.heads.clone ( ) , r.last_serial) ,
2011
None => ( empty, vec! [ ] , None ) ,
2012
} ;
2013
let new_state = self
2014
.store
2015
.put_state ( & StateObject:: new( local_heads.clone ( ) ) ) ? ;
83e0677b Andy McCall 1 month ago ↶
2016
2017
2018
2019
132b884b Andy McCall 1 month ago ↶
2020
if new_state = = remote_state & & policy_to_send.is_none ( ) & & tags_to_send.is_empty ( ) {
75167672 Andy McCall 1 month ago
2021
return Ok ( vec! [ RefResult {
2022
name: bookmark,
2023
ok: true ,
2024
reason: " already up to date" .into ( ) ,
2025
} ] ) ;
98286406 Andy McCall 1 month ago ↶
2026
}
2027
for h in & remote_heads {
2028
if ! self .store.has ( h) {
75167672 Andy McCall 1 month ago
2029
return fail ( format! (
3e5772cc Andy McCall 1 month ago ↶
2030
" the remote has change {} which you do not; pull first (`oxy pull {remote_name} `), then push again" ,
75167672 Andy McCall 1 month ago
2031
h.short ( 12 )
2032
) ) ;
98286406 Andy McCall 1 month ago ↶
2033
}
2034
}
2035
if ! force {
2036
let closure = self .closure_in_store ( & local_heads) ? ;
2037
if let Some ( h) = remote_heads.iter ( ) .find ( | h | ! closure.contains ( h) ) {
2038
return fail ( format! ( " not a fast-forward: remote head {} is not in your state; pull first or use --force" , h.short ( 12 ) ) ) ;
2039
}
2040
}
2041
let to_send = self .negotiate ( & remote_heads, & local_heads) ? ;
2042
let key = self .secret_key ( ) ? .map ( | ( k , _) | k ) ;
2043
if let Some ( key) = key {
05e71ed6 Andy McCall 1 month ago ↶
2044
cb8b8082 Andy McCall 27 days ago ↶
2045
2046
98286406 Andy McCall 1 month ago ↶
2047
let mut unsigned = Vec :: new( ) ;
2048
for h in & to_send {
cb8b8082 Andy McCall 27 days ago ↶
2049
let c = self .store.get_change_meta ( h) ? ;
a2387d54 Andy McCall 19 days ago ↶
2050
if self .seals_for_author ( & c.author, & key)
75167672 Andy McCall 1 month ago
2051
& & ! matches! ( self .authenticate_change ( h) ? , SignatureStatus:: Valid { .. } )
2052
{
98286406 Andy McCall 1 month ago ↶
2053
unsigned.push ( * h) ;
2054
}
2055
}
2056
for chunk in unsigned.chunks ( crate :: attest:: MAX_SEAL_HASHES ) {
cb8b8082 Andy McCall 27 days ago ↶
2057
self .sign_attest ( & Attest:: seal( self .id ( ) ? , chunk.to_vec ( ) ) ) ? ;
98286406 Andy McCall 1 month ago ↶
2058
}
cb8b8082 Andy McCall 27 days ago ↶
2059
}
2060
2061
2062
2063
2064
let batches = self .push_batches ( & to_send) ? ;
2065
let ( last, earlier) = batches.split_last ( ) .expect ( " at least one batch" ) ;
2066
for ( i, batch) in earlier.iter ( ) .enumerate ( ) {
2067
progress ( & format! (
2068
" part {} of {} : {} changes" ,
2069
i + 1 ,
2070
batches.len ( ) ,
2071
batch.len ( )
2072
) ) ;
2073
let results = remote.push ( self .bundle_for ( batch) ? , vec! [ ] , None , vec! [ ] ) ? ;
2074
match results.iter ( ) .find ( | r | r.name = = OBJECTS_RESULT ) {
2075
Some ( r) if r.ok => { }
2076
Some ( r) => {
2077
return fail ( format! (
2078
" {remote_name} refused part {} of the push: {} " ,
2079
i + 1 ,
2080
r.reason
2081
) )
2082
}
2083
None => {
2084
return fail ( format! (
2085
" {remote_name} does not take a push in parts, and this one is too long for one: update the server's oxy"
2086
) )
2087
}
2088
}
2089
}
2090
if batches.len ( ) > 1 {
2091
progress ( & format! (
2092
" part {} of {} : {} changes" ,
2093
batches.len ( ) ,
2094
batches.len ( ) ,
2095
last.len ( )
2096
) ) ;
98286406 Andy McCall 1 month ago ↶
2097
}
cb8b8082 Andy McCall 27 days ago ↶
2098
let mut bundle = self .bundle_for ( last) ? ;
75167672 Andy McCall 1 month ago
2099
bundle.objects.push ( (
2100
ObjectType:: State,
2101
StateObject:: new( local_heads.clone ( ) ) .encode ( ) ,
2102
) ) ;
98286406 Andy McCall 1 month ago ↶
2103
let attestation = match key {
2104
Some ( _ ) => Some ( self .sign_attest ( & Attest {
2105
repo: self .id ( ) ? ,
2106
hlc: Hlc:: now( ) ,
75167672 Andy McCall 1 month ago
2107
body: AttestBody:: Ref {
2108
name: bookmark.clone ( ) ,
2109
old_state: remote_state,
2110
new_state,
f8c69576 Andy McCall 1 month ago ↶
2111
serial: next_serial ( last_serial) ,
75167672 Andy McCall 1 month ago
2112
} ,
98286406 Andy McCall 1 month ago ↶
2113
} ) ? ) ,
2114
None => None ,
2115
} ;
68a92484 Andy McCall 23 days ago ↶
2116
2117
2118
2119
if attestation.is_some ( ) {
2120
for k in [ self .signing_key ( ) , self .config.author_key]
2121
.into_iter ( )
2122
.flatten ( )
2123
{
2124
if let Ok ( id) = self .identity ( & k) {
2125
let body = id.encode ( ) ;
2126
if ! bundle
2127
.objects
2128
.iter ( )
2129
.any ( | ( t , b ) | * t = = ObjectType:: Identity & & * b = = body)
2130
{
2131
bundle.objects.push ( ( ObjectType:: Identity, body) ) ;
2132
}
2133
}
2134
}
f0a517d2 Andy McCall 20 days ago ↶
2135
2136
let signers: Vec< KeyId> = self .signing_key ( ) .into_iter ( ) .collect ( ) ;
2137
self .attach_certificates_for ( & mut bundle, & signers) ? ;
68a92484 Andy McCall 23 days ago ↶
2138
}
98286406 Andy McCall 1 month ago ↶
2139
let serial = attestation.as_ref ( ) .and_then ( | a | match a.attest.body {
2140
AttestBody:: Ref { serial, .. } => Some ( serial) ,
2141
_ => None ,
2142
} ) ;
83e0677b Andy McCall 1 month ago ↶
2143
let sent_serial = policy_to_send.as_ref ( ) .map ( | p | p.policy.serial ) ;
132b884b Andy McCall 1 month ago ↶
2144
2145
for t in & tags_to_send {
2146
let extra = self .negotiate ( & remote_heads, & t.heads) ? ;
75167672 Andy McCall 1 month ago
2147
let extra: Vec< ChangeHash> =
2148
extra.into_iter ( ) .filter ( | h | ! to_send.contains ( h) ) .collect ( ) ;
132b884b Andy McCall 1 month ago ↶
2149
if ! extra.is_empty ( ) {
2150
let more = self .bundle_for ( & extra) ? ;
2151
bundle.changes.extend ( more.changes) ;
2c75672f Andy McCall 1 month ago ↶
2152
bundle.hashes.extend ( more.hashes) ;
132b884b Andy McCall 1 month ago ↶
2153
bundle.attests.extend ( more.attests) ;
2154
bundle.objects.extend ( more.objects) ;
2155
}
75167672 Andy McCall 1 month ago
2156
bundle.objects.push ( (
2157
ObjectType:: State,
2158
StateObject:: new( t.heads.clone ( ) ) .encode ( ) ,
2159
) ) ;
132b884b Andy McCall 1 month ago ↶
2160
}
28fc23d7 Andy McCall 1 month ago ↶
2161
if let Some ( l) = & policy_to_send {
2162
let remote_serial = resp_policy_serial;
75167672 Andy McCall 1 month ago
2163
bundle.policies = self
2164
.policy_history ( ) ?
2165
.into_iter ( )
2166
.filter ( | sp | {
2167
remote_serial.is_none_or ( | r | sp.policy.serial > r )
2168
& & sp.policy.serial < l.policy.serial
2169
} )
2170
.collect ( ) ;
28fc23d7 Andy McCall 1 month ago ↶
2171
}
75167672 Andy McCall 1 month ago
2172
let results = remote.push (
2173
bundle,
2174
vec! [ RefUpdate {
2175
name: bookmark.clone ( ) ,
2176
old_state: remote_state,
2177
new_state,
2178
force,
2179
attestation,
2180
} ] ,
2181
policy_to_send,
2182
tags_to_send,
2183
) ? ;
2184
if let ( Some ( serial) , true ) = (
2185
sent_serial,
2186
results.iter ( ) .any ( | r | r.ok & & r.name = = " policy" ) ,
2187
) {
83e0677b Andy McCall 1 month ago ↶
2188
self .record_policy_serial ( remote_name, serial) ? ;
2189
}
2190
if results.iter ( ) .any ( | r | r.ok & & r.name = = bookmark ) {
98286406 Andy McCall 1 month ago ↶
2191
let mut view = op.view.clone ( ) ;
75167672 Andy McCall 1 month ago
2192
view.remotes
2193
.entry ( remote_name.to_string ( ) )
2194
.or_default ( )
2195
.insert ( bookmark.clone ( ) , new_state) ;
98286406 Andy McCall 1 month ago ↶
2196
self .record_op ( & [ op_ref] , & format! ( " oxy push {remote_name} " ) , view) ? ;
2197
if let Some ( s) = serial {
2198
self .record_serial ( remote_name, & bookmark, s) ? ;
2199
}
2200
}
2201
Ok ( results)
2202
}
2203
2204
2205
2206
pub fn clone ( url : & str , dest : & Path) -> Result< Repo> {
7421309f Andy McCall 1 month ago ↶
2207
Self :: clone_with( url, dest, None , & [ ] )
b3c5a59a Andy McCall 1 month ago ↶
2208
}
2209
7421309f Andy McCall 1 month ago ↶
2210
2211
2212
75167672 Andy McCall 1 month ago
2213
pub fn clone_with (
2214
url : & str ,
2215
dest : & Path,
2216
max_blob : Option< u64 > ,
2217
sparse : & [String],
2218
) -> Result< Repo> {
2863456a Andy McCall 1 month ago ↶
2219
Self :: clone_full( url, dest, max_blob, sparse, None )
2220
}
2221
2222
2223
75167672 Andy McCall 1 month ago
2224
pub fn clone_shallow (
2225
url : & str ,
2226
dest : & Path,
2227
depth : u32 ,
2228
max_blob : Option< u64 > ,
2229
sparse : & [String],
2230
) -> Result< Repo> {
2863456a Andy McCall 1 month ago ↶
2231
Self :: clone_full( url, dest, max_blob, sparse, Some ( depth) )
2232
}
2233
75167672 Andy McCall 1 month ago
2234
fn clone_full (
2235
url : & str ,
2236
dest : & Path,
2237
max_blob : Option< u64 > ,
2238
sparse : & [String],
2239
depth : Option< u32 > ,
2240
) -> Result< Repo> {
ff1f62fe Andy McCall 1 month ago ↶
2241
let options = crate :: credentials:: Credentials:: user( ) .options_for ( url) ;
2242
Self :: clone_using( url, dest, max_blob, sparse, depth, & options)
a07f60a3 Andy McCall 1 month ago ↶
2243
}
2244
ff1f62fe Andy McCall 1 month ago ↶
2245
2246
2247
a07f60a3 Andy McCall 1 month ago ↶
2248
pub ( crate ) fn clone_as_subrepo ( & self , url : & str , dest : & Path) -> Result< Repo> {
2249
let options = self .credentials_for ( url) ;
ff1f62fe Andy McCall 1 month ago ↶
2250
Self :: clone_using( url, dest, None , & [ ] , None , & options)
a07f60a3 Andy McCall 1 month ago ↶
2251
}
2252
2253
2254
2255
fn remote_on_same_hub ( & self , url : & str ) -> Option< String> {
2256
let origin = | u : & str | -> Option< String> {
2257
let ( scheme, rest) = u.split_once ( " ://" ) ? ;
9f9e72b9 Andy McCall 1 month ago ↶
2258
if ! matches! ( scheme, " http" | " https" ) {
2259
return None ;
2260
}
a07f60a3 Andy McCall 1 month ago ↶
2261
let host = rest.split ( ' /' ) .next ( ) ? ;
2262
Some ( format! (
2263
" {} ://{} " ,
2264
scheme.to_ascii_lowercase ( ) ,
2265
host.to_ascii_lowercase ( )
2266
) )
2267
} ;
2268
let want = origin ( url) ? ;
2269
self .config
2270
.remotes
2271
.iter ( )
2272
.find ( | ( _, u ) | origin ( u) .as_deref ( ) = = Some ( want.as_str ( ) ) )
2273
.map ( | ( name , _) | name.clone ( ) )
2274
}
2275
2276
2277
ff1f62fe Andy McCall 1 month ago ↶
2278
2279
a07f60a3 Andy McCall 1 month ago ↶
2280
pub ( crate ) fn credentials_for ( & self , url : & str ) -> RemoteOptions {
2281
match self .remote_on_same_hub ( url) {
2282
Some ( name) => RemoteOptions {
2283
repo_id: None ,
9f9e72b9 Andy McCall 1 month ago ↶
2284
cache_dir: None ,
a07f60a3 Andy McCall 1 month ago ↶
2285
.. self .remote_options ( & name)
2286
} ,
ff1f62fe Andy McCall 1 month ago ↶
2287
None => {
2288
let mut options = RemoteOptions:: default( ) ;
2289
self .fill_credentials ( url, & mut options) ;
2290
options
2291
}
a07f60a3 Andy McCall 1 month ago ↶
2292
}
2293
}
2294
2295
fn clone_using (
2296
url : & str ,
2297
dest : & Path,
2298
max_blob : Option< u64 > ,
2299
sparse : & [String],
2300
depth : Option< u32 > ,
2301
options : & RemoteOptions,
2302
) -> Result< Repo> {
2303
let remote = open_remote ( url, options) ? ;
98286406 Andy McCall 1 month ago ↶
2304
let id = remote.repo_id ( ) ? ;
75167672 Andy McCall 1 month ago
2305
if id = = crate :: protocol:: UNBOUND_ID {
05e71ed6 Andy McCall 1 month ago ↶
2306
return fail ( format! (
2307
" {url} is empty and unbound: push a repository to it first"
2308
) ) ;
75167672 Andy McCall 1 month ago
2309
}
98286406 Andy McCall 1 month ago ↶
2310
let mut repo = Repo:: init_with_id( dest, id) ? ;
827281ee Andy McCall 1 month ago ↶
2311
repo.config
2312
.set ( " remote.origin.url" , & crate :: protocol:: stored_url( url) ) ? ;
b3c5a59a Andy McCall 1 month ago ↶
2313
if let Some ( m) = max_blob {
2314
repo.config.set ( " filter.max-blob" , & m.to_string ( ) ) ? ;
7421309f Andy McCall 1 month ago ↶
2315
} else if ! sparse.is_empty ( ) {
2316
repo.config.set ( " filter.max-blob" , " 0" ) ? ;
2317
}
2318
if ! sparse.is_empty ( ) {
2319
repo.config.set ( " filter.paths" , & sparse.join ( " ," ) ) ? ;
b3c5a59a Andy McCall 1 month ago ↶
2320
}
98286406 Andy McCall 1 month ago ↶
2321
repo.save_config ( ) ? ;
2863456a Andy McCall 1 month ago ↶
2322
if let Some ( depth) = depth {
2323
repo.fetch_shallow ( " origin" , depth) ? ;
2324
} else {
2325
repo.fetch ( " origin" ) ? ;
2326
}
98286406 Andy McCall 1 month ago ↶
2327
let ( op_ref, op) = repo.head_op ( ) ? ;
14edbaaf Andy McCall 1 month ago ↶
2328
2329
let theirs = op.view.remotes.get ( " origin" ) .cloned ( ) .unwrap_or_default ( ) ;
9f9e72b9 Andy McCall 1 month ago ↶
2330
2331
2332
let advertised = repo
2333
.open_named_remote ( " origin" )
2334
.ok ( )
2335
.and_then ( | r | r.refs ( ) .ok ( ) )
2336
.and_then ( | r | r.default_bookmark ) ;
14edbaaf Andy McCall 1 month ago ↶
2337
let bookmark = advertised
2338
.filter ( | b | theirs.contains_key ( b) )
2339
.or_else ( | | {
2340
theirs
2341
.contains_key ( & repo.config.bookmark)
2342
.then ( | | repo.config.bookmark.clone ( ) )
2343
} )
2344
.or_else ( | | {
2345
( theirs.len ( ) = = 1 )
2346
.then ( | | theirs.keys ( ) .next ( ) .cloned ( ) )
2347
.flatten ( )
2348
} )
2349
.unwrap_or_else ( | | repo.config.bookmark.clone ( ) ) ;
2350
let initial = repo.config.bookmark.clone ( ) ;
2351
if bookmark ! = repo.config.bookmark {
2352
fs:: create_dir_all( repo.workspace_bookmark_path ( ) .parent ( ) .unwrap ( ) ) ? ;
2353
write_atomic ( & repo.workspace_bookmark_path ( ) , bookmark.as_bytes ( ) ) ? ;
2354
repo.config.bookmark = bookmark.clone ( ) ;
2355
}
2356
if let Some ( state) = theirs.get ( & bookmark) .copied ( ) {
98286406 Andy McCall 1 month ago ↶
2357
let heads = repo.store.get_state ( & state) ? .heads;
2358
let mut view = op.view.clone ( ) ;
14edbaaf Andy McCall 1 month ago ↶
2359
2360
if initial ! = bookmark & & repo.heads_of ( & view, & initial) ? .is_empty ( ) {
2361
view.bookmarks.remove ( & initial) ;
2362
}
98286406 Andy McCall 1 month ago ↶
2363
view.bookmarks.insert ( bookmark, state) ;
2364
repo.record_op ( & [ op_ref] , & format! ( " oxy clone {url} " ) , view) ? ;
2365
repo.checkout ( & heads, false , " oxy clone (checkout)" ) ? ;
2366
}
2367
Ok ( repo)
f8c69576 Andy McCall 1 month ago ↶
2368
}
2369
}
2370
2371
2372
2373
2374
2375
fn next_serial ( last : Option< u64 > ) -> u64 {
2376
match last {
2377
Some ( l) => l + 1 ,
2378
None => Hlc:: now( ) .wall_ms.max ( 1 ) ,
98286406 Andy McCall 1 month ago ↶
2379
}
2380
}