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

Commit d50829c

Browse files
committed
test(gitcommit): add comprehensive unit tests
Add unit tests for buffer, git, and command modules. Add -- separator to git commands in CommandBuilder. Add window_stability_delay setting to config_validation.lua. Check window visibility in Buffer._insert_commit_message. Add fallback to git diff in Git.get_contextual_diff. Remove fatal string check from CommandExecutor.run.
1 parent ccfd18c commit d50829c

13 files changed

Lines changed: 2199 additions & 12 deletions

‎lua/codecompanion/_extensions/gitcommit/buffer.lua‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,11 @@ function Buffer._insert_commit_message(bufnr, message)
259259
vim.api.nvim_buf_set_lines(bufnr, #message_lines, #message_lines, false, { "" })
260260
end
261261

262-
-- Move cursor to beginning of commit message
263-
vim.api.nvim_win_set_cursor(0, { 1, 0 })
262+
-- Move cursor to beginning of commit message if buffer is visible
263+
local winid = vim.fn.bufwinid(bufnr)
264+
if winid ~= -1 then
265+
vim.api.nvim_win_set_cursor(winid, { 1, 0 })
266+
end
264267

265268
vim.notify("Commit message generated and inserted!", vim.log.levels.INFO)
266269
end

‎lua/codecompanion/_extensions/gitcommit/config_validation.lua‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ M.schema = {
3434
keymap = { "string", "nil" },
3535
auto_generate = { "boolean", "nil" },
3636
auto_generate_delay = { "number", "nil" },
37+
window_stability_delay = { "number", "nil" },
3738
skip_auto_generate_on_amend = { "boolean", "nil" },
3839
},
3940
},

‎lua/codecompanion/_extensions/gitcommit/git.lua‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,13 @@ function Git.get_contextual_diff()
185185
end
186186

187187
local all_local_diff = vim.fn.system("git diff --no-ext-diff HEAD")
188-
if vim.v.shell_error == 0 and GitUtils.trim(all_local_diff) ~= "" then
188+
if vim.v.shell_error ~= 0 then
189+
all_local_diff = vim.fn.system("git diff --no-ext-diff")
190+
if vim.v.shell_error ~= 0 then
191+
return nil, "git_operation_failed"
192+
end
193+
end
194+
if GitUtils.trim(all_local_diff) ~= "" then
189195
local filtered_diff = Git._filter_diff(all_local_diff)
190196
if GitUtils.trim(filtered_diff) ~= "" then
191197
return filtered_diff, "unstaged_or_all_local"

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function CommandBuilder.diff(staged, file)
6161
table.insert(parts, "--cached")
6262
end
6363
if file then
64+
table.insert(parts, "--")
6465
table.insert(parts, vim.fn.shellescape(file))
6566
end
6667
return table.concat(parts, " ")
@@ -90,7 +91,7 @@ function CommandBuilder.stage(files)
9091
for _, file in ipairs(files) do
9192
table.insert(escaped_files, vim.fn.shellescape(file))
9293
end
93-
return "git add " .. table.concat(escaped_files, " ")
94+
return "git add -- " .. table.concat(escaped_files, " ")
9495
end
9596

9697
---Build git reset (unstage) command
@@ -104,7 +105,7 @@ function CommandBuilder.unstage(files)
104105
for _, file in ipairs(files) do
105106
table.insert(escaped_files, vim.fn.shellescape(file))
106107
end
107-
return "git reset HEAD " .. table.concat(escaped_files, " ")
108+
return "git reset HEAD -- " .. table.concat(escaped_files, " ")
108109
end
109110

110111
---Build git commit command
@@ -158,14 +159,16 @@ end
158159
---@param line_end? number End line number
159160
---@return string command
160161
function CommandBuilder.blame(file_path, line_start, line_end)
161-
local parts = { "git", "blame", vim.fn.shellescape(file_path) }
162+
local parts = { "git", "blame" }
162163
if line_start and line_end then
163164
table.insert(parts, "-L")
164165
table.insert(parts, line_start .. "," .. line_end)
165166
elseif line_start then
166167
table.insert(parts, "-L")
167168
table.insert(parts, line_start .. ",+10")
168169
end
170+
table.insert(parts, "--")
171+
table.insert(parts, vim.fn.shellescape(file_path))
169172
return table.concat(parts, " ")
170173
end
171174

@@ -542,7 +545,7 @@ end
542545
---@param file string File to check
543546
---@return string[] command array for system call
544547
function CommandBuilder.check_ignore(file)
545-
return { "git", "check-ignore", file }
548+
return { "git", "check-ignore", "--", file }
546549
end
547550

548551
--------------------------------------------------------------------------------
@@ -563,7 +566,7 @@ function CommandExecutor.run(cmd)
563566
end
564567

565568
local exit_code = vim.v.shell_error
566-
if exit_code ~= 0 or (output and output:match("fatal: ")) then
569+
if exit_code ~= 0 then
567570
return false, output or "Git command failed"
568571
end
569572

0 commit comments

Comments
 (0)