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

Commit 36e69bf

Browse files
committed
refactor(gitcommit): address Gemini code review feedback
- Remove duplicate is_windows/shell_quote from command.lua, import from git_utils - Refactor contributors() to limit results in Lua instead of platform-specific pipes - Simplify filter_diff() by consolidating file path extraction logic
1 parent f7e17e9 commit 36e69bf

File tree

3 files changed

+22
-46
lines changed

3 files changed

+22
-46
lines changed

lua/codecompanion/_extensions/gitcommit/git_utils.lua

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,27 +118,12 @@ function M.filter_diff(diff_content, exclude_patterns)
118118
local skip_current_file = false
119119

120120
for _, line in ipairs(lines) do
121-
local file_match = line:match("^diff %-%-git a/(.*) b/")
122-
if file_match then
123-
current_file = file_match
124-
table.insert(all_files, current_file)
125-
skip_current_file = M.should_exclude_file(current_file, exclude_patterns)
126-
if skip_current_file then
127-
table.insert(excluded_files, current_file)
128-
end
129-
end
121+
local file_path = line:match("^diff %-%-git a/(.*) b/")
122+
or line:match("^%+%+%+ b/(.*)")
123+
or line:match("^%-%-%-a/(.*)")
130124

131-
local plus_file = line:match("^%+%+%+ b/(.*)")
132-
local minus_file = line:match("^%-%-%-a/(.*)")
133-
if plus_file then
134-
current_file = plus_file
135-
table.insert(all_files, current_file)
136-
skip_current_file = M.should_exclude_file(current_file, exclude_patterns)
137-
if skip_current_file then
138-
table.insert(excluded_files, current_file)
139-
end
140-
elseif minus_file then
141-
current_file = minus_file
125+
if file_path then
126+
current_file = file_path
142127
table.insert(all_files, current_file)
143128
skip_current_file = M.should_exclude_file(current_file, exclude_patterns)
144129
if skip_current_file then

lua/codecompanion/_extensions/gitcommit/tools/command.lua

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,10 @@
44

55
local M = {}
66

7-
--- Check if running on Windows
8-
---@return boolean
9-
local function is_windows()
10-
return vim.loop.os_uname().sysname == "Windows_NT"
11-
end
7+
local GitUtils = require("codecompanion._extensions.gitcommit.git_utils")
128

13-
--- Quote a string for shell command (Windows uses double quotes, Unix uses single quotes)
14-
---@param str string The string to quote
15-
---@return string
16-
local function shell_quote(str)
17-
if is_windows() then
18-
-- Windows CMD: use double quotes, escape internal double quotes with \"
19-
return '"' .. str:gsub('"', '\\"') .. '"'
20-
else
21-
-- Unix: use single quotes, escape internal single quotes
22-
return "'" .. str:gsub("'", "'\\''") .. "'"
23-
end
24-
end
9+
local is_windows = GitUtils.is_windows
10+
local shell_quote = GitUtils.shell_quote
2511

2612
--------------------------------------------------------------------------------
2713
-- CommandBuilder: Pure functions for generating git command strings
@@ -244,15 +230,9 @@ function CommandBuilder.diff_commits(commit1, commit2, file_path)
244230
end
245231

246232
---Build git shortlog (contributors) command
247-
---@param count? number Number of top contributors
248233
---@return string command
249-
function CommandBuilder.contributors(count)
250-
count = count or 10
251-
if is_windows() then
252-
return string.format("git shortlog -sn | Select-Object -First %d", count)
253-
else
254-
return string.format("git shortlog -sn | head -%d", count)
255-
end
234+
function CommandBuilder.contributors()
235+
return "git shortlog -sn"
256236
end
257237

258238
---Build git log search command

lua/codecompanion/_extensions/gitcommit/tools/git.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,19 @@ function GitTool.get_contributors(count)
414414
"✗ Not in a git repository",
415415
"<gitContributorsTool>fail: Not in a git repository</gitContributorsTool>"
416416
end
417-
local cmd = CommandBuilder.contributors(count)
417+
count = count or 10
418+
local cmd = CommandBuilder.contributors()
418419
local success, output = CommandExecutor.run(cmd)
420+
if success and output then
421+
local lines = vim.split(output, "\n")
422+
local limited_lines = {}
423+
for i = 1, math.min(count, #lines) do
424+
if lines[i] and lines[i] ~= "" then
425+
table.insert(limited_lines, lines[i])
426+
end
427+
end
428+
output = table.concat(limited_lines, "\n")
429+
end
419430
local user_msg, llm_msg = format_git_response("contributors", success, output)
420431
return success, output, user_msg, llm_msg
421432
end

0 commit comments

Comments
 (0)