2022-09-26 02:09:22 +03:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
|
|
-- the missing split lua method to split a string
|
|
|
|
|
function M.split(string, char)
|
|
|
|
|
local array = {}
|
|
|
|
|
local reg = string.format("([^%s]+)", char)
|
|
|
|
|
for mem in string.gmatch(string, reg) do
|
|
|
|
|
table.insert(array, mem)
|
|
|
|
|
end
|
|
|
|
|
return array
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-26 21:43:50 +03:00
|
|
|
-- trim extra spaces and newlines
|
|
|
|
|
-- useful when working with git commands returned values
|
|
|
|
|
function M.trim(string)
|
2022-09-26 22:57:58 +03:00
|
|
|
return (string:gsub("^%s*(.-)%s*$", "%1"))
|
2022-09-26 21:43:50 +03:00
|
|
|
end
|
|
|
|
|
|
2022-09-26 02:09:22 +03:00
|
|
|
-- returns the username and the reponame form the origin url in a table
|
|
|
|
|
function M.get_username_reponame(url)
|
|
|
|
|
-- ssh has an @ in the url
|
|
|
|
|
if string.find(url, "@") then
|
|
|
|
|
local splitted_user_repo = M.split(url, ":")[2]
|
|
|
|
|
local splitted_username_and_reponame = M.split(splitted_user_repo, "/")
|
|
|
|
|
local username_and_reponame = {
|
|
|
|
|
username = splitted_username_and_reponame[1],
|
2022-09-26 21:43:50 +03:00
|
|
|
reponame = M.trim(string.gsub(splitted_username_and_reponame[2], ".git", "")),
|
2022-09-26 02:09:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return username_and_reponame
|
|
|
|
|
else
|
|
|
|
|
local splitted_username_and_reponame = M.split(url, "/")
|
|
|
|
|
local username_and_reponame = {
|
|
|
|
|
username = splitted_username_and_reponame[3],
|
2022-09-26 21:43:50 +03:00
|
|
|
reponame = M.trim(string.gsub(splitted_username_and_reponame[4], ".git", "")),
|
2022-09-26 02:09:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return username_and_reponame
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-26 22:57:58 +03:00
|
|
|
-- get the remote default branch
|
2022-09-26 21:43:50 +03:00
|
|
|
function M.get_defualt_branch()
|
|
|
|
|
-- will return origin/[branch_name]
|
|
|
|
|
local branch_with_origin = vim.fn.system("git rev-parse --abbrev-ref origin/HEAD")
|
|
|
|
|
local branch_name = M.split(branch_with_origin, "/")[2]
|
|
|
|
|
|
|
|
|
|
return M.trim(branch_name)
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-27 00:44:01 +03:00
|
|
|
-- get the active local branch or commit when HEAD is detached
|
|
|
|
|
function M.get_current_branch_or_commit()
|
|
|
|
|
local current_branch_name = M.trim(vim.fn.system("git rev-parse --abbrev-ref HEAD"))
|
|
|
|
|
|
2022-09-27 00:45:12 +03:00
|
|
|
-- HEAD is detached
|
2022-09-27 00:44:01 +03:00
|
|
|
if current_branch_name ~= "HEAD" then
|
|
|
|
|
return current_branch_name
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local current_commit_hash = vim.fn.system("git rev-parse HEAD")
|
|
|
|
|
return M.trim(current_commit_hash)
|
2022-09-26 21:43:50 +03:00
|
|
|
end
|
|
|
|
|
|
2022-09-26 22:57:58 +03:00
|
|
|
-- get the active buf relative file path form the .git
|
|
|
|
|
function M.get_current_relative_file_path()
|
|
|
|
|
-- we only want the active buffer name
|
|
|
|
|
local absolute_file_path = vim.api.nvim_buf_get_name(0)
|
|
|
|
|
local git_path = vim.fn.system("git rev-parse --show-toplevel")
|
|
|
|
|
|
|
|
|
|
local relative_file_path = "/" .. string.sub(absolute_file_path, git_path:len() + 1)
|
|
|
|
|
|
|
|
|
|
return relative_file_path
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-27 01:37:15 +03:00
|
|
|
-- get the line number in the buffer
|
|
|
|
|
function M.get_line_number_from_buf()
|
|
|
|
|
local lineNum = vim.api.nvim__buf_stats(0).current_lnum
|
|
|
|
|
return lineNum
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-26 02:09:22 +03:00
|
|
|
-- opens a url in the correct OS
|
|
|
|
|
function M.open_url(url)
|
2022-10-06 22:15:21 -07:00
|
|
|
if vim.fn.has("mac") then
|
2022-10-06 22:27:53 -07:00
|
|
|
vim.fn.system("open " .. url)
|
2022-09-26 02:09:22 +03:00
|
|
|
return true
|
2022-10-06 22:15:21 -07:00
|
|
|
elseif vim.fn.has("win64") or vim.fn.has("win32") then
|
2022-10-06 22:27:53 -07:00
|
|
|
vim.fn.system("start " .. url)
|
2022-09-26 02:09:22 +03:00
|
|
|
return true
|
2022-10-06 22:15:21 -07:00
|
|
|
elseif vim.fn.has("wsl") then
|
2022-10-06 22:27:53 -07:00
|
|
|
vim.fn.system("explorer.exe " .. url)
|
|
|
|
|
return true
|
2022-10-06 22:15:21 -07:00
|
|
|
elseif vim.fn.has("linux") then
|
2022-10-06 22:27:53 -07:00
|
|
|
vim.fn.system("xdg-open " .. url)
|
2022-09-26 02:09:22 +03:00
|
|
|
return true
|
|
|
|
|
end
|
2022-10-06 22:15:21 -07:00
|
|
|
|
2022-09-26 02:09:22 +03:00
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.print_no_remote_message()
|
|
|
|
|
print("There is no git origin in this repo!")
|
|
|
|
|
end
|
2022-09-25 23:53:23 +03:00
|
|
|
|
|
|
|
|
return M
|