【发布时间】:2021-12-08 08:39:42
【问题描述】:
我目前正在使用 Git 的 pre-push 钩子来运行 PHP CS Fixer,但我正在寻找一种更简洁的方法。我不知道如何传递 GNU Make 命令的脚本退出代码并传递给 Git 挂钩脚本。
文件设置
pre-push:
#!/bin/bash
output=$(make run-cs-fixer)
exitCode="${output##*$'\n'}" # Gets last line printed from cs-fixer.sh
# Use exitCode to continue or halt push
# Exit code from cs-fixer.sh cannot be obtained here. Make seems to produce its own exit code.
Makefile:
run-cs-fixer:
@-cd docker-compose exec -T workspace bash ./cs-fixer.sh
# echo $? gives me the exit code from cs-fixer.sh, but it's not available outside of this area, even if exited with it.
cs-fixer.sh:
#!/bin/bash
./vendor/bin/php-cs-fixer fix...
exitCode=$?
echo $exitCode # This is the exit code I need
所以,它是:git push... > pre-push > Makefile > cs-fixer 生成需要在 pre-push 中可用的退出代码。
如您所见,我从cs-fixer.sh 手动打印退出代码,以便在使用output=$(make run-cs-fixer) 在pre-push 中捕获此脚本的整个输出时可以提取退出代码。我只是不知道如何自然地传递现有代码。
Make 似乎启动了一个新的 shell 来运行它的命令,所以这似乎是问题之一,但无法让 .ONESHELL 工作。我能够确认我可以使用 Make 命令指令(在 run-cs-fixer: 下方)回显所需的退出代码,但在 pre-push 中仍然不可用。
【问题讨论】:
-
您确定您的
pre-push脚本正在bash 中运行吗?您正在使用在 POSIX shell 中不起作用的特定于 bash 的功能。我认为你应该在运行 make 后添加一个echo "$output"并查看它的值是什么。从这里的代码中,我看不出为什么它不应该包含退出代码。 -
bash 中的退出代码将在 make 命令后直接以
$?的形式提供 -
@MadScientist 是的,这是 bash(更新后的帖子)。
echo "$output"给了我整个 CLI 输出,而不是退出代码。 -
@AnthonySottile 我厌倦了,并且所需的代码在那里可用,但是一旦 make 命令完成执行,即使我
exit使用该代码也是如此。更新帖子。 -
我无法解释您如何看到
./vendor/bin/php-cs-fixer脚本的输出,但看不到echo $exitCode命令的输出,除非问题中的示例中的...以某种方式隐藏该脚本可以提前退出的一种方式。这个环境一定有一些你没有告诉我们的东西。
标签: makefile gnu-make githooks exit-code php-cs-fixer