summaryrefslogtreecommitdiff
path: root/morphlib/gitdir.py
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2013-11-11 17:02:12 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2013-11-22 13:49:25 +0000
commit069255328af9a466987b198accfbacb17eb93c9b (patch)
treecab8ee2cb044dac34e3a5279cf7030ac76fc6f75 /morphlib/gitdir.py
parent84c233d1de3993028663a287caf56a9b370fed97 (diff)
downloadmorph-069255328af9a466987b198accfbacb17eb93c9b.tar.gz
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.
Diffstat (limited to 'morphlib/gitdir.py')
-rw-r--r--morphlib/gitdir.py30
1 files changed, 27 insertions, 3 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):