Skip to content

Commit

Permalink
Move invalidate() to diff lib since it only deals with diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
CKolkey committed Aug 1, 2023
1 parent f73af2f commit b4666a3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
64 changes: 38 additions & 26 deletions lua/neogit/lib/git/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, ", ")
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
10 changes: 0 additions & 10 deletions lua/neogit/lib/git/repository.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lua/neogit/lib/git/status.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit b4666a3

Please sign in to comment.