From f992ceccbf47a3e19ac45d17d624002a422fd2af Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 21 Sep 2018 15:32:22 -0400 Subject: [PATCH] Mock up tree page --- gitsrht/blueprints/repo.py | 46 ++++++++++++++++++++++++-------- gitsrht/git.py | 7 +++++ gitsrht/templates/repo.html | 13 +++++++-- gitsrht/templates/tree.html | 42 +++++++++++++++++++++++++++++ scss/main.scss | 53 +++++++++++++++++++++++++++++++++++++ 5 files changed, 148 insertions(+), 13 deletions(-) create mode 100644 gitsrht/templates/tree.html diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index 8ee193e..054afc6 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -18,7 +18,8 @@ def get_readme(repo, tip): readme = tip.tree["README.md"] if readme.type != "blob": return None - html = redis.get(readme.id.hex) + key = f"git.sr.ht:git:markdown:{readme.id.hex}" + html = redis.get(key) if html: return Markup(html.decode()) try: @@ -26,12 +27,14 @@ def get_readme(repo, tip): except: pass html = markdown(md, ["h1", "h2", "h3", "h4", "h5"]) - redis.setex(readme.id.hex, html, timedelta(days=30)) + redis.setex(key, html, timedelta(days=30)) return Markup(html) @repo.route("//") def summary(owner, repo): owner, repo = get_repo(owner, repo) + if not repo: + abort(404) if not has_access(repo, UserAccess.read): abort(401) git_repo = CachedRepository(repo.path) @@ -45,11 +48,8 @@ def summary(owner, repo): if git_repo.is_empty: return render_template("empty-repo.html", owner=owner, repo=repo, clone_urls=clone_urls) - master = git_repo.branches.get("master") - if not master: - master = list(git_repo.branches.local)[0] - master = git_repo.branches.get(master) - tip = git_repo.get(master.target) + default_branch = git_repo.default_branch() + tip = git_repo.get(default_branch.target) commits = list() for commit in git_repo.walk(tip.id, pygit2.GIT_SORT_TIME): commits.append(commit) @@ -61,7 +61,31 @@ def summary(owner, repo): if ref.startswith("refs/tags/")] tags = sorted(tags, key=lambda c: commit_time(c[1]), reverse=True) latest_tag = tags[0] if len(tags) else None - default_branch = master - return render_template("summary.html", owner=owner, repo=repo, - readme=readme, commits=commits, clone_urls=clone_urls, - latest_tag=latest_tag, default_branch=master) + return render_template("summary.html", view="summary", + owner=owner, repo=repo, readme=readme, commits=commits, + clone_urls=clone_urls, latest_tag=latest_tag, + default_branch=default_branch) + +@repo.route("///tree", defaults={"branch": None, "path": ""}) +@repo.route("///tree/", defaults={"path": ""}) +@repo.route("///tree//") +def tree(owner, repo, branch, path): + owner, repo = get_repo(owner, repo) + if not repo: + abort(404) + if not has_access(repo, UserAccess.read): + abort(401) + git_repo = CachedRepository(repo.path) + if branch is None: + branch = git_repo.default_branch() + else: + branch = git_repo.branches.get(branch) + if not branch: + abort(404) + commit = git_repo.get(branch.target) + # TODO: annotate tree with latest commit for each file (and cache it) + tree = [entry for entry in commit.tree] + 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) diff --git a/gitsrht/git.py b/gitsrht/git.py index 16d7974..8c0a5a4 100644 --- a/gitsrht/git.py +++ b/gitsrht/git.py @@ -32,3 +32,10 @@ class _CachedRepository(Repository): def _get(self, ref): return super().get(ref) + + def default_branch(self): + branch = self.branches.get("master") + if not branch: + branch = list(self.branches.local)[0] + branch = self.branches.get(branch) + return branch diff --git a/gitsrht/templates/repo.html b/gitsrht/templates/repo.html index 7f5fab0..1f3c855 100644 --- a/gitsrht/templates/repo.html +++ b/gitsrht/templates/repo.html @@ -9,12 +9,21 @@ >{{owner.canonical_name}}/{{repo.name}} {% block tabs %} + {% macro link(path, title) %} + {{ title }} + {% endmacro %}