run CopilotChatCommit when starting a commit message #1112
Unanswered
hellupline
asked this question in
Q&A
Replies: 2 comments
-
In case it helps anyone, I made my own version. It generates the commit message based on the diffs of a file in staged. And then performs the commit. You can change de prompt language for you convenience local chat = require("CopilotChat")
local select = require("CopilotChat.select")
local function extract_commit_message(response)
local msg = response:match("```gitcommit(.-)```")
if msg then
return vim.trim(msg)
else
vim.notify("❌ No gitcommit code block found", vim.log.levels.ERROR)
return nil
end
end
local function commit_with_ai()
local prompt = [[
Write a commit message for the change following the commitizen convention.
Keep the title under 50 characters and list each change on a separate line.
Use the appropriate icon according to the type of change:
✨ feat, 🐛 fix, 📝 docs, 🎨 style, ♻️ refactor, 🚀 perf, ✅ test, 📦 build, 👷 ci, 🔥 chore, ⏪ revert.
First, provide a summary of the change, then explain the details.
Format it as a gitcommit code block.
]]
chat.ask(prompt, {
selection = select.buffer,
context = { "git:staged" },
callback = function(response)
local msg = extract_commit_message(response)
if not msg then return end
vim.schedule(function()
local buf = vim.api.nvim_create_buf(false, true)
local lines = { "Commit with this message?", "" }
local first = true
for line in msg:gmatch("[^\r\n]+") do
if first then
table.insert(lines, line)
table.insert(lines, "")
first = false
else
table.insert(lines, line)
end
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
local width = math.max(60, math.min(100, vim.fn.strdisplaywidth(msg) + 4))
local height = #lines + 4
local win = vim.api.nvim_open_win(buf, true, {
relative = "editor",
width = width,
height = height,
row = math.floor((vim.o.lines - height) / 2),
col = math.floor((vim.o.columns - width) / 2),
style = "minimal",
border = "rounded",
})
vim.api.nvim_buf_set_keymap(buf, "n", "y", "", {
nowait = true,
noremap = true,
callback = function()
vim.api.nvim_win_close(win, true)
local result = vim.fn.system({ "git", "commit", "-m", msg })
vim.notify("✅ Commit executed:\n" .. result)
end,
})
vim.api.nvim_buf_set_keymap(buf, "n", "n", "", {
nowait = true,
noremap = true,
callback = function()
vim.api.nvim_win_close(win, true)
vim.notify("❎ Commit canceled by user")
end,
})
vim.api.nvim_buf_set_keymap(buf, "n", "<Esc>", "", {
nowait = true,
noremap = true,
callback = function()
vim.api.nvim_win_close(win, true)
vim.notify("❎ Commit canceled by user")
end,
})
vim.api.nvim_buf_set_option(buf, "modifiable", false)
vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe")
end)
end,
})
end
vim.keymap.set("n", ";ccg", commit_with_ai, { desc = "AI Generate Commit" }) |
Beta Was this translation helpful? Give feedback.
0 replies
-
I also spent some time setting this up. My method adds the generated commit message to the buffer where you can modify it afterwards. I added this to vim.api.nvim_create_autocmd("BufEnter", {
group = vim.api.nvim_create_augroup("gitcommit-copilotchat", { clear = true }),
pattern = "COMMIT_EDITMSG",
once = true,
callback = function()
local chat = require("CopilotChat")
local buf = vim.api.nvim_get_current_buf()
chat.ask(chat.prompts().Commit.prompt, {
selection = require("CopilotChat.select").buffer,
context = chat.prompts().Commit.context,
model = "gpt-4.1",
headless = true,
callback = function(response)
-- Extract content between first code block markers
local commit_msg = response:match("```[%w]*\n(.-)\n?```")
local lines
if commit_msg then
lines = vim.split(commit_msg, "\n", { plain = true })
else
lines = { "# Error: Unable to generate commit", "#" }
for _, line in ipairs(vim.split(response, "\n", { plain = true })) do
table.insert(lines, "# " .. line)
end
end
vim.api.nvim_buf_set_lines(buf, 0, 0, false, lines)
return response
end,
})
end,
})
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I tried to setup a autocmd for when I start a commit, Copilot chat would open automaticaly, but it keeps getting
cwd
error and returning bogus textmy attempt was:
the error message:
Beta Was this translation helpful? Give feedback.
All reactions