Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit a134b04

Browse files
committed
fix(gitcommit): 优化参数校验与响应格式
- 增加参数校验辅助函数,提升健壮性 - 统一各工具响应格式,便于维护和扩展 - 修复部分注释与边界条件判断 - 优化错误处理,确保一致性
1 parent faeddec commit a134b04

7 files changed

Lines changed: 157 additions & 256 deletions

File tree

lua/codecompanion/_extensions/gitcommit/buffer.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ function Buffer._generate_and_insert_commit_message(bufnr)
9797
-- Get relevant changes (staged or amend)
9898
local diff, context = Git.get_contextual_diff()
9999
if not diff then
100-
local msg = context == "no_changes"
101-
and (Git.is_amending() and "No changes to amend" or "No staged changes found. Please stage your changes first.")
102-
or ("Failed to get git changes, context=" .. tostring(context))
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
103106
vim.notify(msg, vim.log.levels.ERROR)
104107
return
105108
end

lua/codecompanion/_extensions/gitcommit/generator.lua

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,25 @@ function Generator._handle_response(err, data, adapter, callback)
122122
return callback(nil, "No response received from LLM")
123123
end
124124

125-
-- Process successful response
126-
if data then
127-
local result = _adapter.handlers.chat_output(_adapter, data)
128-
if result and result.status then
129-
if result.status == CONSTANTS.STATUS_SUCCESS then
130-
local content = result.output and result.output.content
131-
if content and vim.trim(content) ~= "" then
132-
return callback(vim.trim(content), nil)
133-
else
134-
return callback(nil, "Generated content is empty")
135-
end
136-
elseif result.status == CONSTANTS.STATUS_ERROR then
137-
local error_msg = result.output or "Unknown error occurred"
138-
return callback(nil, error_msg)
139-
end
140-
end
125+
-- Process the LLM response
126+
local result = _adapter.handlers.chat_output(_adapter, data)
127+
if not (result and result.status) then
128+
return callback(nil, "No valid response received")
141129
end
142130

143-
return callback(nil, "No valid response received")
131+
if result.status == CONSTANTS.STATUS_SUCCESS then
132+
local content = result.output and result.output.content
133+
if content and vim.trim(content) ~= "" then
134+
return callback(vim.trim(content), nil)
135+
else
136+
return callback(nil, "Generated content is empty")
137+
end
138+
elseif result.status == CONSTANTS.STATUS_ERROR then
139+
local error_msg = result.output or "Unknown error occurred"
140+
return callback(nil, error_msg)
141+
else
142+
return callback(nil, "Unexpected response status: " .. tostring(result.status))
143+
end
144144
end
145145

146146
return Generator

lua/codecompanion/_extensions/gitcommit/git.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ function Git._filter_diff(diff_content)
6868
end
6969
end
7070

71-
-- 如果所有涉及的文件都被排除,则返回原始 diff
72-
if #all_files > 0 and #excluded_files == #all_files then
71+
-- If all files are excluded, return original diff to avoid empty output
72+
if #all_files > 0 and #excluded_files >= #all_files then
7373
return diff_content
7474
end
7575

lua/codecompanion/_extensions/gitcommit/init.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ function M.generate_commit_message()
2020
-- Get relevant changes (staged or amend)
2121
local diff, context = Git.get_contextual_diff()
2222
if not diff then
23-
local msg = context == "no_changes"
24-
and (Git.is_amending() and "No changes to amend" or "No staged changes found. Please stage your changes first.")
25-
or "Failed to get git changes"
23+
local msg
24+
if context == "no_changes" then
25+
msg = Git.is_amending() and "No changes to amend" or "No staged changes found. Please stage your changes first."
26+
else
27+
msg = "Failed to get git changes"
28+
end
2629
vim.notify(msg, vim.log.levels.ERROR)
2730
return
2831
end

0 commit comments

Comments
 (0)