From 596645446286edbbdd52dbab0152faf4b3a3dd7e Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Mon, 27 Apr 2026 13:10:10 +0200 Subject: [PATCH] refactor(git): minor structural cleanups --- lua/git/cmd.lua | 4 ++-- lua/git/diff.lua | 5 ++--- lua/git/repo.lua | 27 +++++++++++++-------------- lua/git/status_win.lua | 40 +++++++++++++++++++--------------------- 4 files changed, 36 insertions(+), 40 deletions(-) diff --git a/lua/git/cmd.lua b/lua/git/cmd.lua index d1a29c9..d586ecb 100644 --- a/lua/git/cmd.lua +++ b/lua/git/cmd.lua @@ -188,7 +188,7 @@ end ---@param arg_lead string ---@param cmd_line string ---@return string[] -function M.complete(arg_lead, cmd_line, _) +local function complete(arg_lead, cmd_line, _) local rest = cmd_line:gsub("^%s*%S+%s*", "", 1) local words = vim.split(rest, "%s+", { trimempty = false }) if #words > 1 then @@ -208,7 +208,7 @@ function M.setup() M.run(opts.fargs) end, { nargs = "*", - complete = M.complete, + complete = complete, desc = "Run git", }) end diff --git a/lua/git/diff.lua b/lua/git/diff.lua index d3070e9..e85e03a 100644 --- a/lua/git/diff.lua +++ b/lua/git/diff.lua @@ -78,11 +78,10 @@ function M.git_show_buf(worktree, ref, path, is_index) vim.bo[buf].buftype = is_index and "acwrite" or "nofile" vim.bo[buf].bufhidden = "wipe" vim.bo[buf].swapfile = false - if not is_index then - vim.bo[buf].modifiable = false - end if is_index then attach_index_writer(buf, worktree, path) + else + vim.bo[buf].modifiable = false end vim.bo[buf].modified = false return buf diff --git a/lua/git/repo.lua b/lua/git/repo.lua index d60a14f..5d52c3a 100644 --- a/lua/git/repo.lua +++ b/lua/git/repo.lua @@ -36,9 +36,6 @@ local function indicator(code) if y == "D" then return "D", "GitDeleted" end - if y == "M" or y == "T" then - return "M", "GitUnstaged" - end return "M", "GitUnstaged" end @@ -87,7 +84,7 @@ end ---@class ow.Git.Repo ---@field gitdir string ---@field worktree string ----@field buffers integer[] +---@field buffers table set of registered buffer numbers ---@field watcher? uv.uv_fs_event_t ---@field refresh fun(self: ow.Git.Repo) ---@field refresh_handle ow.Util.DebounceHandle @@ -116,17 +113,17 @@ end ---@param buf integer function Repo:add_buffer(buf) - table.insert(self.buffers, buf) + self.buffers[buf] = true end ---@param buf integer function Repo:remove_buffer(buf) - for i, b in ipairs(self.buffers) do - if b == buf then - table.remove(self.buffers, i) - break - end - end + self.buffers[buf] = nil +end + +---@return boolean +function Repo:has_buffers() + return next(self.buffers) ~= nil end ---@param repo ow.Git.Repo @@ -152,8 +149,10 @@ local function do_refresh(repo) format(code) end end - for _, buf in ipairs(repo.buffers) do - if vim.api.nvim_buf_is_valid(buf) then + for buf in pairs(repo.buffers) do + if not vim.api.nvim_buf_is_valid(buf) then + repo.buffers[buf] = nil + else local status = statuses[vim.api.nvim_buf_get_name(buf)] if vim.b[buf].git_status ~= status then vim.b[buf].git_status = status @@ -222,7 +221,7 @@ local function unregister(buf) end repo_by_buf[buf] = nil repo:remove_buffer(buf) - if #repo.buffers == 0 then + if not repo:has_buffers() then repo:stop_watcher() repo_by_gitdir[repo.gitdir] = nil end diff --git a/lua/git/status_win.lua b/lua/git/status_win.lua index d91a296..e465347 100644 --- a/lua/git/status_win.lua +++ b/lua/git/status_win.lua @@ -18,8 +18,8 @@ local SIDEBAR_WIDTH = 50 ---@field section string ---@field path string ---@field orig string? ----@field x string? ----@field y string? +---@field x string porcelain v1 column 1 (always set; may be a literal space) +---@field y string porcelain v1 column 2 (always set; may be a literal space) ---@class ow.Git.CommitEntry ---@field section string @@ -61,11 +61,11 @@ local function entry_code(entry) if entry.section == "Untracked" then return "??" elseif entry.section == "Unmerged" then - return (entry.x or " ") .. (entry.y or " ") + return entry.x .. entry.y elseif entry.section == "Staged" then - return (entry.x or " ") .. " " + return entry.x .. " " elseif entry.section == "Unstaged" then - return " " .. (entry.y or " ") + return " " .. entry.y end end @@ -174,24 +174,22 @@ local function fetch_status(worktree, callback) table.insert(groups.Unmerged, entry) else if x ~= " " then - table.insert( - groups.Staged, - vim.tbl_extend( - "force", - entry, - { section = "Staged" } - ) - ) + table.insert(groups.Staged, { + section = "Staged", + path = entry.path, + orig = entry.orig, + x = entry.x, + y = entry.y, + }) end if y ~= " " then - table.insert( - groups.Unstaged, - vim.tbl_extend( - "force", - entry, - { section = "Unstaged" } - ) - ) + table.insert(groups.Unstaged, { + section = "Unstaged", + path = entry.path, + orig = entry.orig, + x = entry.x, + y = entry.y, + }) end end end