Created
September 8, 2011 13:22
-
-
Save techtonik/1203373 to your computer and use it in GitHub Desktop.
VCS - Mercurial - Get revision number for a given repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_hg_repo_revision(path): | |
"""return current revision of a repository at path | |
:raise: EnvironmentError if `hg` isn't found or path invalid | |
:return: string like 'num:hash' | |
""" | |
from subprocess import Popen, PIPE | |
if os.path.isfile(path): | |
path = os.path.dirname(path) | |
try: | |
hgprocess = Popen('hg id -ni "%s"' % path, shell=True, | |
stdout=PIPE, stderr=PIPE) | |
output = hgprocess.communicate() | |
if hgprocess.returncode != 0: | |
raise EnvironmentError(hgprocess.returncode, "'hg' returned error") | |
hgid, hgnum = output[0].strip().split() | |
return hgnum + ':' + hgid | |
except OSError: | |
raise EnvironmentError(2, "'hg' command not found") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment