Skip to content

Commit dda7ee9

Browse files
committed
fix(gitcommit): schedule chat references to avoid async issues
- wrap chat:add_reference calls in vim.schedule for async safety - ensure error handling and commit selection run inside scheduled functions - improve reliability of git log and show command callbacks
1 parent 71e6d3f commit dda7ee9

File tree

1 file changed

+26
-24
lines changed
  • lua/codecompanion/_extensions/gitcommit

1 file changed

+26
-24
lines changed

lua/codecompanion/_extensions/gitcommit/init.lua

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,16 @@ local function setup_slash_commands(opts)
135135
args = { "show", choice.hash },
136136
on_exit = function(j, rv)
137137
local content = table.concat(j:result(), "\n")
138-
if rv ~= 0 or not content or content == "" then
139-
chat:add_reference({ role = "user", content = "Error: Failed to get commit content." }, "git", "<git_error>")
140-
else
141-
chat:add_reference({
142-
role = "user",
143-
content = string.format("Selected commit (%s) full content:\n```\n%s\n```", choice.hash, content),
144-
}, "git", "<git_commit>")
145-
end
138+
vim.schedule(function()
139+
if rv ~= 0 or not content or content == "" then
140+
chat:add_reference({ role = "user", content = "Error: Failed to get commit content." }, "git", "<git_error>")
141+
else
142+
chat:add_reference({
143+
role = "user",
144+
content = string.format("Selected commit (%s) full content:\n```\n%s\n```", choice.hash, content),
145+
}, "git", "<git_commit>")
146+
end
147+
end)
146148
end,
147149
}):start()
148150
end
@@ -165,24 +167,24 @@ local function setup_slash_commands(opts)
165167
command = "git",
166168
args = { "log", "--oneline", "-n", tostring(opts.gitcommit_select_count) },
167169
on_exit = function(j, rv)
168-
if rv ~= 0 then
169-
return chat:add_reference({ role = "user", content = "Error: Failed to get git log" }, "git", "<git_error>")
170-
end
171170
local output = j:result()
172-
if not output or #output == 0 then
173-
return chat:add_reference({ role = "user", content = "No commits found." }, "git", "<git_error>")
174-
end
175-
local items = {}
176-
for _, line in ipairs(output) do
177-
local hash, msg = line:match("^(%w+)%s(.+)$")
178-
if hash and msg then
179-
table.insert(items, { label = hash .. " " .. msg, hash = hash })
180-
end
181-
end
182-
if #items == 0 then
183-
return chat:add_reference({ role = "user", content = "No commits found." }, "git", "<git_error>")
184-
end
185171
vim.schedule(function()
172+
if rv ~= 0 then
173+
return chat:add_reference({ role = "user", content = "Error: Failed to get git log" }, "git", "<git_error>")
174+
end
175+
if not output or #output == 0 then
176+
return chat:add_reference({ role = "user", content = "No commits found." }, "git", "<git_error>")
177+
end
178+
local items = {}
179+
for _, line in ipairs(output) do
180+
local hash, msg = line:match("^(%w+)%s(.+)$")
181+
if hash and msg then
182+
table.insert(items, { label = hash .. " " .. msg, hash = hash })
183+
end
184+
end
185+
if #items == 0 then
186+
return chat:add_reference({ role = "user", content = "No commits found." }, "git", "<git_error>")
187+
end
186188
select_commit(chat, items)
187189
end)
188190
end,

0 commit comments

Comments
 (0)