summaryrefslogtreecommitdiff
path: root/morphlib/git.py
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-10-17 21:15:51 +0100
committerLars Wirzenius <liw@liw.fi>2011-10-17 21:15:51 +0100
commite8c7864240d0707d3c6d937ff8a3aad3d3e30305 (patch)
tree4613e4c46a3c8428f69120a1641170b0ff7e01a3 /morphlib/git.py
parenta7d16bcc787fbce9e6ef431dead91a54bf8a50ca (diff)
downloadmorph-e8c7864240d0707d3c6d937ff8a3aad3d3e30305.tar.gz
Move code to get morphology text from git into morphlib.git.
Diffstat (limited to 'morphlib/git.py')
-rw-r--r--morphlib/git.py38
1 files changed, 37 insertions, 1 deletions
diff --git a/morphlib/git.py b/morphlib/git.py
index f3403e2b..69f8752e 100644
--- a/morphlib/git.py
+++ b/morphlib/git.py
@@ -21,6 +21,22 @@ import urlparse
import morphlib
+class NoMorphs(Exception):
+
+ def __init__(self, repo, ref):
+ Exception.__init__(self,
+ 'Cannot find any morpologies at %s:%s' %
+ (repo, ref))
+
+
+class TooManyMorphs(Exception):
+
+ def __init__(self, repo, ref, morphs):
+ Exception.__init__(self,
+ 'Too many morphologies at %s:%s: %s' %
+ (repo, ref, ', '.join(morphs)))
+
+
def export_sources(repo, ref, tar_filename):
'''Export the contents of a specific commit into a compressed tarball.'''
ex = morphlib.execute.Execute('.', msg=logging.debug)
@@ -32,7 +48,7 @@ def export_sources(repo, ref, tar_filename):
def get_commit_id(repo, ref):
'''Return the full SHA-1 commit id for a repo+ref.'''
- # FIXME: This assume repo is a file:/// URL.
+ # FIXME: This assumes repo is a file:/// URL.
scheme, netlock, path, params, query, frag = urlparse.urlparse(repo)
assert scheme == 'file'
@@ -40,3 +56,23 @@ def get_commit_id(repo, ref):
out = ex.runv(['git', 'rev-list', '-n1', ref])
return out.strip()
+
+def get_morph_text(repo, ref):
+ '''Return a morphology from a git repository.'''
+ # FIXME: This implementation assumes a local repo.
+
+ scheme, netlock, path, params, query, frag = urlparse.urlparse(repo)
+ assert scheme == 'file'
+
+ ex = morphlib.execute.Execute(path, msg=logging.debug)
+ out = ex.runv(['git', 'ls-tree', '--name-only', '-z', ref])
+ names = [x for x in out.split('\0') if x]
+ morphs = [x for x in names if x.endswith('.morph')]
+ if len(morphs) == 0:
+ raise NoMorphs(repo, ref)
+ if len(morphs) > 1:
+ raise TooManyMorphs(repo, ref, morphs)
+ out = ex.runv(['git', 'cat-file', 'blob', '%s:%s' % (ref, morphs[0])])
+
+ return morphs[0], out
+