2

I have the same problem as a guy asking this question: How to git commit nothing without an error? Basically I need to run hg commit, only if there are any changes in my repository. I am using fabric to run the commit, so if there are no changes, it will output a nasty error.

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

This is the answer from aforementioned thread:

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

It works for git, however I use Merucrial. I have no idea how to do it in Mercurial.

Community
  • 1
  • 1
davekr
  • 2,266
  • 1
  • 26
  • 32
  • 1
    Can't you parse the result of `hg status` first to see if there are any changes? – koopajah Feb 06 '13 at 17:21
  • Or run `hg identify` and see if the changeset has a `+` after the changeset id? – icabod Feb 06 '13 at 17:24
  • 1
    can you read the output of other hg command? like hg st, hg diff? if they return empty, there is no changes – Kent Feb 06 '13 at 17:27
  • 1
    @koopajah I think that's maybe the only way how to do it. So your comment is nearly the answer. Thanks. – davekr Feb 07 '13 at 10:51

2 Answers2

2

So as koopajah and Kent sugested in comments, I can use the output of hg status or hg diff to see if there are any changes. Fabric alows me to read output with capture=True flag.

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

Pretty straightforward.

davekr
  • 2,266
  • 1
  • 26
  • 32
0

Going from Tyler Eaves' answer on the page you linked to in your question (and not knowing Fabric), could you do the following:

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

The commit, if there is one, will be quiet, so won't report if there are no files committed. It won't report if there are files committed either, which may not be what you want :)

icabod
  • 6,992
  • 25
  • 41
  • does not help. `hg commit` exit-code != 0, if commit none **with any options**. Fabric expect zero as exit code for "all OK" – Lazy Badger Feb 07 '13 at 02:04
  • Yes, as per the help it will return 1 as an exit code, but [this](http://stackoverflow.com/a/3877312/218597) suggests that the non-zero return in this instance will be treated as a warning rather than an error, and so should not exit the script. In theory :) – icabod Feb 07 '13 at 09:56