On Fri, Nov 15, 2013 at 06:19:56PM +0000, Richard Maw wrote:
This moves the step of joining a treeish to a file path to locate an
object outside the cat_file call, since cat_file is useful for just
having the commit or blob sha1.
---
morphlib/gitdir.py | 6 +++---
morphlib/plugins/branch_and_merge_new_plugin.py | 4 ++--
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py
index 5a62253..d731db2 100644
--- a/morphlib/gitdir.py
+++ b/morphlib/gitdir.py
@@ -96,9 +96,9 @@ 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 cat_file(self, obj_type, object): # pragma: no cover
return self._runcmd(
- ['git', 'cat-file', obj_type, '%s:%s' % (ref,
filename)])
+ ['git', 'cat-file', obj_type, object])
def update_submodules(self, app): # pragma: no cover
'''Change .gitmodules URLs, and checkout submodules.'''
@@ -190,7 +190,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.cat_file('blob', '%s:%s' % (tree, filename))
NAK on this. This inflicts git command line argument syntax into the
Python API. In other words, the caller of the Python metod cat_file
needs to know how to format an argument for git. If and when we start
using a library for git operations, instead of running git directly,
the library may well have an API that requires the arguments to be
separate. In that case, our cat_file method would need to parse the
argument.
Constructing the git command line argument is clearly the job of our
cat_file method. We can, and probably should, have different methods
for different kinds of "git cat-file" calls. After all, the point of
the GitDirectory methods is not to have a 1:1 mapping between method
and git execution, but to be an abstraction of the operations we need
to do: the operation "get file foo from commit bar" is a different
operation than "get git object $sha" even though they are both
implemented by calling "git cat-file". The original cat_file method
was not a shining beacon of excellent API design, either, for what
that's worth.
Thus, I would like this commit to be refactored to replace the
cat_file method with the following methods:
def get_file_from_ref(self, ref, filename):
def get_git_object(self, obj_type, obj_ref):
I'll also note that we should avoid making the GitDirectory as general
as possible: it should, instead, be as minimal as possible so that we
know the _minimal_ things we use for accessing git clones and repos.
That makes it easier to re-implement the class when we want to.
--
http://www.codethink.co.uk/ http://wiki.baserock.org/ http://www.baserock.com/