Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for checking for multiple remotes #28

Merged
merged 1 commit into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ require("cmp").setup({
require("cmp_git").setup({
-- defaults
filetypes = { "gitcommit" },
remote = "origin",
remotes = { "upstream", "origin" }, -- in order of most to least prioritized
git = {
commits = {
limit = 100,
Expand Down
2 changes: 1 addition & 1 deletion lua/cmp_git/config.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local M = {
filetypes = { "gitcommit" },
remote = "origin",
remotes = { "upstream", "origin" }, -- in order of most to least prioritized
git = {
commits = {
limit = 100,
Expand Down
1 change: 0 additions & 1 deletion lua/cmp_git/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ local split_by = function(input, sep)
end

M.update_edit_range = function(commits, cursor, offset)
local updated = {}
for k, v in pairs(commits) do
local sha = v.insertText

Expand Down
10 changes: 8 additions & 2 deletions lua/cmp_git/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ function Source:complete(params, callback)
trigger_character = params.completion_context.triggerCharacter
end

local git_info = utils.get_git_info(self.config.remote)

if trigger_character == ":" then
if not self.cache_commits[bufnr] then
local git_info = utils.get_git_info(self.config.remotes)

if self.config.git and self.config.git.commits then
git.get_git_commits(self, callback, bufnr, params.context.cursor, params.offset)
else
Expand All @@ -59,6 +59,8 @@ function Source:complete(params, callback)
end
elseif trigger_character == "#" then
if not self.cache_issues[bufnr] then
local git_info = utils.get_git_info(self.config.remotes)

if
self.config.github
and self.config.github.issues
Expand All @@ -84,6 +86,8 @@ function Source:complete(params, callback)
end
elseif trigger_character == "@" then
if not self.cache_mentions[bufnr] then
local git_info = utils.get_git_info(self.config.remotes)

if
self.config.github
and self.config.github.mentions
Expand All @@ -109,6 +113,8 @@ function Source:complete(params, callback)
end
elseif trigger_character == "!" then
if not self.cache_merge_requests[bufnr] then
local git_info = utils.get_git_info(self.config.remotes)

if
self.config.gitlab
and self.config.gitlab.mentions
Expand Down
27 changes: 21 additions & 6 deletions lua/cmp_git/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,30 @@ M.url_encode = function(value)
return string.gsub(value, "([^%w _%%%-%.~])", char_to_hex)
end

M.get_git_info = function(remote)
M.get_git_info = function(remotes)
return M.run_in_cwd(M.get_cwd(), function()
local remote_origin_url = vim.fn.system("git config --get remote." .. remote .. ".url")
local clean_remote_origin_url = remote_origin_url:gsub("%.git", ""):gsub("%s", "")
if type(remotes) == "string" then
remotes = { remotes }
end

local host, owner, repo = nil, nil, nil

for _, r in ipairs(remotes) do
local remote_origin_url = vim.fn.system("git config --get remote." .. r .. ".url")

if remote_origin_url ~= "" then
local clean_remote_origin_url = remote_origin_url:gsub("%.git", ""):gsub("%s", "")

host, owner, repo = string.match(clean_remote_origin_url, "^git@(.+):(.+)/(.+)$")

local host, owner, repo = string.match(clean_remote_origin_url, "^git@(.+):(.+)/(.+)$")
if host == nil then
host, owner, repo = string.match(clean_remote_origin_url, "^https?://(.+)/(.+)/(.+)$")
end

if host == nil then
host, owner, repo = string.match(clean_remote_origin_url, "^https?://(.+)/(.+)/(.+)$")
if host ~= nil and owner ~= nil and repo ~= nil then
break
end
end
end

return { host = host, owner = owner, repo = repo }
Expand Down