diff options
-rw-r--r-- | gitsrht/app.py | 4 | ||||
-rw-r--r-- | gitsrht/blueprints/repo.py | 22 | ||||
-rw-r--r-- | gitsrht/git.py | 6 | ||||
-rw-r--r-- | 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("/<owner>/<repo>/tree", defaults={"branch": None, "path": ""}) @repo.route("/<owner>/<repo>/tree/<branch>", defaults={"path": ""}) -@repo.route("/<owner>/<repo>/tree/<branch>/<path>") +@repo.route("/<owner>/<repo>/tree/<branch>/<path:path>") 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"<AnnotatedTreeEntry {self.name} {self.id}>" -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 @@ <div class="header-extension"> <div class="container-fluid"> <span style="padding-left: 1rem"> - /{{path}} + {% if path != [''] %} + <a href="{{url_for("repo.tree", + owner=repo.owner.canonical_name, repo=repo.name, branch=branch_name)}}" + >{{repo.name}}</a>{% endif %}/{% for part in path%}{% + if loop.last %}{{part}}{% else %}<a + href="{{url_for("repo.tree", owner=repo.owner.canonical_name, + repo=repo.name, branch=branch_name, + path=path_join(*path[:loop.index]))}}" + >{{part}}</a>/{% + endif %}{% endfor %} </span> <div class="pull-right"> <a href="#">{{commit.id.hex[:8]}}</a> — @@ -19,12 +28,28 @@ <div class="row" style="margin-bottom: 1rem"> <div class="col-md-12"> <div class="tree-list"> + {% if path != [''] %} + <div class="mode"></div> + <div class="name tree"> + <a href="{{url_for("repo.tree", + owner=repo.owner.canonical_name, repo=repo.name, branch=branch_name, + path=path_join(*path[:-1]) if path[:-1] else None)}}">..</a> + </div> + <div class="commit"></div> + <div class="date"></div> + <div class="size"></div> + {% endif %} {% for entry in tree %} <div class="mode"> {{stat.filemode(entry.filemode)}} </div> <div class="name {{entry.type}}"> - <a href="#">{{entry.name}}{% if entry.type == "tree" %}/{% endif %}</a> + <a href="{{url_for("repo.tree", + owner=repo.owner.canonical_name, repo=repo.name, branch=branch_name, + path=path_join(*(path + [entry.name])))}}" + > + {{entry.name}}{% if entry.type == "tree" %}/{% endif %} + </a> </div> <div class="commit"> {% if entry.commit %} |