Rebuild the FILE_INDEX from the current working copy. Walks every tracked file, stats it on disk, hashes its content, and stores (mtime, size, content_hash) in the FILE_INDEX. After this call, `status` can use the fast stat-comparison path instead of reconstructing graph content for every file. This is essential after `git import` where `restore_from_git` resets all file mtimes, invalidating an
(
&self,
working_copy: WorkingCopyId,
)
| 870 | /// |
| 871 | /// The number of files indexed. |
| 872 | pub fn reindex_working_copy( |
| 873 | &self, |
| 874 | working_copy: WorkingCopyId, |
| 875 | ) -> Result<usize, RepositoryError> { |
| 876 | use std::time::SystemTime; |
| 877 | |
| 878 | self.validate_working_copy(working_copy)?; |
| 879 | let tracked = self.list_tracked_files().unwrap_or_default(); |
| 880 | let repo_root = self.root.clone(); |
| 881 | |
| 882 | let mut entries: Vec<(String, i64, u32, u64, Hash)> = Vec::with_capacity(tracked.len()); |
| 883 | |
| 884 | for file in &tracked { |
| 885 | let abs = repo_root.join(&file.path); |
| 886 | if !abs.exists() || abs.is_dir() { |
| 887 | continue; |
| 888 | } |
| 889 | |
| 890 | let metadata = match std::fs::metadata(&abs) { |
| 891 | Ok(m) => m, |
| 892 | Err(_) => continue, |
| 893 | }; |
| 894 | |
| 895 | let mtime = metadata.modified().unwrap_or(SystemTime::UNIX_EPOCH); |
| 896 | let duration = mtime |
| 897 | .duration_since(SystemTime::UNIX_EPOCH) |
| 898 | .unwrap_or_default(); |
| 899 | let secs = duration.as_secs() as i64; |
| 900 | let nanos = duration.subsec_nanos(); |
| 901 | let size = metadata.len(); |
| 902 | |
| 903 | let content_hash = match std::fs::read(&abs) { |
| 904 | Ok(bytes) => Hash::of(&bytes), |
| 905 | Err(_) => continue, |
| 906 | }; |
| 907 | |
| 908 | let path_str = file.path.to_string_lossy().replace('\\', "/"); |
| 909 | entries.push((path_str, secs, nanos, size, content_hash)); |
| 910 | } |
| 911 | |
| 912 | let count = entries.len(); |
| 913 | |
| 914 | // Write in batches of 5000 to avoid holding the write txn too long |
| 915 | for chunk in entries.chunks(5000) { |
| 916 | self.update_file_index(working_copy, chunk)?; |
| 917 | } |
| 918 | |
| 919 | Ok(count) |
| 920 | } |
| 921 | |
| 922 | /// List tracked files under a given path prefix. |
| 923 | /// Get all tracked files under a directory prefix. |