如何使用 husky 在 git 预提交挂钩中使用带有返回码的 shell 函数?
我有一个由 husky 包管理的 githooks 项目。通常我很欣赏钩子的快速失败特性(不确定 set -e 是否像 shell 脚本中那样使用),但今天我在使用自己的自定义函数时遇到了问题。我正在尝试根据函数的返回值执行两件事之一:
#!/bin/sh
my_func() {
# some stuff
return command
}
if my_func;
then
echo "func returned 0"
else
echo "func returned non-zero"
fi
问题是一旦任何函数返回非零值,哈士奇就会退出:
husky - pre-commit hook exited with code 1 (error)
如何忽略/处理函数调用的非零返回?我不认为我想要某种类型的全局忽略,就像我说的那样,我通常欣赏这种针对未处理错误的快速失败行为。另外,我不认为我可以从函数返回切换到共享变量或其他东西,因为我正在使用这些函数调用执行很多异步操作,所以我需要它们的实际返回值。
I have a project with githooks managed by the husky package. Usually I appreciate the fail-fast nature of the hooks (unsure if set -e
is being used like in a shell script) but I ran into an issue today with my own custom function. I am trying to do one of two things depending on the return value of a function:
#!/bin/sh
my_func() {
# some stuff
return command
}
if my_func;
then
echo "func returned 0"
else
echo "func returned non-zero"
fi
The problem is as soon as any function returns a non-zero value husky quits out:
husky - pre-commit hook exited with code 1 (error)
How can I ignore/handle non-zero returns from function calls? I don't think I want some type of global ignore as like I said I usually appreciate this fail-fast behavior for unhandled errors. Also I don't think I can switch away from function returns to a shared variable or something because I am doing a lot of async stuff with these function calls so I need their actual return values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
语法不是
if [条件]
。它是if cmd
。 (在第一种情况下,命令只是带有多个参数的[
,最后一个参数是]
)。您需要做的就是:The syntax is not
if [ condition ]
. It isif cmd
. (In the first case, the command is simply[
with several arguments, the last of which is]
). All you need to do is: