openingh.nvim/lua/openingh/init.lua
Maximilian Friedersdorff a0a5c6c360
Some checks failed
Tests / unit tests (push) Has been cancelled
lint with luacheck / Luacheck (push) Has been cancelled
Make it work with bitbucket
2024-10-21 14:45:37 +01:00

105 lines
2.6 KiB
Lua

local remote_format = "%s/blob/%s/%s"
local bb_remote_format = "%s/src/%s/%s"
local utils = require("openingh.utils")
local M = {}
function M.setup()
-- get the current working directory and set the url
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
M.is_no_git_origin = true
vim.g.openingh = false
return
end
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", remote.host, remote.user_or_org, remote.reponame)
end
M.priority = { BRANCH = 1, COMMIT = 2 }
local function get_current_branch_or_commit_with_priority(priority)
if priority == M.priority.BRANCH then
return utils.get_current_branch_or_commit()
elseif priority == M.priority.COMMIT then
return utils.get_current_commit_or_branch()
else
return utils.get_current_branch_or_commit()
end
end
function M.get_file_url(
priority,
--[[optional]]
branch,
--[[optional]]
range_start,
--[[optional]]
range_end
)
-- make sure to update the current directory
M.setup()
if M.is_no_git_origin then
utils.print_no_remote_message()
return
end
local file_path = utils.get_current_relative_file_path()
-- if there is no buffer opened
if file_path == "/" then
utils.notify("There is no active file to open!", vim.log.levels.ERROR)
return
end
local rev = get_current_branch_or_commit_with_priority(priority)
if branch ~= nil then
rev = branch
end
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
end
if range_start and range_end then
file_page_url = file_page_url .. "#L" .. range_start .. "-L" .. range_end
end
return file_page_url
end
function M.get_repo_url(priority)
-- make sure to update the current directory
M.setup()
if M.is_no_git_origin then
utils.print_no_remote_message()
return
end
local url = M.repo_url .. "/tree/" .. get_current_branch_or_commit_with_priority(priority)
return url
end
function M.open_url(url)
if not utils.open_url(url) then
utils.notify("Could not open the built URL " .. url, vim.log.levels.ERROR)
end
end
return M