Struct devx_pre_commit::PreCommitContext [−][src]
pub struct PreCommitContext { /* fields omitted */ }
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
impl PreCommitContext
[src]
impl PreCommitContext
[src]pub fn from_git_diff(project_root: impl Into<PathBuf>) -> Result<Self>
[src]
pub fn from_git_diff(project_root: impl Into<PathBuf>) -> Result<Self>
[src]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
.
pub fn staged_files(&self) -> impl Iterator<Item = &Path>
[src]
pub fn staged_files(&self) -> impl Iterator<Item = &Path>
[src]Returns an iterator over all the files staged for the commit.
pub fn retain_staged_files(&mut self, f: impl FnMut(&Path) -> bool)
[src]
pub fn retain_staged_files(&mut self, f: impl FnMut(&Path) -> bool)
[src]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
pub fn touched_crates(&self) -> Vec<String>
[src]
pub fn touched_crates(&self) -> Vec<String>
[src]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.
pub fn staged_rust_files(&self) -> impl Iterator<Item = &Path>
[src]
pub fn staged_rust_files(&self) -> impl Iterator<Item = &Path>
[src]Returns an iterator over all staged files with .rs
extension.
pub fn rustfmt(&self) -> Result<()>
[src]
pub fn rustfmt(&self) -> Result<()>
[src]Runs cargo fmt
against the Self::touched_crates()
pub fn stage_new_changes(&self) -> Result<()>
[src]
pub fn stage_new_changes(&self) -> Result<()>
[src]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()
)