~edwargix/git.sr.ht

98568b137743eed35712bae0644b59c1eed84114 — Robin Jarry 2 years ago 4bddca8
log: display clickable links

Replace explicit urls or email addresses by clickable links. Reuse the
existing regular expression in srht/markdown.py.

Detect references to valid commit ids and replace them by links to
git.sr.ht commits.

Signed-off-by: Robin Jarry <robin@jarry.cc>
3 files changed, 30 insertions(+), 3 deletions(-)

M gitsrht/app.py
M gitsrht/git.py
M gitsrht/templates/utils.html
M gitsrht/app.py => gitsrht/app.py +2 -1
@@ 3,7 3,7 @@ import os
import stat
from functools import lru_cache
from gitsrht import urls
from gitsrht.git import commit_time, trim_commit, signature_time
from gitsrht.git import commit_time, commit_links, trim_commit, signature_time
from gitsrht.repos import GitRepoApi
from gitsrht.service import oauth_service, webhooks_notify
from gitsrht.types import Access, Redirect, Repository, User


@@ 46,6 46,7 @@ class GitApp(ScmSrhtFlask):
        self.add_template_filter(urls.log_rss_url)
        self.add_template_filter(urls.refs_rss_url)
        self.add_template_filter(url_quote)
        self.add_template_filter(commit_links)

        @self.context_processor
        def inject():

M gitsrht/git.py => gitsrht/git.py +27 -1
@@ 4,7 4,9 @@ from pygit2 import Repository as GitRepository, Tag
from jinja2 import Markup, escape
from stat import filemode
import pygit2
import json
import json, re
from srht.config import get_origin
from srht.markdown import PlainLink

def strip_pgp_signature(text):
    if not text.strip().endswith("-----END PGP SIGNATURE-----"):


@@ 33,6 35,30 @@ def commit_time(commit):
    author = commit.author if hasattr(commit, 'author') else commit.get_object().author
    return signature_time(author)

_gitsrht = get_origin('git.sr.ht')
_commit_id_re = re.compile( r'\b[a-f0-9]{7,40}\b')

def commit_links(message, repo):
    def url_or_mail(match):
        mail = match['mail']
        if mail:
            return f'<a href="mailto:{mail}">{mail}</a>'
        url = match['url']
        return f'<a href="{url}">{url}</a>'

    msg = PlainLink.pattern.sub(url_or_mail, escape(message))

    repo_url = f'{_gitsrht}/{repo.owner.canonical_name}/{repo.name}'
    def commit_link(match):
        try:
            commit = repo.git_repo.revparse_single(match[0])
            return f'<a href="{repo_url}/commit/{commit.id}">{match[0]}</a>'
        except (KeyError, ValueError):
            # not a valid commit id in this repository, ignore
            return match[0]

    return _commit_id_re.sub(commit_link, msg)

def _get_ref(repo, ref):
    return repo._get(ref)


M gitsrht/templates/utils.html => gitsrht/templates/utils.html +1 -1
@@ 145,7 145,7 @@ endif %}{% endfor %}
</div>
{% if not skip_body %}
{% if full_body %}
<pre class="commit">{{c.message}}
<pre class="commit">{{ c.message | commit_links(repo) | safe }}
{%- if diff %}
{{diffstat(diff, anchor=c.oid.hex + "-")}}{% endif -%}
</pre>