summaryrefslogtreecommitdiff
path: root/morphcacheserver
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 /morphcacheserver
parentf1fba299bd07510346082ef985ef08a494dca9d9 (diff)
downloadmorph-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" }, ... } }
Diffstat (limited to 'morphcacheserver')
-rw-r--r--morphcacheserver/repocache.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/morphcacheserver/repocache.py b/morphcacheserver/repocache.py
index 3bb348ff..7061508d 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])