98286406 Andy McCall 1 month ago ↶
1
2
3
use super :: * ;
4
7aa2dc93 Andy McCall 1 month ago ↶
5
6
# [ derive ( Clone, Debug, Default ) ]
7
pub struct PushReport {
8
pub results : Vec< RefResult> ,
9
10
pub updated : Vec< RefUpdated> ,
11
12
pub imported : Vec< ChangeHash> ,
13
}
14
15
# [ derive ( Clone, Debug ) ]
16
pub struct RefUpdated {
17
pub name : String,
18
pub old_state : ObjectRef,
19
pub new_state : ObjectRef,
20
pub heads : Vec< ChangeHash> ,
21
}
22
83e0677b Andy McCall 1 month ago ↶
23
24
# [ derive ( Debug, Clone, PartialEq, Eq, Default ) ]
25
pub struct FetchReport {
26
27
pub refs : Vec< ( String, Vec< ChangeHash> , usize ) > ,
132b884b Andy McCall 1 month ago ↶
28
29
pub new_tags : usize ,
83e0677b Andy McCall 1 month ago ↶
30
31
32
pub unverified : bool ,
33
}
34
98286406 Andy McCall 1 month ago ↶
35
# [ derive ( Debug, Clone, PartialEq, Eq ) ]
36
pub struct PullReport {
14edbaaf Andy McCall 1 month ago ↶
37
98286406 Andy McCall 1 month ago ↶
38
pub heads : Vec< ChangeHash> ,
14edbaaf Andy McCall 1 month ago ↶
39
98286406 Andy McCall 1 month ago ↶
40
pub changed : bool ,
41
pub checked_out : bool ,
14edbaaf Andy McCall 1 month ago ↶
42
43
44
pub others : Vec< String> ,
98286406 Andy McCall 1 month ago ↶
45
}
46
47
impl Repo {
48
49
50
fn ref_attest_path ( & self , bookmark : & str ) -> PathBuf {
54cd08a1 Andy McCall 1 month ago ↶
51
self .oxy.join ( " attest" ) .join ( name_file ( bookmark) )
98286406 Andy McCall 1 month ago ↶
52
}
53
54
55
pub fn latest_ref_attest ( & self , bookmark : & str ) -> Result< Option< ( ObjectRef, SignedAttest) > > {
56
match fs:: read_to_string( self .ref_attest_path ( bookmark) ) {
57
Ok ( h) => {
75167672 Andy McCall 1 month ago
58
let r = ObjectRef:: from_hex( h.trim ( ) )
59
.ok_or_else ( | | RepoError:: Msg( " corrupt attestation pointer" .into ( ) ) ) ? ;
60
Ok ( Some ( (
61
r,
62
SignedAttest {
63
attest: self .store.get_attest ( & r) ? ,
64
sigs: self .store.signatures ( & r) ? ,
65
} ,
66
) ) )
98286406 Andy McCall 1 month ago ↶
67
}
68
Err ( e) if e.kind ( ) = = std:: io:: ErrorKind:: NotFound => Ok ( None ) ,
69
Err ( e) => Err ( e.into ( ) ) ,
70
}
71
}
72
9f9e72b9 Andy McCall 1 month ago ↶
73
pub ( crate ) fn set_ref_attest ( & self , bookmark : & str , r : & ObjectRef) -> Result< ( ) > {
98286406 Andy McCall 1 month ago ↶
74
fs:: create_dir_all( self .oxy.join ( " attest" ) ) ? ;
75
write_atomic ( & self .ref_attest_path ( bookmark) , r.to_hex ( ) .as_bytes ( ) )
76
}
77
83e0677b Andy McCall 1 month ago ↶
78
pub fn remote_refs ( & self ) -> Result< RefsResponse> {
98286406 Andy McCall 1 month ago ↶
79
let view = self .view ( ) ? ;
80
let mut out = Vec :: new( ) ;
81
for ( name, state) in & view.bookmarks {
82
let latest = self .latest_ref_attest ( name) ? .map ( | ( _, a ) | a ) ;
83
let last_serial = latest.as_ref ( ) .and_then ( | a | match a.attest.body {
84
AttestBody:: Ref { serial, .. } => Some ( serial) ,
85
_ => None ,
86
} ) ;
87
let attestation = latest.filter ( | a | matches! ( & a.attest.body, AttestBody:: Ref { new_state, .. } if new_state = = state) ) ;
75167672 Andy McCall 1 month ago
88
out.push ( RemoteRef {
89
name: name.clone ( ) ,
90
state: * state,
91
heads: self .store.get_state ( state) ? .heads,
92
attestation,
93
last_serial,
94
} ) ;
98286406 Andy McCall 1 month ago ↶
95
}
28fc23d7 Andy McCall 1 month ago ↶
96
let policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp ) ;
75167672 Andy McCall 1 month ago
97
let history = self
98
.policy_history ( ) ?
99
.into_iter ( )
100
.filter ( | sp | {
101
policy
102
.as_ref ( )
103
.is_some_and ( | p | sp.policy.serial < p.policy.serial )
104
} )
105
.collect ( ) ;
106
Ok ( RefsResponse {
107
refs: out,
108
policy,
109
tags: self .tag_refs ( ) ? ,
110
history,
14edbaaf Andy McCall 1 month ago ↶
111
default_bookmark: Some ( self .config.bookmark.clone ( ) ) ,
75167672 Andy McCall 1 month ago
112
} )
98286406 Andy McCall 1 month ago ↶
113
}
114
115
116
fn closure_in_store ( & self , heads : & [ChangeHash]) -> Result< BTreeSet< ChangeHash> > {
117
let mut seen = BTreeSet:: new( ) ;
118
let mut stack: Vec< ChangeHash> = heads.to_vec ( ) ;
119
while let Some ( h) = stack.pop ( ) {
120
if seen.contains ( & h) | | ! self .store.has ( & h) {
121
continue ;
122
}
123
seen.insert ( h) ;
69816d68 Andy McCall 1 month ago ↶
124
stack.extend ( self .store.get_change_meta ( & h) ? .deps) ;
98286406 Andy McCall 1 month ago ↶
125
}
126
Ok ( seen)
127
}
128
2863456a Andy McCall 1 month ago ↶
129
130
131
pub fn shallow_changes ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > {
132
let mut out = Vec :: new( ) ;
133
let mut seen: BTreeSet< ChangeHash> = BTreeSet:: new( ) ;
134
let mut layer: Vec< ChangeHash> = heads.to_vec ( ) ;
135
for _ in 0 .. depth.max ( 1 ) {
136
let mut next = Vec :: new( ) ;
137
for h in layer {
138
if ! seen.insert ( h) | | ! self .store.has ( & h) {
139
continue ;
140
}
141
let c = self .store.get_change ( & h) ? ;
142
next.extend ( c.deps.iter ( ) .copied ( ) ) ;
143
out.push ( h) ;
144
}
145
if next.is_empty ( ) {
146
break ;
147
}
148
layer = next;
149
}
150
out.reverse ( ) ;
151
Ok ( out)
152
}
153
154
fn shallow_path ( & self ) -> PathBuf {
155
self .oxy.join ( " shallow" )
156
}
157
158
159
pub fn boundary ( & self ) -> BTreeSet< ChangeHash> {
75167672 Andy McCall 1 month ago
160
fs:: read_to_string( self .shallow_path ( ) )
161
.map ( | t | t.lines ( ) .filter_map ( ObjectRef:: from_hex) .collect ( ) )
162
.unwrap_or_default ( )
2863456a Andy McCall 1 month ago ↶
163
}
164
165
pub fn is_shallow ( & self ) -> bool {
166
! self .boundary ( ) .is_empty ( )
167
}
168
169
fn write_boundary ( & self , boundary : & BTreeSet< ChangeHash> ) -> Result< ( ) > {
170
if boundary.is_empty ( ) {
171
let _ = fs:: remove_file( self .shallow_path ( ) ) ;
172
return Ok ( ( ) ) ;
173
}
75167672 Andy McCall 1 month ago
174
let text: String = boundary
175
.iter ( )
176
.map ( | h | format! ( " {} \n " , h.to_hex ( ) ) )
177
.collect ( ) ;
2863456a Andy McCall 1 month ago ↶
178
write_atomic ( & self .shallow_path ( ) , text.as_bytes ( ) )
179
}
180
181
182
183
fn refresh_boundary ( & self , imported : & [ChangeHash]) -> Result< ( ) > {
184
let mut boundary = self .boundary ( ) ;
185
boundary.retain ( | h | ! self .store.has ( h) ) ;
186
for h in imported {
69816d68 Andy McCall 1 month ago ↶
187
for d in self .store.get_change_meta ( h) ? .deps {
2863456a Andy McCall 1 month ago ↶
188
if ! self .store.has ( & d) {
189
boundary.insert ( d) ;
190
}
191
}
192
}
193
self .write_boundary ( & boundary)
194
}
195
196
197
pub fn deepen ( & self , remote_name : & str , depth : u32 ) -> Result< usize > {
198
let boundary = self .boundary ( ) ;
199
if boundary.is_empty ( ) {
200
return Ok ( 0 ) ;
201
}
202
let remote = self .open_named_remote ( remote_name) ? ;
203
let wanted = remote.shallow ( & boundary.iter ( ) .copied ( ) .collect:: < Vec< _ > > ( ) , depth) ? ;
204
let bundle = remote.fetch ( & wanted, self .config.max_blob) ? ;
205
let imported = self .import_bundle_shallow ( & bundle, false ) ? ;
206
if self .config.max_blob.is_some ( ) {
207
self .register_missing_blobs ( & imported, remote_name) ? ;
208
}
209
self .refresh_boundary ( & imported) ? ;
210
Ok ( imported.len ( ) )
211
}
212
98286406 Andy McCall 1 month ago ↶
213
214
pub fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > {
215
let have = self .closure_in_store ( have) ? ;
2863456a Andy McCall 1 month ago ↶
216
217
218
let boundary = self .boundary ( ) ;
98286406 Andy McCall 1 month ago ↶
219
let mut out = Vec :: new( ) ;
220
let mut done: BTreeSet< ChangeHash> = have.clone ( ) ;
2863456a Andy McCall 1 month ago ↶
221
done.extend ( boundary.iter ( ) .copied ( ) ) ;
98286406 Andy McCall 1 month ago ↶
222
223
let mut stack: Vec< ( ChangeHash, bool ) > = want.iter ( ) .rev ( ) .map ( | h | ( * h, false ) ) .collect ( ) ;
224
while let Some ( ( h, expanded) ) = stack.pop ( ) {
225
if done.contains ( & h) {
226
continue ;
227
}
228
if ! self .store.has ( & h) {
229
return fail ( format! ( " change {} is not in this repository" , h.short ( 12 ) ) ) ;
230
}
231
if expanded {
232
done.insert ( h) ;
233
out.push ( h) ;
234
continue ;
235
}
236
stack.push ( ( h, true ) ) ;
69816d68 Andy McCall 1 month ago ↶
237
for d in self .store.get_change_meta ( & h) ? .deps {
98286406 Andy McCall 1 month ago ↶
238
if ! done.contains ( & d) {
239
stack.push ( ( d, false ) ) ;
240
}
241
}
242
}
243
Ok ( out)
244
}
245
246
247
248
pub fn bundle_for ( & self , changes : & [ChangeHash]) -> Result< Bundle> {
b3c5a59a Andy McCall 1 month ago ↶
249
self .bundle_for_filtered ( changes, None )
250
}
251
252
253
pub fn objects_for ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > {
254
let mut out = Vec :: new( ) ;
255
for r in refs {
256
let ( ty, body) = match self .store.get ( r) {
257
Ok ( x) => x,
258
Err ( StoreError:: NotFound( _ ) ) => continue ,
259
Err ( e) => return Err ( e.into ( ) ) ,
260
} ;
261
if ty = = ObjectType:: Blob {
262
for c in Blob:: decode( & body) ? .chunks {
263
if let Ok ( ( cty, cb) ) = self .store.get ( & c) {
264
out.push ( ( cty, cb) ) ;
265
}
266
}
267
}
268
out.push ( ( ty, body) ) ;
269
}
270
Ok ( out)
271
}
272
273
274
75167672 Andy McCall 1 month ago
275
pub fn bundle_for_filtered (
276
& self ,
277
changes : & [ChangeHash],
278
max_blob : Option< u64 > ,
279
) -> Result< Bundle> {
2c75672f Andy McCall 1 month ago ↶
280
self .bundle_of ( changes, max_blob, false )
281
}
282
283
284
285
286
287
pub fn bundle_raw_for ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> {
288
self .bundle_of ( changes, max_blob, true )
289
}
290
291
fn bundle_of (
292
& self ,
293
changes : & [ChangeHash],
294
max_blob : Option< u64 > ,
295
raw : bool ,
296
) -> Result< Bundle> {
98286406 Andy McCall 1 month ago ↶
297
let mut bundle = Bundle:: default( ) ;
298
let mut objects: BTreeSet< ObjectRef> = BTreeSet:: new( ) ;
299
let mut seals: BTreeSet< ObjectRef> = BTreeSet:: new( ) ;
300
let add_object = | r : & ObjectRef, objects : & mut BTreeSet< ObjectRef> | {
301
if ! r.is_zero ( ) & & self .store.has ( r) {
302
objects.insert ( * r) ;
303
}
304
} ;
305
for h in changes {
2c75672f Andy McCall 1 month ago ↶
306
307
let ( c, encoded) = if raw {
308
let bytes = self .store.get_typed ( h, ObjectType:: Change) ? ;
309
let ( skel, texts) = Change:: decode_skeleton( & bytes, * h) ? ;
310
for t in texts {
0f93f49c Andy McCall 1 month ago ↶
311
if let crate :: objects:: TextRef:: Blob( r, _ , _ ) = t {
2c75672f Andy McCall 1 month ago ↶
312
let blob = Blob:: decode( & self .store.get_typed ( & r, ObjectType:: Blob) ? ) ? ;
313
objects.insert ( r) ;
314
objects.extend ( blob.chunks) ;
315
}
316
}
317
( skel, Some ( bytes) )
318
} else {
319
( self .store.get_change ( h) ? , None )
320
} ;
98286406 Andy McCall 1 month ago ↶
321
let sigs = self .store.signatures ( h) ? ;
322
add_object ( & c.author, & mut objects) ;
323
for s in & sigs {
324
add_object ( & s.key, & mut objects) ;
325
}
326
for e in & c.edits {
327
if let Edit:: Replace { old, new, .. } = e {
328
for b in [ old, new] .into_iter ( ) .flatten ( ) {
329
if self .store.has ( b) {
75167672 Andy McCall 1 month ago
330
if let Ok ( blob) =
331
Blob:: decode( & self .store.get_typed ( b, ObjectType:: Blob) ? )
332
{
b3c5a59a Andy McCall 1 month ago ↶
333
if max_blob.is_some_and ( | m | blob.length > m ) {
334
continue ;
335
}
336
objects.insert ( * b) ;
98286406 Andy McCall 1 month ago ↶
337
objects.extend ( blob.chunks.iter ( ) .copied ( ) ) ;
338
}
339
}
340
}
341
}
342
}
343
for s in self .store.seals_for ( h) ? {
344
seals.insert ( s) ;
2c75672f Andy McCall 1 month ago ↶
345
}
346
match encoded {
347
Some ( bytes) => bundle.raw_changes.push ( ( bytes, sigs) ) ,
348
None => {
349
bundle.changes.push ( ( c, sigs) ) ;
350
bundle.hashes.push ( * h) ;
351
}
98286406 Andy McCall 1 month ago ↶
352
}
353
}
354
for r in seals {
355
let attest = self .store.get_attest ( & r) ? ;
356
let sigs = self .store.signatures ( & r) ? ;
357
for s in & sigs {
358
add_object ( & s.key, & mut objects) ;
359
}
360
bundle.attests.push ( SignedAttest { attest, sigs } ) ;
361
}
362
for r in objects {
363
let ( ty, body) = self .store.get ( & r) ? ;
364
bundle.objects.push ( ( ty, body) ) ;
365
}
366
Ok ( bundle)
367
}
368
369
370
371
pub fn import_bundle ( & self , bundle : & Bundle, strict : bool ) -> Result< Vec< ChangeHash> > {
2863456a Andy McCall 1 month ago ↶
372
self .import_bundle_inner ( bundle, strict, false )
373
}
374
375
376
377
pub fn import_bundle_shallow ( & self , bundle : & Bundle, strict : bool ) -> Result< Vec< ChangeHash> > {
378
self .import_bundle_inner ( bundle, strict, true )
379
}
380
75167672 Andy McCall 1 month ago
381
fn import_bundle_inner (
382
& self ,
383
bundle : & Bundle,
384
strict : bool ,
385
shallow : bool ,
386
) -> Result< Vec< ChangeHash> > {
8f10a07a Andy McCall 1 month ago ↶
387
let batch = self .store.begin_batch ( ) ? ;
388
let result = self .import_bundle_batched ( bundle, strict, shallow) ;
389
batch.finish ( ) ? ;
390
result
391
}
392
75167672 Andy McCall 1 month ago
393
fn import_bundle_batched (
394
& self ,
395
bundle : & Bundle,
396
strict : bool ,
397
shallow : bool ,
398
) -> Result< Vec< ChangeHash> > {
2c75672f Andy McCall 1 month ago ↶
399
for ( ty, _ ) in & bundle.objects {
400
if ! matches! (
401
ty,
75167672 Andy McCall 1 month ago
402
ObjectType:: Chunk
2c75672f Andy McCall 1 month ago ↶
403
| ObjectType:: Blob
404
| ObjectType:: Identity
405
| ObjectType:: State
406
| ObjectType:: Checkpoint
407
) {
408
return fail ( format! ( " bundle may not carry {} objects" , ty.name ( ) ) ) ;
98286406 Andy McCall 1 month ago ↶
409
}
410
}
2c75672f Andy McCall 1 month ago ↶
411
412
let objects: Vec< ( ObjectType, & [u8 ]) > = bundle
413
.objects
414
.iter ( )
415
.map ( | ( ty , body ) | ( * ty, body.as_slice ( ) ) )
416
.collect ( ) ;
417
self .store.put_many ( & objects) ? ;
98286406 Andy McCall 1 month ago ↶
418
for sa in & bundle.attests {
419
let r = self .store.put_attest ( & sa.attest) ? ;
420
for sig in & sa.sigs {
421
self .store.put_signature ( & r, sig) ? ;
422
}
423
if let AttestBody:: Seal { hashes } = & sa.attest.body {
424
for h in hashes {
425
self .store.index_seal ( h, & r) ? ;
426
}
427
}
428
}
598009a7 Andy McCall 1 month ago ↶
429
430
431
432
let hashes: Vec< ChangeHash> = if bundle.hashes.len ( ) = = bundle.changes.len ( ) {
433
bundle.hashes.clone ( )
434
} else {
435
bundle.changes.iter ( ) .map ( | ( c , _) | c.hash ( ) ) .collect ( )
436
} ;
2c75672f Andy McCall 1 month ago ↶
437
let raw_hashes: Vec< ChangeHash> = bundle
438
.raw_changes
439
.iter ( )
440
.map ( | ( b , _) | hash_object ( ObjectType:: Change, b) )
441
.collect ( ) ;
442
let incoming: BTreeSet< ChangeHash> = hashes.iter ( ) .chain ( & raw_hashes) .copied ( ) .collect ( ) ;
2863456a Andy McCall 1 month ago ↶
443
let boundary = self .boundary ( ) ;
98286406 Andy McCall 1 month ago ↶
444
let mut imported = Vec :: new( ) ;
2c75672f Andy McCall 1 month ago ↶
445
446
447
for ( ( bytes, sigs) , h) in bundle.raw_changes.iter ( ) .zip ( raw_hashes) {
448
let ( skel, texts) = Change:: decode_skeleton( bytes, h) ? ;
449
for d in & skel.deps {
450
if ! self .store.has ( d) & & ! incoming.contains ( d) & & ! shallow & & ! boundary.contains ( d)
451
{
452
return fail ( format! (
453
" change {} depends on {} , which is neither stored nor in the bundle" ,
454
h.short ( 12 ) ,
455
d.short ( 12 )
456
) ) ;
457
}
458
}
459
for t in texts {
0f93f49c Andy McCall 1 month ago ↶
460
if let crate :: objects:: TextRef:: Blob( r, _ , _ ) = t {
2c75672f Andy McCall 1 month ago ↶
461
if ! self .store.has ( & r) {
462
return fail ( format! (
463
" change {} came without its text blob {} " ,
464
h.short ( 12 ) ,
465
r.short ( 12 )
466
) ) ;
467
}
468
}
469
}
470
self .store.put ( ObjectType:: Change, bytes) ? ;
471
for sig in sigs {
472
self .store.put_signature ( & h, sig) ? ;
473
}
474
imported.push ( h) ;
475
}
598009a7 Andy McCall 1 month ago ↶
476
for ( ( c, sigs) , h) in bundle.changes.iter ( ) .zip ( hashes) {
98286406 Andy McCall 1 month ago ↶
477
for d in & c.deps {
75167672 Andy McCall 1 month ago
478
if ! self .store.has ( d) & & ! incoming.contains ( d) & & ! shallow & & ! boundary.contains ( d)
479
{
480
return fail ( format! (
481
" change {} depends on {} , which is neither stored nor in the bundle" ,
482
h.short ( 12 ) ,
483
d.short ( 12 )
484
) ) ;
98286406 Andy McCall 1 month ago ↶
485
}
486
}
598009a7 Andy McCall 1 month ago ↶
487
let stored = self .store.put_change ( c) ? ;
488
if stored ! = h {
489
return fail ( format! (
490
" change {} is not the change the bundle names" ,
491
h.short ( 12 )
492
) ) ;
493
}
98286406 Andy McCall 1 month ago ↶
494
for sig in sigs {
495
self .store.put_signature ( & h, sig) ? ;
496
}
497
imported.push ( h) ;
498
}
499
if strict {
500
for h in & imported {
501
if ! matches! ( self .authenticate_change ( h) ? , SignatureStatus:: Valid { .. } ) {
75167672 Andy McCall 1 month ago
502
return fail ( format! (
503
" change {} is not authenticated by its author" ,
504
h.short ( 12 )
505
) ) ;
98286406 Andy McCall 1 month ago ↶
506
}
507
}
508
}
509
Ok ( imported)
510
}
511
512
513
514
84b9e9ac Andy McCall 1 month ago ↶
515
516
7ba155f2 Andy McCall 1 month ago ↶
517
518
519
eae59556 Andy McCall 1 month ago ↶
520
pub ( crate ) fn delegate_authorized (
521
& self ,
522
p : & crate:: policy:: Policy,
523
signer : & KeyId,
524
reference : & str ,
7ba155f2 Andy McCall 1 month ago ↶
525
at_ms : u64 ,
eae59556 Andy McCall 1 month ago ↶
526
) -> bool {
84b9e9ac Andy McCall 1 month ago ↶
527
if p.is_revoked ( signer) {
528
return false ;
529
}
eae59556 Andy McCall 1 month ago ↶
530
let Some ( rule) = p.rule_for ( reference) else {
531
return false ;
532
} ;
84b9e9ac Andy McCall 1 month ago ↶
533
rule.keys.iter ( ) .any ( | k | {
534
! p.is_revoked ( k)
535
& & p.identity ( k)
536
.cloned ( )
537
.or_else ( | | self .store.get_identity ( k) .ok ( ) )
7ba155f2 Andy McCall 1 month ago ↶
538
.is_some_and ( | id | id.delegates_to ( signer, Scope:: REFS , at_ms) )
84b9e9ac Andy McCall 1 month ago ↶
539
} )
540
}
541
75167672 Andy McCall 1 month ago
542
fn check_ref_attest (
543
& self ,
544
sa : & SignedAttest,
545
name : & str ,
546
old_state : & ObjectRef,
547
new_state : & ObjectRef,
548
last_serial : Option< u64 > ,
549
allow_equal : bool ,
550
) -> std:: result:: Result< u64 , String> {
551
let AttestBody:: Ref {
552
name: n,
553
old_state: o,
554
new_state: nw,
555
serial,
556
} = & sa.attest.body
557
else {
98286406 Andy McCall 1 month ago ↶
558
return Err ( " attestation is not a ref attestation" .into ( ) ) ;
559
} ;
560
if n ! = name | | o ! = old_state | | nw ! = new_state {
561
return Err ( " attestation does not describe this update" .into ( ) ) ;
562
}
563
if sa.attest.repo ! = self .id ( ) .map_err ( | e | e.to_string ( ) ) ? {
564
return Err ( " attestation is for a different repository" .into ( ) ) ;
565
}
566
if let Some ( last) = last_serial {
567
if * serial < last | | ( * serial = = last & & ! allow_equal) {
75167672 Andy McCall 1 month ago
568
return Err ( format! (
569
" attestation serial {serial} is not greater than the last seen {last} "
570
) ) ;
98286406 Andy McCall 1 month ago ↶
571
}
572
}
573
let r = sa.attest.hash ( ) ;
574
let sig = sa.sigs.first ( ) .ok_or ( " attestation carries no signature" ) ? ;
75167672 Andy McCall 1 month ago
575
let id = self
576
.store
577
.get_identity ( & sig.key)
578
.map_err ( | _| format! ( " unknown key {} " , sig.key.short ( 12 ) ) ) ? ;
98286406 Andy McCall 1 month ago ↶
579
verify_signature ( & id, sig, Domain:: Attest, & r) .map_err ( | e | e.to_string ( ) ) ? ;
580
Ok ( * serial)
581
}
582
83e0677b Andy McCall 1 month ago ↶
583
98286406 Andy McCall 1 month ago ↶
584
83e0677b Andy McCall 1 month ago ↶
585
586
587
75167672 Andy McCall 1 month ago
588
pub fn receive_push (
589
& self ,
590
bundle : Bundle,
591
updates : Vec< RefUpdate> ,
592
policy : Option< SignedPolicy> ,
593
tags : Vec< crate:: protocol:: TagRef> ,
594
) -> Result< Vec< RefResult> > {
119bbf50 Andy McCall 1 month ago ↶
595
Ok ( self
596
.receive_push_report ( bundle, updates, policy, tags) ?
597
.results )
7aa2dc93 Andy McCall 1 month ago ↶
598
}
599
600
601
602
603
pub fn receive_push_report (
604
& self ,
605
bundle : Bundle,
606
updates : Vec< RefUpdate> ,
607
policy : Option< SignedPolicy> ,
608
tags : Vec< crate:: protocol:: TagRef> ,
609
) -> Result< PushReport> {
610
let mut report = PushReport:: default( ) ;
611
let results = & mut report.results;
83e0677b Andy McCall 1 month ago ↶
612
if let Some ( sp) = policy {
28fc23d7 Andy McCall 1 month ago ↶
613
match self .adopt_policy_chain ( & bundle.policies, & sp, " push received" ) {
83e0677b Andy McCall 1 month ago ↶
614
Ok ( ( ) ) => {
75167672 Andy McCall 1 month ago
615
results.push ( RefResult {
616
name: " policy" .into ( ) ,
617
ok: true ,
618
reason: format! ( " serial {} " , sp.policy.serial) ,
619
} ) ;
83e0677b Andy McCall 1 month ago ↶
620
}
621
Err ( e) => {
75167672 Andy McCall 1 month ago
622
results.push ( RefResult {
623
name: " policy" .into ( ) ,
624
ok: false ,
625
reason: e,
626
} ) ;
83e0677b Andy McCall 1 month ago ↶
627
for u in updates {
75167672 Andy McCall 1 month ago
628
results.push ( RefResult {
629
name: u.name,
630
ok: false ,
631
reason: " policy update was rejected" .into ( ) ,
632
} ) ;
83e0677b Andy McCall 1 month ago ↶
633
}
7aa2dc93 Andy McCall 1 month ago ↶
634
return Ok ( report) ;
83e0677b Andy McCall 1 month ago ↶
635
}
636
}
637
}
638
let policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp.policy ) ;
7aa2dc93 Andy McCall 1 month ago ↶
639
match self .import_bundle ( & bundle, policy.is_some ( ) ) {
640
Ok ( imported) => report.imported = imported,
641
Err ( e) => {
642
for u in updates {
643
results.push ( RefResult {
644
name: u.name,
645
ok: false ,
646
reason: e.to_string ( ) ,
647
} ) ;
648
}
649
return Ok ( report) ;
83e0677b Andy McCall 1 month ago ↶
650
}
651
}
98286406 Andy McCall 1 month ago ↶
652
let ( op_ref, op) = self .head_op ( ) ? ;
653
let mut view = op.view.clone ( ) ;
654
let mut names = Vec :: new( ) ;
655
for u in updates {
83e0677b Andy McCall 1 month ago ↶
656
let outcome = self .apply_update ( & mut view, & u, policy.as_ref ( ) ) ;
98286406 Andy McCall 1 month ago ↶
657
let ( ok, reason) = match outcome {
658
Ok ( ( ) ) => ( true , String :: new( ) ) ,
659
Err ( e) => ( false , e) ,
660
} ;
661
if ok {
662
names.push ( u.name.clone ( ) ) ;
119bbf50 Andy McCall 1 month ago ↶
663
let heads = self
664
.store
665
.get_state ( & u.new_state)
666
.map ( | s | s.heads )
667
.unwrap_or_default ( ) ;
668
report.updated.push ( RefUpdated {
669
name: u.name.clone ( ) ,
670
old_state: u.old_state,
671
new_state: u.new_state,
672
heads,
673
} ) ;
98286406 Andy McCall 1 month ago ↶
674
}
75167672 Andy McCall 1 month ago
675
results.push ( RefResult {
676
name: u.name,
677
ok,
678
reason,
679
} ) ;
98286406 Andy McCall 1 month ago ↶
680
}
132b884b Andy McCall 1 month ago ↶
681
for t in & tags {
682
let name = format! ( " tags/{} " , t.name) ;
683
match self .adopt_tag ( & mut view, t, policy.as_ref ( ) ) {
684
Ok ( ( ) ) => {
685
names.push ( name.clone ( ) ) ;
75167672 Andy McCall 1 month ago
686
results.push ( RefResult {
687
name,
688
ok: true ,
689
reason: String :: new( ) ,
690
} ) ;
132b884b Andy McCall 1 month ago ↶
691
}
75167672 Andy McCall 1 month ago
692
Err ( e) => results.push ( RefResult {
693
name,
694
ok: false ,
695
reason: e,
696
} ) ,
132b884b Andy McCall 1 month ago ↶
697
}
698
}
98286406 Andy McCall 1 month ago ↶
699
if ! names.is_empty ( ) {
75167672 Andy McCall 1 month ago
700
self .record_op (
701
& [ op_ref] ,
702
& format! ( " push received: {} " , names.join ( " " ) ) ,
703
view,
704
) ? ;
98286406 Andy McCall 1 month ago ↶
705
}
7aa2dc93 Andy McCall 1 month ago ↶
706
Ok ( report)
98286406 Andy McCall 1 month ago ↶
707
}
708
f8c69576 Andy McCall 1 month ago ↶
709
710
711
712
713
714
fn apply_deletion (
715
& self ,
716
view : & mut View,
717
u : & RefUpdate,
718
policy : Option< & crate:: policy:: Policy> ,
719
) -> std:: result:: Result< ( ) , String> {
720
let Some ( current) = view.bookmarks.get ( & u.name) .copied ( ) else {
721
return Err ( format! ( " no bookmark {:?} to delete" , u.name) ) ;
722
} ;
723
if current ! = u.old_state {
724
return Err ( " stale: the remote bookmark has moved; pull first" .into ( ) ) ;
725
}
726
if u.name = = self .config.bookmark {
727
return Err ( format! (
728
" {:?} is this repository's default bookmark and cannot be deleted" ,
729
u.name
730
) ) ;
731
}
732
if let Some ( p) = policy {
733
let Some ( sa) = & u.attestation else {
734
return Err ( format! (
735
" deleting {:?} requires a signed reference attestation under this repository's policy" ,
736
u.name
737
) ) ;
738
} ;
739
let signer = sa
740
.sigs
741
.first ( )
742
.map ( | s | s.key )
743
.ok_or ( " attestation carries no signature" ) ? ;
7ba155f2 Andy McCall 1 month ago ↶
744
if ! p.authorizes ( & signer, & u.name)
745
& & ! self .delegate_authorized ( p, & signer, & u.name, sa.attest.hlc.wall_ms)
746
{
f8c69576 Andy McCall 1 month ago ↶
747
return Err ( format! (
748
" key {} may not delete {:?} under this repository's policy" ,
749
signer.short ( 12 ) ,
750
u.name
751
) ) ;
752
}
753
}
754
if let Some ( sa) = & u.attestation {
755
let last = self
756
.latest_ref_attest ( & u.name)
757
.map_err ( | e | e.to_string ( ) ) ?
758
.and_then ( | ( _, a ) | match a.attest.body {
759
AttestBody:: Ref { serial, .. } => Some ( serial) ,
760
_ => None ,
761
} ) ;
762
self .check_ref_attest ( sa, & u.name, & u.old_state, & u.new_state, last, false ) ? ;
763
let r = self
764
.store
765
.put_attest ( & sa.attest)
766
.map_err ( | e | e.to_string ( ) ) ? ;
767
for sig in & sa.sigs {
768
self .store
769
.put_signature ( & r, sig)
770
.map_err ( | e | e.to_string ( ) ) ? ;
771
}
772
self .set_ref_attest ( & u.name, & r)
773
.map_err ( | e | e.to_string ( ) ) ? ;
774
}
775
view.bookmarks.remove ( & u.name) ;
776
Ok ( ( ) )
777
}
778
75167672 Andy McCall 1 month ago
779
fn apply_update (
780
& self ,
781
view : & mut View,
782
u : & RefUpdate,
783
policy : Option< & crate:: policy:: Policy> ,
784
) -> std:: result:: Result< ( ) , String> {
83e0677b Andy McCall 1 month ago ↶
785
if u.name = = " policy" {
75167672 Andy McCall 1 month ago
786
return Err (
787
" the policy reference is updated by sending a policy, not a bookmark update" .into ( ) ,
788
) ;
83e0677b Andy McCall 1 month ago ↶
789
}
75167672 Andy McCall 1 month ago
790
let empty = self
791
.store
792
.put_state ( & StateObject:: new( vec! [ ] ) )
793
.map_err ( | e | e.to_string ( ) ) ? ;
f8c69576 Andy McCall 1 month ago ↶
794
if u.new_state.is_zero ( ) {
795
return self .apply_deletion ( view, u, policy) ;
796
}
98286406 Andy McCall 1 month ago ↶
797
let current = view.bookmarks.get ( & u.name) .copied ( ) .unwrap_or ( empty) ;
798
if current ! = u.old_state {
799
return Err ( " stale: the remote bookmark has moved; pull first" .into ( ) ) ;
800
}
75167672 Andy McCall 1 month ago
801
let new_state = self
802
.store
803
.get_state ( & u.new_state)
804
.map_err ( | _| " new state object is missing" .to_string ( ) ) ? ;
98286406 Andy McCall 1 month ago ↶
805
for h in & new_state.heads {
806
if ! self .store.has ( h) {
807
return Err ( format! ( " head {} is missing" , h.short ( 12 ) ) ) ;
808
}
809
}
810
if ! u.force {
75167672 Andy McCall 1 month ago
811
let old_heads = self
812
.store
813
.get_state ( & current)
814
.map_err ( | e | e.to_string ( ) ) ?
815
.heads;
816
let closure = self
817
.closure_in_store ( & new_state.heads)
818
.map_err ( | e | e.to_string ( ) ) ? ;
98286406 Andy McCall 1 month ago ↶
819
if let Some ( h) = old_heads.iter ( ) .find ( | h | ! closure.contains ( h) ) {
75167672 Andy McCall 1 month ago
820
return Err ( format! (
821
" not a fast-forward: current head {} would be dropped (use --force)" ,
822
h.short ( 12 )
823
) ) ;
98286406 Andy McCall 1 month ago ↶
824
}
825
}
83e0677b Andy McCall 1 month ago ↶
826
if let Some ( p) = policy {
827
let Some ( sa) = & u.attestation else {
75167672 Andy McCall 1 month ago
828
return Err ( format! (
829
" {:?} requires a signed reference attestation under this repository's policy" ,
830
u.name
831
) ) ;
83e0677b Andy McCall 1 month ago ↶
832
} ;
75167672 Andy McCall 1 month ago
833
let signer = sa
834
.sigs
835
.first ( )
836
.map ( | s | s.key )
837
.ok_or ( " attestation carries no signature" ) ? ;
7ba155f2 Andy McCall 1 month ago ↶
838
if ! p.authorizes ( & signer, & u.name)
839
& & ! self .delegate_authorized ( p, & signer, & u.name, sa.attest.hlc.wall_ms)
840
{
75167672 Andy McCall 1 month ago
841
return Err ( format! (
842
" key {} may not update {:?} under this repository's policy" ,
843
signer.short ( 12 ) ,
844
u.name
845
) ) ;
83e0677b Andy McCall 1 month ago ↶
846
}
75167672 Andy McCall 1 month ago
847
let old_heads = self
848
.store
849
.get_state ( & current)
850
.map_err ( | e | e.to_string ( ) ) ?
851
.heads;
83e0677b Andy McCall 1 month ago ↶
852
self .state_allowed ( & old_heads, & new_state.heads, p, & u.name) ? ;
853
}
98286406 Andy McCall 1 month ago ↶
854
if let Some ( sa) = & u.attestation {
75167672 Andy McCall 1 month ago
855
let last = self
856
.latest_ref_attest ( & u.name)
857
.map_err ( | e | e.to_string ( ) ) ?
858
.and_then ( | ( _, a ) | match a.attest.body {
859
AttestBody:: Ref { serial, .. } => Some ( serial) ,
860
_ => None ,
861
} ) ;
98286406 Andy McCall 1 month ago ↶
862
self .check_ref_attest ( sa, & u.name, & u.old_state, & u.new_state, last, false ) ? ;
75167672 Andy McCall 1 month ago
863
let r = self
864
.store
865
.put_attest ( & sa.attest)
866
.map_err ( | e | e.to_string ( ) ) ? ;
98286406 Andy McCall 1 month ago ↶
867
for sig in & sa.sigs {
75167672 Andy McCall 1 month ago
868
self .store
869
.put_signature ( & r, sig)
870
.map_err ( | e | e.to_string ( ) ) ? ;
98286406 Andy McCall 1 month ago ↶
871
}
75167672 Andy McCall 1 month ago
872
self .set_ref_attest ( & u.name, & r)
873
.map_err ( | e | e.to_string ( ) ) ? ;
98286406 Andy McCall 1 month ago ↶
874
}
875
view.bookmarks.insert ( u.name.clone ( ) , u.new_state) ;
876
Ok ( ( ) )
877
}
878
879
880
881
fn remote_url ( & self , name : & str ) -> Result< String> {
75167672 Andy McCall 1 month ago
882
self .config.remotes.get ( name) .cloned ( ) .ok_or_else ( | | {
42ccf504 Andy McCall 28 days ago ↶
883
let names: Vec< & str > = self .config.remotes.keys ( ) .map ( String :: as_str) .collect ( ) ;
884
RepoError:: Msg( match names.as_slice ( ) {
885
[ ] => format! ( " no remote named {name:?} ; add one with `oxy remote add`" ) ,
886
_ if name = = " origin" & & names.len ( ) > 1 & & self .config.default_remote.is_none ( ) => format! (
887
" this repository has no remote named origin, and several others ({} ): name one (`oxy pull {} `), or make one the default with `oxy config core.remote {} `" ,
888
names.join ( " , " ) ,
889
names[ 0 ] ,
890
names[ 0 ]
891
) ,
892
_ => format! (
893
" no remote named {name:?} ; this repository has {} " ,
894
names.join ( " , " )
895
) ,
896
} )
75167672 Andy McCall 1 month ago
897
} )
98286406 Andy McCall 1 month ago ↶
898
}
899
75167672 Andy McCall 1 month ago
900
901
902
05e71ed6 Andy McCall 1 month ago ↶
903
fn check_remote_id (
904
& self ,
905
remote : & dyn crate:: protocol:: Remote,
906
remote_name : & str ,
907
allow_unbound : bool ,
908
) -> Result< ( ) > {
75167672 Andy McCall 1 month ago
909
let theirs = remote.repo_id ( ) ? ;
910
if theirs = = self .id ( ) ? {
911
return Ok ( ( ) ) ;
912
}
913
if theirs = = crate :: protocol:: UNBOUND_ID {
914
if allow_unbound {
915
return Ok ( ( ) ) ;
916
}
05e71ed6 Andy McCall 1 month ago ↶
917
return fail ( format! (
918
" remote {remote_name:?} is empty and unbound: push to it first"
919
) ) ;
75167672 Andy McCall 1 month ago
920
}
05e71ed6 Andy McCall 1 month ago ↶
921
fail ( format! (
3e5772cc Andy McCall 1 month ago ↶
922
" remote {remote_name:?} is a different repository (repository IDs differ); check its URL with `oxy remote list`"
05e71ed6 Andy McCall 1 month ago ↶
923
) )
75167672 Andy McCall 1 month ago
924
}
925
ff1f62fe Andy McCall 1 month ago ↶
926
927
928
3e5772cc Andy McCall 1 month ago ↶
929
pub ( crate ) fn remote_options ( & self , name : & str ) -> RemoteOptions {
ff1f62fe Andy McCall 1 month ago ↶
930
let url = self .config.remotes.get ( name) .cloned ( ) .unwrap_or_default ( ) ;
931
let mut options = RemoteOptions {
75167672 Andy McCall 1 month ago
932
repo_id: self .id ( ) .ok ( ) ,
ff1f62fe Andy McCall 1 month ago ↶
933
token: self .config.remote_tokens.get ( name) .cloned ( ) ,
934
ca: self .config.remote_cas.get ( name) .map ( PathBuf:: from) ,
9f9e72b9 Andy McCall 1 month ago ↶
935
cache_dir: Some ( self .oxy.join ( " remotes" ) .join ( name) .join ( " strata" ) ) ,
ff1f62fe Andy McCall 1 month ago ↶
936
} ;
937
self .fill_credentials ( & url, & mut options) ;
938
options
939
}
940
941
942
943
pub fn credentials ( & self ) -> crate:: credentials:: Credentials {
944
crate :: credentials:: Credentials:: at(
945
self .user_config
946
.parent ( )
947
.map ( Path:: to_path_buf)
948
.unwrap_or_else ( default_home) ,
949
)
950
}
951
952
953
954
955
fn fill_credentials ( & self , url : & str , options : & mut RemoteOptions) {
956
if options.token.is_some ( ) & & options.ca.is_some ( ) {
957
return ;
958
}
959
let stored = self .credentials ( ) .options_for ( url) ;
960
options.token = options.token.take ( ) .or ( stored.token) ;
961
options.ca = options.ca.take ( ) .or ( stored.ca) ;
962
if options.token.is_some ( ) & & options.ca.is_some ( ) {
963
return ;
2b5ee371 Andy McCall 1 month ago ↶
964
}
ff1f62fe Andy McCall 1 month ago ↶
965
let Some ( hub) = crate :: credentials:: hub_of( url) else {
966
return ;
967
} ;
968
let mut at = self .root.parent ( ) ;
969
while let Some ( dir) = at {
970
let Ok ( cfg) = read_config_file ( & dir.join ( OXY_DIR ) .join ( " config" ) ) else {
971
break ;
972
} ;
973
for ( key, remote_url) in & cfg {
974
let Some ( name) = key
975
.strip_prefix ( " remote." )
976
.and_then ( | k | k.strip_suffix ( " .url" ) )
977
else {
978
continue ;
979
} ;
980
if crate :: credentials:: hub_of( remote_url) .as_deref ( ) ! = Some ( hub.as_str ( ) ) {
981
continue ;
982
}
983
if options.token.is_none ( ) {
984
options.token = cfg.get ( & format! ( " remote.{name} .token" ) ) .cloned ( ) ;
985
}
986
if options.ca.is_none ( ) {
987
options.ca = cfg.get ( & format! ( " remote.{name} .ca" ) ) .map ( PathBuf:: from) ;
988
}
989
}
990
if options.token.is_some ( ) & & options.ca.is_some ( ) {
991
return ;
992
}
993
at = dir.parent ( ) ;
994
}
98286406 Andy McCall 1 month ago ↶
995
}
996
d82e4409 Andy McCall 1 month ago ↶
997
998
999
pub fn push_seals ( & self , remote_name : & str , seals : & [ObjectRef]) -> Result< ( ) > {
1000
let remote = self .open_named_remote ( remote_name) ? ;
1001
self .check_remote_id ( remote.as_ref ( ) , remote_name, false ) ? ;
1002
let mut bundle = Bundle:: default( ) ;
1003
for r in seals {
1004
let attest = self .store.get_attest ( r) ? ;
1005
let sigs = self .store.signatures ( r) ? ;
4213c41d Andy McCall 1 month ago ↶
1006
bundle
1007
.attests
1008
.push ( crate :: protocol:: SignedAttest { attest, sigs } ) ;
d82e4409 Andy McCall 1 month ago ↶
1009
}
1010
let results = remote.push ( bundle, vec! [ ] , None , vec! [ ] ) ? ;
1011
if let Some ( r) = results.iter ( ) .find ( | r | ! r.ok) {
1012
return fail ( format! ( " {} : {} " , r.name, r.reason) ) ;
1013
}
1014
Ok ( ( ) )
1015
}
1016
1017
1018
pub fn remote_for_url ( & self , base : & str ) -> Option< String> {
1019
let base = base.trim_end_matches ( ' /' ) ;
4213c41d Andy McCall 1 month ago ↶
1020
self .config
1021
.remotes
1022
.iter ( )
1023
.find ( | ( _, url ) | url.trim_end_matches ( ' /' ) = = base )
1024
.map ( | ( name , _) | name.clone ( ) )
d82e4409 Andy McCall 1 month ago ↶
1025
}
1026
1027
ff1f62fe Andy McCall 1 month ago ↶
1028
d82e4409 Andy McCall 1 month ago ↶
1029
pub fn open_remote_at ( & self , url : & str ) -> Result< ( String, Box< dyn crate:: protocol:: Remote> ) > {
1030
if let Some ( name) = self .remote_for_url ( url) {
1031
return Ok ( ( name.clone ( ) , self .open_named_remote ( & name) ? ) ) ;
1032
}
1033
let options = RemoteOptions {
1034
repo_id: self .id ( ) .ok ( ) ,
ff1f62fe Andy McCall 1 month ago ↶
1035
.. self .credentials_for ( url)
d82e4409 Andy McCall 1 month ago ↶
1036
} ;
1037
Ok ( ( url.to_string ( ) , open_remote ( url, & options) ? ) )
1038
}
1039
1040
1041
1042
pub fn options_for_url ( & self , url : & str ) -> RemoteOptions {
1043
match self .remote_for_url ( url) {
1044
Some ( name) => self .remote_options ( & name) ,
1045
None => RemoteOptions {
1046
repo_id: self .id ( ) .ok ( ) ,
ff1f62fe Andy McCall 1 month ago ↶
1047
.. self .credentials_for ( url)
d82e4409 Andy McCall 1 month ago ↶
1048
} ,
1049
}
1050
}
1051
98286406 Andy McCall 1 month ago ↶
1052
fn open_named_remote ( & self , name : & str ) -> Result< Box< dyn crate:: protocol:: Remote> > {
2b5ee371 Andy McCall 1 month ago ↶
1053
open_remote ( & self .remote_url ( name) ? , & self .remote_options ( name) )
98286406 Andy McCall 1 month ago ↶
1054
}
1055
1056
fn serial_path ( & self , remote : & str , bookmark : & str ) -> PathBuf {
75167672 Andy McCall 1 month ago
1057
self .oxy
1058
.join ( " remotes" )
1059
.join ( remote)
1060
.join ( format! ( " {bookmark} .serial" ) )
98286406 Andy McCall 1 month ago ↶
1061
}
1062
1063
fn last_seen_serial ( & self , remote : & str , bookmark : & str ) -> Option< u64 > {
75167672 Andy McCall 1 month ago
1064
fs:: read_to_string( self .serial_path ( remote, bookmark) )
1065
.ok ( )
1066
.and_then ( | s | s.trim ( ) .parse ( ) .ok ( ) )
98286406 Andy McCall 1 month ago ↶
1067
}
1068
1069
fn record_serial ( & self , remote : & str , bookmark : & str , serial : u64 ) -> Result< ( ) > {
1070
let p = self .serial_path ( remote, bookmark) ;
1071
fs:: create_dir_all( p.parent ( ) .unwrap ( ) ) ? ;
1072
write_atomic ( & p, serial.to_string ( ) .as_bytes ( ) )
1073
}
1074
83e0677b Andy McCall 1 month ago ↶
1075
fn policy_serial_path ( & self , remote : & str ) -> PathBuf {
1076
self .oxy.join ( " remotes" ) .join ( remote) .join ( " policy.serial" )
1077
}
1078
1079
1080
fn remote_policy_serial ( & self , remote : & str ) -> Option< u64 > {
75167672 Andy McCall 1 month ago
1081
fs:: read_to_string( self .policy_serial_path ( remote) )
1082
.ok ( )
1083
.and_then ( | s | s.trim ( ) .parse ( ) .ok ( ) )
83e0677b Andy McCall 1 month ago ↶
1084
}
1085
1086
fn record_policy_serial ( & self , remote : & str , serial : u64 ) -> Result< ( ) > {
1087
let p = self .policy_serial_path ( remote) ;
1088
fs:: create_dir_all( p.parent ( ) .unwrap ( ) ) ? ;
1089
write_atomic ( & p, serial.to_string ( ) .as_bytes ( ) )
1090
}
1091
b3c5a59a Andy McCall 1 month ago ↶
1092
fn lazy_dir ( & self ) -> PathBuf {
1093
self .oxy.join ( " lazy" )
1094
}
1095
1096
1097
fn register_lazy ( & self , blob : & ObjectRef, remote : & str ) -> Result< ( ) > {
1098
fs:: create_dir_all( self .lazy_dir ( ) ) ? ;
1099
write_atomic ( & self .lazy_dir ( ) .join ( blob.to_hex ( ) ) , remote.as_bytes ( ) )
1100
}
1101
1102
1103
pub fn lazy_remote ( & self , blob : & ObjectRef) -> Option< String> {
75167672 Andy McCall 1 month ago
1104
fs:: read_to_string( self .lazy_dir ( ) .join ( blob.to_hex ( ) ) )
1105
.ok ( )
1106
.map ( | s | s.trim ( ) .to_string ( ) )
b3c5a59a Andy McCall 1 month ago ↶
1107
}
1108
1109
1110
pub ( crate ) fn fetch_lazy_blob ( & self , blob : & ObjectRef) -> Result< ( ) > {
1111
let Some ( remote_name) = self .lazy_remote ( blob) else {
1112
return Err ( RepoError:: Store( StoreError:: NotFound( * blob) ) ) ;
1113
} ;
1114
let remote = self .open_named_remote ( & remote_name) ? ;
1115
let objects = remote.objects ( & [ * blob] ) ? ;
1116
for ( ty, body) in objects {
1117
if matches! ( ty, ObjectType:: Blob | ObjectType:: Chunk) {
1118
self .store.put ( ty, & body) ? ;
1119
}
1120
}
1121
if ! self .store.has ( blob) {
75167672 Andy McCall 1 month ago
1122
return fail ( format! (
1123
" {remote_name} did not send blob {} " ,
1124
blob.short ( 12 )
1125
) ) ;
b3c5a59a Andy McCall 1 month ago ↶
1126
}
1127
let _ = fs:: remove_file( self .lazy_dir ( ) .join ( blob.to_hex ( ) ) ) ;
1128
Ok ( ( ) )
1129
}
1130
1131
1132
1133
fn register_missing_blobs ( & self , changes : & [ChangeHash], remote : & str ) -> Result< usize > {
1134
let mut n = 0 ;
1135
for h in changes {
1136
let c = self .store.get_change ( h) ? ;
1137
for e in & c.edits {
1138
if let Edit:: Replace { old, new, .. } = e {
1139
for b in [ old, new] .into_iter ( ) .flatten ( ) {
1140
if ! self .store.has ( b) {
1141
self .register_lazy ( b, remote) ? ;
1142
n + = 1 ;
1143
}
1144
}
1145
}
1146
}
1147
}
1148
Ok ( n)
1149
}
1150
98286406 Andy McCall 1 month ago ↶
1151
d82e4409 Andy McCall 1 month ago ↶
1152
pub fn known_heads ( & self , view : & View) -> Result< Vec< ChangeHash> > {
98286406 Andy McCall 1 month ago ↶
1153
let mut have: BTreeSet< ChangeHash> = BTreeSet:: new( ) ;
75167672 Andy McCall 1 month ago
1154
for st in view
1155
.bookmarks
1156
.values ( )
1157
.chain ( view.remotes.values ( ) .flat_map ( | b | b.values ( ) ) )
1158
{
98286406 Andy McCall 1 month ago ↶
1159
if let Ok ( s) = self .store.get_state ( st) {
1160
have.extend ( s.heads) ;
1161
}
1162
}
1163
have.extend ( view.workspaces.values ( ) .copied ( ) ) ;
1164
Ok ( have.into_iter ( ) .collect ( ) )
1165
}
1166
1167
83e0677b Andy McCall 1 month ago ↶
1168
pub fn fetch ( & self , remote_name : & str ) -> Result< FetchReport> {
98286406 Andy McCall 1 month ago ↶
1169
let remote = self .open_named_remote ( remote_name) ? ;
75167672 Andy McCall 1 month ago
1170
self .check_remote_id ( remote.as_ref ( ) , remote_name, false ) ? ;
83e0677b Andy McCall 1 month ago ↶
1171
let resp = remote.refs ( ) ? ;
1172
1173
1174
1175
1176
1177
let local_policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp.policy ) ;
1178
let seen = self .remote_policy_serial ( remote_name) ;
1179
let mut unverified = false ;
1180
match & resp.policy {
1181
Some ( sp) => {
1182
if seen.is_some_and ( | s | sp.policy.serial < s ) {
1183
return fail ( format! ( " {remote_name} presents policy serial {} but presented {} before: refusing the downgrade" , sp.policy.serial, seen.unwrap ( ) ) ) ;
1184
}
1185
match & local_policy {
1186
Some ( l) if sp.policy.serial < l.serial => { }
1187
Some ( l) if sp.policy.serial = = l.serial => {
1188
if sp.policy.hash ( ) ! = l.hash ( ) {
75167672 Andy McCall 1 month ago
1189
return fail ( format! (
1190
" {remote_name} presents a different policy with the same serial {} " ,
1191
l.serial
1192
) ) ;
83e0677b Andy McCall 1 month ago ↶
1193
}
1194
}
28fc23d7 Andy McCall 1 month ago ↶
1195
_ => {
75167672 Andy McCall 1 month ago
1196
self .adopt_policy_chain (
1197
& resp.history,
1198
sp,
1199
& format! ( " oxy fetch {remote_name} " ) ,
1200
)
1201
.map_err ( | e | RepoError:: Msg( format! ( " policy from {remote_name} : {e} " ) ) ) ? ;
83e0677b Andy McCall 1 month ago ↶
1202
}
1203
}
1204
self .record_policy_serial ( remote_name, sp.policy.serial) ? ;
1205
}
1206
None => {
1207
if seen.is_some ( ) {
1208
return fail ( format! ( " {remote_name} presented a policy before but has none now: refusing the downgrade" ) ) ;
1209
}
1210
unverified = true ;
1211
}
1212
}
75167672 Andy McCall 1 month ago
1213
let policy = if unverified {
1214
None
1215
} else {
1216
self .current_policy ( ) ? .map ( | ( _, sp ) | sp.policy )
1217
} ;
98286406 Andy McCall 1 month ago ↶
1218
let ( op_ref, op) = self .head_op ( ) ? ;
1219
let mut view = op.view.clone ( ) ;
1220
let have = self .known_heads ( & view) ? ;
1221
let mut report = Vec :: new( ) ;
b3c5a59a Andy McCall 1 month ago ↶
1222
let max_blob = self .config.max_blob;
83e0677b Andy McCall 1 month ago ↶
1223
for r in resp.refs {
091fef80 Andy McCall 1 month ago ↶
1224
let needed = {
1225
let _t = crate :: timing:: phase( " fetch: negotiate" ) ;
1226
remote.negotiate ( & have, & r.heads) ?
1227
} ;
1228
let bundle = {
1229
let _t = crate :: timing:: phase( " fetch: bundle" ) ;
1230
remote.fetch ( & needed, max_blob) ?
1231
} ;
83e0677b Andy McCall 1 month ago ↶
1232
1233
1234
091fef80 Andy McCall 1 month ago ↶
1235
let imported = {
1236
let _t = crate :: timing:: phase( " fetch: import" ) ;
1237
self .import_bundle ( & bundle, false ) ?
1238
} ;
b3c5a59a Andy McCall 1 month ago ↶
1239
if max_blob.is_some ( ) {
1240
self .register_missing_blobs ( & imported, remote_name) ? ;
1241
}
98286406 Andy McCall 1 month ago ↶
1242
for h in & r.heads {
1243
if ! self .store.has ( h) {
1244
return fail ( format! ( " remote did not send head {} " , h.short ( 12 ) ) ) ;
1245
}
1246
}
1247
let state = self .store.put_state ( & StateObject:: new( r.heads.clone ( ) ) ) ? ;
1248
if state ! = r.state {
75167672 Andy McCall 1 month ago
1249
return fail ( format! (
1250
" remote state {} does not match its heads" ,
1251
r.state.short ( 12 )
1252
) ) ;
98286406 Andy McCall 1 month ago ↶
1253
}
83e0677b Andy McCall 1 month ago ↶
1254
if let Some ( p) = & policy {
1255
let Some ( sa) = & r.attestation else {
75167672 Andy McCall 1 month ago
1256
return fail ( format! (
1257
" {remote_name} /{} has no attestation, which its policy requires" ,
1258
r.name
1259
) ) ;
83e0677b Andy McCall 1 month ago ↶
1260
} ;
75167672 Andy McCall 1 month ago
1261
let signer = sa
1262
.sigs
1263
.first ( )
1264
.map ( | s | s.key )
1265
.ok_or_else ( | | RepoError:: Msg( " attestation carries no signature" .into ( ) ) ) ? ;
7ba155f2 Andy McCall 1 month ago ↶
1266
if ! p.authorizes ( & signer, & r.name)
1267
& & ! self .delegate_authorized ( p, & signer, & r.name, sa.attest.hlc.wall_ms)
3526d339 Andy McCall 1 month ago ↶
1268
{
75167672 Andy McCall 1 month ago
1269
return fail ( format! (
3e5772cc Andy McCall 1 month ago ↶
1270
" {remote_name} /{} : attested by {} , which the policy does not authorize for this bookmark; a policy owner can grant it with `oxy policy grant`" ,
75167672 Andy McCall 1 month ago
1271
r.name,
1272
signer.short ( 12 )
1273
) ) ;
83e0677b Andy McCall 1 month ago ↶
1274
}
1275
}
98286406 Andy McCall 1 month ago ↶
1276
if let Some ( sa) = & r.attestation {
75167672 Andy McCall 1 month ago
1277
let AttestBody:: Ref {
1278
old_state,
1279
new_state,
1280
..
1281
} = & sa.attest.body
1282
else {
98286406 Andy McCall 1 month ago ↶
1283
return fail ( " remote sent a non-ref attestation for a bookmark" ) ;
1284
} ;
1285
let ( old_state, new_state) = ( * old_state, * new_state) ;
1286
let serial = self
75167672 Andy McCall 1 month ago
1287
.check_ref_attest (
1288
sa,
1289
& r.name,
1290
& old_state,
1291
& new_state,
1292
self .last_seen_serial ( remote_name, & r.name) ,
1293
true ,
1294
)
1295
.map_err ( | e | {
1296
RepoError:: Msg( format! ( " attestation for {} /{} : {e} " , remote_name, r.name) )
1297
} ) ? ;
98286406 Andy McCall 1 month ago ↶
1298
if new_state ! = r.state {
75167672 Andy McCall 1 month ago
1299
return fail ( format! (
1300
" attestation for {} /{} does not cover its current state" ,
1301
remote_name, r.name
1302
) ) ;
98286406 Andy McCall 1 month ago ↶
1303
}
1304
let ar = self .store.put_attest ( & sa.attest) ? ;
1305
for sig in & sa.sigs {
1306
self .store.put_signature ( & ar, sig) ? ;
1307
}
1308
self .record_serial ( remote_name, & r.name, serial) ? ;
1309
}
75167672 Andy McCall 1 month ago
1310
view.remotes
1311
.entry ( remote_name.to_string ( ) )
1312
.or_default ( )
1313
.insert ( r.name.clone ( ) , state) ;
98286406 Andy McCall 1 month ago ↶
1314
report.push ( ( r.name, r.heads, imported.len ( ) ) ) ;
1315
}
132b884b Andy McCall 1 month ago ↶
1316
let mut new_tags = 0 ;
1317
for t in & resp.tags {
1318
if view.tags.contains_key ( & t.name) {
1319
continue ;
1320
}
1321
75167672 Andy McCall 1 month ago
1322
let missing: Vec< ChangeHash> = t
1323
.heads
1324
.iter ( )
1325
.filter ( | h | ! self .store.has ( h) )
1326
.copied ( )
1327
.collect ( ) ;
132b884b Andy McCall 1 month ago ↶
1328
if ! missing.is_empty ( ) {
1329
let needed = remote.negotiate ( & self .known_heads ( & view) ? , & missing) ? ;
1330
let bundle = remote.fetch ( & needed, max_blob) ? ;
1331
self .import_bundle ( & bundle, false ) ? ;
1332
}
1333
self .store.put_state ( & StateObject:: new( t.heads.clone ( ) ) ) ? ;
75167672 Andy McCall 1 month ago
1334
self .adopt_tag ( & mut view, t, policy.as_ref ( ) )
1335
.map_err ( | e | RepoError:: Msg( format! ( " {remote_name} {} : {e} " , t.name) ) ) ? ;
132b884b Andy McCall 1 month ago ↶
1336
new_tags + = 1 ;
1337
}
98286406 Andy McCall 1 month ago ↶
1338
if view ! = op.view {
1339
self .record_op ( & [ op_ref] , & format! ( " oxy fetch {remote_name} " ) , view) ? ;
1340
}
6d3eefbc Andy McCall 1 month ago ↶
1341
let _ = self .run_hook (
1342
" post-fetch" ,
1343
& format! (
1344
" {{ \" event\" :\" post-fetch\" ,\" remote\" :{} ,\" bookmarks\" :{} }} " ,
1345
json_str ( remote_name) ,
75167672 Andy McCall 1 month ago
1346
json_array ( report.iter ( ) .map ( | ( n , heads , _) | format! (
1347
" {{ \" name\" :{} ,\" heads\" :{} }} " ,
1348
json_str ( n) ,
1349
json_array ( heads.iter ( ) .map ( | h | json_str ( & h.to_hex ( ) ) ) )
1350
) ) )
6d3eefbc Andy McCall 1 month ago ↶
1351
) ,
1352
) ;
75167672 Andy McCall 1 month ago
1353
Ok ( FetchReport {
1354
refs: report,
1355
unverified,
1356
new_tags,
1357
} )
98286406 Andy McCall 1 month ago ↶
1358
}
1359
2863456a Andy McCall 1 month ago ↶
1360
1361
1362
pub fn fetch_shallow ( & self , remote_name : & str , depth : u32 ) -> Result< usize > {
1363
let remote = self .open_named_remote ( remote_name) ? ;
75167672 Andy McCall 1 month ago
1364
self .check_remote_id ( remote.as_ref ( ) , remote_name, false ) ? ;
2863456a Andy McCall 1 month ago ↶
1365
let resp = remote.refs ( ) ? ;
1366
let bookmark = self .config.bookmark.clone ( ) ;
1367
if let Some ( sp) = & resp.policy {
75167672 Andy McCall 1 month ago
1368
self .adopt_policy_chain ( & resp.history, sp, " oxy clone --depth" )
1369
.map_err ( | e | RepoError:: Msg( format! ( " policy from {remote_name} : {e} " ) ) ) ? ;
2863456a Andy McCall 1 month ago ↶
1370
self .record_policy_serial ( remote_name, sp.policy.serial) ? ;
1371
}
1372
let Some ( r) = resp.refs.into_iter ( ) .find ( | r | r.name = = bookmark ) else {
1373
return fail ( format! ( " {remote_name} has no bookmark {bookmark:?} " ) ) ;
1374
} ;
1375
if r.heads.is_empty ( ) {
1376
return Ok ( 0 ) ;
1377
}
1378
1379
let cp_bundle = remote.checkpoint ( & r.heads) ? ;
1380
let mut cp_ref = None ;
1381
for ( ty, body) in & cp_bundle.objects {
1382
let put = self .store.put ( * ty, body) ? ;
1383
if * ty = = ObjectType:: Checkpoint {
1384
cp_ref = Some ( put) ;
1385
}
1386
}
75167672 Andy McCall 1 month ago
1387
let Some ( cp_ref) = cp_ref else {
1388
return fail ( format! ( " {remote_name} sent no checkpoint" ) ) ;
1389
} ;
2863456a Andy McCall 1 month ago ↶
1390
self .install_checkpoint ( & bookmark, & cp_ref) ? ;
1391
1392
let wanted = remote.shallow ( & r.heads, depth) ? ;
1393
let bundle = remote.fetch ( & wanted, self .config.max_blob) ? ;
1394
let imported = self .import_bundle_shallow ( & bundle, false ) ? ;
1395
if self .config.max_blob.is_some ( ) {
1396
self .register_missing_blobs ( & imported, remote_name) ? ;
1397
}
1398
self .refresh_boundary ( & imported) ? ;
1399
if let Some ( sa) = & r.attestation {
1400
let ar = self .store.put_attest ( & sa.attest) ? ;
1401
for sig in & sa.sigs {
1402
self .store.put_signature ( & ar, sig) ? ;
1403
}
1404
if let AttestBody:: Ref { serial, .. } = sa.attest.body {
1405
self .record_serial ( remote_name, & r.name, serial) ? ;
1406
}
1407
}
1408
let state = self .store.put_state ( & StateObject:: new( r.heads.clone ( ) ) ) ? ;
1409
let ( op_ref, op) = self .head_op ( ) ? ;
1410
let mut view = op.view.clone ( ) ;
75167672 Andy McCall 1 month ago
1411
view.remotes
1412
.entry ( remote_name.to_string ( ) )
1413
.or_default ( )
1414
.insert ( r.name.clone ( ) , state) ;
1415
self .record_op (
1416
& [ op_ref] ,
1417
& format! ( " oxy fetch {remote_name} --depth {depth} " ) ,
1418
view,
1419
) ? ;
2863456a Andy McCall 1 month ago ↶
1420
Ok ( imported.len ( ) )
1421
}
1422
98286406 Andy McCall 1 month ago ↶
1423
1424
1425
pub fn pull ( & self , remote_name : & str ) -> Result< PullReport> {
6caacfff Andy McCall 1 month ago ↶
1426
let _wc = self .lock_working_copy ( ) ? ;
98286406 Andy McCall 1 month ago ↶
1427
self .fetch ( remote_name) ? ;
1428
let ( op_ref, op) = self .head_op ( ) ? ;
14edbaaf Andy McCall 1 month ago ↶
1429
let current = self .config.bookmark.clone ( ) ;
1430
let mut local_current = self .heads_of ( & op.view, & current) ? ;
1431
local_current.sort ( ) ;
1432
let mut view = op.view.clone ( ) ;
1433
let mut current_heads = local_current.clone ( ) ;
1434
let mut changed = false ;
1435
let mut others = Vec :: new( ) ;
1436
let tracked = op
75167672 Andy McCall 1 month ago
1437
.view
1438
.remotes
1439
.get ( remote_name)
14edbaaf Andy McCall 1 month ago ↶
1440
.cloned ( )
1441
.unwrap_or_default ( ) ;
1442
for ( name, remote_state) in tracked {
1443
1444
1445
let local_state = op.view.bookmarks.get ( & name) .copied ( ) ;
1446
if local_state.is_none ( ) & & name ! = current {
1447
continue ;
1448
}
1449
if local_state = = Some ( remote_state) {
1450
continue ;
1451
}
1452
let local = self .heads_of ( & op.view, & name) ? ;
1453
let remote_heads = self .store.get_state ( & remote_state) ? .heads;
1454
if remote_heads.iter ( ) .all ( | h | local.contains ( h) ) {
1455
continue ;
1456
}
1457
let mut all = local.clone ( ) ;
1458
all.extend ( remote_heads) ;
1459
let st = self .load_state_history ( & all) ? ;
1460
let mut heads = st.maximal ( & all) ;
1461
heads.sort ( ) ;
1462
let mut sorted_local = local;
1463
sorted_local.sort ( ) ;
1464
if heads = = sorted_local {
1465
continue ;
1466
}
1467
view.bookmarks.insert (
1468
name.clone ( ) ,
1469
self .store.put_state ( & StateObject:: new( heads.clone ( ) ) ) ? ,
1470
) ;
1471
if name = = current {
1472
changed = true ;
1473
current_heads = heads;
1474
} else {
1475
others.push ( name) ;
1476
}
1477
}
f6dccc45 Andy McCall 27 days ago ↶
1478
if changed | | ! others.is_empty ( ) {
1479
self .record_op ( & [ op_ref] , & format! ( " oxy pull {remote_name} " ) , view) ? ;
98286406 Andy McCall 1 month ago ↶
1480
}
f6dccc45 Andy McCall 27 days ago ↶
1481
1482
1483
1484
let checked_out = self .follow_bookmark ( & current_heads, & current, " oxy pull" ) ? ;
75167672 Andy McCall 1 month ago
1485
Ok ( PullReport {
f6dccc45 Andy McCall 27 days ago ↶
1486
heads: if changed {
1487
current_heads
1488
} else {
1489
local_current
1490
} ,
14edbaaf Andy McCall 1 month ago ↶
1491
changed,
75167672 Andy McCall 1 month ago
1492
checked_out,
14edbaaf Andy McCall 1 month ago ↶
1493
others,
75167672 Andy McCall 1 month ago
1494
} )
f6dccc45 Andy McCall 27 days ago ↶
1495
}
1496
1497
1498
1499
1500
1501
1502
pub fn working_copy_behind ( & self ) -> Result< Option< usize > > {
1503
let ( _ , wc) = self .working_copy ( ) ? ;
1504
let mut base = wc.deps.clone ( ) ;
1505
base.sort ( ) ;
1506
let mut heads = self .bookmark_heads ( ) ? ;
1507
heads.sort ( ) ;
1508
if base = = heads | | heads.is_empty ( ) {
1509
return Ok ( None ) ;
1510
}
1511
let state = self .load_state_history ( & heads) ? ;
1512
if ! base.iter ( ) .all ( | h | state.contains ( h) ) {
1513
return Ok ( None ) ;
1514
}
1515
let had = if base.is_empty ( ) {
1516
0
1517
} else {
1518
self .load_state_history ( & base) ? .len ( )
1519
} ;
1520
Ok ( Some ( state.len ( ) .saturating_sub ( had) ) .filter ( | n | * n > 0 ) )
1521
}
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
pub fn follow_bookmark (
1533
& self ,
1534
heads : & [ChangeHash],
1535
bookmark : & str ,
1536
command : & str ,
1537
) -> Result< bool > {
1538
let snap = self .snapshot ( command) ? ;
1539
let clean = snap.wc.tree_ops.is_empty ( ) & & snap.wc.edits.is_empty ( ) ;
1540
let mut base = snap.base.maximal ( & snap.wc.deps) ;
1541
base.sort ( ) ;
1542
let mut target = heads.to_vec ( ) ;
1543
target.sort ( ) ;
1544
if base = = target {
1545
return Ok ( false ) ;
1546
}
1547
let state = self .load_state_history ( heads) ? ;
1548
if ! base.iter ( ) .all ( | h | state.contains ( h) ) {
1549
1550
return Ok ( false ) ;
1551
}
1552
if clean {
1553
self .checkout ( heads, false , & format! ( " {command} (checkout)" ) ) ? ;
1554
Ok ( true )
1555
} else {
1556
Ok ( self
1557
.checkout_carry ( heads, bookmark, & format! ( " {command} (checkout --carry)" ) )
1558
.is_ok ( ) )
1559
}
98286406 Andy McCall 1 month ago ↶
1560
}
1561
1562
1563
f8c69576 Andy McCall 1 month ago ↶
1564
1565
1566
1567
pub fn push_delete ( & self , remote_name : & str , bookmark : & str ) -> Result< Vec< RefResult> > {
1568
let remote = self .open_named_remote ( remote_name) ? ;
1569
self .check_remote_id ( remote.as_ref ( ) , remote_name, true ) ? ;
1570
let resp = remote.refs ( ) ? ;
1571
let Some ( r) = resp.refs.iter ( ) .find ( | r | r.name = = bookmark ) else {
1572
return fail ( format! ( " {remote_name} has no bookmark {bookmark} " ) ) ;
1573
} ;
1574
if resp.policy.is_some ( ) & & self .secret_key ( ) ? .is_none ( ) {
1575
return fail ( format! ( " {remote_name} enforces a trust policy; deleting needs an identity (`oxy identity create`)" ) ) ;
1576
}
1577
let attestation = match self .secret_key ( ) ? {
1578
Some ( _ ) => Some ( self .sign_attest ( & Attest {
1579
repo: self .id ( ) ? ,
1580
hlc: Hlc:: now( ) ,
1581
body: AttestBody:: Ref {
1582
name: bookmark.to_string ( ) ,
1583
old_state: r.state,
1584
new_state: ObjectRef:: ZERO ,
1585
serial: r.last_serial.unwrap_or ( 0 ) + 1 ,
1586
} ,
1587
} ) ? ) ,
1588
None => None ,
1589
} ;
1590
let results = remote.push (
1591
Bundle:: default( ) ,
1592
vec! [ RefUpdate {
1593
name: bookmark.to_string ( ) ,
1594
old_state: r.state,
1595
new_state: ObjectRef:: ZERO ,
1596
force: true ,
1597
attestation,
1598
} ] ,
1599
None ,
1600
vec! [ ] ,
1601
) ? ;
1602
if results.iter ( ) .any ( | x | x.ok & & x.name = = bookmark ) {
1603
let ( op_ref, op) = self .head_op ( ) ? ;
1604
let mut view = op.view.clone ( ) ;
1605
if let Some ( m) = view.remotes.get_mut ( remote_name) {
1606
m.remove ( bookmark) ;
1607
}
1608
self .record_op (
1609
& [ op_ref] ,
1610
& format! ( " oxy push {remote_name} --delete {bookmark} " ) ,
1611
view,
1612
) ? ;
1613
}
1614
Ok ( results)
1615
}
1616
98286406 Andy McCall 1 month ago ↶
1617
pub fn push ( & self , remote_name : & str , force : bool ) -> Result< Vec< RefResult> > {
6d3eefbc Andy McCall 1 month ago ↶
1618
let bookmark = self .config.bookmark.clone ( ) ;
7b9809e6 Andy McCall 1 month ago ↶
1619
self .push_bookmark ( remote_name, & bookmark, force)
1620
}
1621
1622
75167672 Andy McCall 1 month ago
1623
pub fn push_bookmark (
1624
& self ,
1625
remote_name : & str ,
1626
bookmark : & str ,
1627
force : bool ,
1628
) -> Result< Vec< RefResult> > {
7b9809e6 Andy McCall 1 month ago ↶
1629
let ( op_ref, op) = self .head_op ( ) ? ;
1630
if ! op.view.bookmarks.contains_key ( bookmark) {
1631
return fail ( format! ( " no bookmark {bookmark} " ) ) ;
1632
}
1633
let bookmark = bookmark.to_string ( ) ;
6d3eefbc Andy McCall 1 month ago ↶
1634
let local_heads = self .heads_of ( & op.view, & bookmark) ? ;
1635
1636
self .run_hook (
1637
" pre-push" ,
75167672 Andy McCall 1 month ago
1638
& format! (
1639
" {{ \" event\" :\" pre-push\" ,\" remote\" :{} ,\" bookmark\" :{} ,\" heads\" :{} }} " ,
1640
json_str ( remote_name) ,
1641
json_str ( & bookmark) ,
1642
json_array ( local_heads.iter ( ) .map ( | h | json_str ( & h.to_hex ( ) ) ) )
1643
) ,
6d3eefbc Andy McCall 1 month ago ↶
1644
) ? ;
98286406 Andy McCall 1 month ago ↶
1645
let remote = self .open_named_remote ( remote_name) ? ;
75167672 Andy McCall 1 month ago
1646
self .check_remote_id ( remote.as_ref ( ) , remote_name, true ) ? ;
83e0677b Andy McCall 1 month ago ↶
1647
let resp = remote.refs ( ) ? ;
1648
let local_policy = self .current_policy ( ) ? .map ( | ( _, sp ) | sp ) ;
28fc23d7 Andy McCall 1 month ago ↶
1649
let resp_policy_serial = resp.policy.as_ref ( ) .map ( | p | p.policy.serial ) ;
83e0677b Andy McCall 1 month ago ↶
1650
let policy_to_send = match ( & local_policy, & resp.policy) {
1651
( Some ( l) , Some ( r) ) if l.policy.serial > r.policy.serial => Some ( l.clone ( ) ) ,
1652
( Some ( l) , None ) => Some ( l.clone ( ) ) ,
1653
( Some ( l) , Some ( r) ) if l.policy.serial < r.policy.serial => {
75167672 Andy McCall 1 month ago
1654
return fail ( format! (
1655
" {remote_name} has a newer policy (serial {} ); fetch first" ,
1656
r.policy.serial
1657
) ) ;
83e0677b Andy McCall 1 month ago ↶
1658
}
1659
_ => None ,
1660
} ;
1661
let remote_has_policy = resp.policy.is_some ( ) | | policy_to_send.is_some ( ) ;
1662
if remote_has_policy & & self .secret_key ( ) ? .is_none ( ) {
1663
return fail ( format! ( " {remote_name} enforces a trust policy; pushing needs an identity (`oxy identity create`)" ) ) ;
1664
}
132b884b Andy McCall 1 month ago ↶
1665
let remote_tags: BTreeSet< String> = resp.tags.iter ( ) .map ( | t | t.name.clone ( ) ) .collect ( ) ;
75167672 Andy McCall 1 month ago
1666
let tags_to_send: Vec< crate:: protocol:: TagRef> = self
1667
.tag_refs ( ) ?
1668
.into_iter ( )
1669
.filter ( | t | ! remote_tags.contains ( & t.name) )
1670
.collect ( ) ;
83e0677b Andy McCall 1 month ago ↶
1671
let refs = resp.refs;
98286406 Andy McCall 1 month ago ↶
1672
let empty = self .store.put_state ( & StateObject:: new( vec! [ ] ) ) ? ;
75167672 Andy McCall 1 month ago
1673
let ( remote_state, remote_heads, last_serial) =
1674
match refs.iter ( ) .find ( | r | r.name = = bookmark ) {
1675
Some ( r) => ( r.state, r.heads.clone ( ) , r.last_serial) ,
1676
None => ( empty, vec! [ ] , None ) ,
1677
} ;
1678
let new_state = self
1679
.store
1680
.put_state ( & StateObject:: new( local_heads.clone ( ) ) ) ? ;
83e0677b Andy McCall 1 month ago ↶
1681
1682
1683
1684
132b884b Andy McCall 1 month ago ↶
1685
if new_state = = remote_state & & policy_to_send.is_none ( ) & & tags_to_send.is_empty ( ) {
75167672 Andy McCall 1 month ago
1686
return Ok ( vec! [ RefResult {
1687
name: bookmark,
1688
ok: true ,
1689
reason: " already up to date" .into ( ) ,
1690
} ] ) ;
98286406 Andy McCall 1 month ago ↶
1691
}
1692
for h in & remote_heads {
1693
if ! self .store.has ( h) {
75167672 Andy McCall 1 month ago
1694
return fail ( format! (
3e5772cc Andy McCall 1 month ago ↶
1695
" the remote has change {} which you do not; pull first (`oxy pull {remote_name} `), then push again" ,
75167672 Andy McCall 1 month ago
1696
h.short ( 12 )
1697
) ) ;
98286406 Andy McCall 1 month ago ↶
1698
}
1699
}
1700
if ! force {
1701
let closure = self .closure_in_store ( & local_heads) ? ;
1702
if let Some ( h) = remote_heads.iter ( ) .find ( | h | ! closure.contains ( h) ) {
1703
return fail ( format! ( " not a fast-forward: remote head {} is not in your state; pull first or use --force" , h.short ( 12 ) ) ) ;
1704
}
1705
}
1706
let to_send = self .negotiate ( & remote_heads, & local_heads) ? ;
1707
let mut bundle = self .bundle_for ( & to_send) ? ;
1708
let key = self .secret_key ( ) ? .map ( | ( k , _) | k ) ;
1709
if let Some ( key) = key {
05e71ed6 Andy McCall 1 month ago ↶
1710
1711
98286406 Andy McCall 1 month ago ↶
1712
let mut unsigned = Vec :: new( ) ;
1713
for h in & to_send {
1714
let c = self .store.get_change ( h) ? ;
05e71ed6 Andy McCall 1 month ago ↶
1715
if ( c.author = = key | | c.author.is_zero ( ) )
75167672 Andy McCall 1 month ago
1716
& & ! matches! ( self .authenticate_change ( h) ? , SignatureStatus:: Valid { .. } )
1717
{
98286406 Andy McCall 1 month ago ↶
1718
unsigned.push ( * h) ;
1719
}
1720
}
1721
for chunk in unsigned.chunks ( crate :: attest:: MAX_SEAL_HASHES ) {
1722
let sa = self .sign_attest ( & Attest:: seal( self .id ( ) ? , chunk.to_vec ( ) ) ) ? ;
1723
bundle.attests.push ( sa) ;
1724
}
1725
bundle = self .bundle_for ( & to_send) ? ;
1726
}
75167672 Andy McCall 1 month ago
1727
bundle.objects.push ( (
1728
ObjectType:: State,
1729
StateObject:: new( local_heads.clone ( ) ) .encode ( ) ,
1730
) ) ;
98286406 Andy McCall 1 month ago ↶
1731
let attestation = match key {
1732
Some ( _ ) => Some ( self .sign_attest ( & Attest {
1733
repo: self .id ( ) ? ,
1734
hlc: Hlc:: now( ) ,
75167672 Andy McCall 1 month ago
1735
body: AttestBody:: Ref {
1736
name: bookmark.clone ( ) ,
1737
old_state: remote_state,
1738
new_state,
f8c69576 Andy McCall 1 month ago ↶
1739
serial: next_serial ( last_serial) ,
75167672 Andy McCall 1 month ago
1740
} ,
98286406 Andy McCall 1 month ago ↶
1741
} ) ? ) ,
1742
None => None ,
1743
} ;
1744
let serial = attestation.as_ref ( ) .and_then ( | a | match a.attest.body {
1745
AttestBody:: Ref { serial, .. } => Some ( serial) ,
1746
_ => None ,
1747
} ) ;
83e0677b Andy McCall 1 month ago ↶
1748
let sent_serial = policy_to_send.as_ref ( ) .map ( | p | p.policy.serial ) ;
132b884b Andy McCall 1 month ago ↶
1749
1750
for t in & tags_to_send {
1751
let extra = self .negotiate ( & remote_heads, & t.heads) ? ;
75167672 Andy McCall 1 month ago
1752
let extra: Vec< ChangeHash> =
1753
extra.into_iter ( ) .filter ( | h | ! to_send.contains ( h) ) .collect ( ) ;
132b884b Andy McCall 1 month ago ↶
1754
if ! extra.is_empty ( ) {
1755
let more = self .bundle_for ( & extra) ? ;
1756
bundle.changes.extend ( more.changes) ;
2c75672f Andy McCall 1 month ago ↶
1757
bundle.hashes.extend ( more.hashes) ;
132b884b Andy McCall 1 month ago ↶
1758
bundle.attests.extend ( more.attests) ;
1759
bundle.objects.extend ( more.objects) ;
1760
}
75167672 Andy McCall 1 month ago
1761
bundle.objects.push ( (
1762
ObjectType:: State,
1763
StateObject:: new( t.heads.clone ( ) ) .encode ( ) ,
1764
) ) ;
132b884b Andy McCall 1 month ago ↶
1765
}
28fc23d7 Andy McCall 1 month ago ↶
1766
if let Some ( l) = & policy_to_send {
1767
let remote_serial = resp_policy_serial;
75167672 Andy McCall 1 month ago
1768
bundle.policies = self
1769
.policy_history ( ) ?
1770
.into_iter ( )
1771
.filter ( | sp | {
1772
remote_serial.is_none_or ( | r | sp.policy.serial > r )
1773
& & sp.policy.serial < l.policy.serial
1774
} )
1775
.collect ( ) ;
28fc23d7 Andy McCall 1 month ago ↶
1776
}
75167672 Andy McCall 1 month ago
1777
let results = remote.push (
1778
bundle,
1779
vec! [ RefUpdate {
1780
name: bookmark.clone ( ) ,
1781
old_state: remote_state,
1782
new_state,
1783
force,
1784
attestation,
1785
} ] ,
1786
policy_to_send,
1787
tags_to_send,
1788
) ? ;
1789
if let ( Some ( serial) , true ) = (
1790
sent_serial,
1791
results.iter ( ) .any ( | r | r.ok & & r.name = = " policy" ) ,
1792
) {
83e0677b Andy McCall 1 month ago ↶
1793
self .record_policy_serial ( remote_name, serial) ? ;
1794
}
1795
if results.iter ( ) .any ( | r | r.ok & & r.name = = bookmark ) {
98286406 Andy McCall 1 month ago ↶
1796
let mut view = op.view.clone ( ) ;
75167672 Andy McCall 1 month ago
1797
view.remotes
1798
.entry ( remote_name.to_string ( ) )
1799
.or_default ( )
1800
.insert ( bookmark.clone ( ) , new_state) ;
98286406 Andy McCall 1 month ago ↶
1801
self .record_op ( & [ op_ref] , & format! ( " oxy push {remote_name} " ) , view) ? ;
1802
if let Some ( s) = serial {
1803
self .record_serial ( remote_name, & bookmark, s) ? ;
1804
}
1805
}
1806
Ok ( results)
1807
}
1808
1809
1810
1811
pub fn clone ( url : & str , dest : & Path) -> Result< Repo> {
7421309f Andy McCall 1 month ago ↶
1812
Self :: clone_with( url, dest, None , & [ ] )
b3c5a59a Andy McCall 1 month ago ↶
1813
}
1814
7421309f Andy McCall 1 month ago ↶
1815
1816
1817
75167672 Andy McCall 1 month ago
1818
pub fn clone_with (
1819
url : & str ,
1820
dest : & Path,
1821
max_blob : Option< u64 > ,
1822
sparse : & [String],
1823
) -> Result< Repo> {
2863456a Andy McCall 1 month ago ↶
1824
Self :: clone_full( url, dest, max_blob, sparse, None )
1825
}
1826
1827
1828
75167672 Andy McCall 1 month ago
1829
pub fn clone_shallow (
1830
url : & str ,
1831
dest : & Path,
1832
depth : u32 ,
1833
max_blob : Option< u64 > ,
1834
sparse : & [String],
1835
) -> Result< Repo> {
2863456a Andy McCall 1 month ago ↶
1836
Self :: clone_full( url, dest, max_blob, sparse, Some ( depth) )
1837
}
1838
75167672 Andy McCall 1 month ago
1839
fn clone_full (
1840
url : & str ,
1841
dest : & Path,
1842
max_blob : Option< u64 > ,
1843
sparse : & [String],
1844
depth : Option< u32 > ,
1845
) -> Result< Repo> {
ff1f62fe Andy McCall 1 month ago ↶
1846
let options = crate :: credentials:: Credentials:: user( ) .options_for ( url) ;
1847
Self :: clone_using( url, dest, max_blob, sparse, depth, & options)
a07f60a3 Andy McCall 1 month ago ↶
1848
}
1849
ff1f62fe Andy McCall 1 month ago ↶
1850
1851
1852
a07f60a3 Andy McCall 1 month ago ↶
1853
pub ( crate ) fn clone_as_subrepo ( & self , url : & str , dest : & Path) -> Result< Repo> {
1854
let options = self .credentials_for ( url) ;
ff1f62fe Andy McCall 1 month ago ↶
1855
Self :: clone_using( url, dest, None , & [ ] , None , & options)
a07f60a3 Andy McCall 1 month ago ↶
1856
}
1857
1858
1859
1860
fn remote_on_same_hub ( & self , url : & str ) -> Option< String> {
1861
let origin = | u : & str | -> Option< String> {
1862
let ( scheme, rest) = u.split_once ( " ://" ) ? ;
9f9e72b9 Andy McCall 1 month ago ↶
1863
if ! matches! ( scheme, " http" | " https" ) {
1864
return None ;
1865
}
a07f60a3 Andy McCall 1 month ago ↶
1866
let host = rest.split ( ' /' ) .next ( ) ? ;
1867
Some ( format! (
1868
" {} ://{} " ,
1869
scheme.to_ascii_lowercase ( ) ,
1870
host.to_ascii_lowercase ( )
1871
) )
1872
} ;
1873
let want = origin ( url) ? ;
1874
self .config
1875
.remotes
1876
.iter ( )
1877
.find ( | ( _, u ) | origin ( u) .as_deref ( ) = = Some ( want.as_str ( ) ) )
1878
.map ( | ( name , _) | name.clone ( ) )
1879
}
1880
1881
1882
ff1f62fe Andy McCall 1 month ago ↶
1883
1884
a07f60a3 Andy McCall 1 month ago ↶
1885
pub ( crate ) fn credentials_for ( & self , url : & str ) -> RemoteOptions {
1886
match self .remote_on_same_hub ( url) {
1887
Some ( name) => RemoteOptions {
1888
repo_id: None ,
9f9e72b9 Andy McCall 1 month ago ↶
1889
cache_dir: None ,
a07f60a3 Andy McCall 1 month ago ↶
1890
.. self .remote_options ( & name)
1891
} ,
ff1f62fe Andy McCall 1 month ago ↶
1892
None => {
1893
let mut options = RemoteOptions:: default( ) ;
1894
self .fill_credentials ( url, & mut options) ;
1895
options
1896
}
a07f60a3 Andy McCall 1 month ago ↶
1897
}
1898
}
1899
1900
fn clone_using (
1901
url : & str ,
1902
dest : & Path,
1903
max_blob : Option< u64 > ,
1904
sparse : & [String],
1905
depth : Option< u32 > ,
1906
options : & RemoteOptions,
1907
) -> Result< Repo> {
1908
let remote = open_remote ( url, options) ? ;
98286406 Andy McCall 1 month ago ↶
1909
let id = remote.repo_id ( ) ? ;
75167672 Andy McCall 1 month ago
1910
if id = = crate :: protocol:: UNBOUND_ID {
05e71ed6 Andy McCall 1 month ago ↶
1911
return fail ( format! (
1912
" {url} is empty and unbound: push a repository to it first"
1913
) ) ;
75167672 Andy McCall 1 month ago
1914
}
98286406 Andy McCall 1 month ago ↶
1915
let mut repo = Repo:: init_with_id( dest, id) ? ;
827281ee Andy McCall 1 month ago ↶
1916
repo.config
1917
.set ( " remote.origin.url" , & crate :: protocol:: stored_url( url) ) ? ;
b3c5a59a Andy McCall 1 month ago ↶
1918
if let Some ( m) = max_blob {
1919
repo.config.set ( " filter.max-blob" , & m.to_string ( ) ) ? ;
7421309f Andy McCall 1 month ago ↶
1920
} else if ! sparse.is_empty ( ) {
1921
repo.config.set ( " filter.max-blob" , " 0" ) ? ;
1922
}
1923
if ! sparse.is_empty ( ) {
1924
repo.config.set ( " filter.paths" , & sparse.join ( " ," ) ) ? ;
b3c5a59a Andy McCall 1 month ago ↶
1925
}
98286406 Andy McCall 1 month ago ↶
1926
repo.save_config ( ) ? ;
2863456a Andy McCall 1 month ago ↶
1927
if let Some ( depth) = depth {
1928
repo.fetch_shallow ( " origin" , depth) ? ;
1929
} else {
1930
repo.fetch ( " origin" ) ? ;
1931
}
98286406 Andy McCall 1 month ago ↶
1932
let ( op_ref, op) = repo.head_op ( ) ? ;
14edbaaf Andy McCall 1 month ago ↶
1933
1934
let theirs = op.view.remotes.get ( " origin" ) .cloned ( ) .unwrap_or_default ( ) ;
9f9e72b9 Andy McCall 1 month ago ↶
1935
1936
1937
let advertised = repo
1938
.open_named_remote ( " origin" )
1939
.ok ( )
1940
.and_then ( | r | r.refs ( ) .ok ( ) )
1941
.and_then ( | r | r.default_bookmark ) ;
14edbaaf Andy McCall 1 month ago ↶
1942
let bookmark = advertised
1943
.filter ( | b | theirs.contains_key ( b) )
1944
.or_else ( | | {
1945
theirs
1946
.contains_key ( & repo.config.bookmark)
1947
.then ( | | repo.config.bookmark.clone ( ) )
1948
} )
1949
.or_else ( | | {
1950
( theirs.len ( ) = = 1 )
1951
.then ( | | theirs.keys ( ) .next ( ) .cloned ( ) )
1952
.flatten ( )
1953
} )
1954
.unwrap_or_else ( | | repo.config.bookmark.clone ( ) ) ;
1955
let initial = repo.config.bookmark.clone ( ) ;
1956
if bookmark ! = repo.config.bookmark {
1957
fs:: create_dir_all( repo.workspace_bookmark_path ( ) .parent ( ) .unwrap ( ) ) ? ;
1958
write_atomic ( & repo.workspace_bookmark_path ( ) , bookmark.as_bytes ( ) ) ? ;
1959
repo.config.bookmark = bookmark.clone ( ) ;
1960
}
1961
if let Some ( state) = theirs.get ( & bookmark) .copied ( ) {
98286406 Andy McCall 1 month ago ↶
1962
let heads = repo.store.get_state ( & state) ? .heads;
1963
let mut view = op.view.clone ( ) ;
14edbaaf Andy McCall 1 month ago ↶
1964
1965
if initial ! = bookmark & & repo.heads_of ( & view, & initial) ? .is_empty ( ) {
1966
view.bookmarks.remove ( & initial) ;
1967
}
98286406 Andy McCall 1 month ago ↶
1968
view.bookmarks.insert ( bookmark, state) ;
1969
repo.record_op ( & [ op_ref] , & format! ( " oxy clone {url} " ) , view) ? ;
1970
repo.checkout ( & heads, false , " oxy clone (checkout)" ) ? ;
1971
}
1972
Ok ( repo)
f8c69576 Andy McCall 1 month ago ↶
1973
}
1974
}
1975
1976
1977
1978
1979
1980
fn next_serial ( last : Option< u64 > ) -> u64 {
1981
match last {
1982
Some ( l) => l + 1 ,
1983
None => Hlc:: now( ) .wall_ms.max ( 1 ) ,
98286406 Andy McCall 1 month ago ↶
1984
}
1985
}