summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJannis Pohlmann <jannis.pohlmann@codethink.co.uk>2012-04-20 15:46:53 +0100
committerJannis Pohlmann <jannis.pohlmann@codethink.co.uk>2012-04-20 15:46:53 +0100
commiteb4c1530c57b6ae200643b259e6ed95904951681 (patch)
tree68406cff27bfbc9bc9380088a8b9edb217a69e69
parentf1fba299bd07510346082ef985ef08a494dca9d9 (diff)
downloadmorph-cache-server-eb4c1530c57b6ae200643b259e6ed95904951681.tar.gz
Add /trees which serves the contents of a git tree using ls-tree.
/trees queries take repo URI, a SHA1 ref and an optional path parameter. The result is a JSON dictionary of the form { "repo": "<repo URI>", "ref": "<SHA1 ref>", "tree": { "filename1": { "mode": "100644", "kind": "blob", "sha1": "FOOBARBAZ" }, ... } }
-rwxr-xr-xmorph-cache-server16
-rw-r--r--morphcacheserver/repocache.py29
2 files changed, 45 insertions, 0 deletions
diff --git a/morph-cache-server b/morph-cache-server
index 7618f5b..86a1fe2 100755
--- a/morph-cache-server
+++ b/morph-cache-server
@@ -85,6 +85,22 @@ class MorphCacheServer(cliapp.Application):
response.status = 404
logging.debug('%s' % e)
+ @app.get('/trees')
+ def tree():
+ repo = self._unescape_parameter(request.query.repo)
+ ref = self._unescape_parameter(request.query.ref)
+ path = self._unescape_parameter(request.query.path)
+ try:
+ tree = repo_cache.ls_tree(repo, ref, path)
+ return {
+ 'repo': '%s' % repo,
+ 'ref': '%s' % ref,
+ 'tree': tree,
+ }
+ except Exception, e:
+ response.status = 404
+ logging.debug('%s' % e)
+
@app.get('/bundles')
def bundle():
repo = self._unescape_parameter(request.query.repo)
diff --git a/morphcacheserver/repocache.py b/morphcacheserver/repocache.py
index 3bb348f..7061508 100644
--- a/morphcacheserver/repocache.py
+++ b/morphcacheserver/repocache.py
@@ -81,6 +81,32 @@ class RepoCache(object):
return self._cat_file(repo_dir, sha1, filename)
+ def ls_tree(self, repo_url, ref, path):
+ quoted_url = self._quote_url(repo_url)
+ repo_dir = os.path.join(self.repo_cache_dir, quoted_url)
+ if not self._is_valid_sha1(ref):
+ raise UnresolvedNamedReferenceError(repo_url, ref)
+ if not os.path.exists(repo_dir):
+ raise RepositoryNotFoundError(repo_url)
+
+ try:
+ sha1 = self._rev_list(repo_dir, ref).strip()
+ except:
+ raise InvalidReferenceError(repo_url, ref)
+
+ lines = self._ls_tree(repo_dir, sha1, path).strip()
+ lines = lines.splitlines()
+ data = {}
+ for line in lines:
+ elements = line.split()
+ basename = elements[3]
+ data[basename] = {
+ 'mode': elements[0],
+ 'kind': elements[1],
+ 'sha1': elements[2],
+ }
+ return data
+
def get_bundle_filename(self, repo_url):
quoted_url = self._quote_url(repo_url)
return os.path.join(self.bundle_cache_dir, '%s.bndl' % quoted_url)
@@ -102,6 +128,9 @@ class RepoCache(object):
['git', 'cat-file', 'blob', '%s:%s' % (sha1, filename)],
cwd=repo_dir)
+ def _ls_tree(self, repo_dir, sha1, path):
+ return self.app.runcmd(['git', 'ls-tree', sha1, path], cwd=repo_dir)
+
def _is_valid_sha1(self, ref):
valid_chars = 'abcdefABCDEF0123456789'
return len(ref) == 40 and all([x in valid_chars for x in ref])