|
| 1 | +//! Changes between the index and the worktree along with optional rename tracking. |
| 2 | +mod types { |
| 3 | + use crate::index_as_worktree::EntryStatus; |
| 4 | + use bstr::BStr; |
| 5 | + |
| 6 | + /// Options for use in [index_as_worktree_with_renames()](super::). |
| 7 | + #[derive(Clone, Default)] |
| 8 | + pub struct Options { |
| 9 | + /// Options to configure how modifications to tracked files should be obtained. |
| 10 | + pub tracked_file_modifications: crate::index_as_worktree::Options, |
| 11 | + /// Options to control the directory walk that informs about untracked files. |
| 12 | + /// |
| 13 | + /// Note that we forcefully disable emission of tracked files to avoid any overlap |
| 14 | + /// between emissions to indicate modifications, and those that are obtained by |
| 15 | + /// the directory walk. |
| 16 | + /// |
| 17 | + /// If `None`, the directory walk portion will not run at all, yielding data similar |
| 18 | + /// to a bare [index_as_worktree()](crate::index_as_worktree()) call. |
| 19 | + pub untracked_file_options: Option<gix_dir::walk::Options>, |
| 20 | + } |
| 21 | + |
| 22 | + /// An 'entry' in the sense of a merge of modified tracked files and results from a directory walk. |
| 23 | + pub enum Entry<'a, ContentChange, SubmoduleStatus> { |
| 24 | + /// A tracked file was modified, and index-specific information is passed. |
| 25 | + Modification { |
| 26 | + /// All entries in the index. |
| 27 | + entries: &'a [gix_index::Entry], |
| 28 | + /// The entry with modifications. |
| 29 | + entry: &'a gix_index::Entry, |
| 30 | + /// The index of the `entry` for lookup in `entries` - useful to look at neighbors. |
| 31 | + entry_index: usize, |
| 32 | + /// The repository-relative path of the entry. |
| 33 | + rela_path: &'a BStr, |
| 34 | + /// The computed status of the entry. |
| 35 | + status: EntryStatus<ContentChange, SubmoduleStatus>, |
| 36 | + }, |
| 37 | + /// An entry returned by the directory walk, without any relation to the index. |
| 38 | + /// |
| 39 | + /// This can happen if ignored files are returned as well, or if rename-tracking is disabled. |
| 40 | + DirectoryContents { |
| 41 | + /// The entry found during the disk traversal. |
| 42 | + entry: gix_dir::EntryRef<'a>, |
| 43 | + /// `collapsed_directory_status` is `Some(dir_status)` if this `entry` was part of a directory with the given |
| 44 | + /// `dir_status` that wasn't the same as the one of `entry` and if [gix_dir::walk::Options::emit_collapsed] was |
| 45 | + /// [CollapsedEntriesEmissionMode::OnStatusMismatch](gix_dir::walk::CollapsedEntriesEmissionMode::OnStatusMismatch). |
| 46 | + /// It will also be `Some(dir_status)` if that option was [CollapsedEntriesEmissionMode::All](gix_dir::walk::CollapsedEntriesEmissionMode::All). |
| 47 | + collapsed_directory_status: Option<gix_dir::entry::Status>, |
| 48 | + }, |
| 49 | + /// The rewrite tracking discovered a match between a deleted and added file, and considers them equal enough, |
| 50 | + /// depending on the tracker settings. |
| 51 | + /// |
| 52 | + /// Note that the source of the rewrite is always the index as it detects the absence of entries, something that |
| 53 | + /// can't be done during a directory walk. |
| 54 | + Rewrite { |
| 55 | + /// All entries in the index. |
| 56 | + index_entries: &'a [gix_index::Entry], |
| 57 | + /// The entry that is the source of the rewrite, which means it was removed on disk, |
| 58 | + /// equivalent to [Change::Removed](crate::index_as_worktree::Change::Removed). |
| 59 | + /// |
| 60 | + /// Note that the [entry-id](gix_index::Entry::id) is the content-id of the source of the rewrite. |
| 61 | + source_entry: &'a gix_index::Entry, |
| 62 | + /// The index of the `source_entry` for lookup in `index_entries` - useful to look at neighbors. |
| 63 | + source_entry_index: usize, |
| 64 | + /// The repository-relative path of the `source_entry`. |
| 65 | + source_rela_path: &'a BStr, |
| 66 | + /// The computed status of the `source_entry`, which would always be |
| 67 | + source_status: EntryStatus<ContentChange, SubmoduleStatus>, |
| 68 | + |
| 69 | + /// The untracked entry found during the disk traversal, the destination of the rewrite. |
| 70 | + /// |
| 71 | + /// Note that its [`rela_path`](gix_dir::EntryRef::rela_path) is the destination of the rewrite, and the current |
| 72 | + /// location of the entry. |
| 73 | + dirwalk_entry: gix_dir::EntryRef<'a>, |
| 74 | + /// `collapsed_directory_status` is `Some(dir_status)` if this `dirwalk_entry` was part of a directory with the given |
| 75 | + /// `dir_status` that wasn't the same as the one of `entry` and if [gix_dir::walk::Options::emit_collapsed] was |
| 76 | + /// [CollapsedEntriesEmissionMode::OnStatusMismatch](gix_dir::walk::CollapsedEntriesEmissionMode::OnStatusMismatch). |
| 77 | + /// It will also be `Some(dir_status)` if that option was [CollapsedEntriesEmissionMode::All](gix_dir::walk::CollapsedEntriesEmissionMode::All). |
| 78 | + dirwalk_entry_collapsed_directory_status: Option<gix_dir::entry::Status>, |
| 79 | + /// The object id after the rename, specifically hashed in order to determine equality. |
| 80 | + dirwalk_entry_id: gix_hash::ObjectId, |
| 81 | + /// It's `None` if `source_entry.id` is equal to `dirwalk_entry_id`, as identity made an actual diff computation unnecessary. |
| 82 | + /// Otherwise, and if enabled, it's `Some(stats)` to indicate how similar both entries were. |
| 83 | + diff: Option<gix_diff::blob::DiffLineStats>, |
| 84 | + |
| 85 | + /// If true, this rewrite is created by copy, and `source_entry.id` is pointing to its source. |
| 86 | + /// Otherwise it's a rename, and `source_entry.id` points to a deleted object, |
| 87 | + /// as renames are tracked as deletions and additions of the same or similar content. |
| 88 | + copy: bool, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + /// Observe the status of an entry by comparing an index entry to the worktree, along |
| 93 | + /// with potential directory walk results. |
| 94 | + pub trait VisitEntry<'index> { |
| 95 | + /// Data generated by comparing an entry with a file. |
| 96 | + type ContentChange; |
| 97 | + /// Data obtained when checking the submodule status. |
| 98 | + type SubmoduleStatus; |
| 99 | + /// Observe the `status` of `entry` at the repository-relative `rela_path` at `entry_index` |
| 100 | + /// (for accessing `entry` and surrounding in the complete list of `entries`). |
| 101 | + fn visit_entry( |
| 102 | + &mut self, |
| 103 | + entries: &'index [gix_index::Entry], |
| 104 | + entry: &'index gix_index::Entry, |
| 105 | + entry_index: usize, |
| 106 | + rela_path: &'index BStr, |
| 107 | + status: EntryStatus<Self::ContentChange, Self::SubmoduleStatus>, |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
| 111 | +pub use types::{Entry, Options, VisitEntry}; |
| 112 | + |
| 113 | +#[allow(dead_code, unused_variables)] |
| 114 | +pub(super) mod function { |
| 115 | + use crate::index_as_worktree::traits::{CompareBlobs, SubmoduleStatus}; |
| 116 | + use crate::index_as_worktree::{Context, Error, Outcome, VisitEntry}; |
| 117 | + use crate::index_as_worktree_with_renames::Options; |
| 118 | + use std::path::Path; |
| 119 | + |
| 120 | + /// Similar to [`index_as_worktree(…)`](crate::index_as_worktree()), except that it will automatically |
| 121 | + /// track renames if enabled, while additionally providing information about untracked files |
| 122 | + /// (or more, depending on the configuration). |
| 123 | + #[allow(clippy::too_many_arguments)] |
| 124 | + pub fn index_as_worktree_with_renames<'index, T, U, Find, E>( |
| 125 | + index: &'index gix_index::State, |
| 126 | + worktree: &Path, |
| 127 | + collector: &mut impl VisitEntry<'index, ContentChange = T, SubmoduleStatus = U>, |
| 128 | + compare: impl CompareBlobs<Output = T> + Send + Clone, |
| 129 | + submodule: impl SubmoduleStatus<Output = U, Error = E> + Send + Clone, |
| 130 | + objects: Find, |
| 131 | + progress: &mut dyn gix_features::progress::Progress, |
| 132 | + Context { |
| 133 | + pathspec, |
| 134 | + stack, |
| 135 | + filter, |
| 136 | + should_interrupt, |
| 137 | + }: Context<'_>, |
| 138 | + options: Options, |
| 139 | + ) -> Result<Outcome, Error> |
| 140 | + where |
| 141 | + T: Send, |
| 142 | + U: Send, |
| 143 | + E: std::error::Error + Send + Sync + 'static, |
| 144 | + Find: gix_object::Find + Send + Clone, |
| 145 | + { |
| 146 | + todo!() |
| 147 | + } |
| 148 | +} |
0 commit comments