Skip to content

Commit 2b4afbe

Browse files
authored
feat(gitcommit): improve diff filtering and notification flow (#7)
* feat(gitcommit): improve diff filtering and notification flow - add tracking of all and excluded files in diff filtering - return original diff if all files are excluded - move commit message notification to after language selection - trim whitespace in contextual diff checks for accuracy - enable auto submit on git tool success in config - refactor generate_commit_message for clearer notification order * feat(git-tool): 丰富所有工具函数的返回值,增加 user_msg 和 llm_msg 字段 - 为 status、log、diff、branch、remotes、show、blame、stash_list、diff_commits、contributors、search_commits、tags 等函数增加 user_msg 和 llm_msg 字段 - 便于工具链和 LLM 处理更丰富的上下文信息 - 保持原有接口兼容 * fix(gitcommit): improve .gitignore and git read tool error handling - 增强 .gitignore 相关函数的错误提示和返回结构 - git_read 工具输出增加 user_msg/llm_msg,提升交互体验 - 修复部分判断和消息提示逻辑 * feat(git): improve error handling and robustness in git tools - wrap all git operations in pcall for safer execution - ensure consistent error messages and output formatting - enhance commit, stage, unstage, and read tool reliability - update user and LLM messages for clearer feedback - refactor contextual diff and commit logic for edge cases - add utility trim function and remove redundant code * fix(gitcommit): 优化参数校验与响应格式 - 增加参数校验辅助函数,提升健壮性 - 统一各工具响应格式,便于维护和扩展 - 修复部分注释与边界条件判断 - 优化错误处理,确保一致性 * fix(generator): improve LLM response handling logic - restructure response processing for clarity - handle error status with specific error messages - ensure callback is called for all response cases - add missing handler comment for LLM response
1 parent b95cf5b commit 2b4afbe

File tree

8 files changed

+595
-377
lines changed

8 files changed

+595
-377
lines changed

lua/codecompanion/_extensions/gitcommit/buffer.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ end
8888
---Generate commit message and insert into gitcommit buffer
8989
---@param bufnr number Buffer number
9090
function Buffer._generate_and_insert_commit_message(bufnr)
91-
vim.notify("Generating commit message...", vim.log.levels.INFO)
92-
9391
-- Check if we're in a git repository
9492
if not Git.is_repository() then
9593
vim.notify("Not in a git repository", vim.log.levels.ERROR)
@@ -99,14 +97,18 @@ function Buffer._generate_and_insert_commit_message(bufnr)
9997
-- Get relevant changes (staged or amend)
10098
local diff, context = Git.get_contextual_diff()
10199
if not diff then
102-
local msg = context == "no_changes"
103-
and (Git.is_amending() and "No changes to amend" or "No staged changes found. Please stage your changes first.")
104-
or "Failed to get git changes"
100+
local msg
101+
if context == "no_changes" then
102+
msg = Git.is_amending() and "No changes to amend" or "No staged changes found. Please stage your changes first."
103+
else
104+
msg = "Failed to get git changes, context=" .. tostring(context)
105+
end
105106
vim.notify(msg, vim.log.levels.ERROR)
106107
return
107108
end
108109

109110
Langs.select_lang(function(lang)
111+
vim.notify("Generating commit message...", vim.log.levels.INFO)
110112
-- Generate commit message using LLM
111113
Generator.generate_commit_message(diff, lang, function(result, error)
112114
if error then

lua/codecompanion/_extensions/gitcommit/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ M.default_opts = {
3131
enable_git_bot = true, -- Only enabled if both enable_git_read and enable_git_edit are true
3232
add_git_commands = true,
3333
git_tool_auto_submit_errors = false,
34-
git_tool_auto_submit_success = false,
34+
git_tool_auto_submit_success = true,
3535
gitcommit_select_count = 100,
3636
}
3737

lua/codecompanion/_extensions/gitcommit/generator.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ function Generator.generate_commit_message(diff, lang, callback)
5959

6060
-- Send request to LLM
6161
new_client:request(payload, {
62-
callback = function(err, data, _adapter)
63-
Generator._handle_response(err, data, _adapter, callback)
62+
callback = function(err, data, adapter)
63+
Generator._handle_response(err, data, adapter, callback)
6464
end,
6565
}, {
6666
silent = true,
@@ -109,9 +109,9 @@ end
109109
---Handle LLM response
110110
---@param err table|nil Error from request
111111
---@param data table|nil Response data
112-
---@param _adapter table The adapter used
112+
---@param adapter table The adapter used
113113
---@param callback fun(result: string|nil, error: string|nil) Callback function
114-
function Generator._handle_response(err, data, _adapter, callback)
114+
function Generator._handle_response(err, data, adapter, callback)
115115
-- Handle request errors
116116
if err then
117117
local error_msg = "Error generating commit message: " .. (err.stderr or err.message or "Unknown error")
@@ -125,7 +125,7 @@ function Generator._handle_response(err, data, _adapter, callback)
125125

126126
-- Process successful response
127127
if data then
128-
local result = _adapter.handlers.chat_output(_adapter, data)
128+
local result = adapter.handlers.chat_output(adapter, data)
129129
if result and result.status then
130130
if result.status == CONSTANTS.STATUS_SUCCESS then
131131
local content = result.output and result.output.content

0 commit comments

Comments
 (0)