diff options
| author | tv <tv@krebsco.de> | 2026-11-12 17:51:42 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-12 18:56:06 +0100 |
| commit | 4f3bc08757f73e9a282ea41c72816610d7e1d7d4 (patch) | |
| tree | 2e5b8117dc586c11e012e36a88266c37bce86843 /pkgs/simple | |
| parent | 17105dc54db3f6e28ce8e987eb0d870304669bb9 (diff) | |
cgit-about-filter: keep the branch in links
Diffstat (limited to 'pkgs/simple')
| -rw-r--r-- | pkgs/simple/cgit-about-filter/cgit-about-filter.lua | 34 |
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) |
