From 069255328af9a466987b198accfbacb17eb93c9b Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Mon, 11 Nov 2013 17:02:12 +0000 Subject: GitDir: Provide more specific object access than cat-file We need to use cat-file for files by SHA1, commits by SHA1 and files by ref and path, so provide access in separate methods, since while it's all the same thing "under the hood", it avoids the user needing to know the command-line syntax. --- morphlib/gitdir.py | 30 ++++++++++++++++++++++--- morphlib/plugins/branch_and_merge_new_plugin.py | 4 ++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py index 5a622539..e6426e03 100644 --- a/morphlib/gitdir.py +++ b/morphlib/gitdir.py @@ -96,9 +96,33 @@ class GitDirectory(object): parsed_head = self._runcmd(['git', 'rev-parse', 'HEAD']).strip() return parsed_ref == parsed_head - def cat_file(self, obj_type, ref, filename): # pragma: no cover + def get_file_from_ref(self, ref, filename): # pragma: no cover + '''Get file contents from git by ref and filename. + + `ref` should be a tree-ish e.g. HEAD, master, refs/heads/master, + refs/tags/foo, though SHA1 tag, commit or tree IDs are also valid. + + `filename` is the path to the file object from the base of the + git directory. + + Returns the contents of the referred to file as a string. + + ''' + + # Blob ID is left as the git revision, rather than SHA1, since + # we know get_blob_contents will accept it + blob_id = '%s:%s' % (ref, filename) + return self.get_blob_contents(blob_id) + + def get_blob_contents(self, blob_id): # pragma: no cover + '''Get file contents from git by ID''' + return self._runcmd( + ['git', 'cat-file', 'blob', blob_id]) + + def get_commit_contents(self, commit_id): # pragma: no cover + '''Get commit contents from git by ID''' return self._runcmd( - ['git', 'cat-file', obj_type, '%s:%s' % (ref, filename)]) + ['git', 'cat-file', 'commit', commit_id]) def update_submodules(self, app): # pragma: no cover '''Change .gitmodules URLs, and checkout submodules.''' @@ -190,7 +214,7 @@ class GitDirectory(object): with open(os.path.join(self.dirname, filename)) as f: return f.read() tree = self._rev_parse_tree(ref) - return self.cat_file('blob', tree, filename) + return self.get_file_from_ref(tree, filename) @property def HEAD(self): diff --git a/morphlib/plugins/branch_and_merge_new_plugin.py b/morphlib/plugins/branch_and_merge_new_plugin.py index 09dd5bd8..e7b5abc1 100644 --- a/morphlib/plugins/branch_and_merge_new_plugin.py +++ b/morphlib/plugins/branch_and_merge_new_plugin.py @@ -305,9 +305,9 @@ class SimpleBranchAndMergePlugin(cliapp.Plugin): def _load_morphology_from_git(self, loader, gd, ref, filename): try: - text = gd.cat_file('blob', ref, filename) + text = gd.get_file_from_ref(ref, filename) except cliapp.AppException: - text = gd.cat_file('blob', 'origin/%s' % ref, filename) + text = gd.get_file_from_ref('origin/%s' % ref, filename) return loader.load_from_string(text, filename) def _load_stratum_morphologies(self, loader, sb, system_morph): -- cgit v1.2.1