Skip to content

Instantly share code, notes, and snippets.

@techtonik
Created September 8, 2011 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techtonik/1203373 to your computer and use it in GitHub Desktop.
Save techtonik/1203373 to your computer and use it in GitHub Desktop.
VCS - Mercurial - Get revision number for a given repository
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