From eb4c1530c57b6ae200643b259e6ed95904951681 Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Fri, 20 Apr 2012 15:46:53 +0100 Subject: 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": "", "ref": "", "tree": { "filename1": { "mode": "100644", "kind": "blob", "sha1": "FOOBARBAZ" }, ... } } --- morph-cache-server | 16 ++++++++++++++++ morphcacheserver/repocache.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) 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]) -- cgit v1.2.1