From 8ee6a3cf3b9647623d0abee76b0303b379c36723 Mon Sep 17 00:00:00 2001 From: Bez Hermoso Date: Mon, 11 Sep 2023 10:09:10 -0700 Subject: [PATCH 1/7] feat: Support alternate SSH usernames i.e. non-standard GitHub Enterprise setups --- lua/openingh/utils.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index 30a58e9..10ddca6 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -37,8 +37,9 @@ function M.parse_gh_remote(url) -- ssh url can be of type: -- git@some.github.com:user_or_org/reponame.git -- ssh://git@some.github.com/user_or_org/reponame.git + -- ssh://org-12345@some.github.com/org/reponame.git -- .* is used for ssh:// since lua matching doesn't support optional groups, only chars - local ssh = { string.find(url, ".*git@(.*)[:/]([^/]*)/([^%s/]*)") } + local ssh = { string.find(url, ".*@(.*)[:/]([^/]*)/([^%s/]*)") } local matches = http[1] == nil and ssh or http if matches[1] == nil then From d20131133fe3cbe58662f0297d523c59b66d362e Mon Sep 17 00:00:00 2001 From: Devidxyz Date: Fri, 15 Dec 2023 17:30:24 +0100 Subject: [PATCH 2/7] Escape square brackets in path --- lua/openingh/init.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index c8b0253..a6b5b1e 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -3,7 +3,7 @@ local M = {} function M.setup() -- get the current working directory and set the url - local current_buffer = vim.fn.expand("%:p:h") + local current_buffer = vim.fn.expand("%:p:h"):gsub("%[", "\\["):gsub("%]", "\\]") local repo_url = vim.fn.system("git -C " .. current_buffer .. " config --get remote.origin.url") if repo_url:len() == 0 then @@ -56,7 +56,6 @@ function M.get_file_url( return end - local file_page_url = M.repo_url .. "/blob/" .. get_current_branch_or_commit_with_priority(priority) .. file_path if range_start and not range_end then From 0887d7fa532cee12d989a694a3bb0465ab069e10 Mon Sep 17 00:00:00 2001 From: Devidxyz Date: Fri, 15 Dec 2023 17:32:17 +0100 Subject: [PATCH 3/7] Put empty line back --- lua/openingh/init.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index a6b5b1e..4b53a15 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -56,6 +56,7 @@ function M.get_file_url( return end + local file_page_url = M.repo_url .. "/blob/" .. get_current_branch_or_commit_with_priority(priority) .. file_path if range_start and not range_end then From 02e6036f39bf766a608b92aa1800acd85112d983 Mon Sep 17 00:00:00 2001 From: Sathvik Birudavolu <8377116+BSathvik@users.noreply.github.com> Date: Fri, 29 Dec 2023 18:11:14 -0600 Subject: [PATCH 4/7] add override branch option --- doc/openingh.txt | 9 +++++++-- lua/openingh/init.lua | 10 ++++++++-- plugin/openingh.lua | 22 +++++++++++++--------- tests/utils_spec.lua | 7 +++++-- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/doc/openingh.txt b/doc/openingh.txt index 21e781f..09fbebb 100644 --- a/doc/openingh.txt +++ b/doc/openingh.txt @@ -33,8 +33,9 @@ CONTENTS *openingh* :OpenInGHRepo - Opens the project's git repository page in GitHub. - :OpenInGHFile - - Opens the current file page in GitHub. + :OpenInGHFile [git-rev] + - Opens the current file page in GitHub. Providing an optional [git-rev] will override the + current branch/rev. ============================================================================== 4. USAGE *openingh-usage* @@ -47,6 +48,10 @@ CONTENTS *openingh* -- for current file page vim.api.nvim_set_keymap("n", "gf", ":OpenInGHFile ", { expr = true, noremap = true }) + To copy the URL into a register set + `vim.g.openingh_copy_to_register = true` + `OpenInGHFile a` should place the URL into register `a` + ============================================================================== 5. LICENSE *openingh-license* diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index c8b0253..6ba2b70 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -22,7 +22,7 @@ function M.setup() M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame) end -M.priority = { BRANCH = 1, COMMIT = 2, } +M.priority = { BRANCH = 1, COMMIT = 2 } local function get_current_branch_or_commit_with_priority(priority) if priority == M.priority.BRANCH then @@ -37,6 +37,8 @@ end function M.get_file_url( priority, --[[optional]] + branch, + --[[optional]] range_start, --[[optional]] range_end @@ -56,8 +58,12 @@ function M.get_file_url( return end + local rev = get_current_branch_or_commit_with_priority(priority) + if branch ~= nil then + rev = branch + end - local file_page_url = M.repo_url .. "/blob/" .. get_current_branch_or_commit_with_priority(priority) .. file_path + local file_page_url = M.repo_url .. "/blob/" .. rev .. file_path if range_start and not range_end then file_page_url = file_page_url .. "#L" .. range_start diff --git a/plugin/openingh.lua b/plugin/openingh.lua index 783d64b..d5978ac 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -17,10 +17,11 @@ end vim.api.nvim_create_user_command("OpenInGHFile", function(opts) local url + local branch = opts.fargs[1] if opts.range == 0 then -- Nothing was selected - url = openingh.get_file_url(judge_priority(opts.bang)) - else -- Current line or block was selected - url = openingh.get_file_url(judge_priority(opts.bang), opts.line1, opts.line2) + url = openingh.get_file_url(judge_priority(opts.bang), branch) + else -- Current line or block was selected + url = openingh.get_file_url(judge_priority(opts.bang), branch, opts.line1, opts.line2) end if opts.reg == "" then @@ -30,18 +31,20 @@ vim.api.nvim_create_user_command("OpenInGHFile", function(opts) print("URL put into register " .. opts.reg) end end, { - register = true, + register = vim.g.openingh_copy_to_register, range = true, bang = true, + nargs = "*", }) vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) local url + local branch = opts.fargs[1] if opts.range == 0 then -- Nothing was selected - url = openingh.get_file_url(judge_priority(opts.bang), opts.line1) - else -- Current line or block was selected - url = openingh.get_file_url(judge_priority(opts.bang), opts.line1, opts.line2) + url = openingh.get_file_url(judge_priority(opts.bang), branch, opts.line1) + else -- Current line or block was selected + url = openingh.get_file_url(judge_priority(opts.bang), branch, opts.line1, opts.line2) end if opts.reg == "" then @@ -51,9 +54,10 @@ vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) print("URL put into register " .. opts.reg) end end, { - register = true, + register = vim.g.openingh_copy_to_register, range = true, bang = true, + nargs = "*", }) vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) @@ -66,6 +70,6 @@ vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) print("URL put into register " .. opts.reg) end end, { - register = true, + register = vim.g.openingh_copy_to_register, bang = true, }) diff --git a/tests/utils_spec.lua b/tests/utils_spec.lua index b3377b9..acb1acc 100644 --- a/tests/utils_spec.lua +++ b/tests/utils_spec.lua @@ -40,11 +40,14 @@ describe("encode_uri_component", function() 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) + 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 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) From 935009f81b3190d7b1baf4905f249019e7419553 Mon Sep 17 00:00:00 2001 From: Sathvik Birudavolu <8377116+BSathvik@users.noreply.github.com> Date: Fri, 29 Dec 2023 18:13:07 -0600 Subject: [PATCH 5/7] rm format --- tests/utils_spec.lua | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/utils_spec.lua b/tests/utils_spec.lua index acb1acc..b3377b9 100644 --- a/tests/utils_spec.lua +++ b/tests/utils_spec.lua @@ -40,14 +40,11 @@ describe("encode_uri_component", function() 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 - ) + 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 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) From 4a0d169e905b082a82f37f7dedddfe044340d443 Mon Sep 17 00:00:00 2001 From: AnsonH Date: Sat, 17 Feb 2024 13:17:33 +0800 Subject: [PATCH 6/7] fix: backward slash in url when using Windows --- lua/openingh/utils.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index 10ddca6..e1f8ae5 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -25,7 +25,9 @@ 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)) + 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 @@ -126,13 +128,17 @@ 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") + if vim.fn.has("win32") == 1 then + absolute_file_path = string.gsub(absolute_file_path, "\\", "/") + end + 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 "/" .. table.concat(encoded_components, '/') + return "/" .. table.concat(encoded_components, "/") end -- get the line number in the buffer From a0a5c6c360cfa896655f7f0cd73ff88bc1dc9f90 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Mon, 21 Oct 2024 14:45:37 +0100 Subject: [PATCH 7/7] Make it work with bitbucket --- lua/openingh/init.lua | 18 +++++++++++++----- lua/openingh/utils.lua | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index dbda762..dfb8ad9 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -1,3 +1,5 @@ +local remote_format = "%s/blob/%s/%s" +local bb_remote_format = "%s/src/%s/%s" local utils = require("openingh.utils") local M = {} @@ -12,14 +14,14 @@ function M.setup() return end - local gh = utils.parse_gh_remote(repo_url) - if gh == nil then - print("Error parsing GitHub remote URL") + local remote = utils.parse_remote(repo_url) + if remote == nil then + print("Error parsing remote URL") vim.g.openingh = false return end - M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame) + M.repo_url = string.format("http://%s/%s/%s", remote.host, remote.user_or_org, remote.reponame) end M.priority = { BRANCH = 1, COMMIT = 2 } @@ -63,7 +65,13 @@ function M.get_file_url( rev = branch end - local file_page_url = M.repo_url .. "/blob/" .. rev .. file_path + local file_page_url = "" + + if string.find(M.repo_url, "bitbucket") then + file_page_url = string.format(bb_remote_format, M.repo_url, rev, file_path) + else + file_page_url = string.format(remote_format, M.repo_url, rev, file_path) + end if range_start and not range_end then file_page_url = file_page_url .. "#L" .. range_start diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index e1f8ae5..5164453 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -32,7 +32,7 @@ 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) +function M.parse_remote(url) -- 3 capture groups for host, org/user and repo. whitespace is trimmed -- when cloning with http://, gh redirects to https://, but remote stays http local http = { string.find(url, "https?://([^/]*)/([^/]*)/([^%s/]*)") }