Skip to content

Commit

Permalink
feat: add support for checking for multiple remotes, only retrieve gi…
Browse files Browse the repository at this point in the history
…t_info when necessary
  • Loading branch information
petertriho committed Oct 31, 2021
1 parent ba71c5e commit c071b8b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
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

0 comments on commit c071b8b

Please sign in to comment.