summaryrefslogtreecommitdiff
path: root/morphlib/gitdir.py
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2013-11-11 17:11:26 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2013-11-22 13:49:25 +0000
commit994f06bc33ff2829e261eeec8b21ae1904967fc9 (patch)
tree6691dfd74614efdd1e55ff0f0d64a86619cf5864 /morphlib/gitdir.py
parent069255328af9a466987b198accfbacb17eb93c9b (diff)
downloadmorph-994f06bc33ff2829e261eeec8b21ae1904967fc9.tar.gz
GitDir: Add resolve_ref_to_{commit,tree} methods
Some APIs should take SHA1 object IDs, there needs to be a way to get one from a ref. To handle this, we add APIs for getting either the commit or the tree pointed to by the ref. Commits are provided because we need to create commits from existing commits and update refs from existing values. Trees are provided because we need trees to create commits, and we can read trees into the index to eventually create another tree.
Diffstat (limited to 'morphlib/gitdir.py')
-rw-r--r--morphlib/gitdir.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py
index e6426e03..b08f69d2 100644
--- a/morphlib/gitdir.py
+++ b/morphlib/gitdir.py
@@ -185,13 +185,18 @@ class GitDirectory(object):
else:
return self._list_files_in_ref(ref)
- def _rev_parse_tree(self, ref):
+ def _rev_parse(self, ref):
try:
- return self._runcmd(['git', 'rev-parse', '--verify',
- '%s^{tree}' % ref]).strip()
+ return self._runcmd(['git', 'rev-parse', '--verify', ref]).strip()
except cliapp.AppException as e:
raise InvalidRefError(self, ref)
+ def resolve_ref_to_commit(self, ref):
+ return self._rev_parse('%s^{commit}' % ref)
+
+ def resolve_ref_to_tree(self, ref):
+ return self._rev_parse('%s^{tree}' % ref)
+
def _list_files_in_work_tree(self):
for dirpath, subdirs, filenames in os.walk(self.dirname):
if dirpath == self.dirname and '.git' in subdirs:
@@ -200,7 +205,7 @@ class GitDirectory(object):
yield os.path.join(dirpath, filename)[len(self.dirname)+1:]
def _list_files_in_ref(self, ref):
- tree = self._rev_parse_tree(ref)
+ tree = self.resolve_ref_to_tree(ref)
output = self._runcmd(['git', 'ls-tree', '--name-only', '-rz', tree])
# ls-tree appends \0 instead of interspersing, so we need to
# strip the trailing \0 before splitting
@@ -213,7 +218,7 @@ class GitDirectory(object):
if ref is None:
with open(os.path.join(self.dirname, filename)) as f:
return f.read()
- tree = self._rev_parse_tree(ref)
+ tree = self.resolve_ref_to_tree(ref)
return self.get_file_from_ref(tree, filename)
@property