How do you check if a mercurial repo is in a clean state?
Asked Answered
A

4

9

As a user, I usually use hg st to check the status of a repo, and verify that it is in a clean state, with no modified files.

Here I would like to do this programmatically. I know I can also use hg st for that, but the output is less than ideal for consumption by a computer program. Is there a better way to check whether a mercurial repo is in a clean state?

Amygdalate answered 13/6, 2012 at 8:26 Comment(1)
I don't see why the output is less than ideal for consumption by a computer program. If there working copy is not clean, it outputs a status character, a space and the path to the file for each file that is not clean. If it is clean, it doesn't output anything. It doesn't get much easier than that.Amygdala
C
7

If you issue the hg identify --id command, it will suffix the ID with a + character when the repository has modified files. (Note: this flag does not report untracked files.)

If you grep the output of this command for the + character, you can use the exit status to determine whether there are modifications or no:

$ hg init
$ hg identify --id | grep --quiet + ; echo $?
1
$ touch a
$ hg identify --id | grep --quiet + ; echo $?
1
$ hg add a
$ hg identify --id | grep --quiet + ; echo $?
0
Cannonade answered 13/6, 2012 at 10:4 Comment(0)
G
2

You should use hg summary:

$ hg init
$ echo blablabla > test.txt
$ hg summary
parent: -1:000000000000 tip (empty repository)
branch: default
commit: 1 unknown (clean)
update: (current)
Gripsack answered 17/6, 2012 at 19:4 Comment(0)
Y
0

Most major programming languages have HG APIs you can access.

Yvetteyvon answered 17/6, 2012 at 22:11 Comment(1)
What about sh or CMD?Strain
F
0

This answer might be useful for other people searching this topic:

I agree to @SteveKayes comment above that hg status is a good command for programmatic consumption.

Here is an example how to use it in a bash script:

#!/bin/bash

set -e

cd /path/to/hg-repo

repo_status=`hg status | wc -l`
if [ $repo_status -ne 0 ]; then
    echo "Repo is not clean"
else
    echo "Repo is clean"
fi
Fluffy answered 7/2, 2019 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.