From 65dad7a287a6cdbb882cb723b2e4ac958e18023d Mon Sep 17 00:00:00 2001 From: Danilo Favato Date: Fri, 28 Jun 2024 09:31:15 -0300 Subject: [PATCH 1/2] feat: Support remote names other than origin --- lua/openingh/init.lua | 3 ++- lua/openingh/utils.lua | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index dbda762..d39321b 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -4,7 +4,8 @@ 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") + local remote = utils.get_default_remote() + local repo_url = vim.fn.system("git -C " .. current_buffer .. " config --get remote." .. remote .. ".url") if repo_url:len() == 0 then M.is_no_git_origin = true diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index e1f8ae5..fb7db8c 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -52,10 +52,21 @@ function M.parse_gh_remote(url) return { host = host, user_or_org = user_or_org, reponame = string.gsub(reponame, ".git", "") } end +-- get the default push remote +function M.get_default_remote() + -- will return origin by default + local remote = vim.fn.system("git config remote.pushDefault") + if remote == "" then + return "origin" + end + return M.trim(remote) +end + -- get the remote default branch function M.get_default_branch() -- will return origin/[branch_name] - local branch_with_origin = vim.fn.system("git rev-parse --abbrev-ref origin/HEAD") + local remote = M.get_default_remote() + local branch_with_origin = vim.fn.system("git rev-parse --abbrev-ref " .. remote .. "/HEAD") local branch_name = M.split(branch_with_origin, "/")[2] return M.trim(branch_name) @@ -63,13 +74,14 @@ end -- Checks if the supplied branch is available on the remote function M.is_branch_upstreamed(branch) - local output = M.trim(vim.fn.system("git branch -r --list origin/" .. branch)) + local remote = M.get_default_remote() + local output = M.trim(vim.fn.system("git branch -r --list " .. remote .. "/" .. branch)) if output:find(branch, 1, true) then return true end -- ls-remote is more expensive so only use it as a fallback - output = M.trim(vim.fn.system("git ls-remote --exit-code --heads origin " .. branch)) + output = M.trim(vim.fn.system("git ls-remote --exit-code --heads " .. remote .. " " .. branch)) return output ~= "" end From f6c6e8e5f0638abe5a6479f570390875c10e0a00 Mon Sep 17 00:00:00 2001 From: Danilo Favato Date: Fri, 28 Jun 2024 10:50:56 -0300 Subject: [PATCH 2/2] Add docs --- README.md | 6 ++++++ doc/openingh.txt | 3 +++ 2 files changed, 9 insertions(+) diff --git a/README.md b/README.md index f6b4738..aa9b4ae 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,12 @@ vim.api.nvim_set_keymap("n", "gf", ":OpenInGHFile ", { silent = true vim.api.nvim_set_keymap("v", "gf", ":OpenInGHFileLines ", { silent = true, noremap = true }) ``` +In case your remote repository name is different from `origin`, please set a pushDefault so that openingh can get the remote name from it. + +```sh +git config remote.pushDefault # e.g. git config remote.pushDefault upstream +``` + ## TODO - [x] Support the current file cursor position diff --git a/doc/openingh.txt b/doc/openingh.txt index 09fbebb..6d90e9b 100644 --- a/doc/openingh.txt +++ b/doc/openingh.txt @@ -52,6 +52,9 @@ CONTENTS *openingh* `vim.g.openingh_copy_to_register = true` `OpenInGHFile a` should place the URL into register `a` + In case your remote repository name is different from `origin`, please set a pushDefault so that openingh can get the remote name from it. + `git config remote.pushDefault # e.g. git config remote.pushDefault upstream` + ============================================================================== 5. LICENSE *openingh-license*