Skip to content

Commit

Permalink
fix: update git status.
Browse files Browse the repository at this point in the history
  • Loading branch information
obaland committed Mar 25, 2024
1 parent 5cc6361 commit d31c59b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
4 changes: 2 additions & 2 deletions lua/vfiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ local vim = require('vfiler/libs/vim')

local VFiler = require('vfiler/vfiler')

local M = {}

local function open(configs, dirpath)
local options = configs.options

Expand Down Expand Up @@ -44,8 +46,6 @@ local function toggle(configs, dirpath)
return open(configs, dirpath)
end

local M = {}

--- Start vfiler from command line arguments
---@param args string: command line argumets
function M.start_command(args)
Expand Down
12 changes: 6 additions & 6 deletions lua/vfiler/events/config.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
local core = require('vfiler/libs/core')
--local git = require('vfiler/events/git')
local git = require('vfiler/events/git')

local M = {}

-- Default configs
-- Default vfiler global event configs
M.configs = {
enabled = true,
events = {
vfiler = {
--{
-- event = { 'BufWritePost' },
-- callback = git.update_file,
--},
{
event = { 'BufWritePost' },
callback = git.update_file,
},
},
},
}
Expand Down
9 changes: 7 additions & 2 deletions lua/vfiler/events/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@ local VFiler = require('vfiler/vfiler')

local M = {}

local function action_update_status(vfiler, context, view, path)
local function action_update_file(_, _, view, path)
local item = view:itemof(path)
if not item then
return
end
item:reload()
view:git_status_async(item.parent.path, function(v)
v:redraw()
end)
end

--- Action for an event to update file information.
---@param bufnr number
---@param group string
---@param event string
function M.update_file(bufnr, group, event)
local vfilers = VFiler.get_visible_in_tabpage(0)
if #vfilers == 0 then
return
end
local path = core.path.normalize(vim.fn.bufname(bufnr))
for _, vfiler in ipairs(vfilers) do
vfiler:do_action(action_update_status, path)
vfiler:do_action(action_update_file, path)
end
end

Expand Down
37 changes: 12 additions & 25 deletions lua/vfiler/git.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local core = require('vfiler/libs/core')
local fs = require('vfiler/libs/filesystem')
local git = require('vfiler/libs/git')
local vim = require('vfiler/libs/vim')

Expand All @@ -11,7 +10,7 @@ Git.__index = Git
function Git.new(options)
local self = setmetatable({
_jobs = {},
_status_reports = {},
_statuses = {},
_status_cache = {},
}, Git)
self:reset(options)
Expand All @@ -32,24 +31,14 @@ function Git:status_async(dirpath, callback)
return
end

-- Update when the git status acquisition date/time is newer than
-- the modification date/time of the target directory.
local report = self._status_reports[root]
if report and report.time > fs.ftime(dirpath) then
return
end

-- Stop previous job
local job = self._jobs[root]
if job then
job:stop()
end

self._jobs[root] = git.status_async(root, self._options, function(status)
self._status_reports[root] = {
time = vim.fn.localtime(),
status = status,
}
self._statuses[root] = status
callback(self, root, status)
self._jobs[root] = nil
end)
Expand All @@ -67,14 +56,14 @@ end
---@param path string
---@return table?
function Git:status(path)
local status = self._status_cache[path]
if status then
return status
local cached = self._status_cache[path]
if cached then
return cached
end
for toplevel, report in pairs(self._status_reports) do
if toplevel == path:sub(1, #toplevel) then
self._status_cache = report.status
return report.status[path]
for root, status in pairs(self._statuses) do
if root == path:sub(1, #root) then
self._status_cache = status
return status[path]
end
end
return nil
Expand All @@ -83,14 +72,12 @@ end
--- Iterator to walk each status
---@return function?
function Git:walk_status()
if not self._status_reports then
if not self._statuses then
return nil
end
local function _walk()
for _, report in pairs(self._status_reports) do
for path, status in pairs(report.status) do
coroutine.yield(path, status)
end
for path, status in pairs(self._statuses) do
coroutine.yield(path, status)
end
end
return coroutine.wrap(_walk)
Expand Down
4 changes: 2 additions & 2 deletions lua/vfiler/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ function View:git_status_async(dirpath, callback)
if not self:has_column('git') then
return
end
self._git:status_async(dirpath, function(root, status)
self._items:update_git(self._git)
self._git:status_async(dirpath, function(git, root, status)
self._items:update_git(git)
callback(self, root, status)
end)
end
Expand Down

0 comments on commit d31c59b

Please sign in to comment.