Skip to content

Commit c1247f6

Browse files
committed
perf: Update blob hash immediately after staging index buffer
1 parent 18d88c8 commit c1247f6

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

lua/diffview/scene/file_entry.lua

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -127,41 +127,38 @@ function FileEntry:convert_layout(target_layout)
127127
})
128128
end
129129

130-
---@param adapter VCSAdapter
131130
---@param stat? table
132-
function FileEntry:validate_stage_buffers(adapter, stat)
133-
stat = stat or utils.path:stat(utils.path:join(adapter.ctx.dir, "index"))
134-
local cached_stat = utils.tbl_access(fstat_cache, { adapter.ctx.toplevel, "index" })
135-
136-
if stat then
137-
if not cached_stat or cached_stat.mtime < stat.mtime.sec then
138-
for _, f in ipairs(self.layout:files()) do
139-
if f.rev.type == RevType.STAGE and f:is_valid() then
140-
if f.rev.stage > 0 then
141-
-- We only care about stage 0 here
142-
f:dispose_buffer()
143-
else
144-
local is_modified = vim.bo[f.bufnr].modified
145-
146-
if f.blob_hash then
147-
local new_hash = f.adapter:file_blob_hash(f.path)
148-
149-
if new_hash and new_hash ~= f.blob_hash then
150-
if is_modified then
151-
utils.warn((
152-
"A file was changed in the index since you started editing it!"
153-
.. " Be careful not to lose any staged changes when writing to this buffer: %s"
154-
):format(api.nvim_buf_get_name(f.bufnr)))
155-
else
156-
f:dispose_buffer()
157-
end
131+
function FileEntry:validate_stage_buffers(stat)
132+
stat = stat or utils.path:stat(utils.path:join(self.adapter.ctx.dir, "index"))
133+
local cached_stat = utils.tbl_access(fstat_cache, { self.adapter.ctx.toplevel, "index" })
134+
135+
if stat and (not cached_stat or cached_stat.mtime < stat.mtime.sec) then
136+
for _, f in ipairs(self.layout:files()) do
137+
if f.rev.type == RevType.STAGE and f:is_valid() then
138+
if f.rev.stage > 0 then
139+
-- We only care about stage 0 here
140+
f:dispose_buffer()
141+
else
142+
local is_modified = vim.bo[f.bufnr].modified
143+
144+
if f.blob_hash then
145+
local new_hash = self.adapter:file_blob_hash(f.path)
146+
147+
if new_hash and new_hash ~= f.blob_hash then
148+
if is_modified then
149+
utils.warn((
150+
"A file was changed in the index since you started editing it!"
151+
.. " Be careful not to lose any staged changes when writing to this buffer: %s"
152+
):format(api.nvim_buf_get_name(f.bufnr)))
153+
else
154+
f:dispose_buffer()
158155
end
159-
elseif not is_modified then
160-
-- Should be very rare that we don't have an index-buffer's blob
161-
-- hash. But in that case, we can't warn the user when a file
162-
-- changes in the index while they're editing its index buffer.
163-
f:dispose_buffer()
164156
end
157+
elseif not is_modified then
158+
-- Should be very rare that we don't have an index-buffer's blob
159+
-- hash. But in that case, we can't warn the user when a file
160+
-- changes in the index while they're editing its index buffer.
161+
f:dispose_buffer()
165162
end
166163
end
167164
end

lua/diffview/scene/views/diff/diff_view.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ DiffView.update_files = debounce.debounce_trailing(100, true, vim.schedule_wrap(
357357
end
358358

359359
v.cur_files[ai].status = v.new_files[bi].status
360-
v.cur_files[ai]:validate_stage_buffers(self.adapter, index_stat)
360+
v.cur_files[ai]:validate_stage_buffers(index_stat)
361361

362362
if new_head then
363363
v.cur_files[ai]:update_heads(new_head)

lua/diffview/vcs/adapters/git/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,7 @@ function GitAdapter:stage_index_file(file)
13721372
return false
13731373
end
13741374

1375+
file.blob_hash = blob_hash
13751376
vim.bo[file.bufnr].modified = false
13761377
api.nvim_exec_autocmds("BufWritePost", {
13771378
pattern = api.nvim_buf_get_name(file.bufnr),

0 commit comments

Comments
 (0)