Skip to content

Commit 24915ec

Browse files
committed
fix(githubUrl): url-encoding of paths with special characters
1 parent 34aaab6 commit 24915ec

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

lua/tinygit/commands/github.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ end
1919

2020
--------------------------------------------------------------------------------
2121

22+
---@param unencoded string
23+
---@return string
24+
local function urlEncode(unencoded)
25+
local encoded = unencoded:gsub(
26+
"([^%w%-_.~])",
27+
function(c) return string.format("%%%02X", string.byte(c)) end
28+
)
29+
return encoded
30+
end
31+
2232
---opens current buffer in the browser & copies the link to the clipboard
2333
---normal mode: link to file
2434
---visual mode: link to selected lines
@@ -48,7 +58,6 @@ function M.githubUrl(what)
4858
local filepath = vim.api.nvim_buf_get_name(0)
4959
local gitroot = u.syncShellCmd { "git", "rev-parse", "--show-toplevel" }
5060
local pathInRepo = filepath:sub(#gitroot + 2)
51-
local pathInRepoEncoded = pathInRepo:gsub("%s+", "%%20")
5261
local hash = u.syncShellCmd { "git", "rev-parse", "HEAD" }
5362
local url = "https://github.com/" .. repo
5463
local location = ""
@@ -67,7 +76,7 @@ function M.githubUrl(what)
6776
end
6877
end
6978
local type = what == "blame" and "blame" or "blob"
70-
url = url .. ("/%s/%s/%s%s"):format(type, hash, pathInRepoEncoded, location)
79+
url = url .. ("/%s/%s/%s%s"):format(type, hash, urlEncode(pathInRepo), location)
7180

7281
vim.ui.open(url)
7382
vim.fn.setreg("+", url) -- copy to clipboard

0 commit comments

Comments
 (0)