~edwargix/git.sr.ht

33956600602a5eb037a643b98104eed6f30e538d — Drew DeVault 6 years ago bea00bb
Support rendering any level of the tree
4 files changed, 51 insertions(+), 10 deletions(-)

M gitsrht/app.py
M gitsrht/blueprints/repo.py
M gitsrht/git.py
M gitsrht/templates/tree.html
M gitsrht/app.py => gitsrht/app.py +3 -1
@@ 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

M gitsrht/blueprints/repo.py => gitsrht/blueprints/repo.py +18 -4
@@ 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)

M gitsrht/git.py => gitsrht/git.py +3 -3
@@ 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())

M gitsrht/templates/tree.html => gitsrht/templates/tree.html +27 -2
@@ 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> &mdash;


@@ 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 %}