tags:

views:

173

answers:

3

What's the best way to check in script if there're uncommitted changes in mercurial's working tree.

(the way I would with git diff --quiet in git)

A: 

There should be something more elegant that simply

[ `hg st |wc -l` -eq 0 ] && echo hi
Antony Hatchkins
+3  A: 

In mercurial 1.4 and later you can use the summary command, which gives output like this when changes exist:

$ hg summary
parent: 0:ad218537bdef tip
 commited
branch: default
commit: 1 modified
update: (current)

and this post-commit:

$ hg summary
parent: 1:ef93d692f646 tip
 sfsdf
branch: default
commit: (clean)
update: (current)

Alternately, you could install the prompt extension and do something like this:

$ hg prompt '{status}'

which will output a ! or ? or nothing as appropriate.

Both of those, of course, are just alternate text outputs. I couldn't find anything that used the exit code directly, but since $? checks the last command in a pipe you could do?

hg summary | grep -q 'commit: (clean)'

which will set $? non-zero if any changes are uncommitted:

$ hg summary | grep -q 'commit: (clean)' ; echo $?
0
$ echo more >> that 
$ hg summary | grep -q 'commit: (clean)' ; echo $?
1
Ry4an
Thanks for your answer. I ended up with (in zsh) `[[ "nothing changed" = ``hg ci -m tick`` ]] || hg push`. And I don't like it because in some future version of mercurial the exact wording could change (that also applies to your hg summary solution). Yes, `hg prompt '{status}'` should work fine, except that I would prefer using core hg functionality because the script will be running on an external hosting so my custom extension (thus updated manually) could one day become incompartible with new hg (autoupdated) version.
Antony Hatchkins
Looks as if mercurial is not designed to be used in scripts as much as git, is it?
Antony Hatchkins
Your concerns about the string changes make sense, but short of running on a different locale I don't think it will be as issue. Matt Mackal, mercurial author, considers the text output part of the API and no change is allowed to alter it if any other option is available. You'll not see that format change.
Ry4an
Hmm. Good to know. Not that I would consider this a great design decision ;) but it is somewhat comforting to bear this in mind.
Antony Hatchkins
Yeah, part of it was a licensing thing. Mercurial was a GPL2 hold out, so it couldn't legally be in-process with non-GPL2 software, including GPL3 stuff. Thus, a lot of interaction had to happen via stdin/stdout, and recognizing that MPM did a great job of watching for needless interface changes.
Ry4an
+3  A: 

You can also run hg id. If the hash ends with a + it indicates the working copy has changes. This should even work with old versions of hg.

It sounds like you're already using zsh; well, a couple days ago I helped to update the Mercurial support for the built-in VCS_INFO for putting VCS info in your prompt. Slated for the next release is support for showing changes to the working directory (among other things). If you don't want to wait you can grab the necessary files from CVS.

At the moment my prompt includes this (using only built-in zsh functionality):

   (hg)[1801+ branchname somemq.patch, anycurrentbookmarks]
whiteinge
Thanks! It sounds like a hack but in fact seems to be working :) Hmm. It's been awhile since I have last seen CVS in use ;)
Antony Hatchkins