98286406 Andy McCall 1 month ago ↶
1
2
1ec6fdbc Andy McCall 16 days ago ↶
3
use super :: tags:: TagCheck;
98286406 Andy McCall 1 month ago ↶
4
use super :: * ;
5
7aa2dc93 Andy McCall 1 month ago ↶
6
7
# [ derive ( Clone, Debug, Default ) ]
8
pub struct PushReport {
9
pub results : Vec< RefResult> ,
10
11
pub updated : Vec< RefUpdated> ,
12
13
pub imported : Vec< ChangeHash> ,
14
}
15
16
# [ derive ( Clone, Debug ) ]
17
pub struct RefUpdated {
18
pub name : String,
19
pub old_state : ObjectRef,
20
pub new_state : ObjectRef,
21
pub heads : Vec< ChangeHash> ,
22
}
23
83e0677b Andy McCall 1 month ago ↶
24
25
# [ derive ( Debug, Clone, PartialEq, Eq, Default ) ]
26
pub struct FetchReport {
27
28
pub refs : Vec< ( String, Vec< ChangeHash> , usize ) > ,
132b884b Andy McCall 1 month ago ↶
29
30
pub new_tags : usize ,
b63fc7a7 Andy McCall 11 days ago ↶
31
32
pub withdrawn_tags : Vec< String> ,
83e0677b Andy McCall 1 month ago ↶
33
34
35
pub unverified : bool ,
4b9d0677 Andy McCall 18 days ago ↶
36
37
38
39
pub log_notes : Vec< String> ,
d36ba2f7 Andy McCall 16 days ago ↶
40
41
42
43
44
pub skipped : Vec< ( String, String) > ,
83e0677b Andy McCall 1 month ago ↶
45
}
46
98286406 Andy McCall 1 month ago ↶
47
# [ derive ( Debug, Clone, PartialEq, Eq ) ]
48
pub struct PullReport {
14edbaaf Andy McCall 1 month ago ↶
49
98286406 Andy McCall 1 month ago ↶
50
pub heads : Vec< ChangeHash> ,
14edbaaf Andy McCall 1 month ago ↶
51
98286406 Andy McCall 1 month ago ↶
52
pub changed : bool ,
53
pub checked_out : bool ,
14edbaaf Andy McCall 1 month ago ↶
54
55
56
pub others : Vec< String> ,
d36ba2f7 Andy McCall 16 days ago ↶
57
58
pub skipped : Vec< ( String, String) > ,
98286406 Andy McCall 1 month ago ↶
59
}
60
cb8b8082 Andy McCall 26 days ago ↶
61
62
63
pub const OBJECTS_RESULT : & str = " objects" ;
64
98286406 Andy McCall 1 month ago ↶
65
impl Repo {
66
67
68
fn ref_attest_path ( & self , bookmark : & str ) -> PathBuf {
54cd08a1 Andy McCall 1 month ago ↶
69
self .oxy.join ( " attest" ) .join ( name_file ( bookmark) )
98286406 Andy McCall 1 month ago ↶
70
}
71
72
73
pub fn latest_ref_attest ( & self , bookmark : & str ) -> Result< Option< ( ObjectRef, SignedAttest) > > {
74
match fs:: read_to_string( self .ref_attest_path ( bookmark) ) {
75
Ok ( h) => {
75167672 Andy McCall 1 month ago
76
let r = ObjectRef:: from_hex( h.trim ( ) )
77
.ok_or_else ( | | RepoError:: Msg( " corrupt attestation pointer" .into ( ) ) ) ? ;
78
Ok ( Some ( (
79
r,
80
SignedAttest {
81
attest: self .store.get_attest ( & r) ? ,
82
sigs: self .store.signatures ( & r) ? ,
83
} ,
84
) ) )
98286406 Andy McCall 1 month ago ↶
85
}
86
Err ( e) if e.kind ( ) = = std:: io:: ErrorKind:: NotFound => Ok ( None ) ,
87
Err ( e) => Err ( e.into ( ) ) ,
88
}
89
}
90
9f9e72b9 Andy McCall 1 month ago ↶
91
pub ( crate ) fn set_ref_attest ( & self , bookmark : & str , r : & ObjectRef) -> Result< ( ) > {
98286406 Andy McCall 1 month ago ↶
92
fs:: create_dir_all( self .oxy.join ( " attest" ) ) ? ;
93
write_atomic ( & self .ref_attest_path ( bookmark) , r.to_hex ( ) .as_bytes ( ) )
94
}
95
83e0677b Andy McCall 1 month ago ↶
96
pub fn remote_refs ( & self ) -> Result< RefsResponse> {
98286406 Andy McCall 1 month ago ↶
97
let view = self .view ( ) ? ;
98
let mut out = Vec :: new( ) ;
99
for ( name, state) in & view.bookmarks {
100
let latest = self .latest_ref_attest ( name) ? .map ( | ( _, a ) | a ) ;
101
let last_serial = latest.as_ref ( ) .and_then ( | a | match a.attest.body {
102
AttestBody:: Ref { serial, .. } => Some ( serial) ,
103
_ => None ,
104
} ) ;
105
let attestation = latest.filter ( | a | matches! ( & a.attest.body, AttestBody:: Ref { new_state, .. } if new_state = = state) ) ;
f53cbe7a Andy McCall 18 days ago ↶
106
let accepted = match & attestation {
107
Some ( a) => self .acceptance_of ( & a.attest.hash ( ) ) ? ,
108
None => None ,
109
} ;
75167672 Andy McCall 1 month ago
110
out.push ( RemoteRef {
111
name: name.clone ( ) ,
112
state: * state,
113
heads: self .store.get_state ( state) ? .heads,
114
attestation,
115
last_serial,
f53cbe7a Andy McCall 18 days ago ↶
116
accepted,
4b9d0677 Andy McCall 18 days ago ↶
117
logged: None ,
028545df Andy McCall 17 days ago ↶
118
integration: vec! [ ] ,
75167672 Andy McCall 1 month ago
119
} ) ;
98286406 Andy McCall 1 month ago ↶
120
}
28fc23d7 Andy McCall 1 month ago ↶
121
let policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp ) ;
75167672 Andy McCall 1 month ago
122
let history = self
123
.policy_history ( ) ?
124
.into_iter ( )
125
.filter ( | sp | {
126
policy
127
.as_ref ( )
128
.is_some_and ( | p | sp.policy.serial < p.policy.serial )
129
} )
130
.collect ( ) ;
131
Ok ( RefsResponse {
132
refs: out,
133
policy,
134
tags: self .tag_refs ( ) ? ,
135
history,
14edbaaf Andy McCall 1 month ago ↶
136
default_bookmark: Some ( self .config.bookmark.clone ( ) ) ,
4b9d0677 Andy McCall 18 days ago ↶
137
log: None ,
028545df Andy McCall 17 days ago ↶
138
witnesses: vec! [ ] ,
b63fc7a7 Andy McCall 11 days ago ↶
139
withdrawn: self .withdrawn_refs ( ) ? ,
75167672 Andy McCall 1 month ago
140
} )
98286406 Andy McCall 1 month ago ↶
141
}
142
143
144
fn closure_in_store ( & self , heads : & [ChangeHash]) -> Result< BTreeSet< ChangeHash> > {
145
let mut seen = BTreeSet:: new( ) ;
146
let mut stack: Vec< ChangeHash> = heads.to_vec ( ) ;
147
while let Some ( h) = stack.pop ( ) {
148
if seen.contains ( & h) | | ! self .store.has ( & h) {
149
continue ;
150
}
151
seen.insert ( h) ;
69816d68 Andy McCall 1 month ago ↶
152
stack.extend ( self .store.get_change_meta ( & h) ? .deps) ;
98286406 Andy McCall 1 month ago ↶
153
}
154
Ok ( seen)
155
}
156
2863456a Andy McCall 1 month ago ↶
157
158
159
pub fn shallow_changes ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > {
160
let mut out = Vec :: new( ) ;
161
let mut seen: BTreeSet< ChangeHash> = BTreeSet:: new( ) ;
162
let mut layer: Vec< ChangeHash> = heads.to_vec ( ) ;
163
for _ in 0 .. depth.max ( 1 ) {
164
let mut next = Vec :: new( ) ;
165
for h in layer {
166
if ! seen.insert ( h) | | ! self .store.has ( & h) {
167
continue ;
168
}
169
let c = self .store.get_change ( & h) ? ;
170
next.extend ( c.deps.iter ( ) .copied ( ) ) ;
171
out.push ( h) ;
172
}
173
if next.is_empty ( ) {
174
break ;
175
}
176
layer = next;
177
}
178
out.reverse ( ) ;
179
Ok ( out)
180
}
181
182
fn shallow_path ( & self ) -> PathBuf {
183
self .oxy.join ( " shallow" )
184
}
185
186
187
pub fn boundary ( & self ) -> BTreeSet< ChangeHash> {
75167672 Andy McCall 1 month ago
188
fs:: read_to_string( self .shallow_path ( ) )
189
.map ( | t | t.lines ( ) .filter_map ( ObjectRef:: from_hex) .collect ( ) )
190
.unwrap_or_default ( )
2863456a Andy McCall 1 month ago ↶
191
}
192
193
pub fn is_shallow ( & self ) -> bool {
194
! self .boundary ( ) .is_empty ( )
195
}
196
197
fn write_boundary ( & self , boundary : & BTreeSet< ChangeHash> ) -> Result< ( ) > {
198
if boundary.is_empty ( ) {
199
let _ = fs:: remove_file( self .shallow_path ( ) ) ;
200
return Ok ( ( ) ) ;
201
}
75167672 Andy McCall 1 month ago
202
let text: String = boundary
203
.iter ( )
204
.map ( | h | format! ( " {} \n " , h.to_hex ( ) ) )
205
.collect ( ) ;
2863456a Andy McCall 1 month ago ↶
206
write_atomic ( & self .shallow_path ( ) , text.as_bytes ( ) )
207
}
208
209
210
211
fn refresh_boundary ( & self , imported : & [ChangeHash]) -> Result< ( ) > {
212
let mut boundary = self .boundary ( ) ;
213
boundary.retain ( | h | ! self .store.has ( h) ) ;
214
for h in imported {
69816d68 Andy McCall 1 month ago ↶
215
for d in self .store.get_change_meta ( h) ? .deps {
2863456a Andy McCall 1 month ago ↶
216
if ! self .store.has ( & d) {
217
boundary.insert ( d) ;
218
}
219
}
220
}
221
self .write_boundary ( & boundary)
222
}
223
224
225
pub fn deepen ( & self , remote_name : & str , depth : u32 ) -> Result< usize > {
226
let boundary = self .boundary ( ) ;
227
if boundary.is_empty ( ) {
228
return Ok ( 0 ) ;
229
}
230
let remote = self .open_named_remote ( remote_name) ? ;
231
let wanted = remote.shallow ( & boundary.iter ( ) .copied ( ) .collect:: < Vec< _ > > ( ) , depth) ? ;
232
let bundle = remote.fetch ( & wanted, self .config.max_blob) ? ;
233
let imported = self .import_bundle_shallow ( & bundle, false ) ? ;
234
if self .config.max_blob.is_some ( ) {
235
self .register_missing_blobs ( & imported, remote_name) ? ;
236
}
237
self .refresh_boundary ( & imported) ? ;
238
Ok ( imported.len ( ) )
239
}
240
98286406 Andy McCall 1 month ago ↶
241
242
pub fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > {
243
let have = self .closure_in_store ( have) ? ;
2863456a Andy McCall 1 month ago ↶
244
245
246
let boundary = self .boundary ( ) ;
98286406 Andy McCall 1 month ago ↶
247
let mut out = Vec :: new( ) ;
248
let mut done: BTreeSet< ChangeHash> = have.clone ( ) ;
2863456a Andy McCall 1 month ago ↶
249
done.extend ( boundary.iter ( ) .copied ( ) ) ;
98286406 Andy McCall 1 month ago ↶
250
f3811eb2 Andy McCall 11 days ago ↶
251
let shallow = self .is_shallow ( ) ;
98286406 Andy McCall 1 month ago ↶
252
let mut stack: Vec< ( ChangeHash, bool ) > = want.iter ( ) .rev ( ) .map ( | h | ( * h, false ) ) .collect ( ) ;
253
while let Some ( ( h, expanded) ) = stack.pop ( ) {
254
if done.contains ( & h) {
255
continue ;
256
}
257
if ! self .store.has ( & h) {
f3811eb2 Andy McCall 11 days ago ↶
258
259
260
261
262
if shallow {
263
done.insert ( h) ;
264
continue ;
265
}
98286406 Andy McCall 1 month ago ↶
266
return fail ( format! ( " change {} is not in this repository" , h.short ( 12 ) ) ) ;
267
}
268
if expanded {
269
done.insert ( h) ;
270
out.push ( h) ;
271
continue ;
272
}
273
stack.push ( ( h, true ) ) ;
69816d68 Andy McCall 1 month ago ↶
274
for d in self .store.get_change_meta ( & h) ? .deps {
98286406 Andy McCall 1 month ago ↶
275
if ! done.contains ( & d) {
276
stack.push ( ( d, false ) ) ;
277
}
278
}
279
}
280
Ok ( out)
281
}
282
283
284
285
pub fn bundle_for ( & self , changes : & [ChangeHash]) -> Result< Bundle> {
d36ba2f7 Andy McCall 16 days ago ↶
286
self .bundle_of ( changes, None , false , false )
b3c5a59a Andy McCall 1 month ago ↶
287
}
288
289
290
pub fn objects_for ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > {
291
let mut out = Vec :: new( ) ;
292
for r in refs {
293
let ( ty, body) = match self .store.get ( r) {
294
Ok ( x) => x,
295
Err ( StoreError:: NotFound( _ ) ) => continue ,
296
Err ( e) => return Err ( e.into ( ) ) ,
297
} ;
298
if ty = = ObjectType:: Blob {
299
for c in Blob:: decode( & body) ? .chunks {
300
if let Ok ( ( cty, cb) ) = self .store.get ( & c) {
301
out.push ( ( cty, cb) ) ;
302
}
303
}
304
}
305
out.push ( ( ty, body) ) ;
306
}
307
Ok ( out)
308
}
309
310
311
d36ba2f7 Andy McCall 16 days ago ↶
312
313
314
315
75167672 Andy McCall 1 month ago
316
pub fn bundle_for_filtered (
317
& self ,
318
changes : & [ChangeHash],
319
max_blob : Option< u64 > ,
320
) -> Result< Bundle> {
d36ba2f7 Andy McCall 16 days ago ↶
321
self .bundle_of ( changes, max_blob, false , true )
2c75672f Andy McCall 1 month ago ↶
322
}
323
324
325
326
327
328
pub fn bundle_raw_for ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> {
d36ba2f7 Andy McCall 16 days ago ↶
329
self .bundle_of ( changes, max_blob, true , true )
2c75672f Andy McCall 1 month ago ↶
330
}
331
d36ba2f7 Andy McCall 16 days ago ↶
332
333
334
335
336
337
e10ba800 Andy McCall 16 days ago ↶
338
339
340
341
342
d36ba2f7 Andy McCall 16 days ago ↶
343
pub fn served_attest_signers ( & self ) -> Result< BTreeSet< KeyId> > {
e10ba800 Andy McCall 16 days ago ↶
344
type Signers = std:: collections:: HashMap< ( PathBuf, ObjectRef) , BTreeSet< KeyId> > ;
345
static SERVED : std:: sync:: Mutex< Option< Signers> > = std:: sync:: Mutex:: new( None ) ;
346
let key = ( self .oxy.clone ( ) , self .head_op ( ) ? .0 ) ;
347
if let Some ( keys) = SERVED
348
.lock ( )
349
.unwrap_or_else ( | p | p.into_inner ( ) )
350
.as_ref ( )
351
.and_then ( | m | m.get ( & key) )
352
{
353
return Ok ( keys.clone ( ) ) ;
354
}
355
let keys = self .attest_signers_now ( ) ? ;
356
let mut served = SERVED .lock ( ) .unwrap_or_else ( | p | p.into_inner ( ) ) ;
357
let map = served.get_or_insert_with ( Default :: default) ;
358
359
360
map.retain ( | ( oxy , _) , _| * oxy ! = key.0 ) ;
361
if map.len ( ) > = 1024 {
362
map.clear ( ) ;
363
}
364
map.insert ( key, keys.clone ( ) ) ;
365
Ok ( keys)
366
}
367
368
fn attest_signers_now ( & self ) -> Result< BTreeSet< KeyId> > {
d36ba2f7 Andy McCall 16 days ago ↶
369
let view = self .view ( ) ? ;
370
let pointers = view
371
.bookmarks
372
.keys ( )
373
.map ( | b | self .ref_attest_path ( b) )
374
.chain ( view.tags.keys ( ) .map ( | t | self .tag_attest_path ( t) ) ) ;
375
let mut keys = BTreeSet:: new( ) ;
376
for p in pointers {
377
let Ok ( text) = fs:: read_to_string( & p) else {
378
continue ;
379
} ;
380
let Some ( r) = ObjectRef:: from_hex( text.trim ( ) ) else {
381
continue ;
382
} ;
383
keys.extend ( self .store.signatures ( & r) ? .into_iter ( ) .map ( | s | s.key ) ) ;
384
}
385
Ok ( keys)
386
}
387
388
389
2c75672f Andy McCall 1 month ago ↶
390
fn bundle_of (
391
& self ,
392
changes : & [ChangeHash],
393
max_blob : Option< u64 > ,
394
raw : bool ,
d36ba2f7 Andy McCall 16 days ago ↶
395
serving : bool ,
2c75672f Andy McCall 1 month ago ↶
396
) -> Result< Bundle> {
98286406 Andy McCall 1 month ago ↶
397
let mut bundle = Bundle:: default( ) ;
398
let mut objects: BTreeSet< ObjectRef> = BTreeSet:: new( ) ;
399
let mut seals: BTreeSet< ObjectRef> = BTreeSet:: new( ) ;
400
let add_object = | r : & ObjectRef, objects : & mut BTreeSet< ObjectRef> | {
401
if ! r.is_zero ( ) & & self .store.has ( r) {
402
objects.insert ( * r) ;
403
}
404
} ;
405
for h in changes {
2c75672f Andy McCall 1 month ago ↶
406
407
let ( c, encoded) = if raw {
408
let bytes = self .store.get_typed ( h, ObjectType:: Change) ? ;
409
let ( skel, texts) = Change:: decode_skeleton( & bytes, * h) ? ;
410
for t in texts {
0f93f49c Andy McCall 1 month ago ↶
411
if let crate :: objects:: TextRef:: Blob( r, _ , _ ) = t {
2c75672f Andy McCall 1 month ago ↶
412
let blob = Blob:: decode( & self .store.get_typed ( & r, ObjectType:: Blob) ? ) ? ;
413
objects.insert ( r) ;
414
objects.extend ( blob.chunks) ;
415
}
416
}
417
( skel, Some ( bytes) )
418
} else {
419
( self .store.get_change ( h) ? , None )
420
} ;
98286406 Andy McCall 1 month ago ↶
421
let sigs = self .store.signatures ( h) ? ;
422
add_object ( & c.author, & mut objects) ;
423
for s in & sigs {
424
add_object ( & s.key, & mut objects) ;
425
}
426
for e in & c.edits {
427
if let Edit:: Replace { old, new, .. } = e {
428
for b in [ old, new] .into_iter ( ) .flatten ( ) {
429
if self .store.has ( b) {
75167672 Andy McCall 1 month ago
430
if let Ok ( blob) =
431
Blob:: decode( & self .store.get_typed ( b, ObjectType:: Blob) ? )
432
{
b3c5a59a Andy McCall 1 month ago ↶
433
if max_blob.is_some_and ( | m | blob.length > m ) {
434
continue ;
435
}
436
objects.insert ( * b) ;
98286406 Andy McCall 1 month ago ↶
437
objects.extend ( blob.chunks.iter ( ) .copied ( ) ) ;
438
}
439
}
440
}
441
}
442
}
443
for s in self .store.seals_for ( h) ? {
444
seals.insert ( s) ;
2c75672f Andy McCall 1 month ago ↶
445
}
446
match encoded {
447
Some ( bytes) => bundle.raw_changes.push ( ( bytes, sigs) ) ,
448
None => {
449
bundle.changes.push ( ( c, sigs) ) ;
450
bundle.hashes.push ( * h) ;
451
}
98286406 Andy McCall 1 month ago ↶
452
}
453
}
454
for r in seals {
455
let attest = self .store.get_attest ( & r) ? ;
456
let sigs = self .store.signatures ( & r) ? ;
457
for s in & sigs {
458
add_object ( & s.key, & mut objects) ;
459
}
460
bundle.attests.push ( SignedAttest { attest, sigs } ) ;
461
}
d36ba2f7 Andy McCall 16 days ago ↶
462
let signers: Vec< KeyId> = if serving {
463
let signers = self .served_attest_signers ( ) ? ;
464
for k in & signers {
465
add_object ( k, & mut objects) ;
466
}
467
signers.into_iter ( ) .collect ( )
468
} else {
469
Vec :: new( )
470
} ;
98286406 Andy McCall 1 month ago ↶
471
for r in objects {
472
let ( ty, body) = self .store.get ( & r) ? ;
473
bundle.objects.push ( ( ty, body) ) ;
474
}
d36ba2f7 Andy McCall 16 days ago ↶
475
self .attach_certificates_for ( & mut bundle, & signers) ? ;
98286406 Andy McCall 1 month ago ↶
476
Ok ( bundle)
477
}
478
f0a517d2 Andy McCall 18 days ago ↶
479
480
481
482
483
pub ( crate ) fn attach_certificates_for (
484
& self ,
485
bundle : & mut Bundle,
486
also : & [KeyId],
487
) -> Result< ( ) > {
488
let mut keys: BTreeSet< KeyId> = also.iter ( ) .copied ( ) .collect ( ) ;
489
keys.extend (
490
bundle
491
.changes
492
.iter ( )
493
.flat_map ( | ( _, s ) | s.iter ( ) .map ( | s | s.key ) ) ,
494
) ;
495
keys.extend (
496
bundle
497
.raw_changes
498
.iter ( )
499
.flat_map ( | ( _, s ) | s.iter ( ) .map ( | s | s.key ) ) ,
500
) ;
501
keys.extend (
502
bundle
503
.attests
504
.iter ( )
505
.flat_map ( | a | a.sigs.iter ( ) .map ( | s | s.key ) ) ,
506
) ;
507
for k in keys {
508
let Some ( r) = self .store.certificate_of ( & k) ? else {
509
continue ;
510
} ;
511
let cert = self .store.get_certificate ( & r) ? ;
512
if bundle
513
.certificates
514
.iter ( )
515
.any ( | c | c.cert.hash ( ) = = cert.cert.hash ( ) )
516
{
517
continue ;
518
}
f0ef9778 Andy McCall 11 days ago ↶
519
520
521
let grantor = cert.cert.grant.as_ref ( ) .map ( | g | g.grant.grantor ) ;
522
for id in [ Some ( k) , Some ( cert.cert.issuer) , grantor] .iter ( ) .flatten ( ) {
f0a517d2 Andy McCall 18 days ago ↶
523
if let Ok ( body) = self .store.get_typed ( id, ObjectType:: Identity) {
524
if ! bundle
525
.objects
526
.iter ( )
527
.any ( | ( t , b ) | * t = = ObjectType:: Identity & & * b = = body)
528
{
529
bundle.objects.push ( ( ObjectType:: Identity, body) ) ;
530
}
531
}
532
}
533
bundle.certificates.push ( cert) ;
534
}
535
Ok ( ( ) )
536
}
537
538
539
540
fn adopt_certificates ( & self , certs : & [crate:: cert:: SignedCertificate]) -> Result< ( ) > {
541
for c in certs {
542
let issuer = self .store.get_identity ( & c.cert.issuer) .map_err ( | _| {
543
RepoError:: Msg( format! (
544
" a certificate names issuer {} , whose identity did not come with it" ,
545
c.cert.issuer.short ( 12 )
546
) )
547
} ) ? ;
548
c.verify ( & issuer)
549
.map_err ( | e | RepoError:: Msg( format! ( " a certificate does not verify: {e} " ) ) ) ? ;
550
self .store.put_identity ( & c.cert.key) ? ;
551
self .store.put_certificate ( c) ? ;
552
}
553
Ok ( ( ) )
554
}
555
556
557
558
559
560
pub ( crate ) fn certified_authorized (
561
& self ,
562
p : & crate:: policy:: Policy,
563
signer : & KeyId,
564
reference : & str ,
565
scope : Scope,
566
at_ms : u64 ,
567
) -> bool {
568
let Ok ( Some ( r) ) = self .store.certificate_of ( signer) else {
569
return false ;
570
} ;
571
let Ok ( c) = self .store.get_certificate ( & r) else {
572
return false ;
573
} ;
574
let Some ( issuer) = p
575
.identity ( & c.cert.issuer)
576
.cloned ( )
577
.or_else ( | | self .store.get_identity ( & c.cert.issuer) .ok ( ) )
578
else {
579
return false ;
580
} ;
581
c.verify ( & issuer) .is_ok ( )
582
& & c.cert.key_id ( ) = = * signer
583
& & c.cert.covers ( scope, at_ms)
584
& & p.subject_grant ( reference, & c.cert.issuer, & c.cert.subject, scope)
585
}
586
98286406 Andy McCall 1 month ago ↶
587
588
589
pub fn import_bundle ( & self , bundle : & Bundle, strict : bool ) -> Result< Vec< ChangeHash> > {
2863456a Andy McCall 1 month ago ↶
590
self .import_bundle_inner ( bundle, strict, false )
591
}
592
593
594
595
pub fn import_bundle_shallow ( & self , bundle : & Bundle, strict : bool ) -> Result< Vec< ChangeHash> > {
596
self .import_bundle_inner ( bundle, strict, true )
597
}
598
75167672 Andy McCall 1 month ago
599
fn import_bundle_inner (
600
& self ,
601
bundle : & Bundle,
602
strict : bool ,
603
shallow : bool ,
604
) -> Result< Vec< ChangeHash> > {
8f10a07a Andy McCall 1 month ago ↶
605
let batch = self .store.begin_batch ( ) ? ;
606
let result = self .import_bundle_batched ( bundle, strict, shallow) ;
607
batch.finish ( ) ? ;
608
result
609
}
610
75167672 Andy McCall 1 month ago
611
fn import_bundle_batched (
612
& self ,
613
bundle : & Bundle,
614
strict : bool ,
615
shallow : bool ,
616
) -> Result< Vec< ChangeHash> > {
2c75672f Andy McCall 1 month ago ↶
617
for ( ty, _ ) in & bundle.objects {
618
if ! matches! (
619
ty,
75167672 Andy McCall 1 month ago
620
ObjectType:: Chunk
2c75672f Andy McCall 1 month ago ↶
621
| ObjectType:: Blob
622
| ObjectType:: Identity
623
| ObjectType:: State
624
| ObjectType:: Checkpoint
625
) {
626
return fail ( format! ( " bundle may not carry {} objects" , ty.name ( ) ) ) ;
98286406 Andy McCall 1 month ago ↶
627
}
628
}
2c75672f Andy McCall 1 month ago ↶
629
630
let objects: Vec< ( ObjectType, & [u8 ]) > = bundle
631
.objects
632
.iter ( )
633
.map ( | ( ty , body ) | ( * ty, body.as_slice ( ) ) )
634
.collect ( ) ;
635
self .store.put_many ( & objects) ? ;
f0a517d2 Andy McCall 18 days ago ↶
636
self .adopt_certificates ( & bundle.certificates) ? ;
98286406 Andy McCall 1 month ago ↶
637
for sa in & bundle.attests {
638
let r = self .store.put_attest ( & sa.attest) ? ;
639
for sig in & sa.sigs {
640
self .store.put_signature ( & r, sig) ? ;
641
}
642
if let AttestBody:: Seal { hashes } = & sa.attest.body {
643
for h in hashes {
644
self .store.index_seal ( h, & r) ? ;
645
}
646
}
647
}
598009a7 Andy McCall 1 month ago ↶
648
649
650
651
let hashes: Vec< ChangeHash> = if bundle.hashes.len ( ) = = bundle.changes.len ( ) {
652
bundle.hashes.clone ( )
653
} else {
654
bundle.changes.iter ( ) .map ( | ( c , _) | c.hash ( ) ) .collect ( )
655
} ;
2c75672f Andy McCall 1 month ago ↶
656
let raw_hashes: Vec< ChangeHash> = bundle
657
.raw_changes
658
.iter ( )
659
.map ( | ( b , _) | hash_object ( ObjectType:: Change, b) )
660
.collect ( ) ;
661
let incoming: BTreeSet< ChangeHash> = hashes.iter ( ) .chain ( & raw_hashes) .copied ( ) .collect ( ) ;
2863456a Andy McCall 1 month ago ↶
662
let boundary = self .boundary ( ) ;
98286406 Andy McCall 1 month ago ↶
663
let mut imported = Vec :: new( ) ;
2c75672f Andy McCall 1 month ago ↶
664
665
666
for ( ( bytes, sigs) , h) in bundle.raw_changes.iter ( ) .zip ( raw_hashes) {
667
let ( skel, texts) = Change:: decode_skeleton( bytes, h) ? ;
668
for d in & skel.deps {
669
if ! self .store.has ( d) & & ! incoming.contains ( d) & & ! shallow & & ! boundary.contains ( d)
670
{
671
return fail ( format! (
672
" change {} depends on {} , which is neither stored nor in the bundle" ,
673
h.short ( 12 ) ,
674
d.short ( 12 )
675
) ) ;
676
}
677
}
678
for t in texts {
0f93f49c Andy McCall 1 month ago ↶
679
if let crate :: objects:: TextRef:: Blob( r, _ , _ ) = t {
2c75672f Andy McCall 1 month ago ↶
680
if ! self .store.has ( & r) {
681
return fail ( format! (
682
" change {} came without its text blob {} " ,
683
h.short ( 12 ) ,
684
r.short ( 12 )
685
) ) ;
686
}
687
}
688
}
689
self .store.put ( ObjectType:: Change, bytes) ? ;
690
for sig in sigs {
691
self .store.put_signature ( & h, sig) ? ;
692
}
693
imported.push ( h) ;
694
}
598009a7 Andy McCall 1 month ago ↶
695
for ( ( c, sigs) , h) in bundle.changes.iter ( ) .zip ( hashes) {
98286406 Andy McCall 1 month ago ↶
696
for d in & c.deps {
75167672 Andy McCall 1 month ago
697
if ! self .store.has ( d) & & ! incoming.contains ( d) & & ! shallow & & ! boundary.contains ( d)
698
{
699
return fail ( format! (
700
" change {} depends on {} , which is neither stored nor in the bundle" ,
701
h.short ( 12 ) ,
702
d.short ( 12 )
703
) ) ;
98286406 Andy McCall 1 month ago ↶
704
}
705
}
598009a7 Andy McCall 1 month ago ↶
706
let stored = self .store.put_change ( c) ? ;
707
if stored ! = h {
708
return fail ( format! (
709
" change {} is not the change the bundle names" ,
710
h.short ( 12 )
711
) ) ;
712
}
98286406 Andy McCall 1 month ago ↶
713
for sig in sigs {
714
self .store.put_signature ( & h, sig) ? ;
715
}
716
imported.push ( h) ;
717
}
718
if strict {
719
for h in & imported {
720
if ! matches! ( self .authenticate_change ( h) ? , SignatureStatus:: Valid { .. } ) {
75167672 Andy McCall 1 month ago
721
return fail ( format! (
722
" change {} is not authenticated by its author" ,
723
h.short ( 12 )
724
) ) ;
98286406 Andy McCall 1 month ago ↶
725
}
726
}
727
}
728
Ok ( imported)
729
}
730
731
732
733
84b9e9ac Andy McCall 1 month ago ↶
734
735
7ba155f2 Andy McCall 1 month ago ↶
736
737
738
eae59556 Andy McCall 1 month ago ↶
739
pub ( crate ) fn delegate_authorized (
740
& self ,
741
p : & crate:: policy:: Policy,
742
signer : & KeyId,
743
reference : & str ,
7ba155f2 Andy McCall 1 month ago ↶
744
at_ms : u64 ,
eae59556 Andy McCall 1 month ago ↶
745
) -> bool {
84b9e9ac Andy McCall 1 month ago ↶
746
if p.is_revoked ( signer) {
747
return false ;
748
}
eae59556 Andy McCall 1 month ago ↶
749
let Some ( rule) = p.rule_for ( reference) else {
750
return false ;
751
} ;
84b9e9ac Andy McCall 1 month ago ↶
752
rule.keys.iter ( ) .any ( | k | {
753
! p.is_revoked ( k)
754
& & p.identity ( k)
755
.cloned ( )
756
.or_else ( | | self .store.get_identity ( k) .ok ( ) )
7ba155f2 Andy McCall 1 month ago ↶
757
.is_some_and ( | id | id.delegates_to ( signer, Scope:: REFS , at_ms) )
84b9e9ac Andy McCall 1 month ago ↶
758
} )
759
}
760
e5d9b051 Andy McCall 19 days ago ↶
761
762
763
764
765
766
767
pub ( crate ) fn predecessor_authorized (
768
& self ,
769
p : & crate:: policy:: Policy,
770
signer : & KeyId,
771
reference : & str ,
772
at_ms : u64 ,
773
) -> bool {
774
if p.is_revoked ( signer) {
775
return false ;
776
}
777
let Some ( rule) = p.rule_for ( reference) else {
778
return false ;
779
} ;
780
let identity = | k : & KeyId| {
781
p.identity ( k)
782
.cloned ( )
783
.or_else ( | | self .store.get_identity ( k) .ok ( ) )
784
} ;
785
rule.keys.iter ( ) .any ( | k | {
786
if p.is_revoked ( k) {
787
return false ;
788
}
789
let mut cur = identity ( k) ;
790
for _ in 0 .. 32 {
791
let Some ( id) = cur else {
792
return false ;
793
} ;
794
match id.previous {
795
Some ( prev) if prev = = * signer => return at_ms < id.created,
796
Some ( prev) if ! p.is_revoked ( & prev) => cur = identity ( & prev) ,
797
_ => return false ,
798
}
799
}
800
false
801
} )
802
}
803
75167672 Andy McCall 1 month ago
804
fn check_ref_attest (
d36ba2f7 Andy McCall 16 days ago ↶
805
& self ,
806
sa : & SignedAttest,
807
name : & str ,
808
old_state : & ObjectRef,
809
new_state : & ObjectRef,
810
last_serial : Option< u64 > ,
811
allow_equal : bool ,
812
) -> std:: result:: Result< u64 , String> {
813
let serial =
814
self .check_ref_attest_body ( sa, name, old_state, new_state, last_serial, allow_equal) ? ;
815
self .verify_attest_signature ( sa) ? ;
816
Ok ( serial)
817
}
818
819
820
821
822
fn check_ref_attest_body (
75167672 Andy McCall 1 month ago
823
& self ,
824
sa : & SignedAttest,
825
name : & str ,
826
old_state : & ObjectRef,
827
new_state : & ObjectRef,
828
last_serial : Option< u64 > ,
829
allow_equal : bool ,
830
) -> std:: result:: Result< u64 , String> {
831
let AttestBody:: Ref {
832
name: n,
833
old_state: o,
834
new_state: nw,
835
serial,
836
} = & sa.attest.body
837
else {
98286406 Andy McCall 1 month ago ↶
838
return Err ( " attestation is not a ref attestation" .into ( ) ) ;
839
} ;
840
if n ! = name | | o ! = old_state | | nw ! = new_state {
841
return Err ( " attestation does not describe this update" .into ( ) ) ;
842
}
843
if sa.attest.repo ! = self .id ( ) .map_err ( | e | e.to_string ( ) ) ? {
844
return Err ( " attestation is for a different repository" .into ( ) ) ;
845
}
846
if let Some ( last) = last_serial {
847
if * serial < last | | ( * serial = = last & & ! allow_equal) {
75167672 Andy McCall 1 month ago
848
return Err ( format! (
849
" attestation serial {serial} is not greater than the last seen {last} "
850
) ) ;
98286406 Andy McCall 1 month ago ↶
851
}
852
}
d36ba2f7 Andy McCall 16 days ago ↶
853
Ok ( * serial)
854
}
855
856
857
858
fn verify_attest_signature ( & self , sa : & SignedAttest) -> std:: result:: Result< ( ) , String> {
98286406 Andy McCall 1 month ago ↶
859
let r = sa.attest.hash ( ) ;
860
let sig = sa.sigs.first ( ) .ok_or ( " attestation carries no signature" ) ? ;
75167672 Andy McCall 1 month ago
861
let id = self
862
.store
863
.get_identity ( & sig.key)
864
.map_err ( | _| format! ( " unknown key {} " , sig.key.short ( 12 ) ) ) ? ;
d36ba2f7 Andy McCall 16 days ago ↶
865
verify_signature ( & id, sig, Domain:: Attest, & r) .map_err ( | e | e.to_string ( ) )
98286406 Andy McCall 1 month ago ↶
866
}
867
83e0677b Andy McCall 1 month ago ↶
868
98286406 Andy McCall 1 month ago ↶
869
83e0677b Andy McCall 1 month ago ↶
870
871
872
75167672 Andy McCall 1 month ago
873
pub fn receive_push (
874
& self ,
875
bundle : Bundle,
876
updates : Vec< RefUpdate> ,
877
policy : Option< SignedPolicy> ,
878
tags : Vec< crate:: protocol:: TagRef> ,
879
) -> Result< Vec< RefResult> > {
119bbf50 Andy McCall 1 month ago ↶
880
Ok ( self
881
.receive_push_report ( bundle, updates, policy, tags) ?
882
.results )
7aa2dc93 Andy McCall 1 month ago ↶
883
}
884
885
886
887
888
pub fn receive_push_report (
889
& self ,
890
bundle : Bundle,
891
updates : Vec< RefUpdate> ,
892
policy : Option< SignedPolicy> ,
893
tags : Vec< crate:: protocol:: TagRef> ,
894
) -> Result< PushReport> {
767ac6cd Andy McCall 17 days ago
895
896
897
898
self .refresh ( ) ? ;
7aa2dc93 Andy McCall 1 month ago ↶
899
let mut report = PushReport:: default( ) ;
900
let results = & mut report.results;
83e0677b Andy McCall 1 month ago ↶
901
if let Some ( sp) = policy {
28fc23d7 Andy McCall 1 month ago ↶
902
match self .adopt_policy_chain ( & bundle.policies, & sp, " push received" ) {
83e0677b Andy McCall 1 month ago ↶
903
Ok ( ( ) ) => {
75167672 Andy McCall 1 month ago
904
results.push ( RefResult {
905
name: " policy" .into ( ) ,
906
ok: true ,
907
reason: format! ( " serial {} " , sp.policy.serial) ,
908
} ) ;
83e0677b Andy McCall 1 month ago ↶
909
}
910
Err ( e) => {
75167672 Andy McCall 1 month ago
911
results.push ( RefResult {
912
name: " policy" .into ( ) ,
913
ok: false ,
914
reason: e,
915
} ) ;
83e0677b Andy McCall 1 month ago ↶
916
for u in updates {
75167672 Andy McCall 1 month ago
917
results.push ( RefResult {
918
name: u.name,
919
ok: false ,
920
reason: " policy update was rejected" .into ( ) ,
921
} ) ;
83e0677b Andy McCall 1 month ago ↶
922
}
7aa2dc93 Andy McCall 1 month ago ↶
923
return Ok ( report) ;
83e0677b Andy McCall 1 month ago ↶
924
}
925
}
926
}
927
let policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp.policy ) ;
cb8b8082 Andy McCall 26 days ago ↶
928
929
930
let objects_only = updates.is_empty ( ) & & tags.is_empty ( ) ;
7aa2dc93 Andy McCall 1 month ago ↶
931
match self .import_bundle ( & bundle, policy.is_some ( ) ) {
cb8b8082 Andy McCall 26 days ago ↶
932
Ok ( imported) => {
933
if objects_only {
934
results.push ( RefResult {
935
name: OBJECTS_RESULT .into ( ) ,
936
ok: true ,
937
reason: format! ( " {} changes stored" , imported.len ( ) ) ,
938
} ) ;
939
}
940
report.imported = imported;
941
}
7aa2dc93 Andy McCall 1 month ago ↶
942
Err ( e) => {
cb8b8082 Andy McCall 26 days ago ↶
943
if objects_only {
944
results.push ( RefResult {
945
name: OBJECTS_RESULT .into ( ) ,
946
ok: false ,
947
reason: e.to_string ( ) ,
948
} ) ;
949
}
7aa2dc93 Andy McCall 1 month ago ↶
950
for u in updates {
951
results.push ( RefResult {
952
name: u.name,
953
ok: false ,
954
reason: e.to_string ( ) ,
955
} ) ;
956
}
957
return Ok ( report) ;
83e0677b Andy McCall 1 month ago ↶
958
}
959
}
98286406 Andy McCall 1 month ago ↶
960
let ( op_ref, op) = self .head_op ( ) ? ;
961
let mut view = op.view.clone ( ) ;
962
let mut names = Vec :: new( ) ;
963
for u in updates {
83e0677b Andy McCall 1 month ago ↶
964
let outcome = self .apply_update ( & mut view, & u, policy.as_ref ( ) ) ;
98286406 Andy McCall 1 month ago ↶
965
let ( ok, reason) = match outcome {
966
Ok ( ( ) ) => ( true , String :: new( ) ) ,
967
Err ( e) => ( false , e) ,
968
} ;
969
if ok {
970
names.push ( u.name.clone ( ) ) ;
119bbf50 Andy McCall 1 month ago ↶
971
let heads = self
972
.store
973
.get_state ( & u.new_state)
974
.map ( | s | s.heads )
975
.unwrap_or_default ( ) ;
976
report.updated.push ( RefUpdated {
977
name: u.name.clone ( ) ,
978
old_state: u.old_state,
979
new_state: u.new_state,
980
heads,
981
} ) ;
98286406 Andy McCall 1 month ago ↶
982
}
75167672 Andy McCall 1 month ago
983
results.push ( RefResult {
984
name: u.name,
985
ok,
986
reason,
987
} ) ;
98286406 Andy McCall 1 month ago ↶
988
}
132b884b Andy McCall 1 month ago ↶
989
for t in & tags {
990
let name = format! ( " tags/{} " , t.name) ;
b63fc7a7 Andy McCall 11 days ago ↶
991
if t.is_withdrawal ( ) {
992
match self .adopt_withdrawal ( & mut view, t, policy.as_ref ( ) , & mut TagCheck:: Received)
993
{
994
Ok ( removed) => {
995
996
997
if removed {
998
report.updated.push ( RefUpdated {
999
name: name.clone ( ) ,
1000
old_state: t.state,
1001
new_state: ObjectRef:: ZERO ,
1002
heads: vec! [ ] ,
1003
} ) ;
1004
}
1005
names.push ( name.clone ( ) ) ;
1006
results.push ( RefResult {
1007
name,
1008
ok: true ,
1009
reason: if removed {
1010
" withdrawn"
1011
} else {
1012
" already withdrawn"
1013
}
1014
.into ( ) ,
1015
} ) ;
1016
}
1017
Err ( e) => results.push ( RefResult {
1018
name,
1019
ok: false ,
1020
reason: e.reason,
1021
} ) ,
1022
}
1023
continue ;
1024
}
b2ef93bb Andy McCall 1 month ago ↶
1025
let new = ! view.tags.contains_key ( & t.name) ;
1ec6fdbc Andy McCall 16 days ago ↶
1026
match self .adopt_tag ( & mut view, t, policy.as_ref ( ) , & mut TagCheck:: Received) {
132b884b Andy McCall 1 month ago ↶
1027
Ok ( ( ) ) => {
b2ef93bb Andy McCall 1 month ago ↶
1028
1029
1030
1031
if new {
1032
report.updated.push ( RefUpdated {
1033
name: name.clone ( ) ,
1034
old_state: ObjectRef:: ZERO ,
1035
new_state: t.state,
1036
heads: t.heads.clone ( ) ,
1037
} ) ;
1038
}
132b884b Andy McCall 1 month ago ↶
1039
names.push ( name.clone ( ) ) ;
75167672 Andy McCall 1 month ago
1040
results.push ( RefResult {
1041
name,
1042
ok: true ,
1043
reason: String :: new( ) ,
1044
} ) ;
132b884b Andy McCall 1 month ago ↶
1045
}
75167672 Andy McCall 1 month ago
1046
Err ( e) => results.push ( RefResult {
1047
name,
1048
ok: false ,
1ec6fdbc Andy McCall 16 days ago ↶
1049
reason: e.reason,
75167672 Andy McCall 1 month ago
1050
} ) ,
132b884b Andy McCall 1 month ago ↶
1051
}
1052
}
98286406 Andy McCall 1 month ago ↶
1053
if ! names.is_empty ( ) {
75167672 Andy McCall 1 month ago
1054
self .record_op (
1055
& [ op_ref] ,
1056
& format! ( " push received: {} " , names.join ( " " ) ) ,
1057
view,
1058
) ? ;
98286406 Andy McCall 1 month ago ↶
1059
}
7aa2dc93 Andy McCall 1 month ago ↶
1060
Ok ( report)
98286406 Andy McCall 1 month ago ↶
1061
}
1062
be451356 Andy McCall 1 month ago ↶
1063
1064
1065
1066
1067
fn record_update_attest (
1068
& self ,
1069
u : & RefUpdate,
1070
sa : & SignedAttest,
1071
) -> std:: result:: Result< ( ) , String> {
1072
let name = & u.name;
1073
let last = self
1074
.latest_ref_attest ( name)
1075
.map_err ( | e | format! ( " reading the last attestation of {name:?} : {e} " ) ) ?
1076
.and_then ( | ( _, a ) | match a.attest.body {
1077
AttestBody:: Ref { serial, .. } => Some ( serial) ,
1078
_ => None ,
1079
} ) ;
1080
self .check_ref_attest ( sa, name, & u.old_state, & u.new_state, last, false ) ? ;
1081
let store = | e : & dyn std:: fmt:: Display| format! ( " storing the attestation of {name:?} : {e} " ) ;
1082
let r = self .store.put_attest ( & sa.attest) .map_err ( | e | store ( & e) ) ? ;
1083
for sig in & sa.sigs {
1084
self .store.put_signature ( & r, sig) .map_err ( | e | store ( & e) ) ? ;
f53cbe7a Andy McCall 18 days ago ↶
1085
}
1086
self .set_ref_attest ( name, & r) .map_err ( | e | store ( & e) ) ? ;
1087
1088
let certified = sa
1089
.sigs
1090
.first ( )
1091
.is_some_and ( | s | self .store.certificate_of ( & s.key) .ok ( ) .flatten ( ) .is_some ( ) ) ;
1092
if certified {
1093
self .record_acceptance ( & r) .map_err ( | e | store ( & e) ) ? ;
be451356 Andy McCall 1 month ago ↶
1094
}
f53cbe7a Andy McCall 18 days ago ↶
1095
Ok ( ( ) )
be451356 Andy McCall 1 month ago ↶
1096
}
1097
f53cbe7a Andy McCall 18 days ago ↶
1098
1099
1100
pub ( crate ) fn record_acceptance ( & self , r : & ObjectRef) -> Result< ( ) > {
1101
let Some ( acceptor) = & self .acceptor else {
1102
return Ok ( ( ) ) ;
1103
} ;
1104
let ( id, sk) = ( & acceptor.0 , & acceptor.1 ) ;
1105
let attest = Attest {
1106
repo: self .id ( ) ? ,
1107
hlc: Hlc:: now( ) ,
1108
body: AttestBody:: Accepted { attest: * r } ,
1109
} ;
1110
let sig = sk.sign ( Domain:: Attest, & id.hash ( ) , & attest.hash ( ) ) ;
1111
self .store.put_identity ( id) ? ;
1112
let a = self .store.put_attest ( & attest) ? ;
1113
self .store.put_signature ( & a, & sig) ? ;
1114
self .store.index_add_pub ( " accepted" , r, & a) ? ;
1115
Ok ( ( ) )
1116
}
1117
1118
1119
pub fn acceptance_of ( & self , r : & ObjectRef) -> Result< Option< SignedAttest> > {
1120
let Some ( a) = self .store.index_get_pub ( " accepted" , r) ? .into_iter ( ) .next ( ) else {
1121
return Ok ( None ) ;
1122
} ;
1123
Ok ( Some ( SignedAttest {
1124
attest: self .store.get_attest ( & a) ? ,
1125
sigs: self .store.signatures ( & a) ? ,
1126
} ) )
1127
}
1128
1129
1130
1131
1132
1133
pub ( crate ) fn certified_time (
1134
& self ,
1135
signer : & KeyId,
1136
sa : & SignedAttest,
1137
accepted : Option< & SignedAttest> ,
1138
) -> u64 {
1ec6fdbc Andy McCall 16 days ago ↶
1139
self .issuer_acceptance ( signer, sa, accepted)
1140
.unwrap_or ( sa.attest.hlc.wall_ms)
1141
}
1142
1143
1144
1145
pub ( crate ) fn issuer_acceptance (
1146
& self ,
1147
signer : & KeyId,
1148
sa : & SignedAttest,
1149
accepted : Option< & SignedAttest> ,
1150
) -> Option< u64 > {
1151
let acc = accepted? ;
1152
let c = self .store.certificate_of ( signer) .ok ( ) .flatten ( ) ? ;
1153
let cert = self .store.get_certificate ( & c) .ok ( ) ? ;
f53cbe7a Andy McCall 18 days ago ↶
1154
let AttestBody:: Accepted { attest } = & acc.attest.body else {
1ec6fdbc Andy McCall 16 days ago ↶
1155
return None ;
f53cbe7a Andy McCall 18 days ago ↶
1156
} ;
1ec6fdbc Andy McCall 16 days ago ↶
1157
let sig = acc.sigs.iter ( ) .find ( | s | s.key = = cert.cert.issuer ) ? ;
1158
let issuer = self .store.get_identity ( & cert.cert.issuer) .ok ( ) ? ;
1159
( * attest = = sa.attest.hash ( )
1160
& & verify_signature ( & issuer, sig, Domain:: Attest, & acc.attest.hash ( ) ) .is_ok ( ) )
1161
.then_some ( acc.attest.hlc.wall_ms)
1162
}
1163
1164
1165
1166
1167
pub ( crate ) fn keep_acceptance ( & self , r : & ObjectRef, acc : & SignedAttest) -> Result< ( ) > {
1168
let a = self .store.put_attest ( & acc.attest) ? ;
1169
for sig in & acc.sigs {
1170
self .store.put_signature ( & a, sig) ? ;
f53cbe7a Andy McCall 18 days ago ↶
1171
}
1ec6fdbc Andy McCall 16 days ago ↶
1172
self .store.index_add_pub ( " accepted" , r, & a) ? ;
1173
Ok ( ( ) )
f53cbe7a Andy McCall 18 days ago ↶
1174
}
1175
028545df Andy McCall 17 days ago ↶
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
# [ allow ( clippy::too_many_arguments ) ]
1186
pub ( crate ) fn authorize_in_time (
1187
& self ,
1188
p : & crate:: policy:: Policy,
1189
remote_name : & str ,
1190
r : & crate:: protocol:: RemoteRef,
1191
sa : & SignedAttest,
1192
signer : & KeyId,
1193
log : & super:: transparency:: LogCheck,
1194
notes : & mut Vec< String> ,
1195
) -> Result< ( ) > {
1196
use super :: transparency:: { gap_note, judge, Timed} ;
1197
let own = sa.attest.hlc.wall_ms;
1198
let at = self .certified_time ( signer, sa, r.accepted.as_ref ( ) ) ;
1199
let time = log.time_of ( & r.name) ;
1200
let check = | t_delegate : u64 , t_cert : u64 | {
1201
self .delegate_authorized ( p, signer, & r.name, t_delegate)
1202
| | self .certified_authorized ( p, signer, & r.name, Scope:: REFS , t_cert)
1203
} ;
1204
let before = if at ! = own {
1205
" the host's acceptance"
1206
} else {
1207
" the time it claims"
1208
} ;
1209
let what = format! ( " {remote_name} /{} " , r.name) ;
1210
match judge ( time.as_ref ( ) .ok ( ) .map ( | i | i.ms ( ) ) , | t | check ( t, t) , | | check ( own, at) ) {
1211
Timed:: Refused => {
1212
return fail ( format! (
1213
" {what} : attested by {} , which the policy does not authorize for this bookmark; a policy owner can grant it with `oxy policy grant`" ,
1214
signer.short ( 12 )
1215
) )
1216
}
1217
Timed:: Integrated => { }
1218
Timed:: Disagrees => notes.push ( format! (
1219
" {what} : {} 's authority had lapsed when witness {} first saw its attestation, though not at {before} ; accepted while transparency logs are new" ,
1220
signer.short ( 12 ) ,
1221
time.as_ref ( ) .map ( | i | i.witness.as_str ( ) ) .unwrap_or ( " ?" )
1222
) ) ,
1223
Timed:: Fallback if log.kept => notes.push ( format! (
1224
" {what} : {} checked at {before} , not at a witnessed time: {} " ,
1225
signer.short ( 12 ) ,
1226
time.as_ref ( ) .err ( ) .map ( String :: as_str) .unwrap_or ( " none" )
1227
) ) ,
1228
Timed:: Fallback => { }
1229
}
1230
if let Some ( n) = time.ok ( ) .and_then ( | i | gap_note ( own, i) ) {
1231
notes.push ( format! ( " {what} 's attestation {n} " ) ) ;
1232
}
1233
Ok ( ( ) )
1234
}
1235
f8c69576 Andy McCall 1 month ago ↶
1236
8c30d814 Andy McCall 1 month ago ↶
1237
1238
1239
1240
f8c69576 Andy McCall 1 month ago ↶
1241
1242
1243
fn apply_deletion (
1244
& self ,
1245
view : & mut View,
1246
u : & RefUpdate,
1247
policy : Option< & crate:: policy:: Policy> ,
1248
) -> std:: result:: Result< ( ) , String> {
1249
let Some ( current) = view.bookmarks.get ( & u.name) .copied ( ) else {
1250
return Err ( format! ( " no bookmark {:?} to delete" , u.name) ) ;
1251
} ;
1252
if current ! = u.old_state {
1253
return Err ( " stale: the remote bookmark has moved; pull first" .into ( ) ) ;
1254
}
8c30d814 Andy McCall 1 month ago ↶
1255
if ! self .is_bare ( ) & & u.name = = self .config.bookmark {
f8c69576 Andy McCall 1 month ago ↶
1256
return Err ( format! (
8c30d814 Andy McCall 1 month ago ↶
1257
" {:?} is this repository's default bookmark (checked out there) and cannot be deleted" ,
f8c69576 Andy McCall 1 month ago ↶
1258
u.name
1259
) ) ;
1260
}
1261
if let Some ( p) = policy {
1262
let Some ( sa) = & u.attestation else {
1263
return Err ( format! (
1264
" deleting {:?} requires a signed reference attestation under this repository's policy" ,
1265
u.name
1266
) ) ;
1267
} ;
1268
let signer = sa
1269
.sigs
1270
.first ( )
1271
.map ( | s | s.key )
1272
.ok_or ( " attestation carries no signature" ) ? ;
f0a517d2 Andy McCall 18 days ago ↶
1273
1274
let now = Hlc:: now( ) .wall_ms;
7ba155f2 Andy McCall 1 month ago ↶
1275
if ! p.authorizes ( & signer, & u.name)
1276
& & ! self .delegate_authorized ( p, & signer, & u.name, sa.attest.hlc.wall_ms)
f0a517d2 Andy McCall 18 days ago ↶
1277
& & ! self .certified_authorized ( p, & signer, & u.name, Scope:: REFS , now)
7ba155f2 Andy McCall 1 month ago ↶
1278
{
f8c69576 Andy McCall 1 month ago ↶
1279
return Err ( format! (
1280
" key {} may not delete {:?} under this repository's policy" ,
1281
signer.short ( 12 ) ,
1282
u.name
1283
) ) ;
1284
}
1285
}
1286
if let Some ( sa) = & u.attestation {
be451356 Andy McCall 1 month ago ↶
1287
self .record_update_attest ( u, sa) ? ;
f8c69576 Andy McCall 1 month ago ↶
1288
}
1289
view.bookmarks.remove ( & u.name) ;
1290
Ok ( ( ) )
1291
}
1292
75167672 Andy McCall 1 month ago
1293
fn apply_update (
1294
& self ,
1295
view : & mut View,
1296
u : & RefUpdate,
1297
policy : Option< & crate:: policy:: Policy> ,
1298
) -> std:: result:: Result< ( ) , String> {
83e0677b Andy McCall 1 month ago ↶
1299
if u.name = = " policy" {
75167672 Andy McCall 1 month ago
1300
return Err (
1301
" the policy reference is updated by sending a policy, not a bookmark update" .into ( ) ,
1302
) ;
83e0677b Andy McCall 1 month ago ↶
1303
}
75167672 Andy McCall 1 month ago
1304
let empty = self
1305
.store
1306
.put_state ( & StateObject:: new( vec! [ ] ) )
1307
.map_err ( | e | e.to_string ( ) ) ? ;
f8c69576 Andy McCall 1 month ago ↶
1308
if u.new_state.is_zero ( ) {
1309
return self .apply_deletion ( view, u, policy) ;
1310
}
98286406 Andy McCall 1 month ago ↶
1311
let current = view.bookmarks.get ( & u.name) .copied ( ) .unwrap_or ( empty) ;
1312
if current ! = u.old_state {
1313
return Err ( " stale: the remote bookmark has moved; pull first" .into ( ) ) ;
1314
}
75167672 Andy McCall 1 month ago
1315
let new_state = self
1316
.store
1317
.get_state ( & u.new_state)
1318
.map_err ( | _| " new state object is missing" .to_string ( ) ) ? ;
98286406 Andy McCall 1 month ago ↶
1319
for h in & new_state.heads {
1320
if ! self .store.has ( h) {
1321
return Err ( format! ( " head {} is missing" , h.short ( 12 ) ) ) ;
1322
}
1323
}
1324
if ! u.force {
75167672 Andy McCall 1 month ago
1325
let old_heads = self
1326
.store
1327
.get_state ( & current)
1328
.map_err ( | e | e.to_string ( ) ) ?
1329
.heads;
1330
let closure = self
1331
.closure_in_store ( & new_state.heads)
1332
.map_err ( | e | e.to_string ( ) ) ? ;
98286406 Andy McCall 1 month ago ↶
1333
if let Some ( h) = old_heads.iter ( ) .find ( | h | ! closure.contains ( h) ) {
75167672 Andy McCall 1 month ago
1334
return Err ( format! (
1335
" not a fast-forward: current head {} would be dropped (use --force)" ,
1336
h.short ( 12 )
1337
) ) ;
98286406 Andy McCall 1 month ago ↶
1338
}
1339
}
83e0677b Andy McCall 1 month ago ↶
1340
if let Some ( p) = policy {
1341
let Some ( sa) = & u.attestation else {
75167672 Andy McCall 1 month ago
1342
return Err ( format! (
1343
" {:?} requires a signed reference attestation under this repository's policy" ,
1344
u.name
1345
) ) ;
83e0677b Andy McCall 1 month ago ↶
1346
} ;
75167672 Andy McCall 1 month ago
1347
let signer = sa
1348
.sigs
1349
.first ( )
1350
.map ( | s | s.key )
1351
.ok_or ( " attestation carries no signature" ) ? ;
f0a517d2 Andy McCall 18 days ago ↶
1352
1353
let now = Hlc:: now( ) .wall_ms;
7ba155f2 Andy McCall 1 month ago ↶
1354
if ! p.authorizes ( & signer, & u.name)
1355
& & ! self .delegate_authorized ( p, & signer, & u.name, sa.attest.hlc.wall_ms)
f0a517d2 Andy McCall 18 days ago ↶
1356
& & ! self .certified_authorized ( p, & signer, & u.name, Scope:: REFS , now)
7ba155f2 Andy McCall 1 month ago ↶
1357
{
75167672 Andy McCall 1 month ago
1358
return Err ( format! (
1359
" key {} may not update {:?} under this repository's policy" ,
1360
signer.short ( 12 ) ,
1361
u.name
1362
) ) ;
83e0677b Andy McCall 1 month ago ↶
1363
}
75167672 Andy McCall 1 month ago
1364
let old_heads = self
1365
.store
1366
.get_state ( & current)
1367
.map_err ( | e | e.to_string ( ) ) ?
1368
.heads;
83e0677b Andy McCall 1 month ago ↶
1369
self .state_allowed ( & old_heads, & new_state.heads, p, & u.name) ? ;
1370
}
98286406 Andy McCall 1 month ago ↶
1371
if let Some ( sa) = & u.attestation {
be451356 Andy McCall 1 month ago ↶
1372
self .record_update_attest ( u, sa) ? ;
98286406 Andy McCall 1 month ago ↶
1373
}
1374
view.bookmarks.insert ( u.name.clone ( ) , u.new_state) ;
1375
Ok ( ( ) )
1376
}
1377
1378
1379
1380
fn remote_url ( & self , name : & str ) -> Result< String> {
75167672 Andy McCall 1 month ago
1381
self .config.remotes.get ( name) .cloned ( ) .ok_or_else ( | | {
42ccf504 Andy McCall 27 days ago ↶
1382
let names: Vec< & str > = self .config.remotes.keys ( ) .map ( String :: as_str) .collect ( ) ;
1383
RepoError:: Msg( match names.as_slice ( ) {
1384
[ ] => format! ( " no remote named {name:?} ; add one with `oxy remote add`" ) ,
1385
_ if name = = " origin" & & names.len ( ) > 1 & & self .config.default_remote.is_none ( ) => format! (
1386
" this repository has no remote named origin, and several others ({} ): name one (`oxy pull {} `), or make one the default with `oxy config core.remote {} `" ,
1387
names.join ( " , " ) ,
1388
names[ 0 ] ,
1389
names[ 0 ]
1390
) ,
1391
_ => format! (
1392
" no remote named {name:?} ; this repository has {} " ,
1393
names.join ( " , " )
1394
) ,
1395
} )
75167672 Andy McCall 1 month ago
1396
} )
98286406 Andy McCall 1 month ago ↶
1397
}
1398
75167672 Andy McCall 1 month ago
1399
1400
1401
05e71ed6 Andy McCall 1 month ago ↶
1402
fn check_remote_id (
1403
& self ,
1404
remote : & dyn crate:: protocol:: Remote,
1405
remote_name : & str ,
1406
allow_unbound : bool ,
1407
) -> Result< ( ) > {
75167672 Andy McCall 1 month ago
1408
let theirs = remote.repo_id ( ) ? ;
1409
if theirs = = self .id ( ) ? {
1410
return Ok ( ( ) ) ;
1411
}
1412
if theirs = = crate :: protocol:: UNBOUND_ID {
1413
if allow_unbound {
1414
return Ok ( ( ) ) ;
1415
}
05e71ed6 Andy McCall 1 month ago ↶
1416
return fail ( format! (
1417
" remote {remote_name:?} is empty and unbound: push to it first"
1418
) ) ;
75167672 Andy McCall 1 month ago
1419
}
05e71ed6 Andy McCall 1 month ago ↶
1420
fail ( format! (
3e5772cc Andy McCall 1 month ago ↶
1421
" remote {remote_name:?} is a different repository (repository IDs differ); check its URL with `oxy remote list`"
05e71ed6 Andy McCall 1 month ago ↶
1422
) )
75167672 Andy McCall 1 month ago
1423
}
1424
ff1f62fe Andy McCall 1 month ago ↶
1425
1426
1427
3e5772cc Andy McCall 1 month ago ↶
1428
pub ( crate ) fn remote_options ( & self , name : & str ) -> RemoteOptions {
ff1f62fe Andy McCall 1 month ago ↶
1429
let url = self .config.remotes.get ( name) .cloned ( ) .unwrap_or_default ( ) ;
1430
let mut options = RemoteOptions {
75167672 Andy McCall 1 month ago
1431
repo_id: self .id ( ) .ok ( ) ,
ff1f62fe Andy McCall 1 month ago ↶
1432
token: self .config.remote_tokens.get ( name) .cloned ( ) ,
1433
ca: self .config.remote_cas.get ( name) .map ( PathBuf:: from) ,
9f9e72b9 Andy McCall 1 month ago ↶
1434
cache_dir: Some ( self .oxy.join ( " remotes" ) .join ( name) .join ( " strata" ) ) ,
68a92484 Andy McCall 22 days ago ↶
1435
.. RemoteOptions:: default( )
ff1f62fe Andy McCall 1 month ago ↶
1436
} ;
1437
self .fill_credentials ( & url, & mut options) ;
1438
options
1439
}
1440
1441
1442
1443
pub fn credentials ( & self ) -> crate:: credentials:: Credentials {
1444
crate :: credentials:: Credentials:: at(
1445
self .user_config
1446
.parent ( )
1447
.map ( Path:: to_path_buf)
1448
.unwrap_or_else ( default_home) ,
1449
)
1450
}
1451
1452
1453
1454
68a92484 Andy McCall 22 days ago ↶
1455
1456
1457
1458
ff1f62fe Andy McCall 1 month ago ↶
1459
fn fill_credentials ( & self , url : & str , options : & mut RemoteOptions) {
1460
if options.token.is_some ( ) & & options.ca.is_some ( ) {
1461
return ;
1462
}
68a92484 Andy McCall 22 days ago ↶
1463
let explicit =
1464
options.token.is_some ( ) | | std:: env:: var( " OXY_TOKEN" ) .is_ok_and ( | t | ! t.is_empty ( ) ) ;
ff1f62fe Andy McCall 1 month ago ↶
1465
let stored = self .credentials ( ) .options_for ( url) ;
1466
options.token = options.token.take ( ) .or ( stored.token) ;
1467
options.ca = options.ca.take ( ) .or ( stored.ca) ;
68a92484 Andy McCall 22 days ago ↶
1468
let enclosing = self .enclosing_credentials ( url) ;
1469
if options.token.is_none ( ) {
1470
options.token = enclosing.iter ( ) .find_map ( | ( t , _) | t.clone ( ) ) ;
1471
}
1472
if options.ca.is_none ( ) {
1473
options.ca = enclosing.iter ( ) .find_map ( | ( _, c ) | c.clone ( ) ) ;
1474
}
1475
if ! explicit {
1476
for t in enclosing.into_iter ( ) .filter_map ( | ( t , _) | t ) {
1477
if options.token.as_ref ( ) ! = Some ( & t) & & ! options.fallback_tokens.contains ( & t) {
1478
options.fallback_tokens.push ( t) ;
1479
}
1480
}
2b5ee371 Andy McCall 1 month ago ↶
1481
}
68a92484 Andy McCall 22 days ago ↶
1482
}
1483
1484
1485
1486
fn enclosing_credentials ( & self , url : & str ) -> Vec< ( Option< String> , Option< PathBuf> ) > {
1487
let mut out = Vec :: new( ) ;
ff1f62fe Andy McCall 1 month ago ↶
1488
let Some ( hub) = crate :: credentials:: hub_of( url) else {
68a92484 Andy McCall 22 days ago ↶
1489
return out;
ff1f62fe Andy McCall 1 month ago ↶
1490
} ;
1491
let mut at = self .root.parent ( ) ;
1492
while let Some ( dir) = at {
1493
let Ok ( cfg) = read_config_file ( & dir.join ( OXY_DIR ) .join ( " config" ) ) else {
1494
break ;
1495
} ;
1496
for ( key, remote_url) in & cfg {
1497
let Some ( name) = key
1498
.strip_prefix ( " remote." )
1499
.and_then ( | k | k.strip_suffix ( " .url" ) )
1500
else {
1501
continue ;
1502
} ;
1503
if crate :: credentials:: hub_of( remote_url) .as_deref ( ) ! = Some ( hub.as_str ( ) ) {
1504
continue ;
1505
}
68a92484 Andy McCall 22 days ago ↶
1506
out.push ( (
1507
cfg.get ( & format! ( " remote.{name} .token" ) ) .cloned ( ) ,
1508
cfg.get ( & format! ( " remote.{name} .ca" ) ) .map ( PathBuf:: from) ,
1509
) ) ;
ff1f62fe Andy McCall 1 month ago ↶
1510
}
1511
at = dir.parent ( ) ;
1512
}
68a92484 Andy McCall 22 days ago ↶
1513
out
98286406 Andy McCall 1 month ago ↶
1514
}
1515
d82e4409 Andy McCall 1 month ago ↶
1516
1517
1518
pub fn push_seals ( & self , remote_name : & str , seals : & [ObjectRef]) -> Result< ( ) > {
1519
let remote = self .open_named_remote ( remote_name) ? ;
1520
self .check_remote_id ( remote.as_ref ( ) , remote_name, false ) ? ;
1521
let mut bundle = Bundle:: default( ) ;
1522
for r in seals {
1523
let attest = self .store.get_attest ( r) ? ;
1524
let sigs = self .store.signatures ( r) ? ;
4213c41d Andy McCall 1 month ago ↶
1525
bundle
1526
.attests
1527
.push ( crate :: protocol:: SignedAttest { attest, sigs } ) ;
d82e4409 Andy McCall 1 month ago ↶
1528
}
1529
let results = remote.push ( bundle, vec! [ ] , None , vec! [ ] ) ? ;
1530
if let Some ( r) = results.iter ( ) .find ( | r | ! r.ok) {
1531
return fail ( format! ( " {} : {} " , r.name, r.reason) ) ;
1532
}
1533
Ok ( ( ) )
1534
}
1535
1536
1537
pub fn remote_for_url ( & self , base : & str ) -> Option< String> {
1538
let base = base.trim_end_matches ( ' /' ) ;
4213c41d Andy McCall 1 month ago ↶
1539
self .config
1540
.remotes
1541
.iter ( )
1542
.find ( | ( _, url ) | url.trim_end_matches ( ' /' ) = = base )
1543
.map ( | ( name , _) | name.clone ( ) )
d82e4409 Andy McCall 1 month ago ↶
1544
}
1545
1546
ff1f62fe Andy McCall 1 month ago ↶
1547
d82e4409 Andy McCall 1 month ago ↶
1548
pub fn open_remote_at ( & self , url : & str ) -> Result< ( String, Box< dyn crate:: protocol:: Remote> ) > {
1549
if let Some ( name) = self .remote_for_url ( url) {
1550
return Ok ( ( name.clone ( ) , self .open_named_remote ( & name) ? ) ) ;
1551
}
1552
let options = RemoteOptions {
1553
repo_id: self .id ( ) .ok ( ) ,
ff1f62fe Andy McCall 1 month ago ↶
1554
.. self .credentials_for ( url)
d82e4409 Andy McCall 1 month ago ↶
1555
} ;
1556
Ok ( ( url.to_string ( ) , open_remote ( url, & options) ? ) )
1557
}
1558
1559
1560
1561
pub fn options_for_url ( & self , url : & str ) -> RemoteOptions {
1562
match self .remote_for_url ( url) {
1563
Some ( name) => self .remote_options ( & name) ,
1564
None => RemoteOptions {
1565
repo_id: self .id ( ) .ok ( ) ,
ff1f62fe Andy McCall 1 month ago ↶
1566
.. self .credentials_for ( url)
d82e4409 Andy McCall 1 month ago ↶
1567
} ,
1568
}
1569
}
1570
98286406 Andy McCall 1 month ago ↶
1571
fn open_named_remote ( & self , name : & str ) -> Result< Box< dyn crate:: protocol:: Remote> > {
2b5ee371 Andy McCall 1 month ago ↶
1572
open_remote ( & self .remote_url ( name) ? , & self .remote_options ( name) )
98286406 Andy McCall 1 month ago ↶
1573
}
1574
1575
fn serial_path ( & self , remote : & str , bookmark : & str ) -> PathBuf {
75167672 Andy McCall 1 month ago
1576
self .oxy
1577
.join ( " remotes" )
1578
.join ( remote)
1579
.join ( format! ( " {bookmark} .serial" ) )
98286406 Andy McCall 1 month ago ↶
1580
}
1581
1582
fn last_seen_serial ( & self , remote : & str , bookmark : & str ) -> Option< u64 > {
75167672 Andy McCall 1 month ago
1583
fs:: read_to_string( self .serial_path ( remote, bookmark) )
1584
.ok ( )
1585
.and_then ( | s | s.trim ( ) .parse ( ) .ok ( ) )
98286406 Andy McCall 1 month ago ↶
1586
}
1587
1588
fn record_serial ( & self , remote : & str , bookmark : & str , serial : u64 ) -> Result< ( ) > {
1589
let p = self .serial_path ( remote, bookmark) ;
1590
fs:: create_dir_all( p.parent ( ) .unwrap ( ) ) ? ;
1591
write_atomic ( & p, serial.to_string ( ) .as_bytes ( ) )
1592
}
1593
83e0677b Andy McCall 1 month ago ↶
1594
fn policy_serial_path ( & self , remote : & str ) -> PathBuf {
1595
self .oxy.join ( " remotes" ) .join ( remote) .join ( " policy.serial" )
1596
}
1597
1598
1599
fn remote_policy_serial ( & self , remote : & str ) -> Option< u64 > {
75167672 Andy McCall 1 month ago
1600
fs:: read_to_string( self .policy_serial_path ( remote) )
1601
.ok ( )
1602
.and_then ( | s | s.trim ( ) .parse ( ) .ok ( ) )
83e0677b Andy McCall 1 month ago ↶
1603
}
1604
1605
fn record_policy_serial ( & self , remote : & str , serial : u64 ) -> Result< ( ) > {
1606
let p = self .policy_serial_path ( remote) ;
1607
fs:: create_dir_all( p.parent ( ) .unwrap ( ) ) ? ;
1608
write_atomic ( & p, serial.to_string ( ) .as_bytes ( ) )
1609
}
1610
b3c5a59a Andy McCall 1 month ago ↶
1611
fn lazy_dir ( & self ) -> PathBuf {
1612
self .oxy.join ( " lazy" )
1613
}
1614
1615
1616
fn register_lazy ( & self , blob : & ObjectRef, remote : & str ) -> Result< ( ) > {
1617
fs:: create_dir_all( self .lazy_dir ( ) ) ? ;
1618
write_atomic ( & self .lazy_dir ( ) .join ( blob.to_hex ( ) ) , remote.as_bytes ( ) )
1619
}
1620
1621
1622
pub fn lazy_remote ( & self , blob : & ObjectRef) -> Option< String> {
75167672 Andy McCall 1 month ago
1623
fs:: read_to_string( self .lazy_dir ( ) .join ( blob.to_hex ( ) ) )
1624
.ok ( )
1625
.map ( | s | s.trim ( ) .to_string ( ) )
b3c5a59a Andy McCall 1 month ago ↶
1626
}
1627
1628
1629
pub ( crate ) fn fetch_lazy_blob ( & self , blob : & ObjectRef) -> Result< ( ) > {
1630
let Some ( remote_name) = self .lazy_remote ( blob) else {
1631
return Err ( RepoError:: Store( StoreError:: NotFound( * blob) ) ) ;
1632
} ;
1633
let remote = self .open_named_remote ( & remote_name) ? ;
1634
let objects = remote.objects ( & [ * blob] ) ? ;
1635
for ( ty, body) in objects {
1636
if matches! ( ty, ObjectType:: Blob | ObjectType:: Chunk) {
1637
self .store.put ( ty, & body) ? ;
1638
}
1639
}
1640
if ! self .store.has ( blob) {
75167672 Andy McCall 1 month ago
1641
return fail ( format! (
1642
" {remote_name} did not send blob {} " ,
1643
blob.short ( 12 )
1644
) ) ;
b3c5a59a Andy McCall 1 month ago ↶
1645
}
1646
let _ = fs:: remove_file( self .lazy_dir ( ) .join ( blob.to_hex ( ) ) ) ;
1647
Ok ( ( ) )
1648
}
1649
1650
1651
1652
fn register_missing_blobs ( & self , changes : & [ChangeHash], remote : & str ) -> Result< usize > {
1653
let mut n = 0 ;
1654
for h in changes {
1655
let c = self .store.get_change ( h) ? ;
1656
for e in & c.edits {
1657
if let Edit:: Replace { old, new, .. } = e {
1658
for b in [ old, new] .into_iter ( ) .flatten ( ) {
1659
if ! self .store.has ( b) {
1660
self .register_lazy ( b, remote) ? ;
1661
n + = 1 ;
1662
}
1663
}
1664
}
1665
}
1666
}
1667
Ok ( n)
1668
}
1669
98286406 Andy McCall 1 month ago ↶
1670
d82e4409 Andy McCall 1 month ago ↶
1671
pub fn known_heads ( & self , view : & View) -> Result< Vec< ChangeHash> > {
98286406 Andy McCall 1 month ago ↶
1672
let mut have: BTreeSet< ChangeHash> = BTreeSet:: new( ) ;
75167672 Andy McCall 1 month ago
1673
for st in view
1674
.bookmarks
1675
.values ( )
1676
.chain ( view.remotes.values ( ) .flat_map ( | b | b.values ( ) ) )
1677
{
98286406 Andy McCall 1 month ago ↶
1678
if let Ok ( s) = self .store.get_state ( st) {
1679
have.extend ( s.heads) ;
1680
}
1681
}
1682
have.extend ( view.workspaces.values ( ) .copied ( ) ) ;
1683
Ok ( have.into_iter ( ) .collect ( ) )
1684
}
1685
1686
83e0677b Andy McCall 1 month ago ↶
1687
pub fn fetch ( & self , remote_name : & str ) -> Result< FetchReport> {
98286406 Andy McCall 1 month ago ↶
1688
let remote = self .open_named_remote ( remote_name) ? ;
75167672 Andy McCall 1 month ago
1689
self .check_remote_id ( remote.as_ref ( ) , remote_name, false ) ? ;
83e0677b Andy McCall 1 month ago ↶
1690
let resp = remote.refs ( ) ? ;
4b9d0677 Andy McCall 18 days ago ↶
1691
028545df Andy McCall 17 days ago ↶
1692
let log = self .check_log (
4b9d0677 Andy McCall 18 days ago ↶
1693
& self .remote_url ( remote_name) ? ,
1694
self .config.remote_cas.get ( remote_name) .map ( Path:: new) ,
1695
& resp,
1696
) ? ;
028545df Andy McCall 17 days ago ↶
1697
let mut log_notes = log.notes.clone ( ) ;
83e0677b Andy McCall 1 month ago ↶
1698
1699
1700
1701
1702
1703
let local_policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp.policy ) ;
1704
let seen = self .remote_policy_serial ( remote_name) ;
1705
let mut unverified = false ;
1706
match & resp.policy {
1707
Some ( sp) => {
1708
if seen.is_some_and ( | s | sp.policy.serial < s ) {
1709
return fail ( format! ( " {remote_name} presents policy serial {} but presented {} before: refusing the downgrade" , sp.policy.serial, seen.unwrap ( ) ) ) ;
1710
}
1711
match & local_policy {
1712
Some ( l) if sp.policy.serial < l.serial => { }
1713
Some ( l) if sp.policy.serial = = l.serial => {
1714
if sp.policy.hash ( ) ! = l.hash ( ) {
75167672 Andy McCall 1 month ago
1715
return fail ( format! (
1716
" {remote_name} presents a different policy with the same serial {} " ,
1717
l.serial
1718
) ) ;
83e0677b Andy McCall 1 month ago ↶
1719
}
1720
}
28fc23d7 Andy McCall 1 month ago ↶
1721
_ => {
75167672 Andy McCall 1 month ago
1722
self .adopt_policy_chain (
1723
& resp.history,
1724
sp,
1725
& format! ( " oxy fetch {remote_name} " ) ,
1726
)
1727
.map_err ( | e | RepoError:: Msg( format! ( " policy from {remote_name} : {e} " ) ) ) ? ;
83e0677b Andy McCall 1 month ago ↶
1728
}
1729
}
1730
self .record_policy_serial ( remote_name, sp.policy.serial) ? ;
1731
}
1732
None => {
1733
if seen.is_some ( ) {
1734
return fail ( format! ( " {remote_name} presented a policy before but has none now: refusing the downgrade" ) ) ;
1735
}
1736
unverified = true ;
1737
}
1738
}
75167672 Andy McCall 1 month ago
1739
let policy = if unverified {
1740
None
1741
} else {
1742
self .current_policy ( ) ? .map ( | ( _, sp ) | sp.policy )
1743
} ;
98286406 Andy McCall 1 month ago ↶
1744
let ( op_ref, op) = self .head_op ( ) ? ;
1745
let mut view = op.view.clone ( ) ;
1746
let have = self .known_heads ( & view) ? ;
1747
let mut report = Vec :: new( ) ;
d36ba2f7 Andy McCall 16 days ago ↶
1748
let mut skipped = Vec :: new( ) ;
b3c5a59a Andy McCall 1 month ago ↶
1749
let max_blob = self .config.max_blob;
83e0677b Andy McCall 1 month ago ↶
1750
for r in resp.refs {
091fef80 Andy McCall 1 month ago ↶
1751
let needed = {
1752
let _t = crate :: timing:: phase( " fetch: negotiate" ) ;
1753
remote.negotiate ( & have, & r.heads) ?
1754
} ;
1755
let bundle = {
1756
let _t = crate :: timing:: phase( " fetch: bundle" ) ;
1757
remote.fetch ( & needed, max_blob) ?
1758
} ;
83e0677b Andy McCall 1 month ago ↶
1759
1760
1761
f3811eb2 Andy McCall 11 days ago ↶
1762
1763
1764
let shallow = self .is_shallow ( ) ;
091fef80 Andy McCall 1 month ago ↶
1765
let imported = {
1766
let _t = crate :: timing:: phase( " fetch: import" ) ;
f3811eb2 Andy McCall 11 days ago ↶
1767
if shallow {
1768
self .import_bundle_shallow ( & bundle, false ) ?
1769
} else {
1770
self .import_bundle ( & bundle, false ) ?
1771
}
091fef80 Andy McCall 1 month ago ↶
1772
} ;
f3811eb2 Andy McCall 11 days ago ↶
1773
if shallow {
1774
self .refresh_boundary ( & imported) ? ;
1775
}
b3c5a59a Andy McCall 1 month ago ↶
1776
if max_blob.is_some ( ) {
1777
self .register_missing_blobs ( & imported, remote_name) ? ;
1778
}
f3811eb2 Andy McCall 11 days ago ↶
1779
let boundary = if shallow {
1780
self .boundary ( )
1781
} else {
1782
BTreeSet:: new( )
1783
} ;
98286406 Andy McCall 1 month ago ↶
1784
for h in & r.heads {
f3811eb2 Andy McCall 11 days ago ↶
1785
if ! self .store.has ( h) & & ! boundary.contains ( h) {
98286406 Andy McCall 1 month ago ↶
1786
return fail ( format! ( " remote did not send head {} " , h.short ( 12 ) ) ) ;
1787
}
1788
}
1789
let state = self .store.put_state ( & StateObject:: new( r.heads.clone ( ) ) ) ? ;
1790
if state ! = r.state {
75167672 Andy McCall 1 month ago
1791
return fail ( format! (
1792
" remote state {} does not match its heads" ,
1793
r.state.short ( 12 )
1794
) ) ;
98286406 Andy McCall 1 month ago ↶
1795
}
83e0677b Andy McCall 1 month ago ↶
1796
if let Some ( p) = & policy {
1797
let Some ( sa) = & r.attestation else {
75167672 Andy McCall 1 month ago
1798
return fail ( format! (
1799
" {remote_name} /{} has no attestation, which its policy requires" ,
1800
r.name
1801
) ) ;
83e0677b Andy McCall 1 month ago ↶
1802
} ;
75167672 Andy McCall 1 month ago
1803
let signer = sa
1804
.sigs
1805
.first ( )
1806
.map ( | s | s.key )
1807
.ok_or_else ( | | RepoError:: Msg( " attestation carries no signature" .into ( ) ) ) ? ;
028545df Andy McCall 17 days ago ↶
1808
if ! p.authorizes ( & signer, & r.name) {
1809
self .authorize_in_time ( p, remote_name, & r, sa, & signer, & log, & mut log_notes) ? ;
83e0677b Andy McCall 1 month ago ↶
1810
}
1811
}
98286406 Andy McCall 1 month ago ↶
1812
if let Some ( sa) = & r.attestation {
75167672 Andy McCall 1 month ago
1813
let AttestBody:: Ref {
1814
old_state,
1815
new_state,
1816
..
1817
} = & sa.attest.body
1818
else {
98286406 Andy McCall 1 month ago ↶
1819
return fail ( " remote sent a non-ref attestation for a bookmark" ) ;
1820
} ;
1821
let ( old_state, new_state) = ( * old_state, * new_state) ;
1822
let serial = self
d36ba2f7 Andy McCall 16 days ago ↶
1823
.check_ref_attest_body (
75167672 Andy McCall 1 month ago
1824
sa,
1825
& r.name,
1826
& old_state,
1827
& new_state,
1828
self .last_seen_serial ( remote_name, & r.name) ,
1829
true ,
1830
)
1831
.map_err ( | e | {
1832
RepoError:: Msg( format! ( " attestation for {} /{} : {e} " , remote_name, r.name) )
1833
} ) ? ;
98286406 Andy McCall 1 month ago ↶
1834
if new_state ! = r.state {
75167672 Andy McCall 1 month ago
1835
return fail ( format! (
1836
" attestation for {} /{} does not cover its current state" ,
1837
remote_name, r.name
1838
) ) ;
98286406 Andy McCall 1 month ago ↶
1839
}
d36ba2f7 Andy McCall 16 days ago ↶
1840
1841
1842
1843
1844
1845
1846
if let Err ( e) = self .verify_attest_signature ( sa) {
1847
let why = format! ( " attestation for {remote_name} /{} : {e} " , r.name) ;
1848
if r.name = = self .config.bookmark {
1849
return fail ( why) ;
1850
}
1851
skipped.push ( ( r.name, why) ) ;
1852
continue ;
1853
}
98286406 Andy McCall 1 month ago ↶
1854
let ar = self .store.put_attest ( & sa.attest) ? ;
1855
for sig in & sa.sigs {
1856
self .store.put_signature ( & ar, sig) ? ;
1857
}
1858
self .record_serial ( remote_name, & r.name, serial) ? ;
1859
}
75167672 Andy McCall 1 month ago
1860
view.remotes
1861
.entry ( remote_name.to_string ( ) )
1862
.or_default ( )
1863
.insert ( r.name.clone ( ) , state) ;
98286406 Andy McCall 1 month ago ↶
1864
report.push ( ( r.name, r.heads, imported.len ( ) ) ) ;
1865
}
132b884b Andy McCall 1 month ago ↶
1866
let mut new_tags = 0 ;
1867
for t in & resp.tags {
1868
if view.tags.contains_key ( & t.name) {
b63fc7a7 Andy McCall 11 days ago ↶
1869
continue ;
1870
}
1871
1872
if self .tag_withdrawal ( & t.name) ? .is_some ( ) {
132b884b Andy McCall 1 month ago ↶
1873
continue ;
1874
}
1875
75167672 Andy McCall 1 month ago
1876
let missing: Vec< ChangeHash> = t
1877
.heads
1878
.iter ( )
1879
.filter ( | h | ! self .store.has ( h) )
1880
.copied ( )
1881
.collect ( ) ;
132b884b Andy McCall 1 month ago ↶
1882
if ! missing.is_empty ( ) {
1883
let needed = remote.negotiate ( & self .known_heads ( & view) ? , & missing) ? ;
1884
let bundle = remote.fetch ( & needed, max_blob) ? ;
1885
self .import_bundle ( & bundle, false ) ? ;
1886
}
1887
self .store.put_state ( & StateObject:: new( t.heads.clone ( ) ) ) ? ;
1ec6fdbc Andy McCall 16 days ago ↶
1888
let mut check = TagCheck:: Fetched {
1889
remote: remote_name,
1890
log: & log,
1891
notes: & mut log_notes,
1892
} ;
1893
match self .adopt_tag ( & mut view, t, policy.as_ref ( ) , & mut check) {
1894
Ok ( ( ) ) => new_tags + = 1 ,
1895
1896
1897
Err ( e) if e.untimed => {
1898
skipped.push ( ( format! ( " tags/{} " , t.name) , e.reason) ) ;
1899
}
1900
Err ( e) => return fail ( format! ( " {remote_name} {} : {} " , t.name, e.reason) ) ,
1901
}
132b884b Andy McCall 1 month ago ↶
1902
}
b63fc7a7 Andy McCall 11 days ago ↶
1903
1904
1905
1906
let mut withdrawn_tags = Vec :: new( ) ;
1907
for t in & resp.withdrawn {
1908
if ! view.tags.contains_key ( & t.name) {
1909
continue ;
1910
}
1911
let mut check = TagCheck:: Fetched {
1912
remote: remote_name,
1913
log: & log,
1914
notes: & mut log_notes,
1915
} ;
1916
match self .adopt_withdrawal ( & mut view, t, policy.as_ref ( ) , & mut check) {
1917
Ok ( true ) => withdrawn_tags.push ( t.name.clone ( ) ) ,
1918
Ok ( false ) => { }
1919
Err ( e) => skipped.push ( (
1920
format! ( " tags/{} " , t.name) ,
1921
format! ( " withdrawn on {remote_name} , kept here: {} " , e.reason) ,
1922
) ) ,
1923
}
1924
}
98286406 Andy McCall 1 month ago ↶
1925
if view ! = op.view {
1926
self .record_op ( & [ op_ref] , & format! ( " oxy fetch {remote_name} " ) , view) ? ;
1927
}
6d3eefbc Andy McCall 1 month ago ↶
1928
let _ = self .run_hook (
1929
" post-fetch" ,
1930
& format! (
1931
" {{ \" event\" :\" post-fetch\" ,\" remote\" :{} ,\" bookmarks\" :{} }} " ,
1932
json_str ( remote_name) ,
75167672 Andy McCall 1 month ago
1933
json_array ( report.iter ( ) .map ( | ( n , heads , _) | format! (
1934
" {{ \" name\" :{} ,\" heads\" :{} }} " ,
1935
json_str ( n) ,
1936
json_array ( heads.iter ( ) .map ( | h | json_str ( & h.to_hex ( ) ) ) )
1937
) ) )
6d3eefbc Andy McCall 1 month ago ↶
1938
) ,
1939
) ;
75167672 Andy McCall 1 month ago
1940
Ok ( FetchReport {
1941
refs: report,
1942
unverified,
1943
new_tags,
b63fc7a7 Andy McCall 11 days ago ↶
1944
withdrawn_tags,
4b9d0677 Andy McCall 18 days ago ↶
1945
log_notes,
d36ba2f7 Andy McCall 16 days ago ↶
1946
skipped,
75167672 Andy McCall 1 month ago
1947
} )
98286406 Andy McCall 1 month ago ↶
1948
}
1949
2863456a Andy McCall 1 month ago ↶
1950
1951
1952
pub fn fetch_shallow ( & self , remote_name : & str , depth : u32 ) -> Result< usize > {
1953
let remote = self .open_named_remote ( remote_name) ? ;
75167672 Andy McCall 1 month ago
1954
self .check_remote_id ( remote.as_ref ( ) , remote_name, false ) ? ;
2863456a Andy McCall 1 month ago ↶
1955
let resp = remote.refs ( ) ? ;
1956
let bookmark = self .config.bookmark.clone ( ) ;
1957
if let Some ( sp) = & resp.policy {
75167672 Andy McCall 1 month ago
1958
self .adopt_policy_chain ( & resp.history, sp, " oxy clone --depth" )
1959
.map_err ( | e | RepoError:: Msg( format! ( " policy from {remote_name} : {e} " ) ) ) ? ;
2863456a Andy McCall 1 month ago ↶
1960
self .record_policy_serial ( remote_name, sp.policy.serial) ? ;
1961
}
1962
let Some ( r) = resp.refs.into_iter ( ) .find ( | r | r.name = = bookmark ) else {
1963
return fail ( format! ( " {remote_name} has no bookmark {bookmark:?} " ) ) ;
1964
} ;
1965
if r.heads.is_empty ( ) {
1966
return Ok ( 0 ) ;
1967
}
1968
f3811eb2 Andy McCall 11 days ago ↶
1969
let cp_bundle = {
1970
let _t = crate :: timing:: phase( " shallow: checkpoint request" ) ;
1971
remote.checkpoint ( & r.heads) ?
1972
} ;
7bd805ff Andy McCall 11 days ago ↶
1973
1974
1975
1976
let objects: Vec< ( ObjectType, & [u8 ]) > = cp_bundle
1977
.objects
1978
.iter ( )
1979
.map ( | ( ty , body ) | ( * ty, body.as_slice ( ) ) )
1980
.collect ( ) ;
f3811eb2 Andy McCall 11 days ago ↶
1981
let t_store = crate :: timing:: phase( " shallow: store the checkpoint" ) ;
7bd805ff Andy McCall 11 days ago ↶
1982
let batch = self .store.begin_batch ( ) ? ;
1983
let stored = self .store.put_many ( & objects) ;
1984
batch.finish ( ) ? ;
f3811eb2 Andy McCall 11 days ago ↶
1985
drop ( t_store) ;
7bd805ff Andy McCall 11 days ago ↶
1986
let cp_ref = objects
1987
.iter ( )
1988
.zip ( stored? )
1989
.find ( | ( (ty , _ ) , _) | * ty = = ObjectType:: Checkpoint)
1990
.map ( | ( _, r ) | r ) ;
75167672 Andy McCall 1 month ago
1991
let Some ( cp_ref) = cp_ref else {
1992
return fail ( format! ( " {remote_name} sent no checkpoint" ) ) ;
1993
} ;
2863456a Andy McCall 1 month ago ↶
1994
self .install_checkpoint ( & bookmark, & cp_ref) ? ;
f3811eb2 Andy McCall 11 days ago ↶
1995
let _t_blobs = crate :: timing:: phase( " shallow: the binary files' blobs" ) ;
7bd805ff Andy McCall 11 days ago ↶
1996
1997
1998
1999
let cp = crate :: objects:: Checkpoint:: decode(
2000
& self .store.get_typed ( & cp_ref, ObjectType:: Checkpoint) ? ,
2001
) ? ;
2002
let base = State:: from_checkpoint_history( & self .read_blob ( & cp.payload) ? ) ? ;
2003
let mut missing: Vec< ObjectRef> = base
2004
.opaque_nodes ( )
2005
.iter ( )
2006
.flat_map ( | n | base.live_blobs ( n) )
2007
.filter ( | b | ! self .store.has ( b) )
2008
.collect ( ) ;
2009
missing.sort ( ) ;
2010
missing.dedup ( ) ;
2011
if self .config.max_blob.is_some ( ) {
2012
for b in & missing {
2013
self .register_lazy ( b, remote_name) ? ;
2014
}
2015
} else {
2016
for part in missing.chunks ( 512 ) {
2017
let objects = remote.objects ( part) ? ;
2018
let objects: Vec< ( ObjectType, & [u8 ]) > = objects
2019
.iter ( )
2020
.filter ( | ( ty , _) | matches! ( ty, ObjectType:: Blob | ObjectType:: Chunk) )
2021
.map ( | ( ty , body ) | ( * ty, body.as_slice ( ) ) )
2022
.collect ( ) ;
2023
let batch = self .store.begin_batch ( ) ? ;
2024
let stored = self .store.put_many ( & objects) ;
2025
batch.finish ( ) ? ;
2026
stored? ;
2027
}
2028
if let Some ( b) = missing.iter ( ) .find ( | b | ! self .store.has ( b) ) {
2029
return fail ( format! ( " {remote_name} did not send blob {} " , b.short ( 12 ) ) ) ;
2030
}
2031
}
f3811eb2 Andy McCall 11 days ago ↶
2032
2033
2034
2035
2036
let imported = if depth = = 0 {
2037
let mut boundary = self .boundary ( ) ;
2038
boundary.extend ( r.heads.iter ( ) .filter ( | h | ! self .store.has ( h) ) .copied ( ) ) ;
2039
self .write_boundary ( & boundary) ? ;
2040
Vec :: new( )
2041
} else {
2042
let wanted = remote.shallow ( & r.heads, depth) ? ;
2043
let bundle = remote.fetch ( & wanted, self .config.max_blob) ? ;
2044
self .import_bundle_shallow ( & bundle, false ) ?
2045
} ;
2863456a Andy McCall 1 month ago ↶
2046
if self .config.max_blob.is_some ( ) {
2047
self .register_missing_blobs ( & imported, remote_name) ? ;
2048
}
2049
self .refresh_boundary ( & imported) ? ;
2050
if let Some ( sa) = & r.attestation {
2051
let ar = self .store.put_attest ( & sa.attest) ? ;
2052
for sig in & sa.sigs {
2053
self .store.put_signature ( & ar, sig) ? ;
2054
}
2055
if let AttestBody:: Ref { serial, .. } = sa.attest.body {
2056
self .record_serial ( remote_name, & r.name, serial) ? ;
2057
}
2058
}
2059
let state = self .store.put_state ( & StateObject:: new( r.heads.clone ( ) ) ) ? ;
2060
let ( op_ref, op) = self .head_op ( ) ? ;
2061
let mut view = op.view.clone ( ) ;
75167672 Andy McCall 1 month ago
2062
view.remotes
2063
.entry ( remote_name.to_string ( ) )
2064
.or_default ( )
2065
.insert ( r.name.clone ( ) , state) ;
2066
self .record_op (
2067
& [ op_ref] ,
2068
& format! ( " oxy fetch {remote_name} --depth {depth} " ) ,
2069
view,
2070
) ? ;
2863456a Andy McCall 1 month ago ↶
2071
Ok ( imported.len ( ) )
2072
}
2073
98286406 Andy McCall 1 month ago ↶
2074
2075
2076
pub fn pull ( & self , remote_name : & str ) -> Result< PullReport> {
6caacfff Andy McCall 1 month ago ↶
2077
let _wc = self .lock_working_copy ( ) ? ;
d36ba2f7 Andy McCall 16 days ago ↶
2078
let skipped = self .fetch ( remote_name) ? .skipped;
98286406 Andy McCall 1 month ago ↶
2079
let ( op_ref, op) = self .head_op ( ) ? ;
14edbaaf Andy McCall 1 month ago ↶
2080
let current = self .config.bookmark.clone ( ) ;
2081
let mut local_current = self .heads_of ( & op.view, & current) ? ;
2082
local_current.sort ( ) ;
2083
let mut view = op.view.clone ( ) ;
2084
let mut current_heads = local_current.clone ( ) ;
2085
let mut changed = false ;
2086
let mut others = Vec :: new( ) ;
2087
let tracked = op
75167672 Andy McCall 1 month ago
2088
.view
2089
.remotes
2090
.get ( remote_name)
14edbaaf Andy McCall 1 month ago ↶
2091
.cloned ( )
2092
.unwrap_or_default ( ) ;
2093
for ( name, remote_state) in tracked {
2094
2095
2096
let local_state = op.view.bookmarks.get ( & name) .copied ( ) ;
2097
if local_state.is_none ( ) & & name ! = current {
2098
continue ;
2099
}
2100
if local_state = = Some ( remote_state) {
2101
continue ;
2102
}
2103
let local = self .heads_of ( & op.view, & name) ? ;
2104
let remote_heads = self .store.get_state ( & remote_state) ? .heads;
2105
if remote_heads.iter ( ) .all ( | h | local.contains ( h) ) {
2106
continue ;
2107
}
2108
let mut all = local.clone ( ) ;
2109
all.extend ( remote_heads) ;
2110
let st = self .load_state_history ( & all) ? ;
2111
let mut heads = st.maximal ( & all) ;
2112
heads.sort ( ) ;
2113
let mut sorted_local = local;
2114
sorted_local.sort ( ) ;
2115
if heads = = sorted_local {
2116
continue ;
2117
}
2118
view.bookmarks.insert (
2119
name.clone ( ) ,
2120
self .store.put_state ( & StateObject:: new( heads.clone ( ) ) ) ? ,
2121
) ;
2122
if name = = current {
2123
changed = true ;
2124
current_heads = heads;
2125
} else {
2126
others.push ( name) ;
2127
}
2128
}
f6dccc45 Andy McCall 27 days ago ↶
2129
if changed | | ! others.is_empty ( ) {
2130
self .record_op ( & [ op_ref] , & format! ( " oxy pull {remote_name} " ) , view) ? ;
98286406 Andy McCall 1 month ago ↶
2131
}
f6dccc45 Andy McCall 27 days ago ↶
2132
2133
2134
2135
let checked_out = self .follow_bookmark ( & current_heads, & current, " oxy pull" ) ? ;
75167672 Andy McCall 1 month ago
2136
Ok ( PullReport {
f6dccc45 Andy McCall 27 days ago ↶
2137
heads: if changed {
2138
current_heads
2139
} else {
2140
local_current
2141
} ,
14edbaaf Andy McCall 1 month ago ↶
2142
changed,
75167672 Andy McCall 1 month ago
2143
checked_out,
14edbaaf Andy McCall 1 month ago ↶
2144
others,
d36ba2f7 Andy McCall 16 days ago ↶
2145
skipped,
75167672 Andy McCall 1 month ago
2146
} )
f6dccc45 Andy McCall 27 days ago ↶
2147
}
2148
2149
2150
2151
2152
2153
2154
pub fn working_copy_behind ( & self ) -> Result< Option< usize > > {
2155
let ( _ , wc) = self .working_copy ( ) ? ;
2156
let mut base = wc.deps.clone ( ) ;
2157
base.sort ( ) ;
2158
let mut heads = self .bookmark_heads ( ) ? ;
2159
heads.sort ( ) ;
2160
if base = = heads | | heads.is_empty ( ) {
2161
return Ok ( None ) ;
2162
}
2163
let state = self .load_state_history ( & heads) ? ;
2164
if ! base.iter ( ) .all ( | h | state.contains ( h) ) {
2165
return Ok ( None ) ;
2166
}
2167
let had = if base.is_empty ( ) {
2168
0
2169
} else {
2170
self .load_state_history ( & base) ? .len ( )
2171
} ;
2172
Ok ( Some ( state.len ( ) .saturating_sub ( had) ) .filter ( | n | * n > 0 ) )
2173
}
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
pub fn follow_bookmark (
2185
& self ,
2186
heads : & [ChangeHash],
2187
bookmark : & str ,
2188
command : & str ,
2189
) -> Result< bool > {
2190
let snap = self .snapshot ( command) ? ;
2191
let clean = snap.wc.tree_ops.is_empty ( ) & & snap.wc.edits.is_empty ( ) ;
2192
let mut base = snap.base.maximal ( & snap.wc.deps) ;
2193
base.sort ( ) ;
2194
let mut target = heads.to_vec ( ) ;
2195
target.sort ( ) ;
2196
if base = = target {
2197
return Ok ( false ) ;
2198
}
2199
let state = self .load_state_history ( heads) ? ;
2200
if ! base.iter ( ) .all ( | h | state.contains ( h) ) {
2201
2202
return Ok ( false ) ;
2203
}
2204
if clean {
2205
self .checkout ( heads, false , & format! ( " {command} (checkout)" ) ) ? ;
2206
Ok ( true )
2207
} else {
2208
Ok ( self
2209
.checkout_carry ( heads, bookmark, & format! ( " {command} (checkout --carry)" ) )
2210
.is_ok ( ) )
b63fc7a7 Andy McCall 11 days ago ↶
2211
}
2212
}
2213
2214
2215
2216
2217
2218
pub fn push_tag_withdrawal ( & self , remote_name : & str , tag : & str ) -> Result< Vec< RefResult> > {
2219
let remote = self .open_named_remote ( remote_name) ? ;
2220
self .check_remote_id ( remote.as_ref ( ) , remote_name, true ) ? ;
2221
let resp = remote.refs ( ) ? ;
2222
let name = format! ( " tags/{tag} " ) ;
2223
if resp.withdrawn.iter ( ) .any ( | t | t.name = = tag ) {
2224
return Ok ( vec! [ RefResult {
2225
name,
2226
ok: true ,
2227
reason: " already withdrawn" .into ( ) ,
2228
} ] ) ;
2229
}
2230
let Some ( there) = resp.tags.iter ( ) .find ( | t | t.name = = tag ) else {
2231
return fail ( format! ( " {remote_name} has no tag {tag} " ) ) ;
2232
} ;
2233
if resp.policy.is_some ( ) & & self .secret_key ( ) ? .is_none ( ) {
2234
return fail ( format! ( " {remote_name} enforces a trust policy; withdrawing a tag needs an identity (`oxy identity create`)" ) ) ;
f6dccc45 Andy McCall 27 days ago ↶
2235
}
b63fc7a7 Andy McCall 11 days ago ↶
2236
if self .tag_withdrawal ( tag) ? .is_none ( ) {
2237
match self .view ( ) ? .tags.get ( tag) {
2238
Some ( st) if * st = = there.state => {
2239
self .tag_delete ( tag, " " ) ? ;
2240
}
2241
Some ( _ ) => {
2242
return fail ( format! (
2243
" tag {tag} names another state here than on {remote_name} "
2244
) )
2245
}
2246
None => {
2247
return fail ( format! (
2248
" there is no tag {tag} here to withdraw: `oxy fetch {remote_name} ` first"
2249
) )
2250
}
2251
}
2252
}
2253
let Some ( w) = self
2254
.withdrawn_refs ( ) ?
2255
.into_iter ( )
2256
.find ( | w | w.name = = tag & & w.state = = there.state )
2257
else {
2258
return fail ( format! (
2259
" tag {tag} was withdrawn here at another state than {remote_name} 's"
2260
) ) ;
2261
} ;
2262
let mut bundle = Bundle:: default( ) ;
2263
let signers: Vec< KeyId> = w
2264
.attestation
2265
.iter ( )
2266
.flat_map ( | a | a.sigs.iter ( ) .map ( | s | s.key ) )
2267
.collect ( ) ;
2268
for k in & signers {
2269
if let Ok ( id) = self .identity ( k) {
2270
bundle.objects.push ( ( ObjectType:: Identity, id.encode ( ) ) ) ;
2271
}
2272
}
2273
self .attach_certificates_for ( & mut bundle, & signers) ? ;
2274
remote.push ( bundle, vec! [ ] , None , vec! [ w] )
98286406 Andy McCall 1 month ago ↶
2275
}
2276
2277
2278
f8c69576 Andy McCall 1 month ago ↶
2279
2280
2281
2282
pub fn push_delete ( & self , remote_name : & str , bookmark : & str ) -> Result< Vec< RefResult> > {
b63fc7a7 Andy McCall 11 days ago ↶
2283
if let Some ( tag) = bookmark.strip_prefix ( " tags/" ) {
2284
return self .push_tag_withdrawal ( remote_name, tag) ;
2285
}
f8c69576 Andy McCall 1 month ago ↶
2286
let remote = self .open_named_remote ( remote_name) ? ;
2287
self .check_remote_id ( remote.as_ref ( ) , remote_name, true ) ? ;
2288
let resp = remote.refs ( ) ? ;
2289
let Some ( r) = resp.refs.iter ( ) .find ( | r | r.name = = bookmark ) else {
2290
return fail ( format! ( " {remote_name} has no bookmark {bookmark} " ) ) ;
2291
} ;
2292
if resp.policy.is_some ( ) & & self .secret_key ( ) ? .is_none ( ) {
2293
return fail ( format! ( " {remote_name} enforces a trust policy; deleting needs an identity (`oxy identity create`)" ) ) ;
2294
}
2295
let attestation = match self .secret_key ( ) ? {
2296
Some ( _ ) => Some ( self .sign_attest ( & Attest {
2297
repo: self .id ( ) ? ,
2298
hlc: Hlc:: now( ) ,
2299
body: AttestBody:: Ref {
2300
name: bookmark.to_string ( ) ,
2301
old_state: r.state,
2302
new_state: ObjectRef:: ZERO ,
2303
serial: r.last_serial.unwrap_or ( 0 ) + 1 ,
2304
} ,
2305
} ) ? ) ,
2306
None => None ,
2307
} ;
2308
let results = remote.push (
2309
Bundle:: default( ) ,
2310
vec! [ RefUpdate {
2311
name: bookmark.to_string ( ) ,
2312
old_state: r.state,
2313
new_state: ObjectRef:: ZERO ,
2314
force: true ,
2315
attestation,
2316
} ] ,
2317
None ,
2318
vec! [ ] ,
2319
) ? ;
2320
if results.iter ( ) .any ( | x | x.ok & & x.name = = bookmark ) {
2321
let ( op_ref, op) = self .head_op ( ) ? ;
2322
let mut view = op.view.clone ( ) ;
2323
if let Some ( m) = view.remotes.get_mut ( remote_name) {
2324
m.remove ( bookmark) ;
2325
}
2326
self .record_op (
2327
& [ op_ref] ,
2328
& format! ( " oxy push {remote_name} --delete {bookmark} " ) ,
2329
view,
2330
) ? ;
2331
}
2332
Ok ( results)
2333
}
2334
34fad4a9 Andy McCall 25 days ago ↶
2335
2336
2337
2338
2339
pub fn remove_remote ( & mut self , name : & str ) -> Result< ( ) > {
2340
if ! self .config.remotes.contains_key ( name) {
2341
return fail ( format! ( " no remote {name} : `oxy remote list` shows them" ) ) ;
2342
}
2343
let prefix = format! ( " remote.{name} ." ) ;
2344
let keys: Vec< String> = self
2345
.config
2346
.repo_layer ( )
2347
.keys ( )
2348
.filter ( | k | k.starts_with ( & prefix) )
2349
.cloned ( )
2350
.collect ( ) ;
2351
for k in keys {
2352
self .config.set ( & k, " " ) ? ;
2353
}
2354
self .config.remotes.remove ( name) ;
2355
self .save_config ( ) ? ;
2356
let ( op_ref, op) = self .head_op ( ) ? ;
2357
if op.view.remotes.contains_key ( name) {
2358
let mut view = op.view.clone ( ) ;
2359
view.remotes.remove ( name) ;
2360
self .record_op ( & [ op_ref] , & format! ( " oxy remote remove {name} " ) , view) ? ;
2361
}
2362
let mirror = self .oxy.join ( " remotes" ) .join ( name) ;
2363
if mirror.is_dir ( ) {
2364
fs:: remove_dir_all( & mirror) ? ;
2365
}
2366
Ok ( ( ) )
2367
}
2368
d3b27090 Andy McCall 11 days ago ↶
2369
2370
2371
2372
2373
2374
2375
2376
fn send_objects_ahead (
2377
& self ,
2378
remote : & dyn crate:: protocol:: Remote,
2379
remote_name : & str ,
2380
mut bundle : Bundle,
2381
progress : crate:: store:: Progress,
2382
) -> Result< Bundle> {
2383
let limit = self .config.push_batch_bytes;
2384
let changes: u64 = bundle
2385
.changes
2386
.iter ( )
2387
.map ( | ( c , _) | c.encode ( ) .len ( ) as u64 )
2388
.sum ( ) ;
2389
let objects: u64 = bundle.objects.iter ( ) .map ( | ( _, b ) | b.len ( ) as u64 ) .sum ( ) ;
2390
if changes + objects < = limit {
2391
return Ok ( bundle) ;
2392
}
2393
let ( mut kept, mut parts, mut part, mut bytes) = ( Vec :: new( ) , Vec :: new( ) , Vec :: new( ) , 0 u64 ) ;
2394
for ( ty, body) in std:: mem:: take( & mut bundle.objects) {
2395
if ! matches! ( ty, ObjectType:: Blob | ObjectType:: Chunk) {
2396
kept.push ( ( ty, body) ) ;
2397
continue ;
2398
}
2399
if ! part.is_empty ( ) & & bytes + body.len ( ) as u64 > limit {
2400
parts.push ( std:: mem:: take( & mut part) ) ;
2401
bytes = 0 ;
2402
}
2403
bytes + = body.len ( ) as u64 ;
2404
part.push ( ( ty, body) ) ;
2405
}
2406
if ! part.is_empty ( ) {
2407
parts.push ( part) ;
2408
}
2409
let n = parts.len ( ) ;
2410
for ( i, objects) in parts.into_iter ( ) .enumerate ( ) {
2411
progress ( & format! (
2412
" objects {} of {n} : {} blobs and chunks" ,
2413
i + 1 ,
2414
objects.len ( )
2415
) ) ;
2416
let results = remote.push (
2417
Bundle {
2418
objects,
2419
.. Default :: default( )
2420
} ,
2421
vec! [ ] ,
2422
None ,
2423
vec! [ ] ,
2424
) ? ;
2425
match results.iter ( ) .find ( | r | r.name = = OBJECTS_RESULT ) {
2426
Some ( r) if r.ok => { }
2427
Some ( r) => {
2428
return fail ( format! (
2429
" {remote_name} refused objects part {} of {n} : {} " ,
2430
i + 1 ,
2431
r.reason
2432
) )
2433
}
2434
None => {
2435
return fail ( format! (
2436
" {remote_name} does not take a push in parts, and this one is too large for one: update the server's oxy"
2437
) )
2438
}
2439
}
2440
}
2441
bundle.objects = kept;
2442
Ok ( bundle)
2443
}
2444
cb8b8082 Andy McCall 26 days ago ↶
2445
2446
2447
2448
fn push_batches ( & self , changes : & [ChangeHash]) -> Result< Vec< Vec< ChangeHash> > > {
2449
let blob_len = | r : & ObjectRef| -> u64 {
2450
self .store
2451
.get_typed ( r, ObjectType:: Blob)
2452
.ok ( )
2453
.and_then ( | b | Blob:: decode( & b) .ok ( ) )
2454
.map_or ( 0 , | b | b.length )
2455
} ;
2456
let ( mut out, mut batch, mut bytes) = ( Vec :: new( ) , Vec :: new( ) , 0 u64 ) ;
2457
for h in changes {
2458
let raw = self .store.get_typed ( h, ObjectType:: Change) ? ;
2459
let ( c, texts) = Change:: decode_skeleton( & raw, * h) ? ;
2460
let mut weight = raw.len ( ) as u64 ;
2461
for t in & texts {
2462
if let crate :: objects:: TextRef:: Blob( r, _ , _ ) = t {
2463
weight + = blob_len ( r) ;
2464
}
2465
}
2466
for e in & c.edits {
2467
if let Edit:: Replace { new: Some ( b) , .. } = e {
2468
weight + = blob_len ( b) ;
2469
}
2470
}
2471
if ! batch.is_empty ( ) & & bytes + weight > self .config.push_batch_bytes {
2472
out.push ( std:: mem:: take( & mut batch) ) ;
2473
bytes = 0 ;
2474
}
2475
batch.push ( * h) ;
2476
bytes + = weight;
2477
}
2478
out.push ( batch) ;
2479
Ok ( out)
2480
}
2481
98286406 Andy McCall 1 month ago ↶
2482
pub fn push ( & self , remote_name : & str , force : bool ) -> Result< Vec< RefResult> > {
6d3eefbc Andy McCall 1 month ago ↶
2483
let bookmark = self .config.bookmark.clone ( ) ;
7b9809e6 Andy McCall 1 month ago ↶
2484
self .push_bookmark ( remote_name, & bookmark, force)
2485
}
2486
2487
75167672 Andy McCall 1 month ago
2488
pub fn push_bookmark (
cb8b8082 Andy McCall 26 days ago ↶
2489
& self ,
2490
remote_name : & str ,
2491
bookmark : & str ,
2492
force : bool ,
2493
) -> Result< Vec< RefResult> > {
2494
self .push_bookmark_with ( remote_name, bookmark, force, & mut | _ | { } )
2495
}
2496
2497
2498
2499
pub fn push_bookmark_with (
75167672 Andy McCall 1 month ago
2500
& self ,
2501
remote_name : & str ,
2502
bookmark : & str ,
2503
force : bool ,
cb8b8082 Andy McCall 26 days ago ↶
2504
progress : crate:: store:: Progress,
75167672 Andy McCall 1 month ago
2505
) -> Result< Vec< RefResult> > {
7b9809e6 Andy McCall 1 month ago ↶
2506
let ( op_ref, op) = self .head_op ( ) ? ;
2507
if ! op.view.bookmarks.contains_key ( bookmark) {
2508
return fail ( format! ( " no bookmark {bookmark} " ) ) ;
2509
}
2510
let bookmark = bookmark.to_string ( ) ;
6d3eefbc Andy McCall 1 month ago ↶
2511
let local_heads = self .heads_of ( & op.view, & bookmark) ? ;
2512
2513
self .run_hook (
2514
" pre-push" ,
75167672 Andy McCall 1 month ago
2515
& format! (
2516
" {{ \" event\" :\" pre-push\" ,\" remote\" :{} ,\" bookmark\" :{} ,\" heads\" :{} }} " ,
2517
json_str ( remote_name) ,
2518
json_str ( & bookmark) ,
2519
json_array ( local_heads.iter ( ) .map ( | h | json_str ( & h.to_hex ( ) ) ) )
2520
) ,
6d3eefbc Andy McCall 1 month ago ↶
2521
) ? ;
98286406 Andy McCall 1 month ago ↶
2522
let remote = self .open_named_remote ( remote_name) ? ;
75167672 Andy McCall 1 month ago
2523
self .check_remote_id ( remote.as_ref ( ) , remote_name, true ) ? ;
83e0677b Andy McCall 1 month ago ↶
2524
let resp = remote.refs ( ) ? ;
2525
let local_policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp ) ;
28fc23d7 Andy McCall 1 month ago ↶
2526
let resp_policy_serial = resp.policy.as_ref ( ) .map ( | p | p.policy.serial ) ;
83e0677b Andy McCall 1 month ago ↶
2527
let policy_to_send = match ( & local_policy, & resp.policy) {
2528
( Some ( l) , Some ( r) ) if l.policy.serial > r.policy.serial => Some ( l.clone ( ) ) ,
2529
( Some ( l) , None ) => Some ( l.clone ( ) ) ,
2530
( Some ( l) , Some ( r) ) if l.policy.serial < r.policy.serial => {
75167672 Andy McCall 1 month ago
2531
return fail ( format! (
2532
" {remote_name} has a newer policy (serial {} ); fetch first" ,
2533
r.policy.serial
2534
) ) ;
83e0677b Andy McCall 1 month ago ↶
2535
}
2536
_ => None ,
2537
} ;
2538
let remote_has_policy = resp.policy.is_some ( ) | | policy_to_send.is_some ( ) ;
2539
if remote_has_policy & & self .secret_key ( ) ? .is_none ( ) {
2540
return fail ( format! ( " {remote_name} enforces a trust policy; pushing needs an identity (`oxy identity create`)" ) ) ;
2541
}
b63fc7a7 Andy McCall 11 days ago ↶
2542
let remote_tags: BTreeSet< String> = resp
2543
.tags
2544
.iter ( )
2545
.chain ( & resp.withdrawn)
2546
.map ( | t | t.name.clone ( ) )
2547
.collect ( ) ;
75167672 Andy McCall 1 month ago
2548
let tags_to_send: Vec< crate:: protocol:: TagRef> = self
2549
.tag_refs ( ) ?
2550
.into_iter ( )
2551
.filter ( | t | ! remote_tags.contains ( & t.name) )
b63fc7a7 Andy McCall 11 days ago ↶
2552
.collect ( ) ;
2553
2554
2555
let withdrawals_to_send: Vec< crate:: protocol:: TagRef> = self
2556
.withdrawn_refs ( ) ?
2557
.into_iter ( )
2558
.filter ( | w | {
2559
resp.tags
2560
.iter ( )
2561
.any ( | t | t.name = = w.name & & t.state = = w.state )
2562
} )
75167672 Andy McCall 1 month ago
2563
.collect ( ) ;
83e0677b Andy McCall 1 month ago ↶
2564
let refs = resp.refs;
98286406 Andy McCall 1 month ago ↶
2565
let empty = self .store.put_state ( & StateObject:: new( vec! [ ] ) ) ? ;
75167672 Andy McCall 1 month ago
2566
let ( remote_state, remote_heads, last_serial) =
2567
match refs.iter ( ) .find ( | r | r.name = = bookmark ) {
2568
Some ( r) => ( r.state, r.heads.clone ( ) , r.last_serial) ,
2569
None => ( empty, vec! [ ] , None ) ,
2570
} ;
2571
let new_state = self
2572
.store
2573
.put_state ( & StateObject:: new( local_heads.clone ( ) ) ) ? ;
83e0677b Andy McCall 1 month ago ↶
2574
2575
2576
2577
b63fc7a7 Andy McCall 11 days ago ↶
2578
if new_state = = remote_state
2579
& & policy_to_send.is_none ( )
2580
& & tags_to_send.is_empty ( )
2581
& & withdrawals_to_send.is_empty ( )
2582
{
75167672 Andy McCall 1 month ago
2583
return Ok ( vec! [ RefResult {
2584
name: bookmark,
2585
ok: true ,
2586
reason: " already up to date" .into ( ) ,
2587
} ] ) ;
98286406 Andy McCall 1 month ago ↶
2588
}
f3811eb2 Andy McCall 11 days ago ↶
2589
2590
2591
let boundary = self .boundary ( ) ;
98286406 Andy McCall 1 month ago ↶
2592
for h in & remote_heads {
f3811eb2 Andy McCall 11 days ago ↶
2593
if ! self .store.has ( h) & & ! boundary.contains ( h) {
75167672 Andy McCall 1 month ago
2594
return fail ( format! (
3e5772cc Andy McCall 1 month ago ↶
2595
" the remote has change {} which you do not; pull first (`oxy pull {remote_name} `), then push again" ,
75167672 Andy McCall 1 month ago
2596
h.short ( 12 )
2597
) ) ;
98286406 Andy McCall 1 month ago ↶
2598
}
2599
}
2600
if ! force {
2601
let closure = self .closure_in_store ( & local_heads) ? ;
f3811eb2 Andy McCall 11 days ago ↶
2602
if let Some ( h) = remote_heads
2603
.iter ( )
2604
.find ( | h | ! closure.contains ( h) & & ! boundary.contains ( h) )
2605
{
98286406 Andy McCall 1 month ago ↶
2606
return fail ( format! ( " not a fast-forward: remote head {} is not in your state; pull first or use --force" , h.short ( 12 ) ) ) ;
2607
}
2608
}
2609
let to_send = self .negotiate ( & remote_heads, & local_heads) ? ;
2610
let key = self .secret_key ( ) ? .map ( | ( k , _) | k ) ;
2611
if let Some ( key) = key {
05e71ed6 Andy McCall 1 month ago ↶
2612
cb8b8082 Andy McCall 26 days ago ↶
2613
2614
98286406 Andy McCall 1 month ago ↶
2615
let mut unsigned = Vec :: new( ) ;
2616
for h in & to_send {
cb8b8082 Andy McCall 26 days ago ↶
2617
let c = self .store.get_change_meta ( h) ? ;
a2387d54 Andy McCall 18 days ago ↶
2618
if self .seals_for_author ( & c.author, & key)
75167672 Andy McCall 1 month ago
2619
& & ! matches! ( self .authenticate_change ( h) ? , SignatureStatus:: Valid { .. } )
2620
{
98286406 Andy McCall 1 month ago ↶
2621
unsigned.push ( * h) ;
2622
}
2623
}
2624
for chunk in unsigned.chunks ( crate :: attest:: MAX_SEAL_HASHES ) {
cb8b8082 Andy McCall 26 days ago ↶
2625
self .sign_attest ( & Attest:: seal( self .id ( ) ? , chunk.to_vec ( ) ) ) ? ;
98286406 Andy McCall 1 month ago ↶
2626
}
cb8b8082 Andy McCall 26 days ago ↶
2627
}
2628
2629
2630
2631
2632
let batches = self .push_batches ( & to_send) ? ;
2633
let ( last, earlier) = batches.split_last ( ) .expect ( " at least one batch" ) ;
2634
for ( i, batch) in earlier.iter ( ) .enumerate ( ) {
2635
progress ( & format! (
2636
" part {} of {} : {} changes" ,
2637
i + 1 ,
2638
batches.len ( ) ,
2639
batch.len ( )
2640
) ) ;
d3b27090 Andy McCall 11 days ago ↶
2641
let bundle = self .send_objects_ahead (
2642
remote.as_ref ( ) ,
2643
remote_name,
2644
self .bundle_for ( batch) ? ,
2645
& mut * progress,
2646
) ? ;
2647
let results = remote.push ( bundle, vec! [ ] , None , vec! [ ] ) ? ;
cb8b8082 Andy McCall 26 days ago ↶
2648
match results.iter ( ) .find ( | r | r.name = = OBJECTS_RESULT ) {
2649
Some ( r) if r.ok => { }
2650
Some ( r) => {
2651
return fail ( format! (
2652
" {remote_name} refused part {} of the push: {} " ,
2653
i + 1 ,
2654
r.reason
2655
) )
2656
}
2657
None => {
2658
return fail ( format! (
2659
" {remote_name} does not take a push in parts, and this one is too long for one: update the server's oxy"
2660
) )
2661
}
2662
}
2663
}
2664
if batches.len ( ) > 1 {
2665
progress ( & format! (
2666
" part {} of {} : {} changes" ,
2667
batches.len ( ) ,
2668
batches.len ( ) ,
2669
last.len ( )
2670
) ) ;
98286406 Andy McCall 1 month ago ↶
2671
}
d3b27090 Andy McCall 11 days ago ↶
2672
let mut bundle = self .send_objects_ahead (
2673
remote.as_ref ( ) ,
2674
remote_name,
2675
self .bundle_for ( last) ? ,
2676
& mut * progress,
2677
) ? ;
75167672 Andy McCall 1 month ago
2678
bundle.objects.push ( (
2679
ObjectType:: State,
2680
StateObject:: new( local_heads.clone ( ) ) .encode ( ) ,
2681
) ) ;
98286406 Andy McCall 1 month ago ↶
2682
let attestation = match key {
2683
Some ( _ ) => Some ( self .sign_attest ( & Attest {
2684
repo: self .id ( ) ? ,
2685
hlc: Hlc:: now( ) ,
75167672 Andy McCall 1 month ago
2686
body: AttestBody:: Ref {
2687
name: bookmark.clone ( ) ,
2688
old_state: remote_state,
2689
new_state,
f8c69576 Andy McCall 1 month ago ↶
2690
serial: next_serial ( last_serial) ,
75167672 Andy McCall 1 month ago
2691
} ,
98286406 Andy McCall 1 month ago ↶
2692
} ) ? ) ,
2693
None => None ,
2694
} ;
68a92484 Andy McCall 22 days ago ↶
2695
2696
2697
2698
if attestation.is_some ( ) {
2699
for k in [ self .signing_key ( ) , self .config.author_key]
2700
.into_iter ( )
2701
.flatten ( )
2702
{
2703
if let Ok ( id) = self .identity ( & k) {
2704
let body = id.encode ( ) ;
2705
if ! bundle
2706
.objects
2707
.iter ( )
2708
.any ( | ( t , b ) | * t = = ObjectType:: Identity & & * b = = body)
2709
{
2710
bundle.objects.push ( ( ObjectType:: Identity, body) ) ;
2711
}
2712
}
2713
}
f0a517d2 Andy McCall 18 days ago ↶
2714
2715
let signers: Vec< KeyId> = self .signing_key ( ) .into_iter ( ) .collect ( ) ;
2716
self .attach_certificates_for ( & mut bundle, & signers) ? ;
68a92484 Andy McCall 22 days ago ↶
2717
}
98286406 Andy McCall 1 month ago ↶
2718
let serial = attestation.as_ref ( ) .and_then ( | a | match a.attest.body {
2719
AttestBody:: Ref { serial, .. } => Some ( serial) ,
2720
_ => None ,
2721
} ) ;
83e0677b Andy McCall 1 month ago ↶
2722
let sent_serial = policy_to_send.as_ref ( ) .map ( | p | p.policy.serial ) ;
132b884b Andy McCall 1 month ago ↶
2723
2724
for t in & tags_to_send {
2725
let extra = self .negotiate ( & remote_heads, & t.heads) ? ;
75167672 Andy McCall 1 month ago
2726
let extra: Vec< ChangeHash> =
2727
extra.into_iter ( ) .filter ( | h | ! to_send.contains ( h) ) .collect ( ) ;
132b884b Andy McCall 1 month ago ↶
2728
if ! extra.is_empty ( ) {
2729
let more = self .bundle_for ( & extra) ? ;
2730
bundle.changes.extend ( more.changes) ;
2c75672f Andy McCall 1 month ago ↶
2731
bundle.hashes.extend ( more.hashes) ;
132b884b Andy McCall 1 month ago ↶
2732
bundle.attests.extend ( more.attests) ;
2733
bundle.objects.extend ( more.objects) ;
2734
}
75167672 Andy McCall 1 month ago
2735
bundle.objects.push ( (
2736
ObjectType:: State,
2737
StateObject:: new( t.heads.clone ( ) ) .encode ( ) ,
2738
) ) ;
132b884b Andy McCall 1 month ago ↶
2739
}
d8524a9f Andy McCall 16 days ago ↶
2740
2741
2742
2743
let tag_signers: Vec< KeyId> = tags_to_send
2744
.iter ( )
b63fc7a7 Andy McCall 11 days ago ↶
2745
.chain ( & withdrawals_to_send)
d8524a9f Andy McCall 16 days ago ↶
2746
.filter_map ( | t | t.attestation.as_ref ( ) )
2747
.flat_map ( | a | a.sigs.iter ( ) .map ( | s | s.key ) )
2748
.collect ( ) ;
2749
for k in & tag_signers {
2750
if let Ok ( id) = self .identity ( k) {
2751
let body = id.encode ( ) ;
2752
if ! bundle
2753
.objects
2754
.iter ( )
2755
.any ( | ( t , b ) | * t = = ObjectType:: Identity & & * b = = body)
2756
{
2757
bundle.objects.push ( ( ObjectType:: Identity, body) ) ;
2758
}
2759
}
2760
}
2761
if ! tag_signers.is_empty ( ) {
2762
self .attach_certificates_for ( & mut bundle, & tag_signers) ? ;
2763
}
28fc23d7 Andy McCall 1 month ago ↶
2764
if let Some ( l) = & policy_to_send {
2765
let remote_serial = resp_policy_serial;
75167672 Andy McCall 1 month ago
2766
bundle.policies = self
2767
.policy_history ( ) ?
2768
.into_iter ( )
2769
.filter ( | sp | {
2770
remote_serial.is_none_or ( | r | sp.policy.serial > r )
2771
& & sp.policy.serial < l.policy.serial
2772
} )
2773
.collect ( ) ;
28fc23d7 Andy McCall 1 month ago ↶
2774
}
75167672 Andy McCall 1 month ago
2775
let results = remote.push (
2776
bundle,
2777
vec! [ RefUpdate {
2778
name: bookmark.clone ( ) ,
2779
old_state: remote_state,
2780
new_state,
2781
force,
2782
attestation,
2783
} ] ,
2784
policy_to_send,
b63fc7a7 Andy McCall 11 days ago ↶
2785
tags_to_send
2786
.into_iter ( )
2787
.chain ( withdrawals_to_send)
2788
.collect ( ) ,
75167672 Andy McCall 1 month ago
2789
) ? ;
2790
if let ( Some ( serial) , true ) = (
2791
sent_serial,
2792
results.iter ( ) .any ( | r | r.ok & & r.name = = " policy" ) ,
2793
) {
83e0677b Andy McCall 1 month ago ↶
2794
self .record_policy_serial ( remote_name, serial) ? ;
2795
}
2796
if results.iter ( ) .any ( | r | r.ok & & r.name = = bookmark ) {
98286406 Andy McCall 1 month ago ↶
2797
let mut view = op.view.clone ( ) ;
75167672 Andy McCall 1 month ago
2798
view.remotes
2799
.entry ( remote_name.to_string ( ) )
2800
.or_default ( )
2801
.insert ( bookmark.clone ( ) , new_state) ;
98286406 Andy McCall 1 month ago ↶
2802
self .record_op ( & [ op_ref] , & format! ( " oxy push {remote_name} " ) , view) ? ;
2803
if let Some ( s) = serial {
2804
self .record_serial ( remote_name, & bookmark, s) ? ;
2805
}
2806
}
2807
Ok ( results)
2808
}
2809
2810
2811
2812
pub fn clone ( url : & str , dest : & Path) -> Result< Repo> {
7421309f Andy McCall 1 month ago ↶
2813
Self :: clone_with( url, dest, None , & [ ] )
b3c5a59a Andy McCall 1 month ago ↶
2814
}
2815
7421309f Andy McCall 1 month ago ↶
2816
2817
2818
75167672 Andy McCall 1 month ago
2819
pub fn clone_with (
2820
url : & str ,
2821
dest : & Path,
2822
max_blob : Option< u64 > ,
2823
sparse : & [String],
2824
) -> Result< Repo> {
2863456a Andy McCall 1 month ago ↶
2825
Self :: clone_full( url, dest, max_blob, sparse, None )
2826
}
2827
2828
2829
75167672 Andy McCall 1 month ago
2830
pub fn clone_shallow (
2831
url : & str ,
2832
dest : & Path,
2833
depth : u32 ,
2834
max_blob : Option< u64 > ,
2835
sparse : & [String],
2836
) -> Result< Repo> {
2863456a Andy McCall 1 month ago ↶
2837
Self :: clone_full( url, dest, max_blob, sparse, Some ( depth) )
2838
}
2839
75167672 Andy McCall 1 month ago
2840
fn clone_full (
2841
url : & str ,
2842
dest : & Path,
2843
max_blob : Option< u64 > ,
2844
sparse : & [String],
2845
depth : Option< u32 > ,
2846
) -> Result< Repo> {
d36ba2f7 Andy McCall 16 days ago ↶
2847
Ok ( Self :: clone_reporting( url, dest, max_blob, sparse, depth) ? .0 )
2848
}
2849
2850
2851
2852
2853
2854
2855
pub fn clone_reporting (
2856
url : & str ,
2857
dest : & Path,
2858
max_blob : Option< u64 > ,
2859
sparse : & [String],
2860
depth : Option< u32 > ,
2861
) -> Result< ( Repo, Vec< ( String, String) > ) > {
ff1f62fe Andy McCall 1 month ago ↶
2862
let options = crate :: credentials:: Credentials:: user( ) .options_for ( url) ;
2863
Self :: clone_using( url, dest, max_blob, sparse, depth, & options)
a07f60a3 Andy McCall 1 month ago ↶
2864
}
2865
ff1f62fe Andy McCall 1 month ago ↶
2866
2867
2868
a07f60a3 Andy McCall 1 month ago ↶
2869
pub ( crate ) fn clone_as_subrepo ( & self , url : & str , dest : & Path) -> Result< Repo> {
2870
let options = self .credentials_for ( url) ;
d36ba2f7 Andy McCall 16 days ago ↶
2871
Ok ( Self :: clone_using( url, dest, None , & [ ] , None , & options) ? .0 )
a07f60a3 Andy McCall 1 month ago ↶
2872
}
2873
2874
2875
2876
fn remote_on_same_hub ( & self , url : & str ) -> Option< String> {
2877
let origin = | u : & str | -> Option< String> {
2878
let ( scheme, rest) = u.split_once ( " ://" ) ? ;
9f9e72b9 Andy McCall 1 month ago ↶
2879
if ! matches! ( scheme, " http" | " https" ) {
2880
return None ;
2881
}
a07f60a3 Andy McCall 1 month ago ↶
2882
let host = rest.split ( ' /' ) .next ( ) ? ;
2883
Some ( format! (
2884
" {} ://{} " ,
2885
scheme.to_ascii_lowercase ( ) ,
2886
host.to_ascii_lowercase ( )
2887
) )
2888
} ;
2889
let want = origin ( url) ? ;
2890
self .config
2891
.remotes
2892
.iter ( )
2893
.find ( | ( _, u ) | origin ( u) .as_deref ( ) = = Some ( want.as_str ( ) ) )
2894
.map ( | ( name , _) | name.clone ( ) )
2895
}
2896
2897
2898
ff1f62fe Andy McCall 1 month ago ↶
2899
2900
a07f60a3 Andy McCall 1 month ago ↶
2901
pub ( crate ) fn credentials_for ( & self , url : & str ) -> RemoteOptions {
2902
match self .remote_on_same_hub ( url) {
2903
Some ( name) => RemoteOptions {
2904
repo_id: None ,
9f9e72b9 Andy McCall 1 month ago ↶
2905
cache_dir: None ,
a07f60a3 Andy McCall 1 month ago ↶
2906
.. self .remote_options ( & name)
2907
} ,
ff1f62fe Andy McCall 1 month ago ↶
2908
None => {
2909
let mut options = RemoteOptions:: default( ) ;
2910
self .fill_credentials ( url, & mut options) ;
2911
options
2912
}
a07f60a3 Andy McCall 1 month ago ↶
2913
}
2914
}
2915
2916
fn clone_using (
2917
url : & str ,
2918
dest : & Path,
2919
max_blob : Option< u64 > ,
2920
sparse : & [String],
2921
depth : Option< u32 > ,
2922
options : & RemoteOptions,
d36ba2f7 Andy McCall 16 days ago ↶
2923
) -> Result< ( Repo, Vec< ( String, String) > ) > {
a07f60a3 Andy McCall 1 month ago ↶
2924
let remote = open_remote ( url, options) ? ;
98286406 Andy McCall 1 month ago ↶
2925
let id = remote.repo_id ( ) ? ;
75167672 Andy McCall 1 month ago
2926
if id = = crate :: protocol:: UNBOUND_ID {
05e71ed6 Andy McCall 1 month ago ↶
2927
return fail ( format! (
2928
" {url} is empty and unbound: push a repository to it first"
2929
) ) ;
75167672 Andy McCall 1 month ago
2930
}
98286406 Andy McCall 1 month ago ↶
2931
let mut repo = Repo:: init_with_id( dest, id) ? ;
827281ee Andy McCall 1 month ago ↶
2932
repo.config
2933
.set ( " remote.origin.url" , & crate :: protocol:: stored_url( url) ) ? ;
b3c5a59a Andy McCall 1 month ago ↶
2934
if let Some ( m) = max_blob {
2935
repo.config.set ( " filter.max-blob" , & m.to_string ( ) ) ? ;
7421309f Andy McCall 1 month ago ↶
2936
} else if ! sparse.is_empty ( ) {
2937
repo.config.set ( " filter.max-blob" , " 0" ) ? ;
2938
}
2939
if ! sparse.is_empty ( ) {
2940
repo.config.set ( " filter.paths" , & sparse.join ( " ," ) ) ? ;
b3c5a59a Andy McCall 1 month ago ↶
2941
}
98286406 Andy McCall 1 month ago ↶
2942
repo.save_config ( ) ? ;
d36ba2f7 Andy McCall 16 days ago ↶
2943
let skipped = if let Some ( depth) = depth {
2863456a Andy McCall 1 month ago ↶
2944
repo.fetch_shallow ( " origin" , depth) ? ;
d36ba2f7 Andy McCall 16 days ago ↶
2945
Vec :: new( )
2863456a Andy McCall 1 month ago ↶
2946
} else {
d36ba2f7 Andy McCall 16 days ago ↶
2947
repo.fetch ( " origin" ) ? .skipped
2948
} ;
98286406 Andy McCall 1 month ago ↶
2949
let ( op_ref, op) = repo.head_op ( ) ? ;
d36ba2f7 Andy McCall 16 days ago ↶
2950
2951
2952
2953
let mut theirs = op.view.remotes.get ( " origin" ) .cloned ( ) .unwrap_or_default ( ) ;
2954
for ( name, _ ) in & skipped {
2955
theirs.insert ( name.clone ( ) , ObjectRef:: ZERO ) ;
2956
}
9f9e72b9 Andy McCall 1 month ago ↶
2957
2958
2959
let advertised = repo
2960
.open_named_remote ( " origin" )
2961
.ok ( )
2962
.and_then ( | r | r.refs ( ) .ok ( ) )
2963
.and_then ( | r | r.default_bookmark ) ;
14edbaaf Andy McCall 1 month ago ↶
2964
let bookmark = advertised
2965
.filter ( | b | theirs.contains_key ( b) )
2966
.or_else ( | | {
2967
theirs
2968
.contains_key ( & repo.config.bookmark)
2969
.then ( | | repo.config.bookmark.clone ( ) )
2970
} )
2971
.or_else ( | | {
2972
( theirs.len ( ) = = 1 )
2973
.then ( | | theirs.keys ( ) .next ( ) .cloned ( ) )
2974
.flatten ( )
2975
} )
2976
.unwrap_or_else ( | | repo.config.bookmark.clone ( ) ) ;
d36ba2f7 Andy McCall 16 days ago ↶
2977
if let Some ( ( _ , why) ) = skipped.iter ( ) .find ( | ( name , _) | * name = = bookmark) {
2978
return fail ( format! ( " cannot check out {bookmark} : {why} " ) ) ;
2979
}
14edbaaf Andy McCall 1 month ago ↶
2980
let initial = repo.config.bookmark.clone ( ) ;
2981
if bookmark ! = repo.config.bookmark {
2982
fs:: create_dir_all( repo.workspace_bookmark_path ( ) .parent ( ) .unwrap ( ) ) ? ;
2983
write_atomic ( & repo.workspace_bookmark_path ( ) , bookmark.as_bytes ( ) ) ? ;
2984
repo.config.bookmark = bookmark.clone ( ) ;
2985
}
2986
if let Some ( state) = theirs.get ( & bookmark) .copied ( ) {
98286406 Andy McCall 1 month ago ↶
2987
let heads = repo.store.get_state ( & state) ? .heads;
2988
let mut view = op.view.clone ( ) ;
14edbaaf Andy McCall 1 month ago ↶
2989
2990
if initial ! = bookmark & & repo.heads_of ( & view, & initial) ? .is_empty ( ) {
2991
view.bookmarks.remove ( & initial) ;
2992
}
98286406 Andy McCall 1 month ago ↶
2993
view.bookmarks.insert ( bookmark, state) ;
2994
repo.record_op ( & [ op_ref] , & format! ( " oxy clone {url} " ) , view) ? ;
2995
repo.checkout ( & heads, false , " oxy clone (checkout)" ) ? ;
2996
}
d36ba2f7 Andy McCall 16 days ago ↶
2997
Ok ( ( repo, skipped) )
f8c69576 Andy McCall 1 month ago ↶
2998
}
2999
}
3000
3001
3002
3003
3004
3005
fn next_serial ( last : Option< u64 > ) -> u64 {
3006
match last {
3007
Some ( l) => l + 1 ,
3008
None => Hlc:: now( ) .wall_ms.max ( 1 ) ,
98286406 Andy McCall 1 month ago ↶
3009
}
3010
}