From dba902f5cc607dc5247f2f7081ac9c6a1293dd50 Mon Sep 17 00:00:00 2001 From: Ali Almohaya Date: Mon, 26 Sep 2022 21:43:50 +0300 Subject: [PATCH] chore: add support for diffrent branches --- lua/openingh/init.lua | 12 ++++++++++++ lua/openingh/utils.lua | 23 +++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index 9e03ecc..7de5c24 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -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 diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index ab5be9e..ad89199 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -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()