仅在Mercurial有更改时运行提交

2 投票
2 回答
669 浏览
提问于 2025-04-17 15:00

我遇到的问题和一个提问者一样:如何在没有错误的情况下提交空的git? 基本上,我需要在我的代码库有变化时才运行hg commit。我使用fabric来执行提交,所以如果没有变化,它会输出一个很糟糕的错误信息。

local() encountered an error (return code 1) while executing 'hg commit...'

这是之前讨论中的一个回答:

git add -A
git diff --quiet --exit-code --cached || git commit -m 'bla'

这个方法适用于git,但我用的是Mercurial。我不知道在Mercurial中该怎么做。

2 个回答

0

根据你在问题中提到的链接,Tyler Eaves的回答,如果你不熟悉Fabric,可以试试下面这个方法:

with hide('warnings'):
  with settings(warn_only=True):
    run('hg commit -q ...')

这个提交(commit),如果有的话,会很安静,也就是说如果没有文件被提交,它不会有任何提示。如果有文件被提交,它也不会提示,这可能不是你想要的结果哦 :)

2

所以,正如koopajah和Kent在评论中提到的,我可以使用hg statushg diff的输出结果来查看是否有任何更改。

Fabric让我可以通过设置capture=True这个选项来读取输出。

if local('hg status', capture=True):
    local('hg commit')

这很简单明了。

撰写回答