Skip to content

Commit

Permalink
Merge pull request #34 from dfavato/main
Browse files Browse the repository at this point in the history
feat: Support remote names other than `origin`
  • Loading branch information
Almo7aya authored Nov 30, 2024
2 parents 0725eb8 + ad84c90 commit cddd087
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ vim.api.nvim_set_keymap("n", "<Leader>gf", ":OpenInGHFile <CR>", { silent = true
vim.api.nvim_set_keymap("v", "<Leader>gf", ":OpenInGHFileLines <CR>", { 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 <your-remote-name> # e.g. git config remote.pushDefault upstream
```

## TODO

- [x] Support the current file cursor position
Expand Down
3 changes: 3 additions & 0 deletions doc/openingh.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <your-remote-name> # e.g. git config remote.pushDefault upstream`

==============================================================================
5. LICENSE *openingh-license*

Expand Down
3 changes: 2 additions & 1 deletion lua/openingh/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions lua/openingh/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,36 @@ 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)
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

Expand Down

0 comments on commit cddd087

Please sign in to comment.