Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit 99077b3

Browse files
committed
feat(gitcommit): update slash command to select and insert commit content
- replace staged diff generation with commit selection from recent history - allow configurable number of commits to select from - fetch and insert full commit content (message and diff) into chat - improve error handling for git operations
1 parent 4ea75b0 commit 99077b3

1 file changed

Lines changed: 66 additions & 24 deletions

File tree

  • lua/codecompanion/_extensions/gitcommit

lua/codecompanion/_extensions/gitcommit/init.lua

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -135,38 +135,80 @@ return {
135135
-- Add to CodeCompanion slash commands if requested
136136
if opts.add_slash_command then
137137
local slash_commands = require("codecompanion.config").strategies.chat.slash_commands
138+
local gitcommit_select_count = (opts.gitcommit_select_count or 50)
138139
slash_commands["gitcommit"] = {
139-
description = "Generate git commit message from staged changes",
140+
description = "Select a commit and insert its full content (message + diff)",
140141
callback = function(chat)
141-
-- Check git repository status
142142
if not Git.is_repository() then
143143
chat:add_reference({ role = "user", content = "Error: Not in a git repository" }, "git", "<git_error>")
144144
return
145145
end
146146

147-
-- Get staged changes
148-
local diff = Git.get_staged_diff()
149-
if not diff then
150-
chat:add_reference({
151-
role = "user",
152-
content = "Error: No staged changes found. Please stage your changes first.",
153-
}, "git", "<git_error>")
154-
return
155-
end
156-
157-
Langs.select_lang(function(lang)
158-
-- Generate commit message
159-
Generator.generate_commit_message(diff, lang, function(result, error)
160-
if error then
161-
chat:add_reference({ role = "user", content = "Error: " .. error }, "git", "<git_error>")
162-
else
163-
chat:add_reference({
164-
role = "user",
165-
content = "Generated commit message:\n```\n" .. result .. "\n```",
166-
}, "git", "<git_commit>")
147+
-- 获取最近N条commit,数量可配置
148+
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
167158
end
168-
end)
169-
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 })
172+
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
187+
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)
206+
end,
207+
}):start()
208+
end)
209+
end)
210+
end,
211+
}):start()
170212
end,
171213
opts = {
172214
contains_code = true,

0 commit comments

Comments
 (0)