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