Skip to main content

IndexFile

Struct IndexFile 

Source
pub struct IndexFile<'repo> { /* private fields */ }
Expand description

Load and persist the Git index for one repository.

§Examples

use grit_lib::index::IndexLoadOptions;
use grit_lib::repo::{InitOptions, Repository};

let dir = tempfile::tempdir()?;
let (repo, _) = Repository::init(dir.path(), &InitOptions::default())?;
let handle = repo.index();
let index = handle.load(&IndexLoadOptions::default())?;
assert!(index.entries().is_empty());

Implementations§

Source§

impl<'repo> IndexFile<'repo>

Source

pub fn new(repo: &'repo Repository) -> Self

Bind index operations to repo.

Source

pub fn repository(&self) -> &'repo Repository

Underlying repository.

Source

pub fn path(&self) -> PathBuf

Path to the index file this repository uses by default.

Source

pub fn path_for_env(&self) -> Result<PathBuf>

Resolve the index path honoring GIT_INDEX_FILE when set.

§Errors

Propagates configuration errors.

Source

pub fn load(&self, options: &IndexLoadOptions<'_>) -> Result<Index>

Load the index, by default from the path selected by GIT_INDEX_FILE / the default .git/index, resolving a split index and expanding sparse-directory placeholders.

A missing index file loads as an empty index.

§Errors

Propagates index parse, sparse, and split-index errors.

Source

pub fn write( &self, index: &mut Index, options: &IndexWriteOptions<'_>, ) -> Result<()>

Write index, by default to the path selected by GIT_INDEX_FILE / the default .git/index.

Collapses skip-worktree subtrees into sparse-directory placeholders when the sparse index is enabled, writes a split index when IndexWriteOptions::split or core.splitIndex asks for one, and runs the post-index-change hook.

§Errors

Propagates index serialization and I/O errors.

Source

pub fn apply_sparse_checkout(&self, index: &mut Index) -> Result<Vec<String>>

Mark stage-0 entries of index that the sparse-checkout patterns exclude as skip-worktree, as Git does after building an index from a tree (read-tree, branch checkout, fast-forward merge).

A no-op when core.sparseCheckout is false or info/sparse-checkout is missing. The index is bumped to version 3 when any entry is marked. On-disk directories in the work tree can match directory-only non-cone patterns.

Returns cone-parse warnings when core.sparseCheckoutCone is set but the file is not a valid cone definition (matching then falls back to non-cone rules).

§Errors

Propagates config load failures.

Source

pub fn begin_update(&self) -> Result<IndexLock>

Acquire a lock on the index path selected by GIT_INDEX_FILE / the default .git/index.

Source

pub fn commit_update( &self, lock: &mut IndexLock, index: &mut Index, updated_workdir: bool, ) -> Result<()>

Write index using a lock from Self::begin_update.

Source

pub fn try_write(&self, index: &mut Index) -> Result<bool>

Persist the index when the lock can be acquired (Git repo_update_index_if_able), using the path selected by GIT_INDEX_FILE when set.

Source

pub fn stage( &self, options: &StageOptions, progress: &mut dyn ProgressSink, cancel: &CancelToken, ) -> Result<StageOutcome>

Stage paths matching StageOptions into the index.

§Errors

Propagates index, worktree, and pathspec failures; returns crate::error::Error::Cancelled when cancel is set.

Source

pub fn stage_for_commit( &self, progress: &mut dyn ProgressSink, cancel: &CancelToken, ) -> Result<StageOutcome>

Stage worktree changes for commit auto-staging.

§Errors

Same as Self::stage for the implicit all-paths staging pass.

Source

pub fn stage_on( &self, index: &mut Index, options: &StageOptions, progress: &mut dyn ProgressSink, cancel: &CancelToken, ) -> Result<StageOutcome>

Stage paths into a caller-owned index without writing it to disk.

§Errors

Same as Self::stage, without persisting index.

Source

pub fn stage_worktree_paths( &self, pathspecs: &[String], ) -> Result<StageWorktreeChangesOutcome>

Stage paths matching pathspecs without a full status pass.

§Errors

Propagates index refresh and worktree scan failures.

Source

pub fn stage_tracked_modifications( &self, work_tree: &Path, ) -> Result<StageTrackedSummary>

Refresh tracked modifications from the worktree into the index.

§Errors

Propagates index load, stat refresh, and worktree scan failures.

Source

pub fn stage_tracked_modifications_on( &self, work_tree: &Path, index_path: &Path, index: &mut Index, options: &StageOptions, ) -> Result<StageTrackedSummary>

Like Self::stage_tracked_modifications, updating a caller-owned index only.

§Errors

Same as Self::stage_tracked_modifications.

Source

pub fn stage_hunks( &self, side: DiffSide, selection: &HunkSelection, pathspecs: &[String], diff_options: DiffOptions, dry_run: bool, cancel: &CancelToken, ) -> Result<StageHunksOutcome>

Stage structured diff hunks by stable id.

§Errors

Propagates diff, index, and hunk-resolution failures.

Source

pub fn restore_hunks( &self, selection: &HunkSelection, pathspecs: &[String], restore_staged: bool, restore_worktree: bool, dry_run: bool, ) -> Result<RestoreHunksOutcome>

Discard or unstage hunks in selection.

§Errors

Propagates diff and index update failures.

Source

pub fn hunk_diff_options(&self) -> Result<DiffOptions>

Diff options for hunk id resolution (same boundaries as grit diff --json).

§Errors

Propagates config and diff-option construction failures.

Source

pub fn unstaged_file_diffs( &self, pathspecs: &[String], ) -> Result<Vec<(FileDiff, Vec<StructuredHunk>)>>

Unstaged file diffs with staged peer hunks on the same path.

§Errors

Propagates index load and diff failures.

Source

pub fn list_unstaged_hunk_ids( &self, pathspecs: &[String], diff_options: DiffOptions, ) -> Result<BTreeMap<String, Vec<HunkId>>>

Unstaged hunk ids per path (same rules as grit diff --json on --unstaged).

§Errors

Propagates diff failures for the given pathspecs.

Source

pub fn resolve_hunk_selection( &self, side: DiffSide, selection: &HunkSelection, pathspecs: &[String], diff_options: DiffOptions, ) -> Result<Vec<FilePatch>>

Resolve a hunk selection to index-ready patches.

§Errors

Propagates diff and selection validation failures.

Source

pub fn pathspec_matches_known(&self, resolved: &str) -> Result<bool>

Whether resolved matches a known path in this repository.

§Errors

Propagates index and ref resolution failures.

Source

pub fn ensure_pathspec_matches_known( &self, display_spec: &str, resolved: &str, ) -> Result<()>

Require that resolved matches a known path, else return a pathspec error.

§Errors

Returns a pathspec error when the path is unknown.

Trait Implementations§

Source§

impl<'repo> Clone for IndexFile<'repo>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'repo> Copy for IndexFile<'repo>

Source§

impl<'repo> Debug for IndexFile<'repo>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'repo> !RefUnwindSafe for IndexFile<'repo>

§

impl<'repo> !UnwindSafe for IndexFile<'repo>

§

impl<'repo> Forget for IndexFile<'repo>

§

impl<'repo> Freeze for IndexFile<'repo>

§

impl<'repo> Send for IndexFile<'repo>

§

impl<'repo> Sync for IndexFile<'repo>

§

impl<'repo> Unpin for IndexFile<'repo>

§

impl<'repo> UnsafeUnpin for IndexFile<'repo>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.