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>
impl<'repo> IndexFile<'repo>
Sourcepub fn new(repo: &'repo Repository) -> Self
pub fn new(repo: &'repo Repository) -> Self
Bind index operations to repo.
Sourcepub fn repository(&self) -> &'repo Repository
pub fn repository(&self) -> &'repo Repository
Underlying repository.
Sourcepub fn path_for_env(&self) -> Result<PathBuf>
pub fn path_for_env(&self) -> Result<PathBuf>
Sourcepub fn load(&self, options: &IndexLoadOptions<'_>) -> Result<Index>
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.
Sourcepub fn write(
&self,
index: &mut Index,
options: &IndexWriteOptions<'_>,
) -> Result<()>
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.
Sourcepub fn apply_sparse_checkout(&self, index: &mut Index) -> Result<Vec<String>>
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.
Sourcepub fn begin_update(&self) -> Result<IndexLock>
pub fn begin_update(&self) -> Result<IndexLock>
Acquire a lock on the index path selected by GIT_INDEX_FILE / the default .git/index.
Sourcepub fn commit_update(
&self,
lock: &mut IndexLock,
index: &mut Index,
updated_workdir: bool,
) -> Result<()>
pub fn commit_update( &self, lock: &mut IndexLock, index: &mut Index, updated_workdir: bool, ) -> Result<()>
Write index using a lock from Self::begin_update.
Sourcepub fn try_write(&self, index: &mut Index) -> Result<bool>
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.
Sourcepub fn stage(
&self,
options: &StageOptions,
progress: &mut dyn ProgressSink,
cancel: &CancelToken,
) -> Result<StageOutcome>
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.
Sourcepub fn stage_for_commit(
&self,
progress: &mut dyn ProgressSink,
cancel: &CancelToken,
) -> Result<StageOutcome>
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.
Sourcepub fn stage_on(
&self,
index: &mut Index,
options: &StageOptions,
progress: &mut dyn ProgressSink,
cancel: &CancelToken,
) -> Result<StageOutcome>
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.
Sourcepub fn stage_worktree_paths(
&self,
pathspecs: &[String],
) -> Result<StageWorktreeChangesOutcome>
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.
Sourcepub fn stage_tracked_modifications(
&self,
work_tree: &Path,
) -> Result<StageTrackedSummary>
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.
Sourcepub fn stage_tracked_modifications_on(
&self,
work_tree: &Path,
index_path: &Path,
index: &mut Index,
options: &StageOptions,
) -> Result<StageTrackedSummary>
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.
Sourcepub fn stage_hunks(
&self,
side: DiffSide,
selection: &HunkSelection,
pathspecs: &[String],
diff_options: DiffOptions,
dry_run: bool,
cancel: &CancelToken,
) -> Result<StageHunksOutcome>
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.
Sourcepub fn restore_hunks(
&self,
selection: &HunkSelection,
pathspecs: &[String],
restore_staged: bool,
restore_worktree: bool,
dry_run: bool,
) -> Result<RestoreHunksOutcome>
pub fn restore_hunks( &self, selection: &HunkSelection, pathspecs: &[String], restore_staged: bool, restore_worktree: bool, dry_run: bool, ) -> Result<RestoreHunksOutcome>
Sourcepub fn hunk_diff_options(&self) -> Result<DiffOptions>
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.
Sourcepub fn unstaged_file_diffs(
&self,
pathspecs: &[String],
) -> Result<Vec<(FileDiff, Vec<StructuredHunk>)>>
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.
Sourcepub fn list_unstaged_hunk_ids(
&self,
pathspecs: &[String],
diff_options: DiffOptions,
) -> Result<BTreeMap<String, Vec<HunkId>>>
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.
Sourcepub fn resolve_hunk_selection(
&self,
side: DiffSide,
selection: &HunkSelection,
pathspecs: &[String],
diff_options: DiffOptions,
) -> Result<Vec<FilePatch>>
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.