Skip to content

Instantly share code, notes, and snippets.

@techtonik
Created September 10, 2011 12:28
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/1208253 to your computer and use it in GitHub Desktop.
Save techtonik/1208253 to your computer and use it in GitHub Desktop.
VCS - Mercurial - Get revision number for a given path in local checkout
def get_hg_path_revision(path):
"""return latest modification revision of a path inside hg
:raise: EnvironmentError if `hg` isn't found or path invalid
:return: string like 'num:hash'
"""
from subprocess import Popen, PIPE
try:
hgprocess = Popen(
'hg log -l 1 --template "{node|short} {rev}" --cwd "%s" "%s"'
% (os.path.dirname(path) or '.', os.path.basename(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