Compare commits

..

No commits in common. "a0a5c6c360cfa896655f7f0cd73ff88bc1dc9f90" and "cdca4f17dbc2ed85ea5e54c594eb57c4057d9290" have entirely different histories.

4 changed files with 22 additions and 52 deletions

View file

@ -33,9 +33,8 @@ CONTENTS *openingh*
:OpenInGHRepo :OpenInGHRepo
- Opens the project's git repository page in GitHub. - Opens the project's git repository page in GitHub.
:OpenInGHFile [git-rev] :OpenInGHFile
- Opens the current file page in GitHub. Providing an optional [git-rev] will override the - Opens the current file page in GitHub.
current branch/rev.
============================================================================== ==============================================================================
4. USAGE *openingh-usage* 4. USAGE *openingh-usage*
@ -48,10 +47,6 @@ CONTENTS *openingh*
-- for current file page -- for current file page
vim.api.nvim_set_keymap("n", "<Leader>gf", ":OpenInGHFile <CR>", { expr = true, noremap = true }) vim.api.nvim_set_keymap("n", "<Leader>gf", ":OpenInGHFile <CR>", { 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* 5. LICENSE *openingh-license*

View file

@ -1,11 +1,9 @@
local remote_format = "%s/blob/%s/%s"
local bb_remote_format = "%s/src/%s/%s"
local utils = require("openingh.utils") local utils = require("openingh.utils")
local M = {} local M = {}
function M.setup() function M.setup()
-- get the current working directory and set the url -- get the current working directory and set the url
local current_buffer = vim.fn.expand("%:p:h"):gsub("%[", "\\["):gsub("%]", "\\]") local current_buffer = vim.fn.expand("%:p:h")
local repo_url = vim.fn.system("git -C " .. current_buffer .. " config --get remote.origin.url") local repo_url = vim.fn.system("git -C " .. current_buffer .. " config --get remote.origin.url")
if repo_url:len() == 0 then if repo_url:len() == 0 then
@ -14,17 +12,17 @@ function M.setup()
return return
end end
local remote = utils.parse_remote(repo_url) local gh = utils.parse_gh_remote(repo_url)
if remote == nil then if gh == nil then
print("Error parsing remote URL") print("Error parsing GitHub remote URL")
vim.g.openingh = false vim.g.openingh = false
return return
end end
M.repo_url = string.format("http://%s/%s/%s", remote.host, remote.user_or_org, remote.reponame) M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame)
end end
M.priority = { BRANCH = 1, COMMIT = 2 } M.priority = { BRANCH = 1, COMMIT = 2, }
local function get_current_branch_or_commit_with_priority(priority) local function get_current_branch_or_commit_with_priority(priority)
if priority == M.priority.BRANCH then if priority == M.priority.BRANCH then
@ -39,8 +37,6 @@ end
function M.get_file_url( function M.get_file_url(
priority, priority,
--[[optional]] --[[optional]]
branch,
--[[optional]]
range_start, range_start,
--[[optional]] --[[optional]]
range_end range_end
@ -60,18 +56,8 @@ function M.get_file_url(
return return
end end
local rev = get_current_branch_or_commit_with_priority(priority)
if branch ~= nil then
rev = branch
end
local file_page_url = "" local file_page_url = M.repo_url .. "/blob/" .. get_current_branch_or_commit_with_priority(priority) .. file_path
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 if range_start and not range_end then
file_page_url = file_page_url .. "#L" .. range_start file_page_url = file_page_url .. "#L" .. range_start

View file

@ -25,23 +25,20 @@ end
-- url encode -- url encode
-- see: https://datatracker.ietf.org/doc/html/rfc3986#section-2.3 -- see: https://datatracker.ietf.org/doc/html/rfc3986#section-2.3
function M.encode_uri_component(string) function M.encode_uri_component(string)
return (string:gsub("[^%w_~%.%-]", function(c) return (string:gsub("[^%w_~%.%-]", function (c) return string.format("%%%02X", string.byte(c)) end))
return string.format("%%%02X", string.byte(c))
end))
end end
-- returns a table with the host, user/org and the reponame given a github remote url -- 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 -- nil is returned when the url cannot be parsed
function M.parse_remote(url) function M.parse_gh_remote(url)
-- 3 capture groups for host, org/user and repo. whitespace is trimmed -- 3 capture groups for host, org/user and repo. whitespace is trimmed
-- when cloning with http://, gh redirects to https://, but remote stays http -- when cloning with http://, gh redirects to https://, but remote stays http
local http = { string.find(url, "https?://([^/]*)/([^/]*)/([^%s/]*)") } local http = { string.find(url, "https?://([^/]*)/([^/]*)/([^%s/]*)") }
-- ssh url can be of type: -- ssh url can be of type:
-- git@some.github.com:user_or_org/reponame.git -- git@some.github.com:user_or_org/reponame.git
-- ssh://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 -- .* is used for ssh:// since lua matching doesn't support optional groups, only chars
local ssh = { string.find(url, ".*@(.*)[:/]([^/]*)/([^%s/]*)") } local ssh = { string.find(url, ".*git@(.*)[:/]([^/]*)/([^%s/]*)") }
local matches = http[1] == nil and ssh or http local matches = http[1] == nil and ssh or http
if matches[1] == nil then if matches[1] == nil then
@ -128,17 +125,13 @@ function M.get_current_relative_file_path()
local absolute_file_path = vim.api.nvim_buf_get_name(0) local absolute_file_path = vim.api.nvim_buf_get_name(0)
local git_path = vim.fn.system("git rev-parse --show-toplevel") 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 relative_file_path_components = M.split(string.sub(absolute_file_path, git_path:len() + 1), "/")
local encoded_components = {} local encoded_components = {}
for i, path_component in pairs(relative_file_path_components) do for i, path_component in pairs(relative_file_path_components) do
table.insert(encoded_components, i, M.encode_uri_component(path_component)) table.insert(encoded_components, i, M.encode_uri_component(path_component))
end end
return "/" .. table.concat(encoded_components, "/") return "/" .. table.concat(encoded_components, '/')
end end
-- get the line number in the buffer -- get the line number in the buffer

View file

@ -17,11 +17,10 @@ end
vim.api.nvim_create_user_command("OpenInGHFile", function(opts) vim.api.nvim_create_user_command("OpenInGHFile", function(opts)
local url local url
local branch = opts.fargs[1]
if opts.range == 0 then -- Nothing was selected if opts.range == 0 then -- Nothing was selected
url = openingh.get_file_url(judge_priority(opts.bang), branch) url = openingh.get_file_url(judge_priority(opts.bang))
else -- Current line or block was selected else -- Current line or block was selected
url = openingh.get_file_url(judge_priority(opts.bang), branch, opts.line1, opts.line2) url = openingh.get_file_url(judge_priority(opts.bang), opts.line1, opts.line2)
end end
if opts.reg == "" then if opts.reg == "" then
@ -31,20 +30,18 @@ vim.api.nvim_create_user_command("OpenInGHFile", function(opts)
print("URL put into register " .. opts.reg) print("URL put into register " .. opts.reg)
end end
end, { end, {
register = vim.g.openingh_copy_to_register, register = true,
range = true, range = true,
bang = true, bang = true,
nargs = "*",
}) })
vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts)
local url local url
local branch = opts.fargs[1]
if opts.range == 0 then -- Nothing was selected if opts.range == 0 then -- Nothing was selected
url = openingh.get_file_url(judge_priority(opts.bang), branch, opts.line1) url = openingh.get_file_url(judge_priority(opts.bang), opts.line1)
else -- Current line or block was selected else -- Current line or block was selected
url = openingh.get_file_url(judge_priority(opts.bang), branch, opts.line1, opts.line2) url = openingh.get_file_url(judge_priority(opts.bang), opts.line1, opts.line2)
end end
if opts.reg == "" then if opts.reg == "" then
@ -54,10 +51,9 @@ vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts)
print("URL put into register " .. opts.reg) print("URL put into register " .. opts.reg)
end end
end, { end, {
register = vim.g.openingh_copy_to_register, register = true,
range = true, range = true,
bang = true, bang = true,
nargs = "*",
}) })
vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) vim.api.nvim_create_user_command("OpenInGHRepo", function(opts)
@ -70,6 +66,6 @@ vim.api.nvim_create_user_command("OpenInGHRepo", function(opts)
print("URL put into register " .. opts.reg) print("URL put into register " .. opts.reg)
end end
end, { end, {
register = vim.g.openingh_copy_to_register, register = true,
bang = true, bang = true,
}) })