@@ -70,12 +70,13 @@ 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()
|
||||
|
||||
-- 1) Read original file (or empty if it doesn't exist)
|
||||
-- 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
|
||||
@@ -84,14 +85,14 @@ function M.apply_diff(filepath, diff_content)
|
||||
uv.fs_close(fd_in)
|
||||
end
|
||||
|
||||
-- 2) Write original content to a temp file
|
||||
-- Write original content to 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
|
||||
|
||||
-- 3) Write diff to a temp file
|
||||
-- Write diff to temp file
|
||||
local fd_patch = uv.fs_open(tmp_patch, "w", 438)
|
||||
if fd_patch then
|
||||
uv.fs_write(fd_patch, diff_content, -1)
|
||||
@@ -100,16 +101,8 @@ function M.apply_diff(filepath, diff_content)
|
||||
return false, "Could not open temporary file to write patch."
|
||||
end
|
||||
|
||||
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)
|
||||
-- Attempt to run 'patch'
|
||||
local patch_cmd = "patch -u " .. 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."
|
||||
@@ -118,13 +111,7 @@ 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
|
||||
@@ -135,17 +122,6 @@ 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)
|
||||
|
||||
Reference in New Issue
Block a user