~edwargix/git.sr.ht

f992ceccbf47a3e19ac45d17d624002a422fd2af — Drew DeVault 6 years ago 06b626d
Mock up tree page
M gitsrht/blueprints/repo.py => gitsrht/blueprints/repo.py +35 -11
@@ 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("/<owner>/<repo>")
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("/<owner>/<repo>/tree", defaults={"branch": None, "path": ""})
@repo.route("/<owner>/<repo>/tree/<branch>", defaults={"path": ""})
@repo.route("/<owner>/<repo>/tree/<branch>/<path>")
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)

M gitsrht/git.py => gitsrht/git.py +7 -0
@@ 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

M gitsrht/templates/repo.html => gitsrht/templates/repo.html +11 -2
@@ 9,12 9,21 @@
        >{{owner.canonical_name}}</a>/{{repo.name}}
      </h2>
      {% block tabs %}
      {% macro link(path, title) %}
      <a
        class="nav-link {% if view == title %}active{% endif %}"
        href="{{ path }}">{{ title }}</a>
      {% endmacro %}
      <ul class="nav nav-tabs">
        <li class="nav-item">
          <a class="nav-link active" href="#">summary</a>
          {{ link(url_for("repo.summary",
            owner=repo.owner.canonical_name,
            repo=repo.name), "summary") }}
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">tree</a>
          {{ link(url_for("repo.tree",
            owner=repo.owner.canonical_name,
            repo=repo.name), "tree") }}
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">log</a>

A gitsrht/templates/tree.html => gitsrht/templates/tree.html +42 -0
@@ 0,0 1,42 @@
{% extends "repo.html" %}
{% block content %}
<div class="header-extension">
  <div class="container">
    <span style="padding-left: 1rem">
      /{{path}}
    </span>
    <div class="pull-right">
      <a href="#">{{commit.id.hex[:8]}}</a> &mdash;
      <a href="#">{{commit.author.name}}</a>:
      {{trim_commit(commit.message)}}
      <span class="text-muted">
        {{ commit_time(commit) | date }}
      </span>
    </div>
  </div>
</div>
<div class="container">
  <div class="row" style="margin-bottom: 1rem">
    <div class="col-md-12">
      <div class="tree-list">
      {% for entry in tree %}
      {# TODO: Decode the mode #}
      <div class="mode">
        {#{{"{0:o}".format(entry.filemode)}}#}
        -rw-r--r--
      </div>
      <div class="name {{entry.type}}">
        <a href="#">{{entry.name}}{% if entry.type == "tree" %}/{% endif %}</a>
      </div>
      <div class="commit">
        <a href="#">{{trim_commit(commit.message)}}</a> &mdash;
        {{ commit_time(commit) | date }}
      </div>
      <div class="size">
        1234 bytes
      </div>
      {% endfor %}
      </div>
    </div>
</div>
{% endblock %}

M scss/main.scss => scss/main.scss +53 -0
@@ 9,3 9,56 @@
.repo-nav {
  margin-left: 1rem;
}

.header-extension {
  margin-top: -1rem;
  margin-bottom: 1rem;
  background: #ddd;
}

.tree-list {
  display: grid;
  // mode name
  grid-template-columns: auto minmax(1fr,auto) 1fr auto;
  font: monospace;

  svg {
    color: #777;
  }

  .size {
    text-align: right;
  }

  .name.blob a {
    color: $gray-900;
  }

  .commit, .commit a {
    color: $gray-600;
  }

  & > div {
    padding: 0.1rem 0.5rem;
    background: transparent;

    &.id {
      text-align: right;
    }

    &.comments {
      text-align: center;
    }

    @for $i from 1 through 4 {
      &:nth-child(4n+#{$i}) {
        grid-column-start: $i;
      }

      // Striped rows
      &:nth-child(8n+#{$i}) {
        background: rgba(0,0,0,.05);
      }
    }
  }
}