From 997c8832e3195e816ae1a47a85791ded26060b93 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Thu, 3 Dec 2026 14:37:55 -0600 Subject: [PATCH v1] Link commit hashes in message bodies. This commit teaches the message views to turn things that look like commit hashes into links to https://postgr.es/c/. Nothing checks that the commit exists, so the new filter only matches 7 to 40 hexadecimal digits that include both digits and letters and aren't part of a longer token (e.g., a message-id). The "index" lines of inline diffs are skipped, too. Hashes posted to most lists are unlikely to refer to commits in the main repository, so links are only added in threads on the lists named by the new COMMIT_LINK_LISTS setting, which defaults to pgsql-bugs, pgsql-committers, pgsql-docs, and pgsql-hackers. --- .../mailarchives/templates/_message.html | 2 +- .../mailarchives/templatetags/pgfilters.py | 35 +++++++++++++++++++ django/archives/settings.py | 3 ++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/django/archives/mailarchives/templates/_message.html b/django/archives/mailarchives/templates/_message.html index 5b7404a..a9157bf 100644 --- a/django/archives/mailarchives/templates/_message.html +++ b/django/archives/mailarchives/templates/_message.html @@ -61,7 +61,7 @@ {%if msg.hiddenstatus%}
{{msg.hiddenreason}}
{%else%} -
{{msg.bodytxt|hideallemail|urlize|linebreaks}}
+
{{msg.bodytxt|hideallemail|urlize|commitlinks:lists|linebreaks}}
{%if msg.has_attachment%} diff --git a/django/archives/mailarchives/templatetags/pgfilters.py b/django/archives/mailarchives/templatetags/pgfilters.py index 2f1d630..e157f0e 100644 --- a/django/archives/mailarchives/templatetags/pgfilters.py +++ b/django/archives/mailarchives/templatetags/pgfilters.py @@ -1,5 +1,7 @@ from django.template.defaultfilters import stringfilter from django import template +from django.conf import settings +from django.utils.safestring import mark_safe from email.utils import parseaddr import re @@ -57,3 +59,36 @@ def nameonly(value): @stringfilter def md5(value): return hashlib.md5(value.encode('utf8')).hexdigest() + + +# Things that look like (possibly abbreviated) commit hashes. The lookarounds +# skip hex strings that are part of a longer token, such as a message-id, a +# file name or an LSN, and the blob hashes on the "index" lines of diffs. +_re_commit = re.compile(r'(?]*>.*?)', re.S) + + +def _link_commit(match): + # Nothing checks that the commit exists, so skip plain numbers and words. + # Nearly all real hashes have both digits and letters. + value = match.group(0) + if value.isdigit() or value.isalpha(): + return value + return '{0}'.format(value) + + +@register.filter(name='commitlinks', is_safe=True) +@stringfilter +def commitlinks(value, lists): + # Hashes posted to other lists are less likely to be commits in the main + # repository, so only link in threads that appear on one of these lists. + if not any(l.listname in settings.COMMIT_LINK_LISTS for l in lists): + return value + + # This runs on the output of urlize. Splitting on the links it made + # leaves the text between them at the even positions, and only that text + # is searched. + parts = _re_anchor.split(value) + for i in range(0, len(parts), 2): + parts[i] = _re_commit.sub(_link_commit, parts[i]) + return mark_safe(''.join(parts)) diff --git a/django/archives/settings.py b/django/archives/settings.py index 4c32b8b..be48ec7 100644 --- a/django/archives/settings.py +++ b/django/archives/settings.py @@ -153,6 +153,9 @@ ALLOW_RESEND = False PGWEB_ADDRESS = 'https://www.postgresql.org' +# Lists on which things that look like commit hashes are turned into links +COMMIT_LINK_LISTS = ['pgsql-bugs', 'pgsql-committers', 'pgsql-docs', 'pgsql-hackers'] + # Bumping this number in local_settings.py will invalidate *all* etags, as a way to force refreshes # (etags are also invalidated if the git revision changes). NOTE! Must be a byte string, not str! ETAG_REVISION_NUMBER = b"0" -- 2.54.0 (Apple Git-157)