Search code examples
mercurial

Testing for uncommitted changes in mercurial


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)


Solution

  • 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