From b4666a39f8c1fcab88d5aa7ac5077d64209c0e82 Mon Sep 17 00:00:00 2001 From: Cameron Date: Tue, 1 Aug 2023 23:51:28 +0200 Subject: [PATCH] Move invalidate() to diff lib since it only deals with diffs --- lua/neogit/lib/git/diff.lua | 64 ++++++++++++++++++------------- lua/neogit/lib/git/repository.lua | 10 ----- lua/neogit/lib/git/status.lua | 7 ++-- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/lua/neogit/lib/git/diff.lua b/lua/neogit/lib/git/diff.lua index d29fc656f..68dbc6984 100644 --- a/lua/neogit/lib/git/diff.lua +++ b/lua/neogit/lib/git/diff.lua @@ -8,6 +8,8 @@ local ItemFilter = require("neogit.lib.item_filter") local insert = table.insert local sha256 = vim.fn.sha256 +local M = {} + local function parse_diff_stats(raw) if type(raw) == "string" then raw = vim.split(raw, ", ") @@ -155,7 +157,7 @@ local function build_hunks(lines) return hunks end -local function parse_diff(raw_diff, raw_stats) +function M.parse(raw_diff, raw_stats) local header, start_idx = build_diff_header(raw_diff) local lines = build_lines(raw_diff, start_idx) local hunks = build_hunks(lines) @@ -179,7 +181,7 @@ local function build_metatable(f, raw_output_fn) if method == "diff" then self.diff = a.util.block_on(function() logger.debug("[DIFF] Loading diff for: " .. f.name) - return parse_diff(raw_output_fn()) + return M.parse(raw_output_fn()) end) return self.diff @@ -230,33 +232,43 @@ local function invalidate_diff(filter, section, item) end end -return { - parse = parse_diff, - register = function(meta) - meta.update_diffs = function(repo) - local filter - if repo.invalid[1] then - filter = ItemFilter.create(repo.invalid) - end +M.invalid = {} - for _, f in ipairs(repo.untracked.items) do - invalidate_diff(filter, "untracked", f) - build_metatable(f, raw_untracked(f.name)) - end +-- Invalidates a cached diff for a file +function M.invalidate(...) + local files = { ... } + for _, path in ipairs(files) do + table.insert(M.invalid, string.format("*:%s", path)) + end +end - for _, f in ipairs(repo.unstaged.items) do - if f.mode ~= "F" then - invalidate_diff(filter, "unstaged", f) - build_metatable(f, raw_unstaged(f.name)) - end +function M.register(meta) + meta.update_diffs = function(repo) + local filter + if #M.invalid > 0 then + filter = ItemFilter.create(M.invalid) + M.invalid = {} + end + + for _, f in ipairs(repo.untracked.items) do + invalidate_diff(filter, "untracked", f) + build_metatable(f, raw_untracked(f.name)) + end + + for _, f in ipairs(repo.unstaged.items) do + if f.mode ~= "F" then + invalidate_diff(filter, "unstaged", f) + build_metatable(f, raw_unstaged(f.name)) end + end - for _, f in ipairs(repo.staged.items) do - if f.mode ~= "F" then - invalidate_diff(filter, "staged", f) - build_metatable(f, raw_staged(f.name)) - end + for _, f in ipairs(repo.staged.items) do + if f.mode ~= "F" then + invalidate_diff(filter, "staged", f) + build_metatable(f, raw_staged(f.name)) end end - end, -} + end +end + +return M diff --git a/lua/neogit/lib/git/repository.lua b/lua/neogit/lib/git/repository.lua index ac64a0276..f8b7751bc 100644 --- a/lua/neogit/lib/git/repository.lua +++ b/lua/neogit/lib/git/repository.lua @@ -66,14 +66,6 @@ function M.reset(self) self.state = empty_state() end --- Invalidates a cached diff for a file -function M.invalidate(self, ...) - local files = { ... } - for _, path in ipairs(files) do - table.insert(self.state.invalid, string.format("*:%s", path)) - end -end - local refresh_lock = a.control.Semaphore.new(1) local lock_holder @@ -121,8 +113,6 @@ local function refresh(self, opts) end a.util.run_all(updates, function() - self.state.invalidate = {} - logger.info("[REPO]: Refreshes completed - freeing refresh lock") permit:forget() lock_holder = nil diff --git a/lua/neogit/lib/git/status.lua b/lua/neogit/lib/git/status.lua index 8a318a2ec..1e7c2595f 100644 --- a/lua/neogit/lib/git/status.lua +++ b/lua/neogit/lib/git/status.lua @@ -1,9 +1,10 @@ local git = { cli = require("neogit.lib.git.cli"), + diff = require("neogit.lib.git.diff"), stash = require("neogit.lib.git.stash"), } -local Collection = require("neogit.lib.collection") local a = require("plenary.async") +local Collection = require("neogit.lib.collection") local function update_file(file, mode, name) local mt, diff, has_diff @@ -134,7 +135,7 @@ end local M = {} function M.stage(...) - require("neogit.lib.git").repo:invalidate(...) + git.diff.invalidate(...) git.cli.add.files(...).call() end @@ -147,7 +148,7 @@ function M.stage_all() end function M.unstage(...) - require("neogit.lib.git").repo:invalidate(...) + git.diff.invalidate(...) git.cli.reset.files(...).call() end