From 5e742c3d7fbad22b27b8ececa0736d66c426bccb Mon Sep 17 00:00:00 2001 From: Masahiro Kasashima Date: Sat, 17 Jun 2023 20:40:36 +0900 Subject: [PATCH 1/3] Roughly create a commit-priority implementation --- lua/openingh/init.lua | 19 ++++++++++++++++--- lua/openingh/utils.lua | 17 +++++++++++++++++ plugin/openingh.lua | 27 +++++++++++++++++++++------ 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index c2e9884..0c2b037 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -22,7 +22,18 @@ function M.setup() M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame) end +local get_current_branch_or_commit_with_priority = function(priority) + if (not priority) or (priority and priority == 'branch') then + return utils.get_current_branch_or_commit() + elseif (priority and priority == 'branch') then + return utils.get_current_commit_or_branch() + else + return utils.get_current_branch_or_commit() + end +end + function M.open_file( + priority, --[[optional]] range_start, --[[optional]] @@ -43,7 +54,8 @@ function M.open_file( return end - local file_page_url = M.repo_url .. "/blob/" .. utils.get_current_branch_or_commit() .. file_path + + local file_page_url = M.repo_url .. "/blob/" .. get_current_branch_or_commit_with_priority(priority) .. file_path if range_start and not range_end then file_page_url = file_page_url .. "#L" .. range_start @@ -58,7 +70,7 @@ function M.open_file( end end -function M.open_repo() +function M.open_repo(priority) -- make sure to update the current directory M.setup() if M.is_no_git_origin then @@ -66,7 +78,8 @@ function M.open_repo() return end - local url = M.repo_url .. "/tree/" .. utils.get_current_branch_or_commit() + + local url = M.repo_url .. "/tree/" .. get_current_branch_or_commit_with_priority(priority) if not utils.open_url(url) then utils.notify("Could not open the built URL " .. url, vim.log.levels.ERROR) end diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index ef39177..6e965dc 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -96,6 +96,23 @@ function M.get_current_branch_or_commit() return M.encode_uri_component(M.get_default_branch()) end +-- Returns the current commit or branch if they are available on remote +-- otherwise this will return the default branch of the repo +-- (This function prioritizes commit than branch) +function M.get_current_commit_or_branch() + local commit_hash = get_current_commit_hash() + if M.is_commit_upstreamed(commit_hash) then + return commit_hash + end + + local current_branch = get_current_branch() + if current_branch ~= "HEAD" and M.is_branch_upstreamed(current_branch) then + return M.encode_uri_component(current_branch) + end + + return M.encode_uri_component(M.get_default_branch()) +end + -- get the active buf relative file path form the .git function M.get_current_relative_file_path() -- we only want the active buffer name diff --git a/plugin/openingh.lua b/plugin/openingh.lua index a40e828..be0b667 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -5,26 +5,41 @@ vim.g.openingh = true local openingh = require("openingh") +local complete_list = { 'branch-priority', 'commit-priority', } +local complete_func = function(arg_lead, _, _) + return vim.tbl_filter(function(item) + return vim.startswith(item, arg_lead) + end, complete_list) +end + vim.api.nvim_create_user_command("OpenInGHFile", function(opts) if opts.range == 0 then -- Nothing was selected - openingh.open_file() + openingh.open_file(opts.args) else -- Current line or block was selected - openingh.open_file(opts.line1, opts.line2) + openingh.open_file(opts.args, opts.line1, opts.line2) end end, { range = true, + nargs = '?', + complete = complete_func, }) vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) if opts.range == 0 then -- Nothing was selected - openingh.open_file(opts.line1) + openingh.open_file(opts.args, opts.line1) else -- Current line or block was selected - openingh.open_file(opts.line1, opts.line2) + openingh.open_file(opts.args, opts.line1, opts.line2) end end, { range = true, + nargs = '?', + complete = complete_func, }) vim.api.nvim_create_user_command("OpenInGHRepo", function() - openingh.open_repo() -end, {}) + print(opts) + openingh.open_repo(opts.args) +end, { + nargs = '?', + complete = complete_func, +}) From 68a3ed8af4a3331ee756fedf59bc372e527a36e8 Mon Sep 17 00:00:00 2001 From: Masahiro Kasashima Date: Sat, 17 Jun 2023 21:02:43 +0900 Subject: [PATCH 2/3] Fix some bugs on previous commit --- lua/openingh/init.lua | 4 ++-- plugin/openingh.lua | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index 0c2b037..d4dd951 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -23,9 +23,9 @@ function M.setup() end local get_current_branch_or_commit_with_priority = function(priority) - if (not priority) or (priority and priority == 'branch') then + if priority == 'branch-priority' then return utils.get_current_branch_or_commit() - elseif (priority and priority == 'branch') then + elseif priority == 'commit-priority' then return utils.get_current_commit_or_branch() else return utils.get_current_branch_or_commit() diff --git a/plugin/openingh.lua b/plugin/openingh.lua index be0b667..e42dff0 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -37,7 +37,6 @@ end, { }) vim.api.nvim_create_user_command("OpenInGHRepo", function() - print(opts) openingh.open_repo(opts.args) end, { nargs = '?', From 6ffd911e7b67798a680b3573bf745000c01728e2 Mon Sep 17 00:00:00 2001 From: Masahiro Kasashima Date: Sun, 18 Jun 2023 06:48:17 +0900 Subject: [PATCH 3/3] Refactoring --- lua/openingh/init.lua | 8 +++++--- plugin/openingh.lua | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index d4dd951..4da7982 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -22,10 +22,12 @@ function M.setup() M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame) end -local get_current_branch_or_commit_with_priority = function(priority) - if priority == 'branch-priority' then +M.priority = { BRANCH = 'branch-priority', COMMIT = 'commit-priority', } + +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 == 'commit-priority' then + elseif priority == M.priority.COMMIT then return utils.get_current_commit_or_branch() else return utils.get_current_branch_or_commit() diff --git a/plugin/openingh.lua b/plugin/openingh.lua index e42dff0..531db6d 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -5,8 +5,8 @@ vim.g.openingh = true local openingh = require("openingh") -local complete_list = { 'branch-priority', 'commit-priority', } -local complete_func = function(arg_lead, _, _) +local complete_list = { openingh.priority.BRANCH, openingh.priority.COMMIT, } +local function complete_func(arg_lead, _, _) return vim.tbl_filter(function(item) return vim.startswith(item, arg_lead) end, complete_list) @@ -36,7 +36,7 @@ end, { complete = complete_func, }) -vim.api.nvim_create_user_command("OpenInGHRepo", function() +vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) openingh.open_repo(opts.args) end, { nargs = '?',