Compare commits

..

11 commits

Author SHA1 Message Date
Maximilian Friedersdorff
a0a5c6c360 Make it work with bitbucket
Some checks failed
Tests / unit tests (push) Has been cancelled
lint with luacheck / Luacheck (push) Has been cancelled
2024-10-21 14:45:37 +01:00
Ali Almohaya
613c18967d
Merge pull request #29 from AnsonH/main
fix: backward slash in url when using Windows
2024-03-10 22:48:37 +03:00
AnsonH
4a0d169e90 fix: backward slash in url when using Windows 2024-02-17 13:17:33 +08:00
Ali Almohaya
52e2727cbd
Merge pull request #27 from Devidxyz/fix/escape_bracket_in_path
Fix: escape bracket in path
2024-02-02 22:45:47 +03:00
Ali Almohaya
46e143bb57
Merge pull request #28 from BSathvik/vik/add-branch-override
Add optional argument to override the current branch
2024-02-02 22:41:56 +03:00
Sathvik Birudavolu
935009f81b rm format 2023-12-29 18:13:07 -06:00
Sathvik Birudavolu
02e6036f39 add override branch option 2023-12-29 18:11:14 -06:00
Devidxyz
0887d7fa53 Put empty line back 2023-12-15 17:32:17 +01:00
Devidxyz
d20131133f Escape square brackets in path 2023-12-15 17:30:24 +01:00
Ali Almohaya
5c9e851d7c
Merge pull request #25 from bezhermoso/alt-ssh-username 2023-10-31 14:36:49 +03:00
Bez Hermoso
8ee6a3cf3b
feat: Support alternate SSH usernames i.e. non-standard GitHub Enterprise setups 2023-10-25 23:52:12 -07:00
4 changed files with 52 additions and 22 deletions

View file

@ -33,8 +33,9 @@ CONTENTS *openingh*
:OpenInGHRepo :OpenInGHRepo
- Opens the project's git repository page in GitHub. - Opens the project's git repository page in GitHub.
:OpenInGHFile :OpenInGHFile [git-rev]
- Opens the current file page in GitHub. - Opens the current file page in GitHub. Providing an optional [git-rev] will override the
current branch/rev.
============================================================================== ==============================================================================
4. USAGE *openingh-usage* 4. USAGE *openingh-usage*
@ -47,6 +48,10 @@ 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,9 +1,11 @@
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") 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") 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
@ -12,17 +14,17 @@ function M.setup()
return return
end end
local gh = utils.parse_gh_remote(repo_url) local remote = utils.parse_remote(repo_url)
if gh == nil then if remote == nil then
print("Error parsing GitHub remote URL") print("Error parsing remote URL")
vim.g.openingh = false vim.g.openingh = false
return return
end 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 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
@ -37,6 +39,8 @@ 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
@ -56,8 +60,18 @@ 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 = M.repo_url .. "/blob/" .. get_current_branch_or_commit_with_priority(priority) .. 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 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,20 +25,23 @@ 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.format("%%%02X", string.byte(c)) end)) return (string:gsub("[^%w_~%.%-]", function(c)
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_gh_remote(url) function M.parse_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, ".*git@(.*)[:/]([^/]*)/([^%s/]*)") } local ssh = { string.find(url, ".*@(.*)[:/]([^/]*)/([^%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
@ -125,13 +128,17 @@ 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,10 +17,11 @@ 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)) url = openingh.get_file_url(judge_priority(opts.bang), branch)
else -- Current line or block was selected 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, opts.line2)
end end
if opts.reg == "" then if opts.reg == "" then
@ -30,18 +31,20 @@ 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 = true, register = vim.g.openingh_copy_to_register,
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), opts.line1) url = openingh.get_file_url(judge_priority(opts.bang), branch, 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), opts.line1, opts.line2) url = openingh.get_file_url(judge_priority(opts.bang), branch, opts.line1, opts.line2)
end end
if opts.reg == "" then if opts.reg == "" then
@ -51,9 +54,10 @@ 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 = true, register = vim.g.openingh_copy_to_register,
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)
@ -66,6 +70,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 = true, register = vim.g.openingh_copy_to_register,
bang = true, bang = true,
}) })