如何git没有提供任何错误?

我试图写一个结构脚本,做一个git commit ; 但是,如果没有提交,git退出状态为1 。 部署脚本将其视为不成功,然后退出。 我确实想要检测到实际的提交失败,所以我不能仅仅为了git commit失败而让fabric被忽略。 我怎样才能允许空承诺失败被忽略,以便部署可以继续,但仍然发现真正的提交失败导致的错误?

 def commit(): local("git add -p && git commit") 

事先通过检查git diff的退出码来捕获这个条件?

例如(在shell中):

 git add -A git diff-index --quiet HEAD || git commit -m 'bla' 

编辑:根据Holger的评论修正了git diff命令。

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. 
 with settings(warn_only=True): run('git commit ...') 

这会导致结构忽略失败。 具有不创build空提交的优点。

你可以with hide('warnings'):来包装它with hide('warnings'):完全抑制输出,否则你会在结构输出中得到提交失败(但fabfile继续执行)的提示。

尝试/抓住宝贝!

 from fabric.api import local from fabric.colors import green def commit(message='updates'): try: local('git add .') local('git commit -m "' + message + '"') local('git push') print(green('Committed and pushed to git.', bold=False)) except: print(green('Done committing, likely nothing new to commit.', bold=False))