From 33956600602a5eb037a643b98104eed6f30e538d Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 21 Sep 2018 22:06:23 -0400 Subject: [PATCH] Support rendering any level of the tree --- gitsrht/app.py | 4 +++- gitsrht/blueprints/repo.py | 22 ++++++++++++++++++---- gitsrht/git.py | 6 +++--- gitsrht/templates/tree.html | 29 +++++++++++++++++++++++++++-- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/gitsrht/app.py b/gitsrht/app.py index 2376a51..3f9fc90 100644 --- a/gitsrht/app.py +++ b/gitsrht/app.py @@ -1,5 +1,6 @@ import humanize import stat +import os from flask import session from srht.flask import SrhtFlask from srht.config import cfg @@ -46,7 +47,8 @@ class GitApp(SrhtFlask): "trim_commit": trim_commit, "humanize": humanize, "stat": stat, - "notice": notice + "notice": notice, + "path_join": os.path.join } @self.login_manager.user_loader diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index e9e5651..b0f90cd 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -68,7 +68,7 @@ def summary(owner, repo): @repo.route("///tree", defaults={"branch": None, "path": ""}) @repo.route("///tree/", defaults={"path": ""}) -@repo.route("///tree//") +@repo.route("///tree//") def tree(owner, repo, branch, path): owner, repo = get_repo(owner, repo) if not repo: @@ -84,8 +84,22 @@ def tree(owner, repo, branch, path): abort(404) commit = git_repo.get(branch.target) - tree = annotate_tree(git_repo, commit) + tree = commit.tree + path = path.split("/") + for part in path: + if part == "": + continue + if part not in tree: + abort(404) + entry = tree[part] + if entry.type == "blob": + return "TODO: render blobs" + tree = git_repo.get(entry.id) + + tree = annotate_tree(git_repo, tree, commit) tree = sorted(tree, key=lambda e: e.name) - # TODO: follow path + return render_template("tree.html", view="tree", - owner=owner, repo=repo, commit=commit, tree=tree, path=path) + owner=owner, repo=repo, branch=branch, + branch_name=branch.name[len("refs/heads/"):], + commit=commit, tree=tree, path=path) diff --git a/gitsrht/git.py b/gitsrht/git.py index acfb6ab..14fcb44 100644 --- a/gitsrht/git.py +++ b/gitsrht/git.py @@ -90,8 +90,8 @@ class AnnotatedTreeEntry: def __repr__(self): return f"" -def annotate_tree(repo, commit): - key = f"git.sr.ht:git:tree:{commit.tree.id.hex}" +def annotate_tree(repo, tree, commit): + key = f"git.sr.ht:git:tree:{tree.id.hex}" cache = redis.get(key) if cache: cache = json.loads(cache.decode()) @@ -99,7 +99,7 @@ def annotate_tree(repo, commit): e, repo).fetch_blob() for e in cache.values()] tree = { entry.id.hex: AnnotatedTreeEntry( - repo, entry) for entry in commit.tree } + repo, entry) for entry in tree } parents = deque(commit.parents) left_tree = set(v for v in tree.values()) diff --git a/gitsrht/templates/tree.html b/gitsrht/templates/tree.html index 6cdf248..d089bc9 100644 --- a/gitsrht/templates/tree.html +++ b/gitsrht/templates/tree.html @@ -3,7 +3,16 @@
- /{{path}} + {% if path != [''] %} + {{repo.name}}{% endif %}/{% for part in path%}{% + if loop.last %}{{part}}{% else %}{{part}}/{% + endif %}{% endfor %}
{{commit.id.hex[:8]}} — @@ -19,12 +28,28 @@
+ {% if path != [''] %} +
+
+ .. +
+
+
+
+ {% endif %} {% for entry in tree %}
{{stat.filemode(entry.filemode)}}
{% if entry.commit %} -- 2.38.4