Bug 1401199 - [mozversioncontrol] Add property to get hash of HEAD revision, r?mshal draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Fri, 29 Sep 2017 11:10:59 -0400
changeset 672691 9f731e41bb287b0f459bf3609dc3401f7257ce89
parent 672608 c7dc2f640e8fc532458630863bd06082239fdb76
child 672692 fb4c533b57ced13922d569a660ad0f78fa344072
child 672875 6c0860b17efbd27d88fde3ad8e03eed49814f519
push id82338
push userahalberstadt@mozilla.com
push dateFri, 29 Sep 2017 16:31:34 +0000
reviewersmshal
bugs1401199
milestone58.0a1
Bug 1401199 - [mozversioncontrol] Add property to get hash of HEAD revision, r?mshal MozReview-Commit-ID: 9XWlLeGcPeQ
python/mozversioncontrol/mozversioncontrol/__init__.py
--- 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()