summaryrefslogtreecommitdiffstats
path: root/pkgs
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/simple/cgit-about-filter/cgit-about-filter.lua34
1 files changed, 32 insertions, 2 deletions
diff --git a/pkgs/simple/cgit-about-filter/cgit-about-filter.lua b/pkgs/simple/cgit-about-filter/cgit-about-filter.lua
index 64e16fa..6e1ccdd 100644
--- a/pkgs/simple/cgit-about-filter/cgit-about-filter.lua
+++ b/pkgs/simple/cgit-about-filter/cgit-about-filter.lua
@@ -22,6 +22,30 @@ local function resolve(base, relative)
return prefix .. table.concat(segments, "/")
end
+-- The branch from the query string (?h=...), or nil. cgit doesn't pass it
+-- to filters, but Lua filters run in cgit's process, which has the CGI
+-- environment.
+local function query_head()
+ local query = os.getenv("QUERY_STRING") or ""
+ for key, value in query:gmatch("([^&=]+)=([^&]*)") do
+ if key == "h" then
+ value = value:gsub("%+", " "):gsub("%%(%x%x)", function(hex)
+ return string.char(tonumber(hex, 16))
+ end)
+ if value ~= "" then
+ return value
+ end
+ end
+ end
+ return nil
+end
+
+local function url_escape(s)
+ return (s:gsub("[^%w._~/-]", function(c)
+ return string.format("%%%02X", c:byte())
+ end))
+end
+
local buffer = ""
local should_render = true
local current_dir = "" -- Stores the directory prefix of the file being processed
@@ -67,6 +91,12 @@ function filter_close()
local repo = os.getenv("CGIT_REPO_URL") or ""
+ local head = query_head()
+ local query = ""
+ if head and head ~= os.getenv("CGIT_REPO_DEFBRANCH") then
+ query = "?h=" .. url_escape(head)
+ end
+
local processed = buffer:gsub("%[([^%]]+)%]%(([^:)]+)%)", function(text, path)
-- Ignore protocols (http, https, etc.), absolute paths (/), and anchors (#)
if path:sub(1, 1) == "/" or path:sub(1, 1) == "#" or path:match("^%w+:") then
@@ -79,9 +109,9 @@ function filter_close()
path = resolve(current_dir, path)
if path:sub(-3) == ".md" then
- return string.format("[%s](/%s/about/%s%s)", text, repo, path, fragment)
+ return string.format("[%s](/%s/about/%s%s%s)", text, repo, path, query, fragment)
else
- return string.format("[%s](/%s/tree/%s%s)", text, repo, path, fragment)
+ return string.format("[%s](/%s/tree/%s%s%s)", text, repo, path, query, fragment)
end
end)