Skip to content

Commit 0ea26d9

Browse files
committed
feat(gitcommit): add auto-generate commit message option for gitcommit buffer
- introduce `auto_generate` and `auto_generate_delay` options to buffer config - automatically generate commit message on entering gitcommit buffer if enabled - update README with new configuration and workflow details - update type definitions for new buffer options
1 parent 02d5669 commit 0ea26d9

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

README.md

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ require("codecompanion").setup({
3434
buffer = {
3535
enabled = true, -- Enable gitcommit buffer keymaps
3636
keymap = "<leader>gc", -- Keymap for generating commit message in gitcommit buffer
37+
auto_generate = true, -- Automatically generate message on entering gitcommit buffer
38+
auto_generate_delay = 100, -- Delay in ms before auto-generating
3739
}
3840
}
3941
}
@@ -51,9 +53,10 @@ require("codecompanion").setup({
5153
### GitCommit Buffer Integration
5254

5355
When you run `git commit` or open a gitcommit buffer:
54-
1. Press `<leader>gc` (or your configured keymap) in normal mode
55-
2. The extension will automatically generate a commit message based on staged changes
56-
3. The generated message will be inserted directly into the commit buffer
56+
57+
1. If `buffer.auto_generate` is `true`, the commit message will be generated and inserted automatically.
58+
2. If `buffer.auto_generate` is `false` (default), press `<leader>gc` (or your configured keymap) in normal mode to trigger generation.
59+
3. The generated message will be inserted directly into the commit buffer.
5760

5861
### Slash Command (if enabled)
5962

@@ -111,6 +114,7 @@ lua/codecompanion/_extensions/gitcommit/
111114
## Module Overview
112115

113116
### `git.lua`
117+
114118
Handles all git-related operations:
115119

116120
- Repository detection with filesystem and git command fallback
@@ -120,6 +124,7 @@ Handles all git-related operations:
120124
- Commit execution with proper error handling
121125

122126
### `generator.lua`
127+
123128
Manages LLM interaction:
124129

125130
- Prompt creation for commit message generation with language support
@@ -128,6 +133,7 @@ Manages LLM interaction:
128133
- Adapter and model configuration
129134

130135
### `ui.lua`
136+
131137
Provides interactive user interface:
132138

133139
- Floating window display with markdown formatting
@@ -136,6 +142,7 @@ Provides interactive user interface:
136142
- Responsive window sizing
137143

138144
### `buffer.lua`
145+
139146
Handles gitcommit buffer integration:
140147

141148
- Automatic keymap setup for gitcommit filetype
@@ -144,20 +151,23 @@ Handles gitcommit buffer integration:
144151
- Language selection integration
145152

146153
### `langs.lua`
154+
147155
Manages language selection:
148156

149157
- Multi-language support configuration
150158
- Interactive language selection UI
151159
- Language preference handling
152160

153161
### `types.lua`
162+
154163
Provides type definitions:
155164

156165
- TypeScript-style type annotations for Lua
157166
- Interface definitions for all modules
158167
- Configuration option types
159168

160169
### `init.lua`
170+
161171
Main extension coordinator:
162172

163173
- Module integration and dependency management
@@ -174,6 +184,7 @@ Main extension coordinator:
174184
## Workflow
175185

176186
### Traditional Workflow
187+
177188
1. Stage your changes with `git add`
178189
2. Run `:CodeCompanionGitCommit`
179190
3. Review the generated commit message in the floating window
@@ -195,13 +206,15 @@ When the floating window is displayed with the generated commit message, the fol
195206
- **`q` or `Esc`** - Close the floating window without taking any action
196207

197208
### GitCommit Buffer Workflow
209+
198210
1. Stage your changes with `git add`
199211
2. Run `git commit` to open the commit buffer
200-
3. Press `<leader>gc` in normal mode to generate commit message
201-
4. The AI-generated message will be inserted into the buffer
202-
5. Edit if needed and save to complete the commit
212+
3. If `auto_generate` is enabled, the message appears automatically. Otherwise, press `<leader>gc` in normal mode to generate it.
213+
4. The AI-generated message will be inserted into the buffer.
214+
5. Edit if needed and save to complete the commit.
203215

204216
### Amend Workflow
217+
205218
1. Make additional changes to your files
206219
2. Stage changes with `git add` (optional, for new changes)
207220
3. Run `git commit --amend` to open the amend buffer
@@ -223,22 +236,28 @@ opts = {
223236
buffer = {
224237
enabled = true, -- Enable gitcommit buffer keymaps (default: true)
225238
keymap = "<leader>gc", -- Keymap for generating commit message (default: "<leader>gc")
239+
auto_generate = false, -- Automatically generate message on entering gitcommit buffer (default: false)
240+
auto_generate_delay = 100, -- Delay in ms before auto-generating (default: 100)
226241
}
227242
}
228243
```
229244

230245
### Configuration Options
231246

232247
#### `add_slash_command` (boolean, default: `false`)
248+
233249
When enabled, adds `/gitcommit` slash command to CodeCompanion chat buffers.
234250

235251
#### `adapter` (string, optional)
252+
236253
The LLM adapter to use for generating commit messages. If not specified, defaults to the adapter configured for CodeCompanion's chat strategy.
237254

238255
#### `model` (string, optional)
256+
239257
The specific model to use with the adapter. If not specified, defaults to the model configured for CodeCompanion's chat strategy.
240258

241259
#### `languages` (table, optional)
260+
242261
A list of languages that can be used for generating commit messages. When specified, the extension will prompt you to select a language before generating the commit message. If not provided or empty, commit messages will be generated in English by default.
243262

244263
Example:
@@ -248,12 +267,13 @@ languages = { "English", "简体中文", "日本語", "Français", "Español" }
248267
```
249268

250269
#### `exclude_files` (table, optional)
270+
251271
A list of file patterns to exclude from git diff analysis when generating commit messages. Supports glob patterns using `*` and `?` wildcards. This is useful for excluding generated files, minified files, or large files that don't need AI analysis.
252272

253273
Examples:
254274

255275
```lua
256-
exclude_files = {
276+
exclude_files = {
257277
"*.pb.go", -- Protocol buffer generated files
258278
"*.min.js", -- Minified JavaScript files
259279
"package-lock.json", -- NPM lock file
@@ -265,11 +285,21 @@ exclude_files = {
265285
```
266286

267287
#### `buffer.enabled` (boolean, default: `true`)
288+
268289
Controls whether gitcommit buffer keymap integration is enabled.
269290

270291
#### `buffer.keymap` (string, default: `"<leader>gc"`)
292+
271293
The keymap used in gitcommit buffers to trigger commit message generation.
272294

295+
#### `buffer.auto_generate` (boolean, default: `false`)
296+
297+
When `true`, automatically generates a commit message upon entering a `gitcommit` buffer, but only if the buffer does not already contain a message (to avoid overwriting during an amend).
298+
299+
#### `buffer.auto_generate_delay` (number, default: `100`)
300+
301+
The delay in milliseconds before the automatic generation is triggered. This helps prevent race conditions with other plugins (like `neogit`) that manage UI elements. You can increase this value if you still experience issues.
302+
273303
## Contributing
274304

275-
Feel free to submit issues and enhancement requests!
305+
Feel free to submit issues and enhancement requests!

lua/codecompanion/_extensions/gitcommit/buffer.lua

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ local Buffer = {}
99
local default_config = {
1010
enabled = true,
1111
keymap = "<leader>gc",
12+
auto_generate = false,
13+
auto_generate_delay = 100, -- Default delay in ms
1214
}
1315

1416
---@type table Current configuration
@@ -28,8 +30,40 @@ function Buffer.setup(opts)
2830
pattern = "gitcommit",
2931
callback = function(event)
3032
Buffer._setup_gitcommit_keymap(event.buf)
33+
34+
if config.auto_generate then
35+
-- This autocommand will trigger once when the user enters the gitcommit window.
36+
-- This avoids race conditions with plugins like neogit that manage multiple windows.
37+
vim.api.nvim_create_autocmd("WinEnter", {
38+
buffer = event.buf,
39+
once = true,
40+
callback = function(args)
41+
-- Defer the execution to ensure other plugins (like neogit) have finished their UI setup.
42+
vim.defer_fn(function()
43+
if not vim.api.nvim_buf_is_valid(args.buf) then
44+
return
45+
end
46+
47+
-- Check if buffer already has a commit message (e.g., during amend)
48+
local lines = vim.api.nvim_buf_get_lines(args.buf, 0, -1, false)
49+
local has_message = false
50+
for _, line in ipairs(lines) do
51+
if not line:match("^%s*#") and vim.trim(line) ~= "" then
52+
has_message = true
53+
break
54+
end
55+
end
56+
57+
if not has_message then
58+
Buffer._generate_and_insert_commit_message(args.buf)
59+
end
60+
end, config.auto_generate_delay)
61+
end,
62+
desc = "Auto-generate GitCommit message",
63+
})
64+
end
3165
end,
32-
desc = "Setup GitCommit AI assistant keymap",
66+
desc = "Setup GitCommit AI assistant",
3367
})
3468
end
3569

lua/codecompanion/_extensions/gitcommit/types.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
---@class CodeCompanion.GitCommit.ExtensionOpts.Buffer
88
---@field enabled boolean Enable buffer-specific keymap for git commit
99
---@field keymap string Keymap for generating commit message in git commit buffer
10+
---@field auto_generate? boolean Automatically generate commit message on entering gitcommit buffer
11+
---@field auto_generate_delay? number Delay in ms before auto-generating to avoid race conditions
1012

1113
---@class CodeCompanion.GitCommit.ExtensionOpts
1214
---@field add_slash_command? boolean Add /gitcommit slash command to chat buffer

0 commit comments

Comments
 (0)