Skip to content

Commit e53bef7

Browse files
committed
feat(gitcommit): improve config and amend detection logic
- align inline comments for buffer and feature toggles in config_example.lua - fix indentation for auto-generation logic in buffer.lua - fix indentation and whitespace in git.lua amend detection - align type annotations in types.lua for skip_auto_generate_on_amend - no functional changes, improves code readability and consistency
1 parent 78c03e4 commit e53bef7

File tree

4 files changed

+78
-78
lines changed

4 files changed

+78
-78
lines changed

config_example.lua

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ return {
2626

2727
-- Buffer configuration
2828
buffer = {
29-
enabled = true, -- Enable buffer integration
30-
keymap = "<leader>gc", -- Keymap
31-
auto_generate = true, -- Auto-generate
32-
auto_generate_delay = 200, -- Auto-generation delay (ms)
29+
enabled = true, -- Enable buffer integration
30+
keymap = "<leader>gc", -- Keymap
31+
auto_generate = true, -- Auto-generate
32+
auto_generate_delay = 200, -- Auto-generation delay (ms)
3333
skip_auto_generate_on_amend = true, -- Skip auto-generation during git commit --amend
3434
},
3535

3636
-- Feature toggles
37-
add_slash_command = true, -- Enable slash command in chat buffer
38-
add_git_tool = true, -- Add @git_read and @git_edit tools to CodeCompanion
39-
enable_git_read = true, -- Enable read-only Git operations
40-
enable_git_edit = true, -- Enable write-access Git operations
41-
enable_git_bot = true, -- Enable @git_bot tool group (requires both read/write enabled)
42-
add_git_commands = true, -- Add :CodeCompanionGitCommit commands
37+
add_slash_command = true, -- Enable slash command in chat buffer
38+
add_git_tool = true, -- Add @git_read and @git_edit tools to CodeCompanion
39+
enable_git_read = true, -- Enable read-only Git operations
40+
enable_git_edit = true, -- Enable write-access Git operations
41+
enable_git_bot = true, -- Enable @git_bot tool group (requires both read/write enabled)
42+
add_git_commands = true, -- Add :CodeCompanionGitCommit commands
4343

4444
-- Git tool configuration
45-
git_tool_auto_submit_errors = false, -- Don't auto-submit errors to LLM
46-
git_tool_auto_submit_success = true, -- Auto-submit success to LLM
47-
gitcommit_select_count = 100, -- Number of recent commits for /gitcommit slash command
45+
git_tool_auto_submit_errors = false, -- Don't auto-submit errors to LLM
46+
git_tool_auto_submit_success = true, -- Auto-submit success to LLM
47+
gitcommit_select_count = 100, -- Number of recent commits for /gitcommit slash command
4848
}

lua/codecompanion/_extensions/gitcommit/buffer.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ function Buffer.setup(opts)
3232
Buffer._setup_gitcommit_keymap(event.buf)
3333

