Bug 1401199 - [mozversioncontrol] Add property to get hash of HEAD revision, r?mshal
MozReview-Commit-ID: 9XWlLeGcPeQ
--- a/python/mozversioncontrol/mozversioncontrol/__init__.py
+++ b/python/mozversioncontrol/mozversioncontrol/__init__.py
@@ -107,16 +107,20 @@ class Repository(object):
self.version = LooseVersion(match.group(1))
return self.version
@abc.abstractproperty
def name(self):
"""Name of the tool."""
+ @abc.abstractproperty
+ def head_ref(self):
+ """Hash of HEAD revision."""
+
@abc.abstractmethod
def sparse_checkout_present(self):
"""Whether the working directory is using a sparse checkout.
A sparse checkout is defined as a working directory that only
materializes a subset of files in a given revision.
Returns a bool.
@@ -199,16 +203,20 @@ class HgRepository(Repository):
# the process lifetime tied to a context manager.
self._client = hglib.client.hgclient(self.path, encoding=b'UTF-8',
configs=None, connect=False)
@property
def name(self):
return 'hg'
+ @property
+ def head_ref(self):
+ return self._run('log', '-r', '.', '-T', '{node}')
+
def __enter__(self):
if self._client.server is None:
# The cwd if the spawned process should be the repo root to ensure
# relative paths are normalized to it.
old_cwd = os.getcwd()
try:
os.chdir(self.path)
self._client.open()
@@ -308,16 +316,20 @@ class GitRepository(Repository):
'''An implementation of `Repository` for Git repositories.'''
def __init__(self, path, git='git'):
super(GitRepository, self).__init__(path, tool=git)
@property
def name(self):
return 'git'
+ @property
+ def head_ref(self):
+ return self._run('rev-parse', 'HEAD')
+
def sparse_checkout_present(self):
# Not yet implemented.
return False
def get_upstream(self):
ref = self._run('symbolic-ref', '-q', 'HEAD').strip()
upstream = self._run('for-each-ref', '--format=%(upstream:short)', ref).strip()