feat: add debug for patching

This commit is contained in:
2025-01-04 18:33:31 +01:00
parent df206dce88
commit bae7d106ac

View File

@@ -70,13 +70,12 @@ function M.delete_file(filepath)
end
-- Applies a unified diff to the specified file.
-- This spawns an external 'patch' command if available.
function M.apply_diff(filepath, diff_content)
local conf = config.load()
local tmp_original = vim.fn.tempname()
local tmp_patch = vim.fn.tempname()
-- Read original file (or empty if it doesn't exist)
-- 1) Read original file (or empty if it doesn't exist)
local fd_in = uv.fs_open(filepath, "r", 438)
local original_data = ""
if fd_in then
@@ -85,14 +84,14 @@ function M.apply_diff(filepath, diff_content)
uv.fs_close(fd_in)
end
-- Write original content to temp file
-- 2) Write original content to a temp file
local fd_orig = uv.fs_open(tmp_original, "w", 438)
if fd_orig then
uv.fs_write(fd_orig, original_data, -1)
uv.fs_close(fd_orig)
end
-- Write diff to temp file
-- 3) Write diff to a temp file
local fd_patch = uv.fs_open(tmp_patch, "w", 438)
if fd_patch then
uv.fs_write(fd_patch, diff_content, -1)
@@ -101,8 +100,16 @@ function M.apply_diff(filepath, diff_content)
return false, "Could not open temporary file to write patch."
end
-- Attempt to run 'patch'
local patch_cmd = "patch -u " .. vim.fn.shellescape(tmp_original) .. " < " .. vim.fn.shellescape(tmp_patch)
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:handler] Applying diff to: " .. filepath .. "\n")
vim.api.nvim_out_write("[chatgpt_nvim:handler] Original file contents saved at: " .. tmp_original .. "\n")
vim.api.nvim_out_write("[chatgpt_nvim:handler] Patch file saved at: " .. tmp_patch .. "\n")
vim.api.nvim_out_write("[chatgpt_nvim:handler] Patch contents:\n" .. diff_content .. "\n")
end
-- 4) Attempt to run 'patch'
local patch_cmd = "patch -u --reject-file=- " .. vim.fn.shellescape(tmp_original)
.. " < " .. vim.fn.shellescape(tmp_patch)
local handle = io.popen(patch_cmd)
if not handle then
return false, "Failed to run patch command."
@@ -111,7 +118,13 @@ function M.apply_diff(filepath, diff_content)
local success_close, errmsg = handle:close()
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:handler] Patch command was: " .. patch_cmd .. "\n")
vim.api.nvim_out_write("[chatgpt_nvim:handler] Patch command output:\n" .. (result or "") .. "\n")
if not success_close then
vim.api.nvim_out_write("[chatgpt_nvim:handler] Patch command closed with error: " .. (errmsg or "unknown") .. "\n")
else
vim.api.nvim_out_write("[chatgpt_nvim:handler] Patch command succeeded.\n")
end
end
if not success_close then
@@ -122,6 +135,17 @@ function M.apply_diff(filepath, diff_content)
end
-- If successful, read the patched file and write it back
if conf.debug then
local debug_read = uv.fs_open(tmp_original, "r", 438)
if debug_read then
local debug_stat = uv.fs_fstat(debug_read)
local debug_data = uv.fs_read(debug_read, debug_stat.size, 0)
uv.fs_close(debug_read)
vim.api.nvim_out_write("[chatgpt_nvim:handler] Post-patch temp file:\n" .. debug_data .. "\n")
vim.api.nvim_out_write("[chatgpt_nvim:handler] End of patched temp file content.\n")
end
end
local fd_out = uv.fs_open(tmp_original, "r", 438)
if fd_out then
local stat_out = uv.fs_fstat(fd_out)