3434
if config.auto_generate then
35-
-- Auto-generation autocommand triggers once when entering gitcommit window
36-
-- This avoids race conditions with plugins like neogit that manage multiple windows
37-
-- Skip auto-generation during git amend to preserve user's intent to modify existing commit
35+
-- Auto-generation autocommand triggers once when entering gitcommit window
36+
-- This avoids race conditions with plugins like neogit that manage multiple windows
37+
-- Skip auto-generation during git amend to preserve user's intent to modify existing commit
3838
vim.api.nvim_create_autocmd("WinEnter", {
3939
buffer = event.buf,
4040
once = true,
4141
callback = function(args)
42-
-- Defer execution to ensure other plugins (like neogit) have finished UI setup
42+
-- Defer execution to ensure other plugins (like neogit) have finished UI setup
4343
vim.defer_fn(function()
4444
if not vim.api.nvim_buf_is_valid(args.buf) then
4545
return
4646
end
4747

48-
-- Check if buffer already has a commit message
48+
-- Check if buffer already has a commit message
4949
local lines = vim.api.nvim_buf_get_lines(args.buf, 0, -1, false)
5050
local has_message = false
5151
for _, line in ipairs(lines) do
@@ -55,13 +55,13 @@ function Buffer.setup(opts)
5555
end
5656
end
5757

58-
-- Skip auto-generation in the following cases:
59-
-- 1. Buffer already has a commit message
60-
-- 2. We are in a git amend operation (user may want to keep existing message)
61-
local should_skip_amend = config.skip_auto_generate_on_amend and Git.is_amending()
62-
if not has_message and not should_skip_amend then
63-
Buffer._generate_and_insert_commit_message(args.buf)
64-
end
58+
-- Skip auto-generation in the following cases:
59+
-- 1. Buffer already has a commit message
60+
-- 2. We are in a git amend operation (user may want to keep existing message)
61+
local should_skip_amend = config.skip_auto_generate_on_amend and Git.is_amending()
62+
if not has_message and not should_skip_amend then
63+
Buffer._generate_and_insert_commit_message(args.buf)
64+
end
6565
end, config.auto_generate_delay)
6666
end,
6767
desc = "Auto-generate GitCommit message",

lua/codecompanion/_extensions/gitcommit/git.lua

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -133,57 +133,57 @@ function Git.is_amending()
133133
return false
134134
end
135135

136-
-- Check if we're in an amend scenario by examining the COMMIT_EDITMSG content
137-
-- During amend, git pre-populates COMMIT_EDITMSG with the previous commit message
138-
local git_dir = vim.trim(vim.fn.system("git rev-parse --git-dir"))
139-
if vim.v.shell_error ~= 0 then
140-
return false
141-
end
142-
143-
-- Use platform-appropriate path separator
144-
local path_sep = package.config:sub(1, 1)
145-
local commit_editmsg = git_dir .. path_sep .. "COMMIT_EDITMSG"
146-
local stat = vim.uv.fs_stat(commit_editmsg)
147-
if not stat then
148-
return false
149-
end
150-
151-
-- Additional check: verify HEAD commit exists (not initial commit)
152-
local redirect = (vim.uv.os_uname().sysname == "Windows_NT") and " 2>nul" or " 2>/dev/null"
153-
vim.fn.system("git rev-parse --verify HEAD" .. redirect)
154-
if vim.v.shell_error ~= 0 then
155-
return false
156-
end
157-
158-
-- Read COMMIT_EDITMSG content and check if it contains a previous commit message
159-
-- During amend, git pre-populates this file with the previous commit message
160-
local fd = vim.uv.fs_open(commit_editmsg, "r", 438)
161-
if not fd then
162-
return false
163-
end
164-
165-
local content = vim.uv.fs_read(fd, stat.size, 0)
166-
vim.uv.fs_close(fd)
167-
168-
if not content then
169-
return false
170-
end
171-
172-
-- Check if there's non-comment content in COMMIT_EDITMSG
173-
-- During amend, this indicates we're editing an existing commit
174-
local lines = vim.split(content, "\n")
175-
local has_existing_message = false
176-
for _, line in ipairs(lines) do
177-
local trimmed = vim.trim(line)
178-
-- Skip empty lines and comment lines (starting with #)
179-
if trimmed ~= "" and not trimmed:match("^#") then
180-
has_existing_message = true
181-
break
182-
end
183-
end
184-
185-
-- If COMMIT_EDITMSG has existing content and HEAD exists, likely an amend
186-
return has_existing_message
136+
-- Check if we're in an amend scenario by examining the COMMIT_EDITMSG content
137+
-- During amend, git pre-populates COMMIT_EDITMSG with the previous commit message
138+
local git_dir = vim.trim(vim.fn.system("git rev-parse --git-dir"))
139+
if vim.v.shell_error ~= 0 then
140+
return false
141+
end
142+
143+
-- Use platform-appropriate path separator
144+
local path_sep = package.config:sub(1, 1)
145+
local commit_editmsg = git_dir .. path_sep .. "COMMIT_EDITMSG"
146+
local stat = vim.uv.fs_stat(commit_editmsg)
147+
if not stat then
148+
return false
149+
end
150+
151+
-- Additional check: verify HEAD commit exists (not initial commit)
152+
local redirect = (vim.uv.os_uname().sysname == "Windows_NT") and " 2>nul" or " 2>/dev/null"
153+
vim.fn.system("git rev-parse --verify HEAD" .. redirect)
154+
if vim.v.shell_error ~= 0 then
155+
return false
156+
end
157+
158+
-- Read COMMIT_EDITMSG content and check if it contains a previous commit message
159+
-- During amend, git pre-populates this file with the previous commit message
160+
local fd = vim.uv.fs_open(commit_editmsg, "r", 438)
161+
if not fd then
162+
return false
163+
end
164+
165+
local content = vim.uv.fs_read(fd, stat.size, 0)
166+
vim.uv.fs_close(fd)
167+
168+
if not content then
169+
return false
170+
end
171+
172+
-- Check if there's non-comment content in COMMIT_EDITMSG
173+
-- During amend, this indicates we're editing an existing commit
174+
local lines = vim.split(content, "\n")
175+
local has_existing_message = false
176+
for _, line in ipairs(lines) do
177+
local trimmed = vim.trim(line)
178+
-- Skip empty lines and comment lines (starting with #)
179+
if trimmed ~= "" and not trimmed:match("^#") then
180+
has_existing_message = true
181+
break
182+
end
183+
end
184+
185+
-- If COMMIT_EDITMSG has existing content and HEAD exists, likely an amend
186+
return has_existing_message
187187
end)
188188

189189
return ok and result or false

lua/codecompanion/_extensions/gitcommit/types.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
---@field keymap string Keymap for generating commit message in git commit buffer
1010
---@field auto_generate? boolean Automatically generate commit message on entering gitcommit buffer
1111

12-
---@field skip_auto_generate_on_amend? boolean Skip auto-generation during git commit --amend operations
13-
12+
---@field skip_auto_generate_on_amend? boolean Skip auto-generation during git commit --amend operations
13+
1414
---@field buffer? CodeCompanion.GitCommit.ExtensionOpts.Buffer Enable buffer-specific keymap for git commit
1515
---@field adapter? string? The adapter to use for generation
1616
---@field model? string? The model of the adapter to use for generation

0 commit comments

Comments
 (0)