Struct PreCommitContext

Source
pub struct PreCommitContext { /* private fields */ }
Expand description

Represents the API entrypoint of the git pre-commit hook. It carries the list of the staged files and the project root path. Note that staged file paths are all relative to the project root path.

Implementations§

Source§

impl PreCommitContext

Source

pub fn from_git_diff(project_root: impl Into<PathBuf>) -> Result<Self>

Creates the git pre-commit context acquiring the staged files via running git diff in project_root. The project_root is expected to contain the .git dir (see locate_project_root function for more on that).

The staged files are stored in PreCommitContext as paths relative to project_root.

Source

pub fn staged_files(&self) -> impl Iterator<Item = &Path>

Returns an iterator over all the files staged for the commit.

Source

pub fn retain_staged_files(&mut self, f: impl FnMut(&Path) -> bool)

Accepts a function predicate that accepts a relative path to the staged file and returns false if the given file should be removed from this PreCommitContext

Source

pub fn touched_crates(&self) -> HashSet<String>

Returns the names of the crates that contain Self::staged_rust_files().

Warning: this heuristically looks for Cargo.toml files and searches for name = " substring in them to get the crate name (i.e. it doesn’t really parse them properly, but this works 99% of the time and lets us save on a full-fledged toml parser dependency). This heuristic may be relaxed in the future, and it shouldn’t be considered a breaking change.

Source

pub fn staged_rust_files(&self) -> impl Iterator<Item = &Path>

Returns an iterator over all staged files with .rs extension.

Source

pub fn rustfmt(&self) -> Result<()>

Runs cargo fmt against the Self::touched_crates()

Source

pub fn stage_new_changes(&self) -> Result<()>

Pushes the changes introduced to staged files in the working tree to the git index. It is important to call this function once you’ve modified some staged files (e.g. via Self::rustfmt())

Auto Trait Implementations§

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, 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.