98286406 Andy McCall 1 month ago ↶
1
2
3
83e0677b Andy McCall 1 month ago ↶
4
5
6
7
98286406 Andy McCall 1 month ago ↶
8
9
10
use crate :: attest:: { Attest, AttestBody} ;
11
use crate :: diff:: { ranges, split_lines, text_edits} ;
83e0677b Andy McCall 1 month ago ↶
12
use crate :: protocol:: { open_remote, Bundle, RefResult, RefUpdate, RefsResponse, RemoteOptions, RemoteRef, SignedAttest, SignedPolicy} ;
98286406 Andy McCall 1 month ago ↶
13
use crate :: hash:: { hash_object, ChangeHash, KeyId, ObjectRef, ObjectType} ;
14
use crate :: identity:: { verify as verify_signature, Domain, Identity, SecretKey, Signature} ;
15
use crate :: ids:: { ChangeId, NodeId, WorkspaceId} ;
16
use crate :: objects:: { Blob, Change, ChangeKind, Edit, Hlc, Mode, NodeKind, OpRef, StateObject, TreeOp} ;
17
use crate :: oplog:: { Format1Op, Op, View, SNAPSHOT_SUFFIX } ;
18
use crate :: state:: { FileContent, Snapshot, State} ;
19
use crate :: store:: { Store, StoreError} ;
20
use crate :: tree:: OpKey;
21
use rand:: RngCore;
22
use std:: collections:: { BTreeMap, BTreeSet, HashMap} ;
23
use std:: fs;
24
use std:: io:: Write;
25
use std:: path:: { Path, PathBuf} ;
26
27
pub const OXY_DIR : & str = " .oxy" ;
28
pub const FORMAT_LINE : & str = " oxy repository format 2\n " ;
29
const FORMAT1_LINE : & str = " oxy repository format 1\n " ;
30
const BINARY_SNIFF : usize = 8 * 1024 ;
d02e545d Andy McCall 1 month ago ↶
31
32
pub const AUTOPACK_THRESHOLD : usize = 4096 ;
98286406 Andy McCall 1 month ago ↶
33
34
# [ derive ( Debug, thiserror::Error ) ]
35
pub enum RepoError {
36
# [ error ( " io: {0}" ) ]
37
Io( # [ from ] std:: io:: Error) ,
38
# [ error ( transparent ) ]
39
Store( # [ from ] StoreError) ,
40
# [ error ( transparent ) ]
41
Object( # [ from ] crate :: objects:: ObjectError) ,
42
# [ error ( transparent ) ]
43
State( # [ from ] crate :: state:: StateError) ,
44
# [ error ( " {0}" ) ]
45
Msg( String ) ,
46
}
47
48
pub type Result < T> = std:: result:: Result< T, RepoError> ;
49
50
fn fail < T> ( s : impl Into< String> ) -> Result< T> {
51
Err ( RepoError:: Msg( s.into ( ) ) )
52
}
53
7b9809e6 Andy McCall 1 month ago ↶
54
mod bookmarks ;
98286406 Andy McCall 1 month ago ↶
55
mod checkout ;
252e8cb2 Andy McCall 1 month ago ↶
56
mod checkpoint ;
98286406 Andy McCall 1 month ago ↶
57
mod config ;
ba160f60 Andy McCall 1 month ago ↶
58
mod gc ;
79ab8528 Andy McCall 1 month ago ↶
59
mod git ;
614541c3 Andy McCall 1 month ago ↶
60
mod history ;
6d3eefbc Andy McCall 1 month ago ↶
61
mod hooks ;
98286406 Andy McCall 1 month ago ↶
62
mod identity ;
83e0677b Andy McCall 1 month ago ↶
63
mod policy ;
135212de Andy McCall 1 month ago ↶
64
mod prompt ;
98286406 Andy McCall 1 month ago ↶
65
mod snapshot ;
66
mod sync ;
132b884b Andy McCall 1 month ago ↶
67
mod tags ;
4e88e774 Andy McCall 1 month ago ↶
68
mod workspace ;
98286406 Andy McCall 1 month ago ↶
69
# [ cfg ( test ) ]
70
mod tests ;
71
7b9809e6 Andy McCall 1 month ago ↶
72
pub use bookmarks:: { BookmarkInfo, MergeReport} ;
98286406 Andy McCall 1 month ago ↶
73
pub use checkout:: CheckoutReport;
b3c5a59a Andy McCall 1 month ago ↶
74
pub use config:: { parse_size, read_config_file, Config} ;
ba160f60 Andy McCall 1 month ago ↶
75
pub use gc:: GcReport;
6d3eefbc Andy McCall 1 month ago ↶
76
pub use hooks:: { json_array, json_str, HookOutcome} ;
135212de Andy McCall 1 month ago ↶
77
pub use prompt:: { PromptInfo, DEFAULT_PROMPT_FORMAT } ;
c3069537 Andy McCall 1 month ago ↶
78
pub use git:: { GitExportReport, GitImportReport} ;
28fc23d7 Andy McCall 1 month ago ↶
79
pub use identity:: { Rotation, SignatureStatus} ;
80
pub use policy:: PolicyUpdate;
6e723b81 Andy McCall 1 month ago ↶
81
pub use snapshot:: { added_node_paths, has_conflict_markers, is_binary, referenced_changes, render_change, similarity, ChangeStatus, PathStatus, SnapshotResult} ;
83e0677b Andy McCall 1 month ago ↶
82
pub use sync:: { FetchReport, PullReport} ;
132b884b Andy McCall 1 month ago ↶
83
pub use tags:: TagInfo;
4e88e774 Andy McCall 1 month ago ↶
84
pub use workspace:: { SnapshotLock, WorkspaceInfo} ;
98286406 Andy McCall 1 month ago ↶
85
86
fn write_atomic ( path : & Path, data : & [u8 ]) -> Result< ( ) > {
87
let tmp = path.with_extension ( " tmp" ) ;
88
{
89
let mut f = fs:: File:: create( & tmp) ? ;
90
f.write_all ( data) ? ;
91
f.sync_all ( ) ? ;
92
}
93
fs:: rename( & tmp, path) ? ;
94
Ok ( ( ) )
95
}
96
97
pub struct LogEntry {
98
pub hash : ChangeHash,
2863456a Andy McCall 1 month ago ↶
99
100
pub change : crate:: state:: ChangeMeta,
98286406 Andy McCall 1 month ago ↶
101
pub is_working_copy : bool ,
102
pub is_head : bool ,
103
}
104
c3069537 Andy McCall 1 month ago ↶
105
106
pub ( crate ) struct FsEntry {
107
pub kind : NodeKind,
108
pub exec : bool ,
b7f102fc Andy McCall 1 month ago ↶
109
c3069537 Andy McCall 1 month ago ↶
110
pub content : Vec< u8 > ,
b7f102fc Andy McCall 1 month ago ↶
111
112
113
114
pub blob : Option< ObjectRef> ,
115
116
117
pub content_hash : Option< String> ,
98286406 Andy McCall 1 month ago ↶
118
}
119
b7f102fc Andy McCall 1 month ago ↶
120
impl FsEntry {
121
pub ( crate ) fn new ( kind : NodeKind, exec : bool , content : Vec< u8 > ) -> FsEntry {
122
FsEntry { kind, exec, content, blob: None , content_hash: None }
123
}
124
}
125
98286406 Andy McCall 1 month ago ↶
126
pub struct Repo {
127
root : PathBuf,
128
oxy : PathBuf,
129
store : Store,
130
workspace : WorkspaceId,
131
keys_dir : PathBuf,
132
user_config : PathBuf,
133
pub config : Config,
134
}
135
136
137
138
pub fn default_home ( ) -> PathBuf {
139
if let Ok ( h) = std:: env:: var( " OXY_HOME" ) {
140
return PathBuf:: from( h) ;
141
}
142
if let Ok ( x) = std:: env:: var( " XDG_CONFIG_HOME" ) {
143
return PathBuf:: from( x) .join ( " oxy" ) ;
144
}
145
let home = std:: env:: var( " HOME" ) .unwrap_or_else ( | _| " ." .into ( ) ) ;
146
PathBuf:: from( home) .join ( " .config" ) .join ( " oxy" )
147
}
148
149
pub fn default_keys_dir ( ) -> PathBuf {
150
default_home ( ) .join ( " keys" )
151
}
152
153
pub fn default_user_config ( ) -> PathBuf {
154
default_home ( ) .join ( " config.toml" )
155
}
156
157
158
# [ derive ( Clone, Copy, Debug, PartialEq, Eq ) ]
159
pub enum IdentityScope {
160
161
User,
162
163
Repo,
164
}
165
166
impl Repo {
167
168
169
pub fn init ( root : & Path) -> Result< Repo> {
170
let mut id = [ 0 u8 ; 16 ] ;
171
rand:: rngs:: OsRng.fill_bytes ( & mut id) ;
172
Self :: init_with_id( root, id)
173
}
174
175
176
177
pub fn init_with_id ( root : & Path, id : [u8 ; 16]) -> Result< Repo> {
178
fs:: create_dir_all( root) ? ;
179
let root = root.canonicalize ( ) .unwrap_or_else ( | _| root.to_path_buf ( ) ) ;
180
let oxy = root.join ( OXY_DIR ) ;
181
if oxy.exists ( ) {
182
return fail ( format! ( " {} is already an oxy repository" , root.display ( ) ) ) ;
183
}
184
fs:: create_dir_all( oxy.join ( " ops" ) ) ? ;
185
write_atomic ( & oxy.join ( " format" ) , FORMAT_LINE .as_bytes ( ) ) ? ;
186
write_atomic ( & oxy.join ( " id" ) , hex:: encode( id) .as_bytes ( ) ) ? ;
187
let config = Config:: load( Some ( & default_user_config ( ) ) , & oxy.join ( " config" ) ) ? ;
188
config.save ( & oxy.join ( " config" ) ) ? ;
189
let workspace = WorkspaceId:: random( ) ;
190
write_atomic ( & oxy.join ( " workspace" ) , workspace.to_hex ( ) .as_bytes ( ) ) ? ;
191
let store = Store:: open( oxy.join ( " objects" ) ) ? ;
192
let repo = Repo { root, oxy, store, workspace, keys_dir: default_keys_dir ( ) , user_config: default_user_config ( ) , config } ;
4e88e774 Andy McCall 1 month ago ↶
193
repo.register_workspace ( & workspace, & repo.root) ? ;
98286406 Andy McCall 1 month ago ↶
194
195
let wc = repo.new_working_copy ( vec! [ ] , " " ) ;
196
let wc_hash = repo.store.put_change ( & wc) ? ;
197
let mut view = View:: default( ) ;
198
view.bookmarks.insert ( repo.config.bookmark.clone ( ) , repo.store.put_state ( & StateObject:: new( vec! [ ] ) ) ? ) ;
199
view.workspaces.insert ( workspace, wc_hash) ;
200
repo.record_op ( & [ ] , " oxy init" , view) ? ;
201
Ok ( repo)
202
}
203
204
205
pub fn open ( start : & Path) -> Result< Repo> {
206
let start = start.canonicalize ( ) ? ;
207
let mut dir: Option< & Path> = Some ( & start) ;
208
while let Some ( d) = dir {
209
let oxy = d.join ( OXY_DIR ) ;
4e88e774 Andy McCall 1 month ago ↶
210
if oxy.is_file ( ) {
211
212
let text = fs:: read_to_string( & oxy) ? ;
213
let Some ( ( shared, workspace) ) = workspace:: parse_workspace_file( & text) else {
214
return fail ( format! ( " {} is not a valid workspace file" , oxy.display ( ) ) ) ;
215
} ;
216
let format = fs:: read_to_string( shared.join ( " format" ) ) ? ;
217
if format ! = FORMAT_LINE {
218
return fail ( format! ( " unsupported repository format: {} " , format.trim ( ) ) ) ;
219
}
220
let config = Config:: load( Some ( & default_user_config ( ) ) , & shared.join ( " config" ) ) ? ;
221
let store = Store:: open( shared.join ( " objects" ) ) ? ;
7b9809e6 Andy McCall 1 month ago ↶
222
let mut repo = Repo { root: d.to_path_buf ( ) , oxy: shared, store, workspace, keys_dir: default_keys_dir ( ) , user_config: default_user_config ( ) , config } ;
223
repo.apply_workspace_bookmark ( ) ;
224
return Ok ( repo) ;
4e88e774 Andy McCall 1 month ago ↶
225
}
98286406 Andy McCall 1 month ago ↶
226
if oxy.is_dir ( ) {
227
let format = fs:: read_to_string( oxy.join ( " format" ) ) ? ;
228
let config = Config:: load( Some ( & default_user_config ( ) ) , & oxy.join ( " config" ) ) ? ;
229
let workspace = WorkspaceId:: from_hex( fs:: read_to_string( oxy.join ( " workspace" ) ) ? .trim ( ) )
230
.ok_or_else ( | | RepoError:: Msg( " corrupt .oxy/workspace" .into ( ) ) ) ? ;
231
let store = Store:: open( oxy.join ( " objects" ) ) ? ;
7b9809e6 Andy McCall 1 month ago ↶
232
let mut repo = Repo { root: d.to_path_buf ( ) , oxy, store, workspace, keys_dir: default_keys_dir ( ) , user_config: default_user_config ( ) , config } ;
233
repo.apply_workspace_bookmark ( ) ;
98286406 Andy McCall 1 month ago ↶
234
if format = = FORMAT1_LINE {
235
repo.migrate_format1 ( ) ? ;
236
} else if format ! = FORMAT_LINE {
237
return fail ( format! ( " unsupported repository format: {} " , format.trim ( ) ) ) ;
238
}
239
return Ok ( repo) ;
240
}
241
dir = d.parent ( ) ;
242
}
243
fail ( format! ( " no oxy repository found at or above {} " , start.display ( ) ) )
244
}
245
246
247
248
fn migrate_format1 ( & self ) -> Result< ( ) > {
249
let hex = fs:: read_to_string( self .oxy.join ( " ops" ) .join ( " head" ) ) ? ;
250
let head = ObjectRef:: from_hex( hex.trim ( ) ) .ok_or_else ( | | RepoError:: Msg( " corrupt .oxy/ops/head" .into ( ) ) ) ? ;
251
252
let mut chain: Vec< Format1Op> = Vec :: new( ) ;
253
let mut cur = Some ( head) ;
254
while let Some ( r) = cur {
255
let op = Op:: decode_format1( & self .store.get_typed ( & r, ObjectType:: Op) ? ) ? ;
256
cur = op.parents.first ( ) .copied ( ) ;
257
chain.push ( op) ;
258
}
259
chain.reverse ( ) ;
260
let mut new_parent: Option< ObjectRef> = None ;
261
for old in chain {
262
let mut view = View:: default( ) ;
263
for ( name, heads) in & old.view.bookmarks {
264
view.bookmarks.insert ( name.clone ( ) , self .store.put_state ( & StateObject:: new( heads.clone ( ) ) ) ? ) ;
265
}
266
view.workspaces = old.view.workspaces.clone ( ) ;
267
let op = Op { parents: new_parent.into_iter ( ) .collect ( ) , hlc: old.hlc, command: old.command, view } ;
268
new_parent = Some ( self .store.put ( ObjectType:: Op, & op.encode ( ) ) ? ) ;
269
}
270
write_atomic ( & self .oxy.join ( " ops" ) .join ( " head" ) , new_parent.unwrap ( ) .to_hex ( ) .as_bytes ( ) ) ? ;
271
write_atomic ( & self .oxy.join ( " format" ) , FORMAT_LINE .as_bytes ( ) ) ? ;
272
Ok ( ( ) )
d02e545d Andy McCall 1 month ago ↶
273
}
274
275
276
pub ( crate ) fn maybe_autopack ( & self ) -> Result< ( ) > {
277
if self .store.loose_count ( ) ? > AUTOPACK_THRESHOLD {
278
self .store.pack_loose ( ) ? ;
135212de Andy McCall 1 month ago ↶
279
self .store.merge_packs ( ) ? ;
d02e545d Andy McCall 1 month ago ↶
280
}
281
Ok ( ( ) )
282
}
283
98286406 Andy McCall 1 month ago ↶
284
285
pub fn id ( & self ) -> Result< [ u8 ; 16 ] > {
286
let path = self .oxy.join ( " id" ) ;
287
match fs:: read_to_string( & path) {
288
Ok ( h) => hex:: decode( h.trim ( ) ) .ok ( ) .and_then ( | v | v.try_into ( ) .ok ( ) ) .ok_or_else ( | | RepoError:: Msg( " corrupt .oxy/id" .into ( ) ) ) ,
289
Err ( e) if e.kind ( ) = = std:: io:: ErrorKind:: NotFound => {
290
let mut id = [ 0 u8 ; 16 ] ;
291
rand:: rngs:: OsRng.fill_bytes ( & mut id) ;
292
write_atomic ( & path, hex:: encode( id) .as_bytes ( ) ) ? ;
293
Ok ( id)
294
}
295
Err ( e) => Err ( e.into ( ) ) ,
296
}
297
}
298
299
pub fn root ( & self ) -> & Path {
300
& self .root
301
}
302
pub fn store ( & self ) -> & Store {
303
& self .store
304
}
305
pub fn workspace ( & self ) -> WorkspaceId {
306
self .workspace
307
}
308
309
310
311
pub fn with_home ( mut self , dir : impl Into< PathBuf> ) -> Repo {
312
let dir = dir.into ( ) ;
313
self .keys_dir = dir.join ( " keys" ) ;
314
self .user_config = dir.join ( " config.toml" ) ;
315
if let Ok ( c) = Config:: load( Some ( & self .user_config) , & self .oxy.join ( " config" ) ) {
316
self .config = c;
317
}
7b9809e6 Andy McCall 1 month ago ↶
318
self .apply_workspace_bookmark ( ) ;
98286406 Andy McCall 1 month ago ↶
319
self
320
}
321
7b9809e6 Andy McCall 1 month ago ↶
322
323
324
fn apply_workspace_bookmark ( & mut self ) {
325
if let Ok ( b) = fs:: read_to_string( self .oxy.join ( " workspaces" ) .join ( self .workspace.to_hex ( ) ) .join ( " bookmark" ) ) {
326
let b = b.trim ( ) ;
327
if ! b.is_empty ( ) {
328
self .config.bookmark = b.to_string ( ) ;
329
}
330
}
331
}
332
98286406 Andy McCall 1 month ago ↶
333
pub fn user_config_path ( & self ) -> & Path {
334
& self .user_config
335
}
336
337
338
pub fn set_user_config ( & mut self , key : & str , value : & str ) -> Result< ( ) > {
339
Config:: set_user( & self .user_config, key, value) ? ;
340
self .config = Config:: load( Some ( & self .user_config) , & self .oxy.join ( " config" ) ) ? ;
341
Ok ( ( ) )
342
}
343
344
pub fn save_config ( & self ) -> Result< ( ) > {
345
self .config.save ( & self .oxy.join ( " config" ) )
346
}
347
348
fn author ( & self ) -> KeyId {
349
self .config.author_key.unwrap_or ( ObjectRef:: ZERO )
350
}
351
}
352
353
impl Repo {
354
fn new_working_copy ( & self , deps : Vec< ChangeHash> , message : & str ) -> Change {
355
let mut c = Change {
356
change_id: ChangeId:: random( ) ,
357
deps,
358
author: self .author ( ) ,
359
hlc: Hlc:: now( ) ,
360
message: message.into ( ) ,
361
tree_ops: vec! [ ] ,
362
edits: vec! [ ] ,
363
kind: ChangeKind:: WorkingCopy,
364
} ;
365
c.normalize ( ) ;
366
c
367
}
368
369
370
371
pub fn head_op ( & self ) -> Result< ( ObjectRef, Op) > {
372
let hex = fs:: read_to_string( self .oxy.join ( " ops" ) .join ( " head" ) ) ? ;
373
let r = ObjectRef:: from_hex( hex.trim ( ) ) .ok_or_else ( | | RepoError:: Msg( " corrupt .oxy/ops/head" .into ( ) ) ) ? ;
374
let op = Op:: decode( & self .store.get_typed ( & r, ObjectType:: Op) ? ) ? ;
375
Ok ( ( r, op) )
376
}
377
378
pub fn view ( & self ) -> Result< View> {
379
Ok ( self .head_op ( ) ? .1. view)
380
}
381
382
fn record_op ( & self , parents : & [ObjectRef], command : & str , view : View) -> Result< ObjectRef> {
383
let op = Op { parents: parents.to_vec ( ) , hlc: Hlc:: now( ) , command: command.into ( ) , view } ;
384
let r = self .store.put ( ObjectType:: Op, & op.encode ( ) ) ? ;
385
write_atomic ( & self .oxy.join ( " ops" ) .join ( " head" ) , r.to_hex ( ) .as_bytes ( ) ) ? ;
386
Ok ( r)
387
}
388
389
pub fn op_log ( & self ) -> Result< Vec< ( ObjectRef, Op) > > {
390
let mut out = Vec :: new( ) ;
391
let ( mut r, mut op) = self .head_op ( ) ? ;
392
loop {
393
let next = op.parents.first ( ) .copied ( ) ;
394
out.push ( ( r, op) ) ;
395
match next {
396
Some ( p) => {
397
r = p;
398
op = Op:: decode( & self .store.get_typed ( & r, ObjectType:: Op) ? ) ? ;
399
}
400
None => break ,
401
}
402
}
403
Ok ( out)
404
}
405
406
407
408
409
pub fn undo ( & self ) -> Result< Op> {
410
let ( _ , mut op) = self .head_op ( ) ? ;
411
while op.is_snapshot ( ) {
412
let Some ( parent) = op.parents.first ( ) else { break } ;
413
op = Op:: decode( & self .store.get_typed ( parent, ObjectType:: Op) ? ) ? ;
414
}
415
let Some ( parent) = op.parents.first ( ) else {
416
return fail ( " nothing to undo" ) ;
417
} ;
418
let parent_op = Op:: decode( & self .store.get_typed ( parent, ObjectType:: Op) ? ) ? ;
419
write_atomic ( & self .oxy.join ( " ops" ) .join ( " head" ) , parent.to_hex ( ) .as_bytes ( ) ) ? ;
135212de Andy McCall 1 month ago ↶
420
self .drop_manifest ( ) ;
98286406 Andy McCall 1 month ago ↶
421
Ok ( parent_op)
422
}
423
424
425
pub fn last_command ( & self ) -> Result< Op> {
426
let ( _ , mut op) = self .head_op ( ) ? ;
427
while op.is_snapshot ( ) {
428
let Some ( parent) = op.parents.first ( ) else { break } ;
429
op = Op:: decode( & self .store.get_typed ( parent, ObjectType:: Op) ? ) ? ;
430
}
431
Ok ( op)
432
}
433
434
435
252e8cb2 Andy McCall 1 month ago ↶
436
98286406 Andy McCall 1 month ago ↶
437
pub fn load_state ( & self , heads : & [ChangeHash]) -> Result< State> {
252e8cb2 Andy McCall 1 month ago ↶
438
if let Some ( st) = self .load_state_checkpointed ( heads) ? {
439
return Ok ( st) ;
440
}
441
self .load_state_full ( heads)
442
}
443
444
445
pub fn load_state_full ( & self , heads : & [ChangeHash]) -> Result< State> {
98286406 Andy McCall 1 month ago ↶
446
let mut state = State:: new( ) ;
447
let mut stack: Vec< ChangeHash> = heads.to_vec ( ) ;
448
let mut seen: BTreeSet< ChangeHash> = BTreeSet:: new( ) ;
449
while let Some ( h) = stack.pop ( ) {
450
if ! seen.insert ( h) {
451
continue ;
452
}
453
let c = self .store.get_change ( & h) ? ;
454
stack.extend ( c.deps.iter ( ) .copied ( ) ) ;
455
state.add_unchecked ( c) ;
456
}
457
state.validate ( ) ? ;
458
Ok ( state)
459
}
460
461
pub fn working_copy ( & self ) -> Result< ( ChangeHash, Change) > {
462
let view = self .view ( ) ? ;
463
let h = * view
464
.workspaces
465
.get ( & self .workspace)
466
.ok_or_else ( | | RepoError:: Msg( " this workspace is not in the current view" .into ( ) ) ) ? ;
467
Ok ( ( h, self .store.get_change ( & h) ? ) )
468
}
469
470
471
pub fn heads_of ( & self , view : & View, bookmark : & str ) -> Result< Vec< ChangeHash> > {
472
match view.bookmarks.get ( bookmark) {
473
Some ( st) => Ok ( self .store.get_state ( st) ? .heads) ,
474
None => Ok ( vec! [ ] ) ,
475
}
476
}
477
478
pub fn bookmark_heads ( & self ) -> Result< Vec< ChangeHash> > {
479
self .heads_of ( & self .view ( ) ? , & self .config.bookmark)
480
}
481
482
483
484
fn blob_for ( content : & [u8 ]) -> Blob {
485
Blob {
486
length: content.len ( ) as u64 ,
487
chunks: crate :: chunk:: chunks( content) .into_iter ( ) .map ( | c | hash_object ( ObjectType:: Chunk, c) ) .collect ( ) ,
488
}
489
}
490
491
pub fn blob_ref ( content : & [u8 ]) -> ObjectRef {
492
Self :: blob_for( content) .hash ( )
493
}
494
495
pub fn store_blob ( & self , content : & [u8 ]) -> Result< ObjectRef> {
496
for c in crate :: chunk:: chunks( content) {
497
self .store.put_chunk ( c) ? ;
498
}
499
Ok ( self .store.put ( ObjectType:: Blob, & Self :: blob_for( content) .encode ( ) ) ? )
500
}
501
b3c5a59a Andy McCall 1 month ago ↶
502
503
98286406 Andy McCall 1 month ago ↶
504
pub fn read_blob ( & self , r : & ObjectRef) -> Result< Vec< u8 > > {
b3c5a59a Andy McCall 1 month ago ↶
505
if ! self .store.has ( r) & & self .lazy_remote ( r) .is_some ( ) {
506
self .fetch_lazy_blob ( r) ? ;
507
}
98286406 Andy McCall 1 month ago ↶
508
let blob = Blob:: decode( & self .store.get_typed ( r, ObjectType:: Blob) ? ) ? ;
509
let mut out = Vec :: with_capacity( blob.length as usize ) ;
510
for c in & blob.chunks {
511
out.extend ( self .store.get_typed ( c, ObjectType:: Chunk) ? ) ;
512
}
513
Ok ( out)
514
}
515
516
517
cdb5beba Andy McCall 1 month ago ↶
518
519
pub fn disk_attributes ( & self ) -> crate:: attributes:: Attributes {
520
let mut files: Vec< ( String, Vec< u8 > ) > = Vec :: new( ) ;
521
if let Ok ( b) = fs:: read( self .root.join ( " .oxyattributes" ) ) {
522
files.push ( ( " .oxyattributes" .into ( ) , b) ) ;
523
}
524
let mut wb = ignore:: WalkBuilder:: new( & self .root) ;
525
wb.hidden ( false ) .git_ignore ( false ) .git_global ( false ) .git_exclude ( false ) .require_git ( false ) .follow_links ( false ) .add_custom_ignore_filename ( " .oxyignore" ) ;
526
wb.filter_entry ( | e | e.file_name ( ) ! = OXY_DIR ) ;
527
for entry in wb.build ( ) .flatten ( ) {
528
if entry.file_name ( ) = = " .oxyattributes" & & entry.path ( ) ! = self .root.join ( " .oxyattributes" ) {
529
if let Ok ( b) = fs:: read( entry.path ( ) ) {
530
let rel: String = entry.path ( ) .strip_prefix ( & self .root) .unwrap ( ) .components ( ) .map ( | c | c.as_os_str ( ) .to_string_lossy ( ) .to_string ( ) ) .collect:: < Vec< _ > > ( ) .join ( " /" ) ;
531
files.push ( ( rel, b) ) ;
532
}
533
}
534
}
535
crate :: attributes:: Attributes:: from_files( files.iter ( ) .map ( | ( p , c ) | ( p.as_str ( ) , c.as_slice ( ) ) ) )
536
}
537
538
539
pub fn state_attributes ( snapshot : & Snapshot) -> crate:: attributes:: Attributes {
540
crate :: attributes:: Attributes:: from_files( snapshot.files.iter ( ) .filter_map ( | ( p , f ) | match & f.content {
541
FileContent:: Text { bytes, .. } if p = = " .oxyattributes" | | p.ends_with ( " /.oxyattributes" ) => Some ( ( p.as_str ( ) , bytes.as_slice ( ) ) ) ,
542
_ => None ,
543
} ) )
544
}
545
546
547
pub fn store_blob_for ( & self , content : & [u8 ], nocompress : bool ) -> Result< ObjectRef> {
548
if ! nocompress {
549
return self .store_blob ( content) ;
550
}
551
for c in crate :: chunk:: chunks( content) {
552
self .store.put_chunk_nocompress ( c) ? ;
553
}
554
Ok ( self .store.put ( ObjectType:: Blob, & Self :: blob_for( content) .encode ( ) ) ? )
555
}
556
557
fn locks_dir ( & self ) -> PathBuf {
558
self .oxy.join ( " locks" )
559
}
560
561
562
pub fn is_locked ( & self , path : & str ) -> bool {
563
self .locks_dir ( ) .join ( path.replace ( ' /' , " %2F" ) ) .exists ( )
564
}
565
566
567
pub fn lock_path ( & self , path : & str ) -> Result< ( ) > {
568
let attrs = self .disk_attributes ( ) ;
569
if ! attrs.lookup ( path) .lockable {
570
return fail ( format! ( " {path} is not marked lockable in .oxyattributes" ) ) ;
571
}
572
fs:: create_dir_all( self .locks_dir ( ) ) ? ;
573
write_atomic ( & self .locks_dir ( ) .join ( path.replace ( ' /' , " %2F" ) ) , std:: process:: id( ) .to_string ( ) .as_bytes ( ) ) ? ;
574
set_readonly ( & self .root.join ( path) , false ) ? ;
575
Ok ( ( ) )
576
}
577
578
579
pub fn unlock_path ( & self , path : & str ) -> Result< ( ) > {
580
let _ = fs:: remove_file( self .locks_dir ( ) .join ( path.replace ( ' /' , " %2F" ) ) ) ;
581
if self .root.join ( path) .exists ( ) {
582
set_readonly ( & self .root.join ( path) , true ) ? ;
583
}
584
Ok ( ( ) )
585
}
586
7421309f Andy McCall 1 month ago ↶
587
588
pub fn in_scope ( & self , path : & str ) -> bool {
589
self .config.sparse_paths.is_empty ( ) | | self .config.sparse_paths.iter ( ) .any ( | p | path = = p | | path.starts_with ( & format! ( " {p} /" ) ) )
590
}
591
98286406 Andy McCall 1 month ago ↶
592
fn walk_working_copy ( & self ) -> Result< BTreeMap< String, FsEntry> > {
9ac1fbe2 Andy McCall 1 month ago ↶
593
self .walk_working_copy_with ( None , & BTreeSet:: new( ) )
b7f102fc Andy McCall 1 month ago ↶
594
}
595
596
597
598
599
9ac1fbe2 Andy McCall 1 month ago ↶
600
601
602
603
604
fn walk_working_copy_with ( & self , cache : Option< & prompt:: Manifest> , tracked : & BTreeSet< String> ) -> Result< BTreeMap< String, FsEntry> > {
98286406 Andy McCall 1 month ago ↶
605
let mut wb = ignore:: WalkBuilder:: new( & self .root) ;
606
wb.hidden ( false )
607
.git_ignore ( false )
608
.git_global ( false )
609
.git_exclude ( false )
610
.require_git ( false )
611
.follow_links ( false )
612
.add_custom_ignore_filename ( " .oxyignore" ) ;
613
wb.filter_entry ( | e | e.file_name ( ) ! = OXY_DIR ) ;
614
let mut out = BTreeMap:: new( ) ;
615
for entry in wb.build ( ) {
616
let entry = entry.map_err ( | e | RepoError:: Msg( e.to_string ( ) ) ) ? ;
617
let path = entry.path ( ) ;
618
if path = = self .root {
619
continue ;
620
}
621
let rel = path.strip_prefix ( & self .root) .unwrap ( ) ;
622
let rel: String = rel.components ( ) .map ( | c | c.as_os_str ( ) .to_string_lossy ( ) .to_string ( ) ) .collect:: < Vec< _ > > ( ) .join ( " /" ) ;
623
let Some ( ft) = entry.file_type ( ) else { continue } ;
7421309f Andy McCall 1 month ago ↶
624
if ft.is_dir ( ) | | ! self .in_scope ( & rel) {
98286406 Andy McCall 1 month ago ↶
625
continue ;
626
}
627
if ft.is_symlink ( ) {
628
let target = fs:: read_link( path) ? ;
b7f102fc Andy McCall 1 month ago ↶
629
out.insert ( rel, FsEntry:: new( NodeKind:: Symlink, false , target.to_string_lossy ( ) .as_bytes ( ) .to_vec ( ) ) ) ;
98286406 Andy McCall 1 month ago ↶
630
} else if ft.is_file ( ) {
b7f102fc Andy McCall 1 month ago ↶
631
let meta = entry.metadata ( ) .map_err ( | e | RepoError:: Msg( e.to_string ( ) ) ) ? ;
9ac1fbe2 Andy McCall 1 month ago ↶
632
let e = self .file_entry ( & rel, path, & meta, cache) ? ;
633
out.insert ( rel, e) ;
98286406 Andy McCall 1 month ago ↶
634
}
635
}
9ac1fbe2 Andy McCall 1 month ago ↶
636
637
for rel in tracked {
638
if out.contains_key ( rel) | | ! self .in_scope ( rel) {
639
continue ;
640
}
641
let path = self .root.join ( rel) ;
642
let Ok ( meta) = fs:: symlink_metadata( & path) else { continue } ;
643
let ft = meta.file_type ( ) ;
644
if ft.is_symlink ( ) {
645
let target = fs:: read_link( & path) ? ;
646
out.insert ( rel.clone ( ) , FsEntry:: new( NodeKind:: Symlink, false , target.to_string_lossy ( ) .as_bytes ( ) .to_vec ( ) ) ) ;
647
} else if ft.is_file ( ) {
648
let e = self .file_entry ( rel, & path, & meta, cache) ? ;
649
out.insert ( rel.clone ( ) , e) ;
650
}
651
}
98286406 Andy McCall 1 month ago ↶
652
Ok ( out)
653
}
9ac1fbe2 Andy McCall 1 month ago ↶
654
655
656
fn file_entry ( & self , rel : & str , path : & Path, meta : & fs:: Metadata, cache : Option< & prompt:: Manifest> ) -> Result< FsEntry> {
657
let exec = is_executable ( meta) ;
658
let cached = cache.and_then ( | m | {
659
let e = m.entries.get ( rel) ? ;
660
let mtime_ns = prompt:: mtime_ns( meta) ;
661
let racy = mtime_ns + 2_000_000_000 > = m.written_ns;
662
( e.size = = meta.len ( ) & & e.mtime_ns = = mtime_ns & & ! racy) .then_some ( e)
663
} ) ;
664
Ok ( match cached {
665
Some ( e) if e.blob.is_some_and ( | b | self .store.has ( & b) ) => FsEntry { kind: NodeKind:: File, exec, content: vec! [ ] , blob: e.blob, content_hash: Some ( e.hash.clone ( ) ) } ,
666
Some ( e) => FsEntry { kind: NodeKind:: File, exec, content: fs:: read( path) ? , blob: None , content_hash: Some ( e.hash.clone ( ) ) } ,
667
None => FsEntry:: new( NodeKind:: File, exec, fs:: read( path) ? ) ,
668
} )
669
}
670
671
672
pub ( crate ) fn tracked_paths ( snapshot : & Snapshot) -> BTreeSet< String> {
673
snapshot.files.iter ( ) .filter ( | ( _, f ) | f.kind ! = NodeKind:: Dir ) .map ( | ( p , _) | p.clone ( ) ) .collect ( )
674
}
98286406 Andy McCall 1 month ago ↶
675
}
676
677
impl Repo {
678
679
pub fn log ( & self ) -> Result< Vec< LogEntry> > {
680
let ( wc_hash, _ ) = self .working_copy ( ) ? ;
681
let mut heads = self .bookmark_heads ( ) ? ;
682
heads.push ( wc_hash) ;
683
let state = self .load_state ( & heads) ? ;
684
let head_set: BTreeSet< ChangeHash> = self .bookmark_heads ( ) ? .into_iter ( ) .collect ( ) ;
252e8cb2 Andy McCall 1 month ago ↶
685
let mut entries: Vec< LogEntry> = Vec :: new( ) ;
2863456a Andy McCall 1 month ago ↶
686
for ( h, meta) in state.changes ( ) {
687
entries.push ( LogEntry { hash: * h, change: meta.clone ( ) , is_working_copy: * h = = wc_hash, is_head: head_set.contains ( h) } ) ;
252e8cb2 Andy McCall 1 month ago ↶
688
}
98286406 Andy McCall 1 month ago ↶
689
entries.sort_by ( | a , b | b.is_working_copy.cmp ( & a.is_working_copy) .then ( b.change.hlc.cmp ( & a.change.hlc) ) .then ( b.hash.cmp ( & a.hash) ) ) ;
690
Ok ( entries)
691
}
692
132b884b Andy McCall 1 month ago ↶
693
694
695
pub fn resolve_heads ( & self , spec : & str ) -> Result< Vec< ChangeHash> > {
7b9809e6 Andy McCall 1 month ago ↶
696
let view = self .view ( ) ? ;
697
if let Some ( st) = view.bookmarks.get ( spec) {
698
return Ok ( self .store.get_state ( st) ? .heads) ;
132b884b Andy McCall 1 month ago ↶
699
}
700
if let Some ( heads) = self .tag_heads ( spec) ? {
701
return Ok ( heads) ;
702
}
703
Ok ( vec! [ self .resolve_change ( spec) ? ] )
704
}
705
98286406 Andy McCall 1 month ago ↶
706
707
pub fn resolve_change ( & self , spec : & str ) -> Result< ChangeHash> {
708
if spec = = " @" {
709
return Ok ( self .working_copy ( ) ? .0 ) ;
710
}
7b9809e6 Andy McCall 1 month ago ↶
711
if let Some ( st) = self .view ( ) ? .bookmarks.get ( spec) {
712
let heads = self .store.get_state ( st) ? .heads;
713
return match heads.len ( ) {
714
1 => Ok ( heads[ 0 ] ) ,
715
n => fail ( format! ( " bookmark {spec} has {n} heads; name a change, or use it where a state is expected" ) ) ,
716
} ;
717
}
98286406 Andy McCall 1 month ago ↶
718
if spec.is_empty ( ) | | spec = = " #" {
719
return fail ( " empty change specifier" ) ;
720
}
721
let entries = self .log ( ) ? ;
722
let matches: Vec< & LogEntry> = if let Some ( hp) = spec.strip_prefix ( ' #' ) {
723
entries.iter ( ) .filter ( | e | e.hash.starts_with_hex ( hp) ) .collect ( )
724
} else {
725
entries.iter ( ) .filter ( | e | e.change.change_id.starts_with_hex ( spec) ) .collect ( )
726
} ;
727
match matches.len ( ) {
728
1 => Ok ( matches[ 0 ] .hash) ,
729
0 => fail ( format! ( " no change matches {spec:?} " ) ) ,
730
_ => {
731
let ids: BTreeSet< ChangeId> = matches.iter ( ) .map ( | e | e.change.change_id ) .collect ( ) ;
732
if ids.len ( ) = = 1 {
733
fail ( format! ( " change {spec} is divergent: {} variants" , matches.len ( ) ) )
734
} else {
735
fail ( format! ( " {spec:?} is ambiguous" ) )
736
}
737
}
738
}
739
}
740
741
742
pub fn describe_change ( & self , hash : & ChangeHash) -> Result< String> {
743
let change = self .store.get_change ( hash) ? ;
744
let base = self .load_state ( & change.deps) ? ;
745
let base_snapshot = base.materialize ( ) ;
746
Ok ( render_change ( & change, & base, & base_snapshot) )
747
}
748
}
749
750
fn remove_any ( path : & Path) -> std:: io:: Result< ( ) > {
751
let m = fs:: symlink_metadata( path) ? ;
752
if m.is_dir ( ) { fs:: remove_dir_all( path) } else { fs:: remove_file( path) }
753
}
754
cdb5beba Andy McCall 1 month ago ↶
755
fn set_readonly ( path : & Path, readonly : bool ) -> std:: io:: Result< ( ) > {
756
let mut perms = fs:: metadata( path) ? .permissions ( ) ;
757
perms.set_readonly ( readonly) ;
758
fs:: set_permissions( path, perms)
759
}
760
98286406 Andy McCall 1 month ago ↶
761
# [ cfg ( unix ) ]
762
fn set_executable ( path : & Path, exec : bool ) -> std:: io:: Result< ( ) > {
763
use std:: os:: unix:: fs:: PermissionsExt;
764
let mut perms = fs:: metadata( path) ? .permissions ( ) ;
765
let mode = perms.mode ( ) ;
766
let want = if exec { mode | 0o111 } else { mode & ! 0o111 } ;
767
if want ! = mode {
768
perms.set_mode ( want) ;
769
fs:: set_permissions( path, perms) ? ;
770
}
771
Ok ( ( ) )
772
}
773
# [ cfg ( not ( unix ) ) ]
774
fn set_executable ( _path : & Path, _exec : bool ) -> std:: io:: Result< ( ) > {
775
Ok ( ( ) )
776
}
777
778
# [ cfg ( unix ) ]
779
fn is_executable ( m : & fs:: Metadata) -> bool {
780
use std:: os:: unix:: fs:: PermissionsExt;
781
m.permissions ( ) .mode ( ) & 0o111 ! = 0
782
}
783
# [ cfg ( not ( unix ) ) ]
784
fn is_executable ( _m : & fs:: Metadata) -> bool {
785
false
786
}
787
788
fn op_ref ( k : OpKey) -> OpRef {
789
OpRef { change: k.0 , index: k.1 }
790
}
791
792
fn split_path ( path : & str ) -> ( & str , & str ) {
793
match path.rfind ( ' /' ) {
794
Some ( i) => ( & path[ .. i] , & path[ i + 1 .. ] ) ,
795
None => ( " " , path) ,
796
}
797
}