refactor(git): split status code parsing into status.lua

This commit is contained in:
2026-04-30 17:24:11 +02:00
parent 9f44c9de40
commit eebe191371
4 changed files with 57 additions and 52 deletions
-39
View File
@@ -2,45 +2,6 @@ local util = require("git.util")
local M = {}
M.UNMERGED = {
DD = true,
AU = true,
UD = true,
UA = true,
DU = true,
AA = true,
UU = true,
}
---@param code string
---@return string? char
---@return string? hl_group
function M.indicator(code)
if code == "" then
return nil
end
if code == "??" then
return "?", "GitUntracked"
end
if code == "!!" then
return "!", "GitIgnored"
end
if M.UNMERGED[code] then
return "U", "GitUnmerged"
end
local x, y = code:sub(1, 1), code:sub(2, 2)
if x == "R" or y == "R" then
return "R", "GitRenamed"
end
if y == " " and x ~= " " then
return x, "GitStaged"
end
if y == "D" then
return "D", "GitDeleted"
end
return "M", "GitUnstaged"
end
---@param path string
---@return string? gitdir
---@return string? worktree
+3 -2
View File
@@ -2,6 +2,7 @@ local Revision = require("git.revision")
local diff = require("git.diff")
local object = require("git.object")
local repo = require("git.repo")
local status = require("git.status")
local util = require("git.util")
local M = {}
@@ -99,7 +100,7 @@ local function format_entry(entry)
if not code then
return nil
end
local char, hl = repo.indicator(code)
local char, hl = status.indicator(code)
if not char then
return nil
end
@@ -177,7 +178,7 @@ local function parse_porcelain(stdout)
if x == "?" and y == "?" then
entry.section = "Untracked"
table.insert(groups.Untracked, entry)
elseif repo.UNMERGED[x .. y] then
elseif status.UNMERGED[x .. y] then
entry.section = "Unmerged"
table.insert(groups.Unmerged, entry)
else
+52
View File
@@ -0,0 +1,52 @@
local M = {}
M.UNMERGED = {
DD = true,
AU = true,
UD = true,
UA = true,
DU = true,
AA = true,
UU = true,
}
---@param code string
---@return string? char
---@return string? hl_group
function M.indicator(code)
if code == "" then
return nil
end
if code == "??" then
return "?", "GitUntracked"
end
if code == "!!" then
return "!", "GitIgnored"
end
if M.UNMERGED[code] then
return "U", "GitUnmerged"
end
local x, y = code:sub(1, 1), code:sub(2, 2)
if x == "R" or y == "R" then
return "R", "GitRenamed"
end
if y == " " and x ~= " " then
return x, "GitStaged"
end
if y == "D" then
return "D", "GitDeleted"
end
return "M", "GitUnstaged"
end
---@param code string
---@return string?
function M.format(code)
local char, hl = M.indicator(code)
if not char then
return nil
end
return string.format("%%#%s#%s%%*", hl, char)
end
return M
+2 -11
View File
@@ -1,18 +1,9 @@
local repo = require("git.repo")
local status = require("git.status")
local util = require("git.util")
local M = {}
---@param code string
---@return string?
local function format(code)
local char, hl = repo.indicator(code)
if not char then
return nil
end
return string.format("%%#%s#%s%%*", hl, char)
end
---@class ow.Git.Repo
---@field gitdir string
---@field worktree string
@@ -89,7 +80,7 @@ local function do_refresh(r)
end
end
statuses[vim.fs.joinpath(r.worktree, path_part)] =
format(code)
status.format(code)
end
end
else