~edwargix/git.sr.ht

9f64a0c4b4701fc8326dedd64f486b3b49ee638d — Drew DeVault 6 years ago 5a6c079
Basic git log support
M gitsrht/blueprints/repo.py => gitsrht/blueprints/repo.py +46 -0
@@ 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("/<owner>/<repo>/log", defaults={"ref": None, "path": ""})
@repo.route("/<owner>/<repo>/log/<ref>", defaults={"path": ""})
@repo.route("/<owner>/<repo>/log/<ref>/<path:path>")
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)

A gitsrht/templates/log.html => gitsrht/templates/log.html +14 -0
@@ 0,0 1,14 @@
{% extends "repo.html" %}
{% import "utils.html" as utils %}
{% block content %}
<div class="container">
  <div class="row">
    <div class="col-md-12">
      <div class="event-list">
        {% for c in commits %}
        {{ utils.commit_event(c, commit_time, None, True, refs) }}
        {% endfor %}
      </div>
    </div>
</div>
{% endblock %}

M gitsrht/templates/repo.html => gitsrht/templates/repo.html +3 -1
@@ 26,7 26,9 @@
            repo=repo.name), "tree")}}
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">log</a>
          {{link(url_for("repo.log",
            owner=repo.owner.canonical_name,
            repo=repo.name), "log")}}
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">refs</a>

M gitsrht/templates/summary.html => gitsrht/templates/summary.html +2 -13
@@ 1,4 1,5 @@
{% extends "repo.html" %}
{% import "utils.html" as utils %}
{% block content %}
{% if repo.description %}
<div class="header-extension">


@@ 12,18 13,7 @@
    <div class="col-md-6">
      <div class="event-list" style="margin-bottom: 0.5rem">
        {% for c in commits %}
        <div class="event">
          <div>
            <a href="#">{{c.id.hex[:8]}}</a> &mdash;
            <a href="#">{{c.author.name}}</a>
            <span class="text-muted pull-right">
              {{ commit_time(c) | date }}
            </span>
          </div>
          <pre
            style="padding-left: 0; padding-right: 0; background: transparent"
          >{{ trim_commit(c.message) }}</pre>
        </div>
        {{ utils.commit_event(c, commit_time, trim_commit) }}
        {% endfor %}
      </div>
    </div>


@@ 48,7 38,6 @@
          <a href="{{url_for("repo.archive", owner=repo.owner.canonical_name,
              repo=repo.name, ref=latest_tag[0][len("refs/tags/"):])}}"
          >.tar.gz {{icon("caret-right")}}</a>
          <a href="#">announcement {{icon("caret-right")}}</a>
        </dd>
        {% endif %}
      </dl>

M gitsrht/templates/utils.html => gitsrht/templates/utils.html +28 -0
@@ 16,3 16,31 @@
  >{{part}}</a>/{%
endif %}{% endfor %}
{% endmacro %}

{% macro commit_event(c, commit_time, trim_commit, full_body=False, refs={}) %}
<div class="event">
  <div>
    <a href="#">{{c.id.hex[:8]}}</a> &mdash;
    <a href="#">{{c.author.name}}</a>
    <span class="text-muted pull-right">
      {{ commit_time(c) | date }}
    </span>
    {% if c.id.hex in refs %}
    {% for ref in refs[c.id.hex] %}
    <span class="ref {{ref.type}}">
      {{ref.name}}
    </span>
    {% endfor %}
    {% endif %}
  </div>
  {% if full_body %}
  <pre
    style="padding-left: 0; padding-right: 0; background: transparent"
  >{{c.message}}</pre>
  {% else %}
  <pre
    style="padding-left: 0; padding-right: 0; background: transparent"
  >{{ trim_commit(c.message) }}</pre>
  {% endif %}
</div>
{% endmacro %}