You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My new git setup for committing (and pushing) changes with AI generated messages. It uses fugitive to execute the git commands and autocommands to update the COMMIT_EDITMSG used by git commit -v (which also shows diffs bellow the commit message). It works when running git commit -v from the command line as well, as long as nvim is setup as the git editor.
lua/plugins/git.lua:
return {
{
"tpope/vim-fugitive",
cmd= { "Git" },
keys= {
-- see autocmds.lua for AI auto-generated functionality
{ "<leader>gc", "<cmd>tab Git commit -v<cr>", desc="Git commit" },
},
},
}
lua/config/autocmds.lua:
localfunctionaugroup(name)
returnvim.api.nvim_create_augroup("lazyvim_" ..name, { clear=true })
end-- Autocommand to populate commit message using ai-commit-msg scriptvim.api.nvim_create_autocmd("BufWinEnter", {
group=augroup("ai_commit_msg_populate"), -- Renamed group for claritypattern="COMMIT_EDITMSG",
callback=function()
vim.system({ "ai-commit-msg" }, {}, function(res)
vim.schedule(function()
ifres.code~=0thenvim.notify(res.stderr)
returnendlocalmsg=res.stdoutifmsg==nilormsg=="" thenvim.notify("No commit message")
elsevim.api.nvim_buf_set_lines(0, 0, 1, false, vim.split(msg, "\n"))
endend)
end)
end,
})
-- Autocommand to prompt for push after writing commit messagevim.api.nvim_create_autocmd("BufWritePost", {
group=augroup("ai_commit_push_prompt"),
pattern="COMMIT_EDITMSG",
callback=function(args)
-- Defer the prompt slightly to allow git commit process to potentially start/finishvim.defer_fn(function()
-- Get the current branch namelocalbranch_name=vim.fn.trim(vim.fn.system("git rev-parse --abbrev-ref HEAD"))
-- Ask the user if they want to pushlocalprompt_message=string.format("Push commit to '%s'? (y/N): ", branch_name)
vim.ui.input({ prompt=prompt_message }, function(input)
ifinputandinput:lower() =="y" thenvim.cmd("Git push")
endend)
end, 100) -- Small delay (100ms)end,
})
The ai-commit-msg script that uses aichat to generate the commit message (needs to be in path of course).
.local/bin/ai-commit-msg:
#!/bin/bash
git diff --staged | aichat \
--model gemini:gemini-2.0-flash \
"Respond with a conventional git commit message for these changes. Don't add any extra text aside from the commit message."
To trigger, after staging commits, run :tab Git commit -v or <leader>gc> or git commit -v from the command line
Bonus: trigger the workflow directly from the Snacks git_status picker after doing the file staging there:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
My new git setup for committing (and pushing) changes with AI generated messages. It uses fugitive to execute the git commands and autocommands to update the
COMMIT_EDITMSGused bygit commit -v(which also shows diffs bellow the commit message). It works when runninggit commit -vfrom the command line as well, as long as nvim is setup as the git editor.lua/plugins/git.lua:
lua/config/autocmds.lua:
The
ai-commit-msgscript that uses aichat to generate the commit message (needs to be in path of course)..local/bin/ai-commit-msg:
To trigger, after staging commits, run
:tab Git commit -vor<leader>gc>orgit commit -vfrom the command lineBonus: trigger the workflow directly from the Snacks
git_statuspicker after doing the file staging there:Beta Was this translation helpful? Give feedback.
All reactions