From 1aabd8cfc3886ef0a65547328c687bcdd08cf5fc Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 22 Sep 2018 15:58:15 -0400 Subject: [PATCH] Add support for browsing refs other than master --- gitsrht/blueprints/repo.py | 46 ++++++++++++++++------------------ gitsrht/templates/blob.html | 15 +++-------- gitsrht/templates/summary.html | 8 ++++-- gitsrht/templates/tree.html | 12 ++------- gitsrht/templates/utils.html | 18 +++++++++++++ 5 files changed, 51 insertions(+), 48 deletions(-) create mode 100644 gitsrht/templates/utils.html diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index b63d406..04b435e 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -86,26 +86,18 @@ def summary(owner, repo): clone_urls=clone_urls, latest_tag=latest_tag, default_branch=default_branch) -@repo.route("///tree", defaults={"ref": None, "path": ""}) -@repo.route("///tree/", defaults={"path": ""}) -@repo.route("///tree//") -def tree(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) +def resolve_ref(git_repo, ref): if ref is None: branch = git_repo.default_branch() - ref = branch.name[len("refs/for/"):] + ref = branch.name[len("refs/heads/"):] commit = git_repo.get(branch.target) else: - if f"refs/heads/{ref}" in repo.references: + if f"refs/heads/{ref}" in git_repo.references: branch = git_repo.references[f"refs/heads/{ref}"] commit = git_repo.get(branch.target) - elif f"refs/tags/{ref}" in repo.references: - tag = git_repo.references[f"refs/tags/{ref}"] + elif f"refs/tags/{ref}" in git_repo.references: + _ref = git_repo.references[f"refs/tags/{ref}"] + tag = git_repo.get(_ref.target) commit = git_repo.get(tag.target) else: try: @@ -114,6 +106,19 @@ def tree(owner, repo, ref, path): abort(404) if not commit: abort(404) + return ref, commit + +@repo.route("///tree", defaults={"ref": None, "path": ""}) +@repo.route("///tree/", defaults={"path": ""}) +@repo.route("///tree//") +def tree(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) tree = commit.tree path = path.split("/") @@ -145,22 +150,15 @@ def tree(owner, repo, ref, path): return render_template("tree.html", view="tree", owner=owner, repo=repo, ref=ref, commit=commit, tree=tree, path=path) -@repo.route("///blob//") -def raw_blob(owner, repo, branch, path): +@repo.route("///blob//") +def raw_blob(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) - if branch is None: - branch = git_repo.default_branch() - else: - branch = git_repo.branches.get(branch) - if not branch: - abort(404) - branch_name = branch.name[len("refs/heads/"):] - commit = git_repo.get(branch.target) + ref, commit = resolve_ref(git_repo, ref) blob = None entry = None diff --git a/gitsrht/templates/blob.html b/gitsrht/templates/blob.html index b015c98..c9a7aeb 100644 --- a/gitsrht/templates/blob.html +++ b/gitsrht/templates/blob.html @@ -1,18 +1,10 @@ {% extends "repo.html" %} +{% import "utils.html" as utils %} {% block content %}
- {% if path != [''] %} - {{repo.name}}{% endif %}/{% for part in path%}{% - if loop.last %}{{part}}{% else %}{{part}}/{% - endif %}{% endfor %} + {{ utils.breadcrumb(ref, repo, path, path_join) }} {{stat.filemode(entry.filemode)}} @@ -26,8 +18,7 @@ + repo=repo.name, ref=ref, path=path_join(*path))}}"> View raw diff --git a/gitsrht/templates/summary.html b/gitsrht/templates/summary.html index 38a8df7..96b4c97 100644 --- a/gitsrht/templates/summary.html +++ b/gitsrht/templates/summary.html @@ -33,14 +33,18 @@ {% if default_branch %}
{{default_branch.name[len("refs/heads/"):]}}
- browse {{icon("caret-right")}} + browse {{icon("caret-right")}} log {{icon("caret-right")}}
{% endif %} {% if latest_tag %}
{{ latest_tag[0][len("refs/tags/"):] }}
- browse {{icon("caret-right")}} + browse {{icon("caret-right")}} .tar.gz {{icon("caret-right")}} announcement {{icon("caret-right")}}
diff --git a/gitsrht/templates/tree.html b/gitsrht/templates/tree.html index e607de2..aa8a5c6 100644 --- a/gitsrht/templates/tree.html +++ b/gitsrht/templates/tree.html @@ -1,18 +1,10 @@ {% extends "repo.html" %} +{% import "utils.html" as utils %} {% block content %}
- {% if path != [''] %} - {{repo.name}}{% endif %}/{% for part in path%}{% - if loop.last %}{{part}}{% else %}{{part}}/{% - endif %}{% endfor %} + {{ utils.breadcrumb(ref, repo, path, path_join) }}
{{commit.id.hex[:8]}} — diff --git a/gitsrht/templates/utils.html b/gitsrht/templates/utils.html new file mode 100644 index 0000000..50d9c56 --- /dev/null +++ b/gitsrht/templates/utils.html @@ -0,0 +1,18 @@ +{% macro breadcrumb(ref, repo, path, path_join) %} +{# TODO: Default branches other than master #} +{% if ref != "master" %} + + ref: {{ ref }} + +{% endif %} +{% if path != [''] %} +{{repo.name}}{% endif %}/{% for part in path%}{% + if loop.last %}{{part}}{% else %}{{part}}/{% +endif %}{% endfor %} +{% endmacro %} -- 2.38.4