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
|
2022-10-10 01:16:44 +03:00
|
|
|
local current_buffer = vim.fn.expand("%:p:h")
|
|
|
|
|
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
|
|
|
|
|
|
2022-10-09 13:55:11 -04:00
|
|
|
local gh = utils.parse_gh_remote(repo_url)
|
2022-10-09 15:34:46 -04:00
|
|
|
if gh == nil then
|
|
|
|
|
print("Error parsing GitHub remote URL")
|
|
|
|
|
vim.g.openingh = false
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2022-10-29 18:15:40 +03:00
|
|
|
M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame)
|
2022-09-26 02:09:22 +03:00
|
|
|
end
|
|
|
|
|
|
2023-04-08 20:25:43 +02:00
|
|
|
function M.open_file(
|
|
|
|
|
--[[optional]]
|
|
|
|
|
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
|
2022-09-26 23:57:02 +03:00
|
|
|
print("There is no active file to open!")
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2023-04-08 20:25:43 +02:00
|
|
|
local file_page_url = M.repo_url .. "/blob/" .. utils.get_current_branch_or_commit() .. file_path
|
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
|
|
|
|
|
|
2022-09-26 22:57:58 +03:00
|
|
|
local result = utils.open_url(file_page_url)
|
|
|
|
|
|
2022-09-27 01:37:15 +03:00
|
|
|
if result == false then
|
2022-09-26 22:57:58 +03:00
|
|
|
print("Unknown OS please open report")
|
|
|
|
|
end
|
2022-09-26 02:09:22 +03:00
|
|
|
end
|
|
|
|
|
|
2023-04-08 20:30:49 +02:00
|
|
|
function M.open_repo()
|
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-27 00:44:01 +03:00
|
|
|
local current_branch_name_or_commit_hash = utils.get_current_branch_or_commit()
|
2022-09-26 21:43:50 +03:00
|
|
|
|
2022-09-27 00:44:01 +03:00
|
|
|
local repo_page_url = M.repo_url .. "/tree/" .. current_branch_name_or_commit_hash
|
2022-09-26 21:43:50 +03:00
|
|
|
|
2022-09-26 22:57:58 +03:00
|
|
|
local result = utils.open_url(repo_page_url)
|
2022-09-26 02:09:22 +03:00
|
|
|
|
2022-09-27 01:37:15 +03:00
|
|
|
if result == false then
|
2022-09-26 02:09:22 +03:00
|
|
|
print("Unknown OS please open report")
|
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
|