Skip to content

Commit 5f95e4f

Browse files
committed
refactor(gitcommit): improve git log job readability and structure
- reformat Job:new to use method chaining for better clarity - adjust indentation and formatting for nested callbacks - enhance error handling and scheduling logic - maintain existing functionality while improving code maintainability
1 parent 99077b3 commit 5f95e4f

File tree

1 file changed

+77
-59
lines changed
  • lua/codecompanion/_extensions/gitcommit

1 file changed

+77
-59
lines changed

lua/codecompanion/_extensions/gitcommit/init.lua

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -146,69 +146,87 @@ return {
146146

147147
-- 获取最近N条commit,数量可配置
148148
local Job = require("plenary.job")
149-
Job:new({
150-
command = "git",
151-
args = { "log", "--oneline", "-n", tostring(gitcommit_select_count) },
152-
on_exit = function(j, return_val)
153-
if return_val ~= 0 then
154-
vim.schedule(function()
155-
chat:add_reference({ role = "user", content = "Error: Failed to get git log" }, "git", "<git_error>")
156-
end)
157-
return
158-
end
159-
local output = j:result()
160-
if not output or #output == 0 then
161-
vim.schedule(function()
162-
chat:add_reference({ role = "user", content = "No commits found." }, "git", "<git_error>")
163-
end)
164-
return
165-
end
166-
-- 解析commit hash和message
167-
local items = {}
168-
for _, line in ipairs(output) do
169-
local hash, msg = line:match("^(%w+)%s(.+)$")
170-
if hash and msg then
171-
table.insert(items, { label = hash .. " " .. msg, hash = hash })
149+
Job
150+
:new({
151+
command = "git",
152+
args = { "log", "--oneline", "-n", tostring(gitcommit_select_count) },
153+
on_exit = function(j, return_val)
154+
if return_val ~= 0 then
155+
vim.schedule(function()
156+
chat:add_reference(
157+
{ role = "user", content = "Error: Failed to get git log" },
158+
"git",
159+
"<git_error>"
160+
)
161+
end)
162+
return
172163
end
173-
end
174-
if #items == 0 then
175-
vim.schedule(function()
176-
chat:add_reference({ role = "user", content = "No commits found." }, "git", "<git_error>")
177-
end)
178-
return
179-
end
180-
vim.schedule(function()
181-
vim.ui.select(items, {
182-
prompt = "Select a commit to insert:",
183-
format_item = function(item) return item.label end,
184-
}, function(choice)
185-
if not choice then
186-
return
164+
local output = j:result()
165+
if not output or #output == 0 then
166+
vim.schedule(function()
167+
chat:add_reference({ role = "user", content = "No commits found." }, "git", "<git_error>")
168+
end)
169+
return
170+
end
171+
-- 解析commit hash和message
172+
local items = {}
173+
for _, line in ipairs(output) do
174+
local hash, msg = line:match("^(%w+)%s(.+)$")
175+
if hash and msg then
176+
table.insert(items, { label = hash .. " " .. msg, hash = hash })
187177
end
188-
-- 获取完整commit内容
189-
Job:new({
190-
command = "git",
191-
args = { "show", choice.hash },
192-
on_exit = function(j2, rv2)
193-
local commit_content = table.concat(j2:result(), "\n")
194-
if rv2 ~= 0 or not commit_content or commit_content == "" then
195-
vim.schedule(function()
196-
chat:add_reference({ role = "user", content = "Error: Failed to get commit content." }, "git", "<git_error>")
197-
end)
198-
return
199-
end
200-
vim.schedule(function()
201-
chat:add_reference({
202-
role = "user",
203-
content = "Selected commit (" .. choice.hash .. ") full content:\n```\n" .. commit_content .. "\n```",
204-
}, "git", "<git_commit>")
205-
end)
178+
end
179+
if #items == 0 then
180+
vim.schedule(function()
181+
chat:add_reference({ role = "user", content = "No commits found." }, "git", "<git_error>")
182+
end)
183+
return
184+
end
185+
vim.schedule(function()
186+
vim.ui.select(items, {
187+
prompt = "Select a commit to insert:",
188+
format_item = function(item)
189+
return item.label
206190
end,
207-
}):start()
191+
}, function(choice)
192+
if not choice then
193+
return
194+
end
195+
-- 获取完整commit内容
196+
Job
197+
:new({
198+
command = "git",
199+
args = { "show", choice.hash },
200+
on_exit = function(j2, rv2)
201+
local commit_content = table.concat(j2:result(), "\n")
202+
if rv2 ~= 0 or not commit_content or commit_content == "" then
203+
vim.schedule(function()
204+
chat:add_reference(
205+
{ role = "user", content = "Error: Failed to get commit content." },
206+
"git",
207+
"<git_error>"
208+
)
209+
end)
210+
return
211+
end
212+
vim.schedule(function()
213+
chat:add_reference({
214+
role = "user",
215+
content = "Selected commit ("
216+
.. choice.hash
217+
.. ") full content:\n```\n"
218+
.. commit_content
219+
.. "\n```",
220+
}, "git", "<git_commit>")
221+
end)
222+
end,
223+
})
224+
:start()
225+
end)
208226
end)
209-
end)
210-
end,
211-
}):start()
227+
end,
228+
})
229+
:start()
212230
end,
213231
opts = {
214232
contains_code = true,

0 commit comments

Comments
 (0)