diff options
context:
space:
mode:
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Convert the commit-links filter to Lua
Lua is becoming the one language for the filter extensions, and this was the last shell filter.
Diffstat (limited to 'extensions')
-rwxr-xr-xextensions/commit-links.sh28
-rw-r--r--extensions/link-commits.lua27
2 files changed, 27 insertions, 28 deletions
diff --git a/extensions/commit-links.sh b/extensions/commit-links.sh
deleted file mode 100755
index 796ac30..0000000
--- a/extensions/commit-links.sh
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/bin/sh
-# This script can be used to generate links in commit messages.
-#
-# To use this script, refer to this file with either the commit-filter or the
-# repo.commit-filter options in cgitrc.
-#
-# The following environment variables can be used to retrieve the configuration
-# of the repository for which this script is called:
-# CGIT_REPO_URL ( = repo.url setting )
-# CGIT_REPO_NAME ( = repo.name setting )
-# CGIT_REPO_PATH ( = repo.path setting )
-# CGIT_REPO_OWNER ( = repo.owner setting )
-# CGIT_REPO_DEFBRANCH ( = repo.defbranch setting )
-# CGIT_REPO_SECTION ( = section setting )
-# CGIT_REPO_CLONE_URL ( = repo.clone-url setting )
-#
-
-regex=''
-
-# This expression generates links to commits referenced by their SHA1.
-regex=$regex'
-s|\b([0-9a-fA-F]{7,64})\b|<a href="./?id=\1">\1</a>|g'
-
-# This expression generates links to a fictional bugtracker.
-regex=$regex'
-s|#([0-9]+)\b|<a href="http://bugs.example.com/?bug=\1">#\1</a>|g'
-
-sed -re "$regex"
diff --git a/extensions/link-commits.lua b/extensions/link-commits.lua
new file mode 100644
index 0000000..160a12d
--- /dev/null
+++ b/extensions/link-commits.lua
@@ -0,0 +1,27 @@
+-- Turns object hashes and issue references in commit messages into links.
+-- Use it with the commit-filter or repo.commit-filter setting and the lua:
+-- prefix. cgit hands the filter the message already HTML-escaped, so this
+-- only wraps matches in anchors.
+
+local buffer
+
+function filter_open(...)
+ buffer = ""
+end
+
+function filter_write(str)
+ buffer = buffer .. str
+end
+
+function filter_close()
+ -- Link a 7 to 64 character object name to this repository.
+ local out = buffer:gsub("%f[%w](%x+)%f[%W]", function(hex)
+ if #hex >= 7 and #hex <= 64 then
+ return '<a href="./?id=' .. hex .. '">' .. hex .. '</a>'
+ end
+ end)
+ -- Link "#123" to a bug tracker. Point this at your own.
+ out = out:gsub("#(%d+)", '<a href="https://bugs.example.com/?bug=%1">#%1</a>')
+ html(out)
+ return 0
+end