chore: add support for diffrent branches

This commit is contained in:
Ali Almohaya 2022-09-26 21:43:50 +03:00
parent 94e9af95c1
commit dba902f5cc
No known key found for this signature in database
GPG key ID: 4B80BC43FC6007E0
2 changed files with 33 additions and 2 deletions

View file

@ -35,6 +35,18 @@ function M.openRepo()
end
local repo_url = M.gh_url .. M.username .. "/" .. M.reponame
-- check if not the default branch add it to the url
local current_branch_name = utils.get_current_branch()
local default_branch_name = utils.get_defualt_branch()
print(current_branch_name)
print(default_branch_name)
if current_branch_name ~= default_branch_name then
repo_url = repo_url .. "/tree/" .. current_branch_name
end
local result = utils.open_url(repo_url)
if result then

View file

@ -10,6 +10,12 @@ function M.split(string, char)
return array
end
-- trim extra spaces and newlines
-- useful when working with git commands returned values
function M.trim(string)
return (string:gsub("^%s*(.-)%s*$", "%1"))
end
-- 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
@ -18,7 +24,7 @@ function M.get_username_reponame(url)
local splitted_username_and_reponame = M.split(splitted_user_repo, "/")
local username_and_reponame = {
username = splitted_username_and_reponame[1],
reponame = string.gsub(splitted_username_and_reponame[2], ".git", ""),
reponame = M.trim(string.gsub(splitted_username_and_reponame[2], ".git", "")),
}
return username_and_reponame
@ -26,13 +32,26 @@ function M.get_username_reponame(url)
local splitted_username_and_reponame = M.split(url, "/")
local username_and_reponame = {
username = splitted_username_and_reponame[3],
reponame = string.gsub(splitted_username_and_reponame[4], ".git", ""),
reponame = M.trim(string.gsub(splitted_username_and_reponame[4], ".git", "")),
}
return username_and_reponame
end
end
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
function M.get_current_branch()
local current_branch_name = vim.fn.system("git rev-parse --abbrev-ref HEAD")
return M.trim(current_branch_name)
end
-- opens a url in the correct OS
function M.open_url(url)
local os = M.get_os()