pub struct File { /* private fields */ }
Expand description
An index file whose state was read from a file on disk.
Implementations§
source§impl File
impl File
Consumption
sourcepub fn into_parts(self) -> (State, PathBuf)
pub fn into_parts(self) -> (State, PathBuf)
Take all non-copy parts of the index.
source§impl File
impl File
Access
sourcepub fn path(&self) -> &Path
pub fn path(&self) -> &Path
The path from which the index was read or to which it is supposed to be written when used with File::from_state()
.
source§impl File
impl File
Initialization
sourcepub fn at_or_default(
path: impl Into<PathBuf>,
object_hash: Kind,
skip_hash: bool,
options: Options
) -> Result<Self, Error>
pub fn at_or_default( path: impl Into<PathBuf>, object_hash: Kind, skip_hash: bool, options: Options ) -> Result<Self, Error>
Try to open the index file at path
with options
, assuming object_hash
is used throughout the file, or create a new
index that merely exists in memory and is empty. skip_hash
will increase the performance by a factor of 2, at the cost of
possibly not detecting corruption.
Note that the path
will not be written if it doesn’t exist.
sourcepub fn at(
path: impl Into<PathBuf>,
object_hash: Kind,
skip_hash: bool,
options: Options
) -> Result<Self, Error>
pub fn at( path: impl Into<PathBuf>, object_hash: Kind, skip_hash: bool, options: Options ) -> Result<Self, Error>
Open an index file at path
with options
, assuming object_hash
is used throughout the file. If skip_hash
is true
,
we will not get or compare the checksum of the index at all, which generally increases performance of this method by a factor
of 2 or more.
Note that the verification of the file hash depends on options
, and even then it’s performed after the file was read and not
before it is read. That way, invalid files would see a more descriptive error message as we try to parse them.
sourcepub fn from_state(state: State, path: impl Into<PathBuf>) -> Self
pub fn from_state(state: State, path: impl Into<PathBuf>) -> Self
Consume state
and pretend it was read from path
, setting our checksum to null
.
File
instances created like that should be written to disk to set the correct checksum via [File::write()]
.
source§impl File
impl File
sourcepub fn verify_integrity(&self) -> Result<(), Error>
pub fn verify_integrity(&self) -> Result<(), Error>
Verify the integrity of the index to assure its consistency.
Methods from Deref<Target = State>§
sourcepub fn version(&self) -> Version
pub fn version(&self) -> Version
Return the version used to store this state’s information on disk.
sourcepub fn timestamp(&self) -> FileTime
pub fn timestamp(&self) -> FileTime
Returns time at which the state was created, indicating its freshness compared to other files on disk.
sourcepub fn set_timestamp(&mut self, timestamp: FileTime)
pub fn set_timestamp(&mut self, timestamp: FileTime)
Updates the timestamp of this state, indicating its freshness compared to other files on disk.
Be careful about using this as setting a timestamp without correctly updating the index will cause (file system) race conditions see racy-git.txt in the git documentation for more details.
sourcepub fn object_hash(&self) -> Kind
pub fn object_hash(&self) -> Kind
Return the kind of hashes used in this instance.
sourcepub fn path_backing(&self) -> &PathStorageRef
pub fn path_backing(&self) -> &PathStorageRef
Return our path backing, the place which keeps all paths one after another, with entries storing only the range to access them.
sourcepub fn entries_with_paths_by_filter_map<'a, T>(
&'a self,
filter_map: impl FnMut(&'a BStr, &Entry) -> Option<T> + 'a
) -> impl Iterator<Item = (&'a BStr, T)> + 'a
pub fn entries_with_paths_by_filter_map<'a, T>( &'a self, filter_map: impl FnMut(&'a BStr, &Entry) -> Option<T> + 'a ) -> impl Iterator<Item = (&'a BStr, T)> + 'a
Runs filter_map
on all entries, returning an iterator over all paths along with the result of filter_map
.
sourcepub fn entries_mut_with_paths_in<'state, 'backing>(
&'state mut self,
backing: &'backing PathStorageRef
) -> impl Iterator<Item = (&'state mut Entry, &'backing BStr)>
pub fn entries_mut_with_paths_in<'state, 'backing>( &'state mut self, backing: &'backing PathStorageRef ) -> impl Iterator<Item = (&'state mut Entry, &'backing BStr)>
Return mutable entries along with their path, as obtained from backing
.
sourcepub fn entry_index_by_path_and_stage(
&self,
path: &BStr,
stage: Stage
) -> Option<usize>
pub fn entry_index_by_path_and_stage( &self, path: &BStr, stage: Stage ) -> Option<usize>
Find the entry index in entries()
matching the given repository-relative
path
and stage
, or None
.
Use the index for accessing multiple stages if they exists, but at least the single matching entry.
sourcepub fn entry_index_by_path_and_stage_icase(
&self,
path: &BStr,
stage: Stage,
ignore_case: bool
) -> Option<usize>
pub fn entry_index_by_path_and_stage_icase( &self, path: &BStr, stage: Stage, ignore_case: bool ) -> Option<usize>
Find the entry index in entries()
matching the given repository-relative
path
and stage
, or None
.
If ignore_case
is true
, a case-insensitive (ASCII-folding only) search will be performed.
Note that if there are ambiguities, like x
and X
being present in the index, any of these will be returned,
deterministically.
Use the index for accessing multiple stages if they exists, but at least the single matching entry.
sourcepub fn entry_index_by_path_and_stage_bounded(
&self,
path: &BStr,
stage: Stage,
upper_bound: usize
) -> Option<usize>
pub fn entry_index_by_path_and_stage_bounded( &self, path: &BStr, stage: Stage, upper_bound: usize ) -> Option<usize>
Find the entry index in entries()[..upper_bound]
matching the given repository-relative
path
and stage
, or None
.
Use the index for accessing multiple stages if they exists, but at least the single matching entry.
Panics
If upper_bound
is out of bounds of our entries array.
sourcepub fn entry_by_path_and_stage(
&self,
path: &BStr,
stage: Stage
) -> Option<&Entry>
pub fn entry_by_path_and_stage( &self, path: &BStr, stage: Stage ) -> Option<&Entry>
Like entry_index_by_path_and_stage()
,
but returns the entry instead of the index.
sourcepub fn entry_by_path_and_stage_icase(
&self,
path: &BStr,
stage: Stage,
ignore_case: bool
) -> Option<&Entry>
pub fn entry_by_path_and_stage_icase( &self, path: &BStr, stage: Stage, ignore_case: bool ) -> Option<&Entry>
Like entry_index_by_path_and_stage_icase()
,
but returns the entry instead of the index.
sourcepub fn directory_kind_by_path_icase(
&self,
path: &BStr,
ignore_case: bool
) -> Option<DirectoryKind>
pub fn directory_kind_by_path_icase( &self, path: &BStr, ignore_case: bool ) -> Option<DirectoryKind>
Return the kind of directory that path
represents, or None
if the path is not a directory, or not
tracked in this index in any other way.
Note that we will not match path
, like a/b
, to a submodule or sparse directory at a
, which means
that path
should be grown one component at a time in order to find the relevant entries.
If ignore_case
is true
, a case-insensitive (ASCII-folding only) search will be performed.
Deviation
We allow conflicting entries to serve as indicator for an inferred directory, whereas git
only looks
at stage 0.
sourcepub fn entry_by_path(&self, path: &BStr) -> Option<&Entry>
pub fn entry_by_path(&self, path: &BStr) -> Option<&Entry>
Return the entry at path
that is either at stage 0, or at stage 2 (ours) in case of a merge conflict.
Using this method is more efficient in comparison to doing two searches, one for stage 0 and one for stage 2.
sourcepub fn entry_by_path_icase(
&self,
path: &BStr,
ignore_case: bool
) -> Option<&Entry>
pub fn entry_by_path_icase( &self, path: &BStr, ignore_case: bool ) -> Option<&Entry>
Return the entry at path
that is either at stage 0, or at stage 2 (ours) in case of a merge conflict.
If ignore_case
is true
, a case-insensitive (ASCII-folding only) search will be performed.
Using this method is more efficient in comparison to doing two searches, one for stage 0 and one for stage 2.
Note that if there are ambiguities, like x
and X
being present in the index, any of these will be returned,
deterministically.
sourcepub fn prefixed_entries(&self, prefix: &BStr) -> Option<&[Entry]>
pub fn prefixed_entries(&self, prefix: &BStr) -> Option<&[Entry]>
Return the slice of entries which all share the same prefix
, or None
if there isn’t a single such entry.
If prefix
is empty, all entries are returned.
sourcepub fn prefixed_entries_range(&self, prefix: &BStr) -> Option<Range<usize>>
pub fn prefixed_entries_range(&self, prefix: &BStr) -> Option<Range<usize>>
Return the range of entries which all share the same prefix
, or None
if there isn’t a single such entry.
If prefix
is empty, the range will include all entries.
sourcepub fn prefixed_entries_range_icase(
&self,
prefix: &BStr,
ignore_case: bool
) -> Option<Range<usize>>
pub fn prefixed_entries_range_icase( &self, prefix: &BStr, ignore_case: bool ) -> Option<Range<usize>>
Return the range of entries which all share the same prefix
, or None
if there isn’t a single such entry.
If ignore_case
is true
, a case-insensitive (ASCII-folding only) search will be performed. Otherwise
the search is case-sensitive.
If prefix
is empty, the range will include all entries.
sourcepub fn entry(&self, idx: usize) -> &Entry
pub fn entry(&self, idx: usize) -> &Entry
Return the entry at idx
or panic if the index is out of bounds.
The idx
is typically returned by entry_by_path_and_stage()
.
sourcepub fn is_sparse(&self) -> bool
pub fn is_sparse(&self) -> bool
Returns a boolean value indicating whether the index is sparse or not.
An index is sparse if it contains at least one Mode::DIR
entry.
sourcepub fn entry_range(&self, path: &BStr) -> Option<Range<usize>>
pub fn entry_range(&self, path: &BStr) -> Option<Range<usize>>
Return the range of entries that exactly match the given path
, in all available stages, or None
if no entry with such
path exists.
The range can be used to access the respective entries via entries()
or `entries_mut().
sourcepub fn return_path_backing(&mut self, backing: PathStorage)
pub fn return_path_backing(&mut self, backing: PathStorage)
After usage of the storage obtained by take_path_backing()
, return it here.
Note that it must not be empty.
sourcepub fn entries_mut(&mut self) -> &mut [Entry]
pub fn entries_mut(&mut self) -> &mut [Entry]
Return mutable entries in a slice.
sourcepub fn entries_mut_and_pathbacking(&mut self) -> (&mut [Entry], &PathStorageRef)
pub fn entries_mut_and_pathbacking(&mut self) -> (&mut [Entry], &PathStorageRef)
Return a writable slice to entries and read-access to their path storage at the same time.
sourcepub fn entries_mut_with_paths(
&mut self
) -> impl Iterator<Item = (&mut Entry, &BStr)>
pub fn entries_mut_with_paths( &mut self ) -> impl Iterator<Item = (&mut Entry, &BStr)>
Return mutable entries along with their paths in an iterator.
sourcepub fn take_path_backing(&mut self) -> PathStorage
pub fn take_path_backing(&mut self) -> PathStorage
Sometimes it’s needed to remove the path backing to allow certain mutation to happen in the state while supporting reading the entry’s path.
sourcepub fn entry_mut_by_path_and_stage(
&mut self,
path: &BStr,
stage: Stage
) -> Option<&mut Entry>
pub fn entry_mut_by_path_and_stage( &mut self, path: &BStr, stage: Stage ) -> Option<&mut Entry>
Like entry_index_by_path_and_stage()
,
but returns the mutable entry instead of the index.
sourcepub fn dangerously_push_entry(
&mut self,
stat: Stat,
id: ObjectId,
flags: Flags,
mode: Mode,
path: &BStr
)
pub fn dangerously_push_entry( &mut self, stat: Stat, id: ObjectId, flags: Flags, mode: Mode, path: &BStr )
Push a new entry containing stat
, id
, flags
and mode
and path
to the end of our storage, without performing
any sanity checks. This means it’s possible to push a new entry to the same path on the same stage and even after sorting
the entries lookups may still return the wrong one of them unless the correct binary search criteria is chosen.
Note that this is likely to break invariants that will prevent further lookups by path unless
entry_index_by_path_and_stage_bounded()
is used with
the upper_bound
being the amount of entries before the first call to this method.
Alternatively, make sure to call sort_entries()
before entry lookup by path to restore
the invariant.
sourcepub fn sort_entries(&mut self)
pub fn sort_entries(&mut self)
Unconditionally sort entries as needed to perform lookups quickly.
sourcepub fn sort_entries_by(
&mut self,
compare: impl FnMut(&Entry, &Entry) -> Ordering
)
pub fn sort_entries_by( &mut self, compare: impl FnMut(&Entry, &Entry) -> Ordering )
Similar to sort_entries()
, but applies compare
after comparing
by path and stage as a third criteria.
sourcepub fn remove_entries(
&mut self,
should_remove: impl FnMut(usize, &BStr, &mut Entry) -> bool
)
pub fn remove_entries( &mut self, should_remove: impl FnMut(usize, &BStr, &mut Entry) -> bool )
Physically remove all entries for which should_remove(idx, path, entry)
returns true
, traversing them from first to last.
Note that the memory used for the removed entries paths is not freed, as it’s append-only.
Performance
To implement this operation typically, one would rather add entry::Flags::REMOVE to each entry to remove them when writing the index.
sourcepub fn resolve_undo(&self) -> Option<&Vec<ResolvePath>>
pub fn resolve_undo(&self) -> Option<&Vec<ResolvePath>>
Obtain the resolve-undo extension.
sourcepub fn untracked(&self) -> Option<&UntrackedCache>
pub fn untracked(&self) -> Option<&UntrackedCache>
Obtain the untracked extension.
sourcepub fn fs_monitor(&self) -> Option<&FsMonitor>
pub fn fs_monitor(&self) -> Option<&FsMonitor>
Obtain the fsmonitor extension.
sourcepub fn verify_entries(&self) -> Result<(), Error>
pub fn verify_entries(&self) -> Result<(), Error>
Assure our entries are consistent.