From 9f64a0c4b4701fc8326dedd64f486b3b49ee638d Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 22 Sep 2018 18:49:42 -0400 Subject: [PATCH] Basic git log support --- gitsrht/blueprints/repo.py | 46 ++++++++++++++++++++++++++++++++++ gitsrht/templates/log.html | 14 +++++++++++ gitsrht/templates/repo.html | 4 ++- gitsrht/templates/summary.html | 15 ++--------- gitsrht/templates/utils.html | 28 +++++++++++++++++++++ 5 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 gitsrht/templates/log.html diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index c1719e6..17990c8 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -227,3 +227,49 @@ def archive(owner, repo, ref): os.unlink(path) return send_file(f, mimetype="application/tar+gzip", as_attachment=True, attachment_filename=f"{repo.name}-{ref}.tar.gz") + +class _AnnotatedRef: + def __init__(self, repo, ref): + self.ref = ref + self.target = ref.target + if ref.name.startswith("refs/heads/"): + self.type = "branch" + self.name = ref.name[len("refs/heads/"):] + self.branch = repo.get(ref.target) + elif ref.name.startswith("refs/tags/"): + self.type = "tag" + self.name = ref.name[len("refs/tags/"):] + self.tag = repo.get(ref.target) + else: + self.type = None + +@repo.route("///log", defaults={"ref": None, "path": ""}) +@repo.route("///log/", defaults={"path": ""}) +@repo.route("///log//") +def log(owner, repo, ref, 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) + ref, commit = resolve_ref(git_repo, ref) + + refs = {} + for ref in git_repo.references: + ref = _AnnotatedRef(git_repo, git_repo.references[ref]) + if not ref.type: + continue + if ref.target.hex not in refs: + refs[ref.target.hex] = [] + refs[ref.target.hex].append(ref) + + commits = list() + for commit in git_repo.walk(commit.id, pygit2.GIT_SORT_TIME): + commits.append(commit) + if len(commits) >= 20: + break + + return render_template("log.html", view="log", + owner=owner, repo=repo, ref=ref, path=path, + commits=commits, refs=refs) diff --git a/gitsrht/templates/log.html b/gitsrht/templates/log.html new file mode 100644 index 0000000..b1b4c00 --- /dev/null +++ b/gitsrht/templates/log.html @@ -0,0 +1,14 @@ +{% extends "repo.html" %} +{% import "utils.html" as utils %} +{% block content %} +
+
+
+
+ {% for c in commits %} + {{ utils.commit_event(c, commit_time, None, True, refs) }} + {% endfor %} +
+
+
+{% endblock %} diff --git a/gitsrht/templates/repo.html b/gitsrht/templates/repo.html index 3539105..f0b1997 100644 --- a/gitsrht/templates/repo.html +++ b/gitsrht/templates/repo.html @@ -26,7 +26,9 @@ repo=repo.name), "tree")}}