fix: nvim sops double save issue

This commit is contained in:
2025-10-22 19:19:40 +02:00
parent b6d44b5a20
commit 27c85ff9d0

View File

@@ -6,7 +6,6 @@ local sops_group = vim.api.nvim_create_augroup("SopsEncryption", { clear = true
-- Pattern matching for secrets files -- Pattern matching for secrets files
local secrets_patterns = { local secrets_patterns = {
"*/secrets.yaml", "*/secrets.yaml",
"*secrets*.yaml",
} }
-- Size limits to prevent memory issues and UI freezes -- Size limits to prevent memory issues and UI freezes
@@ -124,6 +123,12 @@ vim.api.nvim_create_autocmd("BufWriteCmd", {
local filepath = vim.fn.expand("%:p") local filepath = vim.fn.expand("%:p")
if is_secrets_file(filepath) then if is_secrets_file(filepath) then
-- Guard against double-execution
if currently_saving[filepath] then
return
end
currently_saving[filepath] = true
-- Get current buffer content -- Get current buffer content
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local content = table.concat(lines, "\n") local content = table.concat(lines, "\n")
@@ -137,6 +142,7 @@ vim.api.nvim_create_autocmd("BufWriteCmd", {
vim.log.levels.ERROR vim.log.levels.ERROR
) )
-- Don't write anything, leave buffer marked as modified -- Don't write anything, leave buffer marked as modified
currently_saving[filepath] = nil
return return
end end
@@ -151,6 +157,7 @@ vim.api.nvim_create_autocmd("BufWriteCmd", {
if not temp_f then if not temp_f then
vim.notify("SOPS: Failed to create temp file: " .. (temp_err or "unknown error"), vim.log.levels.ERROR) vim.notify("SOPS: Failed to create temp file: " .. (temp_err or "unknown error"), vim.log.levels.ERROR)
-- Don't write anything, leave buffer marked as modified -- Don't write anything, leave buffer marked as modified
currently_saving[filepath] = nil
return return
end end
temp_f:write(content) temp_f:write(content)
@@ -199,17 +206,22 @@ vim.api.nvim_create_autocmd("BufWriteCmd", {
else else
vim.notify("SOPS: Could not re-decrypt after save. Buffer may show encrypted content.", vim.log.levels.WARN) vim.notify("SOPS: Could not re-decrypt after save. Buffer may show encrypted content.", vim.log.levels.WARN)
end end
-- Clear guard after successful save
currently_saving[filepath] = nil
else else
vim.notify("SOPS: Failed to write encrypted content: " .. (write_err or "unknown error"), vim.log.levels.ERROR) vim.notify("SOPS: Failed to write encrypted content: " .. (write_err or "unknown error"), vim.log.levels.ERROR)
-- Don't mark as saved, keep buffer marked as modified -- Don't mark as saved, keep buffer marked as modified
currently_saving[filepath] = nil
end end
else else
vim.notify("SOPS: Failed to open file for writing: " .. (err or "unknown error"), vim.log.levels.ERROR) vim.notify("SOPS: Failed to open file for writing: " .. (err or "unknown error"), vim.log.levels.ERROR)
-- Don't mark as saved, keep buffer marked as modified -- Don't mark as saved, keep buffer marked as modified
currently_saving[filepath] = nil
end end
else else
vim.notify("SOPS: Failed to encrypt file - NOT SAVED! Error: " .. encrypted, vim.log.levels.ERROR) vim.notify("SOPS: Failed to encrypt file - NOT SAVED! Error: " .. encrypted, vim.log.levels.ERROR)
-- Don't write anything, leave buffer marked as modified -- Don't write anything, leave buffer marked as modified
currently_saving[filepath] = nil
end end
end end
end, end,