fix(trust): hash unchanged empty buffers as empty files #39027

Problem:
`vim.secure.trust()` hashes an unchanged empty buffer as
a newline, so trusting an empty file by buffer never works.

Solution:
Hash unchanged empty-buffers `''` so buffer-based
trust matches the on-disk empty file.
This commit is contained in:
Barrett Ruth
2026-04-23 15:01:37 -04:00
committed by GitHub
parent 398f2c108d
commit 0a8218a2b4
3 changed files with 39 additions and 5 deletions

View File

@@ -39,11 +39,18 @@ local function compute_hash(fullpath, bufnr)
end
if bufnr then
local newline = vim.bo[bufnr].fileformat == 'unix' and '\n' or '\r\n'
contents =
table.concat(vim.api.nvim_buf_get_lines(bufnr --[[@as integer]], 0, -1, false), newline)
if vim.bo[bufnr].endofline then
contents = contents .. newline
local is_unchanged_empty = vim.api.nvim_buf_call(bufnr, function()
return not vim.bo[bufnr].modified and vim.fn.line2byte(1) == -1
end)
if is_unchanged_empty then
contents = ''
else
local newline = vim.bo[bufnr].fileformat == 'unix' and '\n' or '\r\n'
contents =
table.concat(vim.api.nvim_buf_get_lines(bufnr --[[@as integer]], 0, -1, false), newline)
if vim.bo[bufnr].endofline then
contents = contents .. newline
end
end
else
do