Merge pull request #17 from msr1k/hash-mark-uri-encoding-for-branch-name

This commit is contained in:
Ali Almohaya 2023-06-17 04:36:42 +03:00 committed by GitHub
commit 53a6e414fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 4 deletions

View file

@ -22,6 +22,12 @@ function M.trim(string)
return (string:gsub("^%s*(.-)%s*$", "%1"))
end
-- url encode
-- see: https://datatracker.ietf.org/doc/html/rfc3986#section-2.3
function M.encode_uri_component(string)
return (string:gsub("[^%w_~%.%-]", function (c) return string.format("%%%02X", string.byte(c)) end))
end
-- returns a table with the host, user/org and the reponame given a github remote url
-- nil is returned when the url cannot be parsed
function M.parse_gh_remote(url)
@ -79,7 +85,7 @@ end
function M.get_current_branch_or_commit()
local current_branch = get_current_branch()
if current_branch ~= "HEAD" and M.is_branch_upstreamed(current_branch) then
return current_branch
return M.encode_uri_component(current_branch)
end
local commit_hash = get_current_commit_hash()
@ -87,7 +93,7 @@ function M.get_current_branch_or_commit()
return commit_hash
end
return M.get_default_branch()
return M.encode_uri_component(M.get_default_branch())
end
-- get the active buf relative file path form the .git
@ -96,9 +102,13 @@ function M.get_current_relative_file_path()
local absolute_file_path = vim.api.nvim_buf_get_name(0)
local git_path = vim.fn.system("git rev-parse --show-toplevel")
local relative_file_path = "/" .. string.sub(absolute_file_path, git_path:len() + 1)
local relative_file_path_components = M.split(string.sub(absolute_file_path, git_path:len() + 1), "/")
local encoded_components = {}
for i, path_component in pairs(relative_file_path_components) do
table.insert(encoded_components, i, M.encode_uri_component(path_component))
end
return relative_file_path
return "/" .. table.concat(encoded_components, '/')
end
-- get the line number in the buffer

View file

@ -26,3 +26,26 @@ describe("is_commit_upstreamed", function()
assert.is.True(output)
end)
end)
describe("encode_uri_component", function()
it("returns the given string without any change if it doesn't contain any uri reserved characters", function()
local input = "asdf_12345.QWER~yuio-hjkl"
local output = utils.encode_uri_component(input)
assert.is.Equal(input, output)
end)
it("returns an encoded string that all non-uri-unreserved characters are converted", function()
local input = ""
for i = 0, 127 do
input = input .. string.char(i)
end
local output = utils.encode_uri_component(input)
assert.is.Equal("%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F", output)
end)
it("returns encoded string for a non-ascii string input (UTF-8)", function()
local input = "ほげ" -- UTF-8 bytewise representation is "e3 81 bb e3 81 92"
local output = utils.encode_uri_component(input)
assert.is.Equal("%E3%81%BB%E3%81%92", output)
end)
end)