-
Notifications
You must be signed in to change notification settings - Fork 423
Expand file tree
/
Copy pathstatus.rs
More file actions
1462 lines (1342 loc) · 54 KB
/
Copy pathstatus.rs
File metadata and controls
1462 lines (1342 loc) · 54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: 2026 Epic Games, Inc.
// SPDX-License-Identifier: MIT
#[cfg(target_family = "unix")]
use std::os::unix::fs::MetadataExt;
#[cfg(target_family = "windows")]
use std::os::windows::fs::MetadataExt;
use std::path::Path;
use std::sync::Arc;
use std::sync::OnceLock;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::time::Instant;
use crossbeam::queue::SegQueue;
use lore_base::lore_spawn;
use lore_error_set::prelude::*;
use serde::Deserialize;
use serde::Serialize;
use tokio::sync::Notify;
use tokio::task::JoinSet;
use super::RepositoryContext;
use crate::branch;
use crate::change::FileAction;
use crate::change::NodeChange;
use crate::errors::*;
use crate::event;
use crate::event::EventError;
use crate::filter::FilterMode;
use crate::find;
use crate::interface::LoreError;
use crate::interface::LoreFileAction;
use crate::interface::LoreNodeType;
use crate::interface::LoreString;
use crate::layer;
use crate::lore::BranchId;
use crate::lore::Hash;
use crate::lore::RepositoryId;
use crate::lore_debug;
use crate::lore_drain_tasks;
use crate::lore_trace;
use crate::metadata::Metadata;
use crate::node::NodeFlags;
use crate::node::NodeID;
use crate::node::NodeIDExt;
use crate::node::ROOT_NODE;
use crate::path::emit_path_ignore;
use crate::state;
use crate::util::path::RelativePath;
use crate::util::serde::u8_as_bool;
/// Revision status of a repository, describing the current, local, and remote
/// positions of the active branch.
#[repr(C)]
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct LoreRepositoryStatusRevisionEventData {
/// Repository identifier
pub repository: RepositoryId,
/// Current branch identifier
pub branch: BranchId,
/// Current branch name
pub branch_name: LoreString,
/// Current revision identifier
pub revision: Hash,
/// Current revision number
pub revision_number: u64,
/// Staged revision identifier (zero when nothing is staged)
pub revision_staged: Hash,
/// Incoming revision identifier of a pending merge (zero when none)
pub revision_merged: Hash,
/// Last revision merged in from the parent branch (calculated and reported if sync point option is set).
pub revision_merged_parent_branch: Hash,
/// Local branch latest revision identifier
pub revision_local: Hash,
/// Local branch latest revision number
pub revision_local_number: u64,
/// Remote branch latest revision identifier (zero if unknown, branch not existing on remote or remote not available)
pub revision_remote: Hash,
/// Remote branch latest revision number (zero if corresponding identifier is zero)
pub revision_remote_number: u64,
/// Local holds revisions not on the remote history line
pub is_local_ahead: u8,
/// Remote holds revisions not present locally
pub is_remote_ahead: u8,
/// Remote configured and reachable with a local identity; connectivity only, not authorization
pub remote_available: u8,
/// Remote revision query returned an authoritative answer, identity is authorized to access the repository
pub remote_authorized: u8,
/// Branch exists on the remote and the query returned a latest revisoin (possibly zero if branch does not exist on remote)
pub remote_branch_exist: u8,
}
#[allow(clippy::too_many_arguments)]
#[allow(clippy::fn_params_excessive_bools)]
impl LoreRepositoryStatusRevisionEventData {
pub fn new(
repository: RepositoryId,
branch: BranchId,
branch_name: &str,
revision: Hash,
revision_number: u64,
revision_staged: Hash,
revision_merged: Hash,
revision_merged_parent_branch: Hash,
revision_local: Hash,
revision_local_number: u64,
revision_remote: Hash,
revision_remote_number: u64,
is_local_ahead: bool,
is_remote_ahead: bool,
remote_available: bool,
remote_authorized: bool,
remote_branch_exist: bool,
) -> Self {
LoreRepositoryStatusRevisionEventData {
repository,
branch,
branch_name: branch_name.into(),
revision,
revision_number,
revision_staged,
revision_merged,
revision_merged_parent_branch,
revision_local,
revision_local_number,
revision_remote,
revision_remote_number,
is_local_ahead: is_local_ahead.into(),
is_remote_ahead: is_remote_ahead.into(),
remote_available: remote_available.into(),
remote_authorized: remote_authorized.into(),
remote_branch_exist: remote_branch_exist.into(),
}
}
}
/// Status of a single file or node reported by a repository status operation.
#[repr(C)]
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct LoreRepositoryStatusFileEventData {
/// Path of the file relative to the repository root.
pub path: LoreString,
/// Size of the file in bytes.
pub size: u64,
/// Change applied to the file, such as add, modify, delete, or move.
pub action: LoreFileAction,
/// Kind of node: file, directory, or link.
pub r#type: LoreNodeType,
/// Non-zero when the change is staged.
#[serde(with = "u8_as_bool")]
pub flag_staged: u8,
/// Non-zero when the change comes from a merge.
#[serde(with = "u8_as_bool")]
pub flag_merged: u8,
/// Non-zero when the file is in conflict.
#[serde(with = "u8_as_bool")]
pub flag_conflict: u8,
/// Non-zero when the conflict is not yet resolved.
#[serde(with = "u8_as_bool")]
pub flag_conflict_unresolved: u8,
/// Non-zero when the conflict was resolved automatically.
#[serde(with = "u8_as_bool")]
pub flag_conflict_automerged: u8,
/// Non-zero when the local side was chosen to resolve the conflict.
#[serde(with = "u8_as_bool")]
pub flag_conflict_mine: u8,
/// Non-zero when the incoming side was chosen to resolve the conflict.
#[serde(with = "u8_as_bool")]
pub flag_conflict_theirs: u8,
/// Non-zero when the file differs from the recorded state.
#[serde(with = "u8_as_bool")]
pub flag_dirty: u8,
/// Previous path of the file when it was moved or copied. Empty otherwise.
pub from_path: LoreString,
}
impl LoreRepositoryStatusFileEventData {
pub fn from_node_change(change: &NodeChange, size: u64) -> Self {
let node_type = if change.action == FileAction::Add
|| change.action == FileAction::Move
|| change.to.node.is_valid_node_id()
{
change.to.flags
} else {
change.from.flags
};
let node_type = if node_type.contains(NodeFlags::File) {
LoreNodeType::File
} else if node_type.contains(NodeFlags::Link) {
LoreNodeType::Link
} else {
LoreNodeType::Directory
};
LoreRepositoryStatusFileEventData {
path: LoreString::from(&change.path),
size,
action: LoreFileAction::from(change.action),
r#type: node_type,
flag_dirty: change.flags.is_dirty().into(),
flag_staged: change.flags.is_stage().into(),
flag_merged: change.flags.is_merge().into(),
flag_conflict: change.flags.is_conflict().into(),
flag_conflict_unresolved: change.flags.is_conflict_unresolved().into(),
flag_conflict_automerged: change.flags.is_conflict_automerged().into(),
flag_conflict_mine: change.flags.is_conflict_mine().into(),
flag_conflict_theirs: change.flags.is_conflict_theirs().into(),
from_path: change.from_path.as_ref().map(|path| path.as_str()).into(),
}
}
pub fn action_as_string_short(&self) -> &'static str {
self.action.as_string_short()
}
pub fn merged_as_string_short(&self) -> &'static str {
if self.flag_merged != 0 {
return "(M)";
}
""
}
pub fn conflict_as_string_short(&self) -> &'static str {
if self.flag_conflict != 0 && self.flag_conflict_unresolved != 0 {
return "!";
}
""
}
}
/// Counts of directories and files in the repository tree.
#[repr(C)]
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct LoreRepositoryStatusCountEventData {
/// Number of directories in the tree, view-filtered (staged state if
/// present, otherwise the current revision)
pub directories: u64,
/// Number of files in the tree, view-filtered (staged state if present,
/// otherwise the current revision)
pub files: u64,
}
/// Aggregate counts of dirty nodes by action type, emitted once at the end of
/// a reconciling status (`--scan` or `--check-dirty`). For `--scan` these are
/// the changes detected against the filesystem; for `--check-dirty` they are
/// the nodes that remained dirty after the filesystem verification.
#[repr(C)]
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct LoreRepositoryStatusSummaryEventData {
/// Number of files added.
pub adds: u64,
/// Number of files deleted.
pub deletes: u64,
/// Number of files modified.
pub modifies: u64,
/// Number of files moved.
pub moves: u64,
/// Number of files copied.
pub copies: u64,
}
/// Thread-safe accumulator for dirty-node counts during the parallel status
/// scan/verify walk. Each spawned task increments the relevant counter via
/// [`StatusSummaryStats::classify`].
#[derive(Default)]
pub struct StatusSummaryStats {
adds: AtomicU64,
deletes: AtomicU64,
modifies: AtomicU64,
moves: AtomicU64,
copies: AtomicU64,
}
impl StatusSummaryStats {
/// Increment the counter matching a reported change's action. `Keep` is a
/// content modification (a filesystem/state diff has no separate "modify"
/// action — modified files surface as `Keep` with the modify flag set).
fn classify(&self, change: &NodeChange) {
let counter = match change.action {
FileAction::Add => &self.adds,
FileAction::Delete => &self.deletes,
FileAction::Move => &self.moves,
FileAction::Copy => &self.copies,
FileAction::Keep => &self.modifies,
};
counter.fetch_add(1, Ordering::Relaxed);
}
fn event_data(&self) -> LoreRepositoryStatusSummaryEventData {
LoreRepositoryStatusSummaryEventData {
adds: self.adds.load(Ordering::Relaxed),
deletes: self.deletes.load(Ordering::Relaxed),
modifies: self.modifies.load(Ordering::Relaxed),
moves: self.moves.load(Ordering::Relaxed),
copies: self.copies.load(Ordering::Relaxed),
}
}
}
#[error_set]
pub enum StatusError {
NodeNotFound,
LinkNotFound,
NotFound,
FileNotFound,
RevisionNotFound,
WriteRequired,
Oversized,
InvalidArguments,
InvalidPath,
InvalidNodeHierarchy,
AddressNotFound,
PayloadNotFound,
AlreadyLinked,
LayerNotFound,
Disconnected,
SlowDown,
NotAuthorized,
NotAuthenticated,
Maintenance,
NoRemote,
NotSupported,
BranchAdvanced,
BranchAlreadyExists,
BranchNotFound,
Conflict,
DeleteCurrent,
DeleteDefault,
DeleteProtected,
Divergent,
IdenticalMetadata,
LinkPathNotFound,
LocalModifications,
LockNotFound,
LockNotOwned,
MaxHistorySearchDepth,
NotALayer,
NotALink,
NotConnected,
NothingStaged,
RepositoryAlreadyExists,
RepositoryNotFound,
SharedStoreNotFound,
TokenNotFound,
MissingIdentity,
}
impl EventError for StatusError {
fn translated(&self) -> LoreError {
match self {
StatusError::Disconnected(_) => LoreError::Connection,
StatusError::SlowDown(_) => LoreError::SlowDown,
StatusError::Oversized(_) => LoreError::Oversized,
StatusError::FileNotFound(_) => LoreError::FileNotFound,
StatusError::NotFound(_)
| StatusError::LayerNotFound(_)
| StatusError::RevisionNotFound(_) => LoreError::NotFound,
StatusError::AddressNotFound(_) => LoreError::AddressNotFound,
StatusError::PayloadNotFound(_) => LoreError::PayloadNotFound,
StatusError::InvalidArguments(_) | StatusError::InvalidPath(_) => {
LoreError::InvalidArguments
}
_ => LoreError::Internal,
}
}
fn inner(&self) -> String {
self.to_string()
}
}
#[derive(Clone, Debug)]
pub struct StatusOptions {
// Include staged or not
pub staged: bool,
/// Reconcile against the filesystem and refresh dirty tracking.
///
/// When `false` (default), status reports the currently tracked state:
/// the staged revision (if any) plus all files and directories marked
/// dirty in the repository. No filesystem reads are performed beyond the
/// existing dirty flags.
///
/// When `true`, the filesystem is walked under each requested path,
/// every file is reconciled against the current revision, and dirty
/// flags are set or cleared accordingly. The refreshed flags are
/// persisted in the staged state so subsequent operations see an
/// accurate picture without rescanning.
pub scan: bool,
/// Verify dirty flags against the filesystem while reporting tracked state.
///
/// Unlike [`scan`](Self::scan), this performs no filesystem walk: it only
/// re-examines files already marked dirty. For each dirty file the on-disk
/// content is checked (a size difference is a modification; otherwise, when
/// the recorded modification time differs, the content is rehashed and
/// compared). A file that turns out unmodified has its dirty flag cleared
/// and is dropped from the report, unless it is also staged. Structural
/// dirty actions (add/move/copy/delete) are always treated as modified.
///
/// The refreshed flags are persisted in the staged state, so this requires
/// write capability.
pub check_dirty: bool,
// Drop the existing staged anchor before computing status.
// Combine with `scan` to scan from a clean slate.
pub reset: bool,
// Include sync point
pub sync_point: bool,
// Only emit revision info, skip all diffs
pub revision_only: bool,
// Count directories and files (view-filtered) in the staged state if
// present, otherwise the current revision
pub count: bool,
}
async fn file_size_from_node_change_id(change: &NodeChange) -> Result<u64, StatusError> {
if change.action == FileAction::Delete {
Ok(0)
} else {
let size = change
.to
.state
.node(change.to.repository.clone(), change.to.node)
.await
.forward::<StatusError>("accessing node path")?
.size;
Ok(size)
}
}
async fn file_size_from_node_change_path(
repository_path: &Path,
change: &NodeChange,
) -> Result<u64, StatusError> {
if change.action == FileAction::Delete {
Ok(0)
} else {
let path_str = change.path.as_str().to_string();
let result = tokio::fs::metadata(change.path.to_absolute_path(repository_path)).await;
// The file may have vanished between the diff's filesystem walk and
// this stat — e.g. a concurrent `branch switch` deleted it from the
// working directory. Treat the concurrent deletion as benign and
// report size 0 (matching the `FileAction::Delete` branch above)
// rather than failing the whole status command with an Internal error.
// On Windows a file mid-deletion stats as PermissionDenied rather than
// NotFound, so treat that as benign too.
if let Err(err) = &result
&& (err.kind() == std::io::ErrorKind::NotFound
|| (cfg!(target_family = "windows")
&& err.kind() == std::io::ErrorKind::PermissionDenied))
{
return Ok(0);
}
let metadata =
result.internal_with(|| format!("accessing metadata for file {path_str}"))?;
#[cfg(target_family = "windows")]
let size = metadata.file_size();
#[cfg(target_family = "unix")]
let size = metadata.size();
Ok(size)
}
}
/// Verify whether a dirty file change reflects a real on-disk modification,
/// clearing the node's dirty flag when it does not.
///
/// Structural dirty actions (add/move/copy/delete) are modifications by
/// definition and always report `true`. For a content modification the file is
/// compared against its tracked node (the staged side of the diff, which
/// carries the tracked content hash and size): a differing size is a modification;
/// otherwise, when the recorded modification time differs, the content is
/// rehashed and compared. A file that turns out unmodified has its dirty flag
/// cleared on the staged node (propagating to parents) and reports `false`.
///
/// A missing or unreadable file is reported as modified — the dirty flag then
/// reflects a real change that the regular diff will surface.
async fn dirty_change_is_modified(
repository: Arc<RepositoryContext>,
change: &NodeChange,
) -> Result<bool, StatusError> {
if change.action != FileAction::Keep {
return Ok(true);
}
let node_state = &change.to;
if !node_state.node.is_valid_node_id() {
return Ok(true);
}
let node = node_state
.get_node()
.await
.forward::<StatusError>("loading dirty node for verification")?;
if !node.is_file() {
return Ok(true);
}
let absolute_path = change.path.to_absolute_path(repository.require_path()?);
let Ok(metadata) = tokio::fs::metadata(&absolute_path).await else {
return Ok(true);
};
if !metadata.is_file() {
return Ok(true);
}
let (file_mtime, file_size) = crate::util::fs::file_mtime_and_size(&metadata);
let (is_modified, _file_hash) = state::is_file_modified(
repository.clone(),
&node,
file_mtime,
file_size,
&change.path,
false,
)
.await
.forward::<StatusError>("comparing dirty file against filesystem")?;
if !is_modified {
node_state
.state
.node_clear_dirty(node_state.repository.clone(), node_state.node)
.await
.forward::<StatusError>("clearing stale dirty flag")?;
}
Ok(is_modified)
}
/// Upper bound on concurrent subtree-counting tasks. Counting is dominated by
/// per-directory node-block reads (and link resolutions that may reach other
/// repositories), so overlapping them up to this many in-flight tasks hides I/O
/// latency while keeping fan-out bounded on huge trees. Scaled to the machine
/// but capped so a many-core host doesn't spawn excessive workers.
const COUNT_MAX_CONCURRENCY: usize = 128;
/// A directory (or resolved link target) whose children still need counting.
struct CountWork {
state: Arc<state::State>,
repository: Arc<RepositoryContext>,
node_id: NodeID,
path: RelativePath,
}
/// Shared state for the bounded worker pool counting view-filtered nodes.
///
/// The recursion is reified as an explicit lock-free `queue` of [`CountWork`]
/// items drained by a fixed set of workers, rather than recursive task
/// spawning, so concurrency is hard-bounded by the worker count regardless of
/// tree shape. `outstanding` tracks items neither fully processed nor yet
/// counted (queued plus in flight); workers exit once it reaches zero. The
/// first error is kept in `error`, after which workers stop processing but
/// keep draining so the counter still reaches zero and every worker terminates.
struct CountShared {
queue: SegQueue<CountWork>,
outstanding: AtomicUsize,
directories: AtomicU64,
files: AtomicU64,
error: OnceLock<StatusError>,
notify: Notify,
}
/// Resolve `source_path` to the work needed to count its subtree, labelling
/// descendant paths under `target_path` for view filtering. The two differ for
/// a layer: the node is resolved at the layer's `source_path` while paths are
/// labelled with the mount `target_path`, so the local view filter matches the
/// working-tree layout. Returns the node's own `(directories, files)`
/// contribution plus an optional root [`CountWork`] for its descendants. A file
/// yields `(0, 1, None)`; a directory or link `(1, 0, Some(work))`. An empty
/// `source_path` is the root of `state` (a layer whose source is the repo
/// root), counted as one directory plus its descendants. An unresolved path
/// yields `(0, 0, None)`.
async fn count_at_path_root(
state: Arc<state::State>,
repository: Arc<RepositoryContext>,
source_path: &RelativePath,
target_path: &RelativePath,
) -> Result<(u64, u64, Option<CountWork>), StatusError> {
if source_path.is_empty() {
return Ok((
1,
0,
Some(CountWork {
state,
repository,
node_id: ROOT_NODE,
path: target_path.clone(),
}),
));
}
let Ok(link) = state
.find_node_link(repository.clone(), source_path.as_str())
.await
else {
return Ok((0, 0, None));
};
if !link.is_valid() {
return Ok((0, 0, None));
}
let (repository, state) = link
.resolve(repository.clone(), state.clone())
.await
.forward::<StatusError>("resolving count path")?;
let node = state
.node(repository.clone(), link.node)
.await
.forward::<StatusError>("reading count path node")?;
if node.is_file() {
return Ok((0, 1, None));
}
if node.is_link() {
let inner = node.linked_node();
let (repository, state) = inner
.resolve(repository.clone(), state.clone())
.await
.forward::<StatusError>("resolving count path link target")?;
return Ok((
1,
0,
Some(CountWork {
state,
repository,
node_id: inner.node,
path: target_path.clone(),
}),
));
}
Ok((
1,
0,
Some(CountWork {
state,
repository,
node_id: link.node,
path: target_path.clone(),
}),
))
}
/// Count `work`'s direct children, honoring the repository's local view filter,
/// adding files/directories to the shared totals and pushing each directory (or
/// resolved link target) back onto the shared stack for later counting. A link
/// node is counted as a directory and descended into.
async fn count_node_children(work: &CountWork, shared: &CountShared) -> Result<(), StatusError> {
let mut children = state::StateNodeChildrenWithNameIterator::new(
work.state.clone(),
work.repository.clone(),
work.node_id,
)
.await
.forward::<StatusError>("iterating revision tree children")?;
let mut directories = 0u64;
let mut files = 0u64;
let mut pushed = Vec::new();
while let Some((child_id, child_node, child_name)) = children
.next()
.await
.forward::<StatusError>("reading revision tree node")?
{
let is_directory = child_node.is_directory();
let is_link = child_node.is_link();
let child_path = work.path.push_into_buf(child_name).freeze();
if work
.repository
.filter
.excludes(&child_path, is_directory || is_link, FilterMode::View)
{
continue;
}
if is_directory {
directories += 1;
pushed.push(CountWork {
state: work.state.clone(),
repository: work.repository.clone(),
node_id: child_id,
path: child_path,
});
} else if is_link {
directories += 1;
let link = child_node.linked_node();
let (link_repository, link_state) = link
.resolve(work.repository.clone(), work.state.clone())
.await
.forward::<StatusError>("resolving link target for count")?;
pushed.push(CountWork {
state: link_state,
repository: link_repository,
node_id: link.node,
path: child_path,
});
} else if child_node.is_file() {
files += 1;
}
}
if directories > 0 {
shared.directories.fetch_add(directories, Ordering::Relaxed);
}
if files > 0 {
shared.files.fetch_add(files, Ordering::Relaxed);
}
if !pushed.is_empty() {
// Account for the new work before it becomes visible so a worker that
// pops and finishes a child can't drive `outstanding` to zero early.
shared.outstanding.fetch_add(pushed.len(), Ordering::AcqRel);
for work in pushed {
shared.queue.push(work);
}
shared.notify.notify_waiters();
}
Ok(())
}
/// A single pool worker: drain the shared queue until no work remains in flight.
///
/// Termination is driven solely by `outstanding` reaching zero, so it is robust
/// regardless of scheduling: a notification interest is registered (`enable`)
/// before each empty-queue check, so a concurrent push or completion can never
/// be missed before the worker parks on `notify`.
async fn count_worker(shared: Arc<CountShared>) -> Result<(), StatusError> {
loop {
let notified = shared.notify.notified();
tokio::pin!(notified);
notified.as_mut().enable();
let Some(work) = shared.queue.pop() else {
if shared.outstanding.load(Ordering::Acquire) == 0 {
shared.notify.notify_waiters();
return Ok(());
}
notified.await;
continue;
};
// Once any worker has failed, stop processing but keep draining so
// `outstanding` still reaches zero and every worker terminates. The
// `OnceLock` keeps the first error and ignores the rest.
if shared.error.get().is_none()
&& let Err(err) = count_node_children(&work, &shared).await
{
let _ = shared.error.set(err);
}
if shared.outstanding.fetch_sub(1, Ordering::AcqRel) == 1 {
shared.notify.notify_waiters();
}
}
}
/// Count directories and files in the subtrees rooted at `roots`, honoring each
/// repository's local view filter, using a pool of at most
/// [`COUNT_MAX_CONCURRENCY`] workers. Returns the summed `(directories, files)`
/// across all roots (excluding the root nodes themselves).
async fn count_subtrees(roots: Vec<CountWork>) -> Result<(u64, u64), StatusError> {
if roots.is_empty() {
return Ok((0, 0));
}
let queue = SegQueue::new();
let outstanding = roots.len();
for work in roots {
queue.push(work);
}
let shared = Arc::new(CountShared {
queue,
outstanding: AtomicUsize::new(outstanding),
directories: AtomicU64::new(0),
files: AtomicU64::new(0),
error: OnceLock::new(),
notify: Notify::new(),
});
let workers = lore_base::runtime::processor_count().clamp(1, COUNT_MAX_CONCURRENCY);
let mut tasks = JoinSet::new();
for _ in 0..workers {
let shared = shared.clone();
lore_spawn!(tasks, count_worker(shared));
}
lore_drain_tasks!(tasks, StatusError::internal("Count worker task failed"))?;
let directories = shared.directories.load(Ordering::Relaxed);
let files = shared.files.load(Ordering::Relaxed);
if shared.error.get().is_some() {
let shared = Arc::into_inner(shared)
.expect("all count workers have completed and released their references");
return Err(shared
.error
.into_inner()
.expect("error presence was just observed"));
}
Ok((directories, files))
}
pub async fn status(
repository: Arc<RepositoryContext>,
paths: Option<Vec<RelativePath>>,
options: StatusOptions,
) -> Result<(), StatusError> {
if options.reset {
crate::instance::delete_staged_anchor(&repository)
.await
.forward::<StatusError>("dropping staged anchor for status reset")?;
}
let (state_current, state_staged, current_branch) =
state::State::deserialize_current_and_staged(repository.clone())
.await
.forward::<StatusError>("deserializing current and staged state")?;
let mut has_staged = state_staged.is_some();
let state_staged = state_staged.unwrap_or_else(|| state_current.clone());
lore_debug!(
"Repository status, current signature {}, staged signature {}",
state_current.revision(),
state_staged.revision()
);
let layers = {
let mut layers = vec![];
let list = layer::list(repository.clone()).await.unwrap_or_default();
for layer in list {
let layer_state = layer
.deserialize_current_and_staged(repository.clone())
.await
.forward::<StatusError>("deserializing layer state")?;
if !layer_state.state_staged.revision().is_zero()
&& layer_state.state_staged.revision() != layer_state.state_current.revision()
{
has_staged = true;
}
layers.push((layer, layer_state));
}
layers
};
// Pre-resolve layer mount metadata for the parent's filesystem walker:
// for each configured layer, find the source_path node in the layer's
// staged state. When the walker hits one of these mount paths it switches
// comparison context to the layer's tree rather than treating the
// mount-point contents as parent-tree adds.
let layer_mounts: Arc<Vec<state::LayerMountInfo>> = {
let mut mounts = Vec::new();
for (layer, layer_state) in layers.iter() {
let source_node_link = layer_state
.state_staged
.find_node_link(layer_state.repository.clone(), &layer.source_path)
.await;
let Ok(source_node_link) = source_node_link else {
lore_debug!(
"Skipping layer mount {} — source path {} not found in layer state",
layer.target_path,
layer.source_path
);
continue;
};
mounts.push(state::LayerMountInfo {
target_path: layer.target_path.clone(),
repository: layer_state.repository.clone(),
state: layer_state.state_staged.clone(),
source_node: source_node_link.node,
});
}
Arc::new(mounts)
};
let branch_metadata = branch::metadata(repository.clone(), current_branch)
.await
.forward::<StatusError>("loading branch metadata")?;
let branch = branch::branch_metadata(repository.clone(), current_branch, &branch_metadata)
.await
.forward::<StatusError>("loading branch info")?;
let branch_stack = branch::stack(&branch_metadata);
let show_staged = options.staged;
let show_scan = options.scan;
let check_dirty = options.check_dirty;
// Accumulates per-action dirty counts across the parallel staged/scan
// walks; emitted as a single summary event for --scan / --check-dirty.
let summary = Arc::new(StatusSummaryStats::default());
let local_latest = branch::load_latest(repository.clone(), branch.id)
.await
.unwrap_or_default();
let local_state = state::State::deserialize(repository.clone(), local_latest)
.await
.forward::<StatusError>("deserializing local state")?;
// Authorized only on an authoritative answer — a latest revision
// or branch not found; proving the identity is authorized and has access
let (remote_latest, remote_authorized) = match repository.remote().await {
Ok(remote) => match branch::load_remote(remote, repository.id, branch.id).await {
Ok(status) => (Some(status.latest), true),
Err(err) if err.is_branch_not_found() => (None, true),
Err(_) => (None, false),
},
Err(_) => (None, false),
};
let remote_state = if let Some(remote_latest) = remote_latest {
state::State::deserialize(repository.clone(), remote_latest)
.await
.ok()
} else {
None
};
let branch_parent = branch_stack
.first()
.map(|parent| parent.branch)
.unwrap_or_default();
let branch_point = branch_stack
.first()
.map(|parent| parent.revision)
.unwrap_or_default();
let revision_merged_parent_branch = if options.sync_point {
if branch_point.is_zero() {
Hash::default()
} else {
let mut search_point = state_current.revision();
// Repeatedly search for a revision that's the result of a merge and then
// check if the merged revision was coming from the parent branch.
loop {
let Ok(signature) = find::find_revision(
repository.clone(),
current_branch,
search_point,
false,
None,
|state, _metadata| {
let is_branch_point = state.revision() == branch_point;
let is_merge = !state.parent_other().is_zero();
if is_merge || is_branch_point {
find::FindMatchResult::Match
} else {
find::FindMatchResult::Continue
}
},
)
.await
else {
break Hash::default();
};
if signature == branch_point {
lore_debug!(
"Found branch point {} as last merged in from parent branch",
signature
);
break signature;
}
let branch_state = state::State::deserialize(repository.clone(), signature)
.await
.forward::<StatusError>("deserializing branch state")?;
let parent_state =
state::State::deserialize(repository.clone(), branch_state.parent_other())
.await
.forward::<StatusError>("deserializing parent state")?;
let parent_state_metadata =
Metadata::deserialize(repository.clone(), parent_state.metadata_hash())
.await
.forward::<StatusError>("deserializing parent metadata")?;
let parent_state_branch = parent_state_metadata
.get_branch()
.forward::<StatusError>("reading parent branch from metadata")?;
if parent_state_branch == branch_parent {
lore_debug!(
"Found revision {} as last merged in from parent branch",
parent_state.revision()
);
break parent_state.revision();
}
search_point = branch_state.parent_self();
}
}
} else {