diff options
author | Rob Taylor <rob.taylor@codethink.co.uk> | 2012-01-18 15:43:57 +0000 |
---|---|---|
committer | Rob Taylor <rob.taylor@codethink.co.uk> | 2012-01-18 15:43:57 +0000 |
commit | eb36c1b9da1e629836264c1ff4fa6f4fb78d3c74 (patch) | |
tree | ff3607c7eee2c4cd14c2df5be030e5b3c978ea90 /morphlib/git.py | |
parent | 35d77de0bd185776630344973a9cc6c8265e3458 (diff) | |
download | morph-eb36c1b9da1e629836264c1ff4fa6f4fb78d3c74.tar.gz |
initial version of Sourcemanager and tests
Diffstat (limited to 'morphlib/git.py')
-rw-r--r-- | morphlib/git.py | 73 |
1 files changed, 53 insertions, 20 deletions
diff --git a/morphlib/git.py b/morphlib/git.py index 59f59c9b..655d4716 100644 --- a/morphlib/git.py +++ b/morphlib/git.py @@ -16,7 +16,7 @@ import logging import urlparse - +import binascii import morphlib @@ -35,29 +35,62 @@ class TooManyMorphs(Exception): 'Too many morphologies at %s:%s: %s' % (repo, ref, ', '.join(morphs))) +class InvalidTreeish(Exception): -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) - ex.runv(['git', 'archive', '-o', tar_filename, '--remote', repo, ref]) - -def get_commit_id(repo, ref): - '''Return the full SHA-1 commit id for a repo+ref.''' - # FIXME: This assumes repo is a file:/// URL. + def __init__(self, repo, ref): + Exception.__init__(self, + '%s is an invalid reference for repo %s' % + (ref,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', 'rev-list', '-n1', ref]) - return out.strip() +class Treeish: + def __init__(self, repo, ref): + self.repo = repo + self.sha1 = None + self.ref = None + self._resolve_ref(ref) + + def _resolve_ref(self, ref): + ex = morphlib.execute.Execute(self.repo, msg=logging.debug) + try: + refs = ex.runv(['git', 'show-ref', ref]).split() + binascii.unhexlify(refs[0]) #Valid hex? + self.sha1 = refs[0] + self.ref = refs[1] + except morphlib.execute.CommandFailure: + self._is_treeish(ref) + + def _is_treeish(self, ref): + try: + if len(ref)==40: + binascii.unhexlify(ref) + ex = morphlib.execute.Execute(self.repo, msg=logging.debug) + try: + refs = ex.runv(['git', 'rev-list', '--no-walk', ref]) + self.sha1=REF + except morphlib.execute.CommandFailure: + raise InvalidTreeish(self.repo,ref) + + except TypeError: + raise InvalidTreeish(self.repo,ref) + +def export_sources(treeish, tar_filename): + '''Export the contents of a specific commit into a compressed tarball.''' + ex = morphlib.execute.Execute('.', msg=logging.debug) + ex.runv(['git', 'archive', '-o', tar_filename, '--remote', treeish.repo, treeish.sha1]) -def get_morph_text(repo, ref, filename): +def get_morph_text(treeish, filename): '''Return a morphology from a git repository.''' - # FIXME: This implementation assumes a local repo. + ex = morphlib.execute.Execute(treeish.repo, msg=logging.debug) + return ex.runv(['git', 'cat-file', 'blob', '%s:%s' % (treeish.sha1, filename)]) - scheme, netlock, path, params, query, frag = urlparse.urlparse(repo) - assert scheme == 'file' - ex = morphlib.execute.Execute(path, msg=logging.debug) - return ex.runv(['git', 'cat-file', 'blob', '%s:%s' % (ref, filename)]) +def extract_bundle(location, bundle): + '''Extract a bundle into git at location''' + ex = morphlib.execute.Execute(location, msg=logging.debug) + return ex.runv(['git', 'bundle', 'unbundle', bundle]) + +def clone(location, repo): + '''clone at git repo into location''' + ex = morphlib.execute.Execute('.', msg=logging.debug) + return ex.runv(['git', 'clone', repo, location]) |