koj*_*iro 73 python git fabric
我正在尝试编写一个可以执行的结构脚本git commit; 但是,如果没有提交,git退出状态为1.部署脚本将其视为不成功,并退出.我确实想要检测实际的失败提交,所以我不能仅仅给织物一个忽略git commit失败的问题.如何允许忽略空提交失败,以便部署可以继续,但仍然捕获真正提交失败时导致的错误?
def commit():
local("git add -p && git commit")
Run Code Online (Sandbox Code Playgroud)
Tob*_*obi 120
通过检查git diff的退出代码事先赶上这个条件?
例如(在shell中):
git add -A
git diff-index --quiet HEAD || git commit -m 'bla'
Run Code Online (Sandbox Code Playgroud)
编辑:修正git diff命令根据霍尔格的评论.
Sve*_*ach 55
从git commit手册页:
--allow-empty
Usually recording a commit that has the exact same tree as its
sole parent commit is a mistake, and the command prevents you
from making such a commit. This option bypassesthe safety, and
is primarily for use by foreign SCM interface scripts.
Run Code Online (Sandbox Code Playgroud)
Nin*_*ham 20
只是用明确的声明扩展了Tobi & Holger的答案if。
git add -A
if ! git diff-index --quiet HEAD; then
git commit -m "Message here"
git push origin main
fi
Run Code Online (Sandbox Code Playgroud)
让我们稍微解释一下。
git add -A:暂存您的更改(下一步需要)
git diff-index --quiet HEAD会将您的分阶段更改与 HEAD 进行比较。
--quiet很重要,因为它意味着--exit-code“如果存在差异,则程序以代码 1 退出,而 0 表示没有差异”。
请参阅--安静。
with settings(warn_only=True):
run('git commit ...')
Run Code Online (Sandbox Code Playgroud)
这会导致结构忽略失败.具有不创建空提交的优点.
您可以将其包装在一个额外的层中with hide('warnings'):以完全抑制输出,否则您将在结构输出中获得提交失败的注释(但fabfile继续执行).