[PATCH] Add permalink hyperlinks on tree and blame pages
Benjamin Linskey
git at linskey.org
Wed Aug 26 01:40:33 UTC 2026
This adds a "permalink" hyperlink that links to the current page with an
explicit commit hash value in the URL. This is useful when, for example, you'd
like to share a link to a file from the current tip of a branch that will not
change when subsequent commits are made to that branch.
Signed-off-by: Benjamin Linskey <git at linskey.org>
---
Thanks to Trevor Gross for suggesting this feature in an earlier message on the
list and providing feedback on this patch. As he pointed out to me, the
permalink will not preserve the URL fragment after a line number is clicked.
This could be remedied, but doing so would turn this from a trivial diff into
a substantially larger set of changes, so I've elected to share this simple but
still useful version for now.
ui-blame.c | 8 ++++++++
ui-tree.c | 8 ++++++++
2 files changed, 16 insertions(+)
diff --git a/ui-blame.c b/ui-blame.c
index 5c6f36e..6c7b4ce 100644
--- a/ui-blame.c
+++ b/ui-blame.c
@@ -157,10 +157,18 @@ static void print_object(const struct object_id *oid, const char *path,
cgit_set_title_from_path(path);
cgit_print_layout_start();
+
+ /* Get the commit hash for use in constructing a permalink. */
+ struct object_id rev_oid;
+ repo_get_oid(the_repository, rev, &rev_oid);
+
htmlf("blob: %s (", oid_to_hex(oid));
cgit_plain_link("plain", NULL, NULL, ctx.qry.head, rev, path);
html(") (");
cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path);
+ html(") (");
+ cgit_blame_link("permalink", NULL, NULL, ctx.qry.head,
+ oid_to_hex(&rev_oid), path);
html(")\n");
if (buffer_is_binary(buf, size)) {
diff --git a/ui-tree.c b/ui-tree.c
index 5ac8c9a..e9a46f4 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -111,6 +111,11 @@ static void print_object(const struct object_id *oid, const char *path, const ch
cgit_set_title_from_path(path);
cgit_print_layout_start();
+
+ /* Get the commit hash for use in constructing a permalink. */
+ struct object_id rev_oid;
+ repo_get_oid(the_repository, rev, &rev_oid);
+
htmlf("blob: %s (", oid_to_hex(oid));
cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
rev, path);
@@ -119,6 +124,9 @@ static void print_object(const struct object_id *oid, const char *path, const ch
cgit_blame_link("blame", NULL, NULL, ctx.qry.head,
rev, path);
}
+ html(") (");
+ cgit_tree_link("permalink", NULL, NULL, ctx.qry.head,
+ oid_to_hex(&rev_oid), path);
html(")\n");
if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
--
2.55.0
More information about the CGit
mailing list