~edwargix/git.sr.ht

1aabd8cfc3886ef0a65547328c687bcdd08cf5fc — Drew DeVault 6 years ago 553e0f5
Add support for browsing refs other than master
M gitsrht/blueprints/repo.py => gitsrht/blueprints/repo.py +22 -24
@@ 86,26 86,18 @@ def summary(owner, repo):
            clone_urls=clone_urls, latest_tag=latest_tag,
            default_branch=default_branch)

@repo.route("/<owner>/<repo>/tree", defaults={"ref": None, "path": ""})
@repo.route("/<owner>/<repo>/tree/<ref>", defaults={"path": ""})
@repo.route("/<owner>/<repo>/tree/<ref>/<path:path>")
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("/<owner>/<repo>/tree", defaults={"ref": None, "path": ""})
@repo.route("/<owner>/<repo>/tree/<ref>", defaults={"path": ""})
@repo.route("/<owner>/<repo>/tree/<ref>/<path:path>")
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("/<owner>/<repo>/blob/<branch>/<path:path>")
def raw_blob(owner, repo, branch, path):
@repo.route("/<owner>/<repo>/blob/<ref>/<path:path>")
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

M gitsrht/templates/blob.html => gitsrht/templates/blob.html +3 -12
@@ 1,18 1,10 @@
{% extends "repo.html" %}
{% import "utils.html" as utils %}
{% block content %}
<div class="header-extension">
  <div class="container-fluid">
    <span style="padding-left: 1rem">
      {% 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 %}
      {{ utils.breadcrumb(ref, repo, path, path_join) }}
      <span class="text-muted" style="margin-left: 1rem">
        <span title="{{"{0:0o}".format(entry.filemode)}}">
          {{stat.filemode(entry.filemode)}}


@@ 26,8 18,7 @@
      </span>
      <span class="text-muted" style="margin-left: 1rem">
        <a href="{{url_for("repo.raw_blob", owner=repo.owner.canonical_name,
            repo=repo.name, branch=branch_name,
            path=path_join(*path))}}">
            repo=repo.name, ref=ref, path=path_join(*path))}}">
          View raw
        </a>
      </span>

M gitsrht/templates/summary.html => gitsrht/templates/summary.html +6 -2
@@ 33,14 33,18 @@
        {% if default_branch %}
        <dt>{{default_branch.name[len("refs/heads/"):]}}</dt>
        <dd>
          <a href="#">browse {{icon("caret-right")}}</a>
          <a href="{{url_for("repo.tree",
              owner=repo.owner.canonical_name, repo=repo.name)}}"
          >browse {{icon("caret-right")}}</a>
          <a href="#">log {{icon("caret-right")}}</a>
        </dd>
        {% endif %}
        {% if latest_tag %}
        <dt>{{ latest_tag[0][len("refs/tags/"):] }}</dt>
        <dd>
          <a href="#">browse {{icon("caret-right")}}</a>
          <a href="{{url_for("repo.tree", owner=repo.owner.canonical_name,
              repo=repo.name, ref=latest_tag[0][len("refs/tags/"):])}}"
          >browse {{icon("caret-right")}}</a>
          <a href="#">.tar.gz {{icon("caret-right")}}</a>
          <a href="#">announcement {{icon("caret-right")}}</a>
        </dd>

M gitsrht/templates/tree.html => gitsrht/templates/tree.html +2 -10
@@ 1,18 1,10 @@
{% extends "repo.html" %}
{% import "utils.html" as utils %}
{% block content %}
<div class="header-extension">
  <div class="container-fluid">
    <span style="padding-left: 1rem">
      {% if path != [''] %}
      <a href="{{url_for("repo.tree",
        owner=repo.owner.canonical_name, repo=repo.name, ref=ref)}}"
      >{{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, ref=ref,
            path=path_join(*path[:loop.index]))}}"
        >{{part}}</a>/{%
      endif %}{% endfor %}
      {{ utils.breadcrumb(ref, repo, path, path_join) }}
    </span>
    <div class="pull-right">
      <a href="#">{{commit.id.hex[:8]}}</a> &mdash;

A gitsrht/templates/utils.html => gitsrht/templates/utils.html +18 -0
@@ 0,0 1,18 @@
{% macro breadcrumb(ref, repo, path, path_join) %}
{# TODO: Default branches other than master #}
{% if ref != "master" %}
<span style="margin-right: 1rem">
  <span class="text-muted">ref:</span> {{ ref }}
</span>
{% endif %}
{% if path != [''] %}
<a href="{{url_for("repo.tree",
  owner=repo.owner.canonical_name, repo=repo.name, ref=ref)}}"
>{{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, ref=ref,
      path=path_join(*path[:loop.index]))}}"
  >{{part}}</a>/{%
endif %}{% endfor %}
{% endmacro %}