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