6 个版本 (重大更新)
0.5.0 | 2021年6月4日 |
---|---|
0.4.0 | 2021年5月28日 |
0.3.1 | 2020年9月22日 |
0.3.0 | 2020年8月29日 |
0.1.0 | 2020年8月16日 |
#1303 在 开发工具 中
每月下载量:12,394
38KB
543 行
devx-pre-commit
devx-pre-commit
提供创建 git 预提交钩子的实用工具。
特别地,它提供了以下方便的 API:
- 在具有待处理 Rust 源文件的 crate 中高效运行
rustfmt
- 将当前二进制文件安装到
.git/hooks/pre-commit
有关更多信息,请参阅 crate 级别文档。
lib.rs
:
devx-pre-commit
提供创建 git 预提交钩子的实用工具。
特别地,它提供了以下方便的 API:
- 在具有待处理 Rust 源文件的 crate 中高效运行
rustfmt
- 将当前二进制文件安装到
.git/hooks/pre-commit
这个 crate 仅适用于开发环境,最好与 cargo-xtask
配置一起使用。通过在 xtask
二进制 crate 中添加以下代码,您将能够运行以下命令来安装 git 预提交钩子,并无需再次手动运行 cargo fmt
cargo xtask install-pre-commit-hook
ℹ️ 注意:此操作假设在
.cargo/config
中存在别名。[alias] xtask = "run --package xtask --bin xtask --"
示例开发 CLI
use devx_pre_commit::{PreCommitContext, locate_project_root};
use anyhow::Result;
use std::{ffi::OsStr, path::PathBuf};
fn run_hook() -> Result<()> {
let mut ctx = PreCommitContext::from_git_diff(locate_project_root()?)?;
// Optionally filter out the files you don't want to format
ctx.retain_staged_files(|path| {
path.components().all(|it| it.as_os_str() != OsStr::new("generated"))
});
// Run `cargo fmt` against the crates with staged rust source files
ctx.rustfmt()?;
// Stage all the changes potenitally introduced by rustfmt
// It is super-important to call this method at the end of the hook
ctx.stage_new_changes()?;
Ok(())
}
fn main() -> Result<()> {
if let Some(true) = std::env::args().next().map(|it| it.contains("pre-commit")) {
return run_hook();
}
match std::env::args().nth(1).expect("No args").as_str() {
"install-pre-commit-hook" => {
devx_pre_commit::install_self_as_hook(&locate_project_root()?)?;
}
_ => {
eprintln!("Hi, this is a dev cli, here are the available commands...");
}
}
Ok(())
}
依赖
~300KB