2024-10-21 14:45:37 +01:00
|
|
|
local remote_format = "%s/blob/%s/%s"
|
|
|
|
|
local bb_remote_format = "%s/src/%s/%s"
|
2024-10-21 14:50:15 +01:00
|
|
|
local forgejo_remote_format = "%s/src/%s/%s"
|
2022-09-26 02:09:22 +03:00
|
|
|
local utils = require("openingh.utils")
|
|
|
|
|
local M = {}
|
2022-09-25 23:53:23 +03:00
|
|
|
|
2022-09-26 02:09:22 +03:00
|
|
|
function M.setup()
|
2022-09-28 12:18:45 -04:00
|
|
|
-- get the current working directory and set the url
|
2023-12-15 17:30:24 +01:00
|
|
|
local current_buffer = vim.fn.expand("%:p:h"):gsub("%[", "\\["):gsub("%]", "\\]")
|
2022-10-10 01:16:44 +03:00
|
|
|
local repo_url = vim.fn.system("git -C " .. current_buffer .. " config --get remote.origin.url")
|
2022-09-26 02:09:22 +03:00
|
|
|
|
|
|
|
|
if repo_url:len() == 0 then
|
|
|
|
|
M.is_no_git_origin = true
|
|
|
|
|
vim.g.openingh = false
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-21 14:45:37 +01:00
|
|
|
local remote = utils.parse_remote(repo_url)
|
|
|
|
|
if remote == nil then
|
|
|
|
|
print("Error parsing remote URL")
|
2022-10-09 15:34:46 -04:00
|
|
|
vim.g.openingh = false
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-21 14:45:37 +01:00
|
|
|
M.repo_url = string.format("http://%s/%s/%s", remote.host, remote.user_or_org, remote.reponame)
|
2022-09-26 02:09:22 +03:00
|
|
|
end
|
|
|
|
|
|
2023-12-29 18:11:14 -06:00
|
|
|
M.priority = { BRANCH = 1, COMMIT = 2 }
|
2023-06-18 06:48:17 +09:00
|
|
|
|
|
|
|
|
local function get_current_branch_or_commit_with_priority(priority)
|
|
|
|
|
if priority == M.priority.BRANCH then
|
2023-06-17 20:40:36 +09:00
|
|
|
return utils.get_current_branch_or_commit()
|
2023-06-18 06:48:17 +09:00
|
|
|
elseif priority == M.priority.COMMIT then
|
2023-06-17 20:40:36 +09:00
|
|
|
return utils.get_current_commit_or_branch()
|
|
|
|
|
else
|
|
|
|
|
return utils.get_current_branch_or_commit()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-08-26 21:51:16 +02:00
|
|
|
function M.get_file_url(
|
2023-06-17 20:40:36 +09:00
|
|
|
priority,
|
2023-04-08 20:25:43 +02:00
|
|
|
--[[optional]]
|
2023-12-29 18:11:14 -06:00
|
|
|
branch,
|
|
|
|
|
--[[optional]]
|
2023-04-08 20:25:43 +02:00
|
|
|
range_start,
|
|
|
|
|
--[[optional]]
|
|
|
|
|
range_end
|
|
|
|
|
)
|
2022-09-28 12:18:45 -04:00
|
|
|
-- make sure to update the current directory
|
|
|
|
|
M.setup()
|
2022-09-26 02:09:22 +03:00
|
|
|
if M.is_no_git_origin then
|
|
|
|
|
utils.print_no_remote_message()
|
|
|
|
|
return
|
|
|
|
|
end
|
2022-09-26 22:57:58 +03:00
|
|
|
|
|
|
|
|
local file_path = utils.get_current_relative_file_path()
|
2022-09-26 23:57:02 +03:00
|
|
|
|
2022-09-27 00:21:42 +03:00
|
|
|
-- if there is no buffer opened
|
|
|
|
|
if file_path == "/" then
|
2023-04-09 01:35:54 +02:00
|
|
|
utils.notify("There is no active file to open!", vim.log.levels.ERROR)
|
2022-09-26 23:57:02 +03:00
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2023-12-29 18:11:14 -06:00
|
|
|
local rev = get_current_branch_or_commit_with_priority(priority)
|
|
|
|
|
if branch ~= nil then
|
|
|
|
|
rev = branch
|
|
|
|
|
end
|
2023-06-17 20:40:36 +09:00
|
|
|
|
2024-10-21 14:45:37 +01:00
|
|
|
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)
|
2024-10-21 14:50:15 +01:00
|
|
|
elseif string.find(M.repo_url, "forgejo") or string.find(M.repo_url, "gitea") then
|
2024-10-21 14:55:05 +01:00
|
|
|
utils.notify(file_path)
|
|
|
|
|
utils.notify(M.repo_url)
|
|
|
|
|
utils.notify(rev)
|
2024-10-21 14:50:15 +01:00
|
|
|
file_page_url = string.format(forgejo_remote_format, M.repo_url, rev, file_path)
|
2024-10-21 14:45:37 +01:00
|
|
|
else
|
|
|
|
|
file_page_url = string.format(remote_format, M.repo_url, rev, file_path)
|
|
|
|
|
end
|
2023-03-21 09:39:12 -04:00
|
|
|
|
2023-05-26 22:31:02 +03:00
|
|
|
if range_start and not range_end then
|
|
|
|
|
file_page_url = file_page_url .. "#L" .. range_start
|
|
|
|
|
end
|
|
|
|
|
|
2023-03-21 09:39:12 -04:00
|
|
|
if range_start and range_end then
|
2023-04-08 20:25:43 +02:00
|
|
|
file_page_url = file_page_url .. "#L" .. range_start .. "-L" .. range_end
|
2023-03-21 09:39:12 -04:00
|
|
|
end
|
|
|
|
|
|
2023-08-26 21:51:16 +02:00
|
|
|
return file_page_url
|
2022-09-26 02:09:22 +03:00
|
|
|
end
|
|
|
|
|
|
2023-08-26 21:51:16 +02:00
|
|
|
function M.get_repo_url(priority)
|
2022-09-28 12:18:45 -04:00
|
|
|
-- make sure to update the current directory
|
|
|
|
|
M.setup()
|
2022-09-26 02:09:22 +03:00
|
|
|
if M.is_no_git_origin then
|
|
|
|
|
utils.print_no_remote_message()
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2023-06-17 20:40:36 +09:00
|
|
|
local url = M.repo_url .. "/tree/" .. get_current_branch_or_commit_with_priority(priority)
|
2023-08-26 21:51:16 +02:00
|
|
|
return url
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.open_url(url)
|
2023-04-09 01:35:54 +02:00
|
|
|
if not utils.open_url(url) then
|
|
|
|
|
utils.notify("Could not open the built URL " .. url, vim.log.levels.ERROR)
|
2022-09-25 23:53:23 +03:00
|
|
|
end
|
2022-09-26 02:09:22 +03:00
|
|
|
end
|
2022-09-25 23:53:23 +03:00
|
|
|
|
|
|
|
|
return M
|