This repository was archived by the owner on Jan 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit_read.lua
More file actions
284 lines (264 loc) · 10.7 KB
/
Copy pathgit_read.lua
File metadata and controls
284 lines (264 loc) · 10.7 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
local GitTool = require("codecompanion._extensions.gitcommit.tools.git").GitTool
---@class CodeCompanion.GitCommit.Tools.GitRead
local GitRead = {}
GitRead.name = "git_read"
GitRead.description = "Tool for read-only Git operations like status, log, diff, etc."
GitRead.schema = {
type = "function",
["function"] = {
name = "git_read",
description = "Execute various read-only Git operations.",
parameters = {
type = "object",
properties = {
operation = {
type = "string",
enum = {
"status",
"log",
"diff",
"branch",
"remotes",
"show",
"blame",
"stash_list",
"diff_commits",
"contributors",
"search_commits",
"tags",
"generate_release_notes",
"help",
"gitignore_get",
"gitignore_check",
},
description = "The read-only Git operation to perform.",
},
args = {
type = "object",
properties = {
count = {
type = "integer",
description = "Number of items to show (for log, contributors, etc.)",
},
format = {
type = "string",
description = "Format for log output (oneline, short, medium, full, fuller)",
},
staged = {
type = "boolean",
description = "Whether to show staged changes for diff",
},
file_path = {
type = "string",
description = "Path to a specific file",
},
remote_only = {
type = "boolean",
description = "Show only remote branches",
},
commit_hash = {
type = "string",
description = "Commit hash or reference",
},
line_start = {
type = "integer",
description = "Start line number for blame",
},
line_end = {
type = "integer",
description = "End line number for blame",
},
commit1 = {
type = "string",
description = "First commit for diff",
},
commit2 = {
type = "string",
description = "Second commit for diff",
},
pattern = {
type = "string",
description = "Search pattern for commits",
},
gitignore_file = {
type = "string",
description = "File to check if ignored",
},
from_tag = {
type = "string",
description = "Starting tag for release notes generation (if not provided, uses second latest tag)",
},
to_tag = {
type = "string",
description = "Ending tag for release notes generation (if not provided, uses latest tag)",
},
release_format = {
type = "string",
description = "Format for release notes (markdown, plain, json)",
default = "markdown",
},
},
additionalProperties = false,
},
},
required = { "operation" },
additionalProperties = false,
},
strict = true,
},
}
GitRead.system_prompt = [[Execute read-only Git repository operations
When to use:
• When examining repository status and history
• When analyzing code changes and diffs
• When investigating commit patterns and contributors
• When checking branch states and configurations
Best practices:
• Must verify Git repository before operations
• Use specific operation parameters for targeted results
• Avoid operations that modify repository state
• Ensure operation args match expected parameters
Available operations: status, log, diff, branch, remotes, show, blame, stash_list, diff_commits, contributors, search_commits, tags, generate_release_notes, gitignore_get, gitignore_check, help]]
-- Helper function to validate required parameters
local function validate_required_param(param_name, param_value, error_msg)
if not param_value or param_value == "" then
return {
status = "error",
data = {
output = error_msg or (param_name .. " is required"),
user_msg = error_msg or (param_name .. " is required"),
llm_msg = "<gitReadTool>fail: " .. (error_msg or (param_name .. " is required")) .. "</gitReadTool>",
},
}
end
return nil
end
GitRead.cmds = {
function(self, args, _input)
local operation = args.operation
local op_args = args.args or {}
if operation == "help" then
local help_text =
"\\\nAvailable read-only Git operations:\n• status: Show repository status\n• log: Show commit history\n• diff: Show file differences\n• branch: List branches\n• remotes: Show remote repositories\n• show: Show commit details\n• blame: Show file blame info\n• stash_list: List stashes\n• diff_commits: Compare commits\n• contributors: Show contributors\n• search_commits: Search commit messages\n• tags: List all tags\n• generate_release_notes: Generate release notes between tags\n• gitignore_get: Get .gitignore content\n• gitignore_check: Check if a file is ignored\n "
return { status = "success", data = help_text }
end
local success, output, user_msg, llm_msg
-- 通过 pcall 安全执行操作,确保始终有响应
local ok, result = pcall(function()
if operation == "status" then
success, output, user_msg, llm_msg = GitTool.get_status()
elseif operation == "log" then
success, output, user_msg, llm_msg = GitTool.get_log(op_args.count, op_args.format)
elseif operation == "diff" then
success, output, user_msg, llm_msg = GitTool.get_diff(op_args.staged, op_args.file_path)
elseif operation == "branch" then
success, output, user_msg, llm_msg = GitTool.get_branches(op_args.remote_only)
elseif operation == "remotes" then
success, output, user_msg, llm_msg = GitTool.get_remotes()
elseif operation == "show" then
success, output, user_msg, llm_msg = GitTool.show_commit(op_args.commit_hash)
elseif operation == "blame" then
local validation_error =
validate_required_param("file_path", op_args.file_path, "File path is required for blame")
if validation_error then
return validation_error
end
success, output, user_msg, llm_msg = GitTool.get_blame(op_args.file_path, op_args.line_start, op_args.line_end)
elseif operation == "stash_list" then
success, output, user_msg, llm_msg = GitTool.list_stashes()
elseif operation == "diff_commits" then
local validation_error =
validate_required_param("commit1", op_args.commit1, "First commit is required for comparison")
if validation_error then
return validation_error
end
success, output, user_msg, llm_msg = GitTool.diff_commits(op_args.commit1, op_args.commit2, op_args.file_path)
elseif operation == "contributors" then
success, output, user_msg, llm_msg = GitTool.get_contributors(op_args.count)
elseif operation == "search_commits" then
local validation_error = validate_required_param("pattern", op_args.pattern, "Search pattern is required")
if validation_error then
return validation_error
end
success, output, user_msg, llm_msg = GitTool.search_commits(op_args.pattern, op_args.count)
elseif operation == "tags" then
success, output, user_msg, llm_msg = GitTool.get_tags()
elseif operation == "generate_release_notes" then
success, output, user_msg, llm_msg =
GitTool.generate_release_notes(op_args.from_tag, op_args.to_tag, op_args.release_format)
elseif operation == "gitignore_get" then
success, output, user_msg, llm_msg = GitTool.get_gitignore()
elseif operation == "gitignore_check" then
local validation_error =
validate_required_param("gitignore_file", op_args.gitignore_file, "No file specified for .gitignore check")
if validation_error then
return validation_error
end
success, output, user_msg, llm_msg = GitTool.is_ignored(op_args.gitignore_file)
else
return {
status = "error",
data = {
output = "Unknown Git read operation: " .. operation,
user_msg = "Unknown Git read operation: " .. operation,
llm_msg = "<gitReadTool>fail: Unknown Git read operation: " .. operation .. "</gitReadTool>",
},
}
end
return { success = success, output = output, user_msg = user_msg, llm_msg = llm_msg }
end)
-- Handle unexpected execution errors
if not ok then
local error_msg = "Git read operation failed unexpectedly: " .. tostring(result)
return {
status = "error",
data = {
output = error_msg,
user_msg = error_msg,
llm_msg = "<gitReadTool>fail: " .. error_msg .. "</gitReadTool>",
},
}
end
local success, output, user_msg, llm_msg = result.success, result.output, result.user_msg, result.llm_msg
-- Ensure proper response format even if operation fails
if success then
return { status = "success", data = { output = output, user_msg = user_msg, llm_msg = llm_msg } }
else
-- Ensure consistent error message format
local formatted_output = {
output = output or "Git read operation failed",
user_msg = user_msg or "Git read operation failed",
llm_msg = llm_msg or "<gitReadTool>fail: Git read operation failed</gitReadTool>",
}
return { status = "error", data = formatted_output }
end
end,
}
GitRead.handlers = {
setup = function(_self, _agent)
return true
end,
on_exit = function(_self, _agent) end,
}
GitRead.output = {
success = function(self, agent, _cmd, stdout)
local chat = agent.chat
local data = stdout[1]
local llm_msg = data and data.llm_msg or data.output
local user_msg = data and data.user_msg or data.output
return chat:add_tool_output(self, llm_msg, user_msg)
end,
error = function(self, agent, _cmd, stderr, stdout)
local chat = agent.chat
local data = stderr[1] or stdout[1]
local llm_msg = data and data.llm_msg or (type(data) == "string" and data or "Git read operation failed")
local user_msg = data and data.user_msg or "Git read operation failed"
return chat:add_tool_output(self, llm_msg, user_msg)
end,
}
GitRead.opts = {
requires_approval = function(_self, _agent)
return false
end,
}
return GitRead