From b9488584415d94fc28c2e064b917183b851af07b Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Wed, 5 Sep 2012 17:12:26 +0000 Subject: Resolve tree SHA1 along with commit SHA1 when resolving refs Adjust all other parts and the tests to work with this. --- morphlib/cachedrepo.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'morphlib/cachedrepo.py') diff --git a/morphlib/cachedrepo.py b/morphlib/cachedrepo.py index f647102f..eef49ffb 100644 --- a/morphlib/cachedrepo.py +++ b/morphlib/cachedrepo.py @@ -91,27 +91,29 @@ class CachedRepo(object): return len(ref) == 40 and all([x in valid_chars for x in ref]) def resolve_ref(self, ref): - '''Attempts to resolve a Git ref into its corresponding SHA1. + '''Attempts to resolve a ref into its SHA1 and tree SHA1. Raises an InvalidReferenceError if the ref is not found in the repository. ''' - try: - refs = self._show_ref(ref).split('\n') - # split each ref line into an array, drop non-origin branches - refs = [x.split() for x in refs if 'origin' in x] - return refs[0][0] - except cliapp.AppException: - pass - if not self.is_valid_sha1(ref): - raise InvalidReferenceError(self, ref) - try: - return self._rev_list(ref).strip() - except cliapp.AppException: - raise InvalidReferenceError(self, ref) + try: + refs = self._show_ref(ref).split('\n') + # split each ref line into an array, drop non-origin branches + refs = [x.split() for x in refs if 'origin' in x] + absref = refs[0][0] + except cliapp.AppException: + raise InvalidReferenceError(self, ref) + else: + try: + absref = self._rev_list(ref).strip() + except cliapp.AppException: + raise InvalidReferenceError(self, ref) + + tree = self._show_tree_hash(absref) + return absref, tree def cat(self, ref, filename): '''Attempts to read a file given a SHA1 ref. @@ -197,6 +199,10 @@ class CachedRepo(object): def _show_ref(self, ref): # pragma: no cover return self._runcmd(['git', 'show-ref', ref]) + def _show_tree_hash(self, absref): # pragma: no cover + return self._runcmd( + ['git', 'log', '-1', '--format=format:%T', absref]).strip() + def _rev_list(self, ref): # pragma: no cover return self._runcmd(['git', 'rev-list', '--no-walk', ref]) -- cgit v1.2.1