-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcommit.lua
More file actions
103 lines (94 loc) · 2.64 KB
/
commit.lua
File metadata and controls
103 lines (94 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
local M = {}
local gpt_provide = require("kide.gpt.provide")
---@type gpt.Client
local client = nil
function M.commit_message(diff, callback)
local messages = {
{
content = "",
role = "system",
},
{
content = "",
role = "user",
},
}
-- see https://github.com/theorib/git-commit-message-ai-prompt/blob/main/prompts/conventional-commit-with-gitmoji-ai-prompt.md
messages[1].content =
"You will act as a git commit message generator. When receiving a git diff, you will ONLY output the commit message itself, nothing else. No explanations, no questions, no additional comments. Commits should follow the Conventional Commits 1.0.0 specification."
messages[2].content = diff
client = gpt_provide.new_client("commit")
client:request(messages, callback)
end
M.commit_diff_msg = function()
local diff = vim.system({ "git", "diff", "--cached" }):wait()
if diff.code ~= 0 then
return
end
local codebuf = vim.api.nvim_get_current_buf()
if "gitcommit" ~= vim.bo[codebuf].filetype then
return
end
local closed = false
vim.cmd("normal! gg0")
vim.api.nvim_create_autocmd("BufWipeout", {
buffer = codebuf,
callback = function()
closed = true
if client then
client:close()
end
end,
})
vim.keymap.set("n", "<C-c>", function()
closed = true
if client then
client:close()
end
vim.keymap.del("n", "<C-c>", { buffer = codebuf })
end, { buffer = codebuf, noremap = true, silent = true })
local callback = function(opt)
local data = opt.data
if closed then
vim.fn.jobstop(opt.job)
return
end
if opt.done then
return
end
local put_data = {}
if vim.api.nvim_buf_is_valid(codebuf) then
if data:match("\n") then
put_data = vim.split(data, "\n")
else
put_data = { data }
end
vim.api.nvim_put(put_data, "c", true, true)
end
end
M.commit_message(diff.stdout, callback)
end
M.setup = function()
local command = vim.api.nvim_buf_create_user_command
local autocmd = vim.api.nvim_create_autocmd
local function augroup(name)
return vim.api.nvim_create_augroup("kide" .. name, { clear = true })
end
autocmd("FileType", {
group = augroup("gpt_commit_msg"),
pattern = "gitcommit",
callback = function(event)
command(event.buf, "GptCommitMsg", function(_)
M.commit_diff_msg()
end, {
desc = "Gpt Commit Message",
nargs = 0,
range = false,
})
vim.keymap.set("n", "<leader>cm", function()
M.commit_diff_msg()
end, { buffer = event.buf, noremap = true, silent = true })
end,
})
end
return M