Skip to main content

Index

Struct Index 

Source
pub struct Index;
Expand description

Index (staging area) write operations.

Implementations§

Source§

impl Index

Source

pub fn stage_path(repo: &Repository, path: impl AsRef<Path>) -> Result<()>

Stage a file path (equivalent to git add <path>).

Source

pub fn stage_paths<I, P>(repo: &Repository, paths: I) -> Result<()>
where I: IntoIterator<Item = P>, P: AsRef<Path>,

Stage every path in ONE git add.

Not a convenience wrapper over Self::stage_path — the number of git invocations is the point. Each one spawns a process and takes .git/index.lock for the duration, so staging N files as N commands is N process spawns and N lock cycles, every one of them a window in which any other git operation in the editor fails with “Unable to create index.lock: File exists” (reported 2026-08-16 while staging a visual-mode selection).

One command is also ATOMIC where the loop was not: a loop can fail partway and leave half the selection staged, which is why the magit layer had to model “3 of 5 staged” as an outcome at all.

Mirrors Self::unstage_paths, which has always been one command — it had to be, because a staged rename occupies two index entries that must reset together.

Source

pub fn unstage_path(repo: &Repository, path: impl AsRef<Path>) -> Result<()>

Unstage a file path (equivalent to git reset HEAD -- <path>).

Source

pub fn unstage_paths<I, P>(repo: &Repository, paths: I) -> Result<()>
where I: IntoIterator<Item = P>, P: AsRef<Path>,

Unstage every path in one git reset, which is what a RENAME requires.

A staged rename occupies TWO index entries — the new path added and the old one deleted — and resetting only the new one leaves D old still staged: a deletion the user never asked for, and one the next commit would record. Resetting both together returns the index to HEAD, leaving the rename visible in the worktree as a delete plus an untracked file, which is exactly how git reports an unstaged rename (it does not detect them).

Source

pub fn apply_patch( repo: &Repository, patch: &str, cached: bool, reverse: bool, ) -> Result<()>

MG.18a: apply a unified-diff patch to the index (cached) or the working tree, forward or reversed. This is the unit of partial staging — the caller synthesizes a patch containing exactly the hunks (or the rewritten hunk) it wants to move, and this applies it. Git has no “stage hunk N of path P” index operation; git add -p builds a patch and pipes it to git apply --cached, and so do we.

Caller intentcachedreverse
stage a hunktruefalse
unstage a hunktruetrue
discard a hunk from the worktreefalsetrue

git apply requires every context line to match the target exactly, which is the safety property we want: if the worktree moved under a stale buffer, this fails loudly instead of staging the wrong lines. Callers surface the error and refresh.

Replaces the former stage_hunk / unstage_hunk, which took a hunk_index they discarded and staged the whole file — a signature that promised precision the body did not deliver. See docs/dev/architecture/magit-hunk-staging.md.

Auto Trait Implementations§

§

impl Freeze for Index

§

impl RefUnwindSafe for Index

§

impl Send for Index

§

impl Sync for Index

§

impl Unpin for Index

§

impl UnsafeUnpin for Index

§

impl UnwindSafe for Index

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.