[英]Husky hook post-merge checking if any changes on package.json and execute npm install if detected changes
I want to the hook to do something like run NPM install after merge branch but only if package.json file is being modified我想在合并分支后执行 NPM install 之类的操作,但前提是 package.json 文件正在被修改
I'm not sure if I'm doing it correctly, please find my post-merge below我不确定我是否做得正确,请在下面找到我的合并后
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
differenceCode=$git diff HEAD^ HEAD --exit-code -- ./package.json
echo "test - $?"
if [ "$?" != 0 ]; then
npm install
fi
I can run git diff HEAD^ HEAD --exit-code --./package.json
from bash and also echo $?
我可以从 bash 运行
git diff HEAD^ HEAD --exit-code --./package.json
并echo $?
show me 1 when there are changes in package.json
else it return me 0.当
package.json
发生变化时显示 1,否则返回 0。
However when I included that command in the post-merge file, and trigger the post-merge event it show me:但是,当我将该命令包含在合并后文件中并触发合并后事件时,它会向我显示:
diff: unknown option -- exit-code
diff: Try 'diff --help' for more information.
husky - post-merge hook exited with code 2 (error)
Anyone can help me with this please.任何人都可以帮我解决这个问题。
You wrote:你写了:
differenceCode=$git diff HEAD^ HEAD --exit-code -- ./package.json
in your script.在你的脚本中。 (Why did you put a dollar sign
$
in front of git
?) (为什么在
git
前面放一个美元符号$
?)
This is the immediate source of your problem.这是您问题的直接根源。
$git
is an unset variable; $git
是一个未设置的变量; in sh
, unset variables expand to nothing;在
sh
中,未设置的变量扩展为空; so you are running the command:所以你正在运行命令:
diff HEAD^ HEAD --exit-code -- ./package.json
and not the command:而不是命令:
git diff HEAD^ HEAD --exit-code -- ./package.json
If you remove the stray $
sign, that problem will go away.如果您删除杂散的
$
符号,该问题将 go 消失。
Your subsequent:您的后续:
echo "test - $?"
will show the exit code from the command you ran, if it gets executed.如果它被执行,将显示您运行的命令的退出代码。 The fact that it doesn't get executed indicates that the line:
它没有被执行的事实表明该行:
. "$(dirname "$0")/_/husky.sh"
must have set the -e
option ("exit on error").必须设置
-e
选项(“出错时退出”)。 If it did that, you don't need a subsequent test.如果这样做了,则不需要后续测试。 But there is another error here:
但是这里还有另一个错误:
echo "test - $?"
if [ "$?" != 0 ]; then
npm install
fi
You would need to remove the echo
command, because echo
itself is a command, and as such, it sets the $?
您需要删除
echo
命令,因为echo
本身就是一个命令,因此它设置了$?
last-command exit status variable. last-command 退出状态变量。 So if
echo
succeeds—it almost always does—it will set $?
因此,如果
echo
成功(几乎总是如此),它将设置$?
to zero.为零。
If -e
is set, as it appears to be, we won't get this far if $?
如果设置了
-e
,看起来是这样,如果$?
is not zero.不为零。 Since
-e
is set, it's important not to allow the git diff
command to cause the script to exit early.由于设置了
-e
,因此不要让git diff
命令导致脚本提前退出,这一点很重要。 To fix that, replace the whole sequence with:要解决此问题,请将整个序列替换为:
if git diff HEAD^ HEAD --exit-code -- ./package.json; then
# ... code to handle a zero exit code here
else
# ... code to handle a nonzero exit here
fi
The test will now work even with -e
set.即使使用
-e
set,测试现在也可以工作。
You can simplify this a bit since you have nothing to do in the then
section by writing:你可以稍微简化一下,因为你在
then
部分中没有任何事情可以写:
if ! git diff HEAD^ HEAD --exit-code -- ./package.json; then
npm install
fi
and once you do that, you can simplify this one more step by writing:一旦你这样做了,你可以通过编写进一步简化这一步骤:
git diff HEAD^ HEAD --exit-code -- ./package.json || npm install
but the main things to do are:但主要要做的是:
$
;$
;git diff
command into the if
test.git diff
命令移动到if
测试中。 These are what's required;这些是必需的; everything else is optional.
其他一切都是可选的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.