Skip to content

Commit b86f51d

Browse files
committed
fix: only pre hook failure triggers procedure to stop
1 parent 11c63a7 commit b86f51d

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

docs/cn/hooks.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ lint:
5757

5858
你可以参阅 [just 手册](https://just.systems/man/en/introduction.html) 来查看更多高级功能。
5959

60+
## 失败的命令
61+
62+
对于 `pre` 钩子,如果钩子命令失败,hj 会阻止版本控制命令的执行。
63+
6064
## 与 Git 的兼容性
6165

6266
我们提供了运行 Git Hooks 相关的功能。在特定时期,hj 会自动运行:

justfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ test:
2929
install:
3030
cargo install --path .
3131

32+
post-commit:
33+
just --quiet install
34+
3235
doc:
3336
cd docs && pnpm install && pnpm run docs:dev
3437

src/hook.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,44 @@ pub(crate) fn check_just_installed() -> bool {
88
cmd!("just", "--version").run().is_ok()
99
}
1010

11-
pub(crate) fn run_hook(config: &AppConfig, script: String, hook_name: &str) -> anyhow::Result<()> {
11+
pub(crate) fn run_hook(
12+
config: &AppConfig,
13+
script: String,
14+
hook_name: &str,
15+
) -> anyhow::Result<bool> {
1216
if config.hooks.use_just && !check_just_installed() {
1317
step("Install just to run hooks");
1418
// TODO: provide installation instructions
1519
}
20+
21+
// Run git hook
1622
if Path::new(&format!(".git/hooks/{hook_name}")).exists()
1723
&& !config
1824
.hooks
1925
.ignore_git_hooks
2026
.contains(&hook_name.to_string())
2127
{
2228
step(&format!("Running {hook_name} hook"));
23-
cmd!("git", "hook", "run", hook_name).run()?;
29+
if let Err(e) = cmd!("git", "hook", "run", hook_name).run()
30+
&& hook_name.starts_with("pre-")
31+
{
32+
anyhow::bail!(
33+
"Git {} hook failed: {}. Aborting.",
34+
hook_name,
35+
e.to_string()
36+
)
37+
}
2438
}
39+
40+
// Run hj hook
2541
let (program, args): (&str, Vec<&str>) = script
2642
.split_once(' ')
2743
.map(|(p, a)| (p, a.trim().split(' ').collect()))
2844
.unwrap_or((&script, Vec::new()));
29-
cmd(program, &args).run()?;
30-
Ok(())
45+
if let Err(e) = cmd(program, &args).run()
46+
&& hook_name.starts_with("pre-")
47+
{
48+
anyhow::bail!("HJ {} hook failed: {}. Aborting.", hook_name, e.to_string());
49+
}
50+
Ok(true)
3151
}

0 commit comments

Comments
 (0)