refactor(git): make the git module self-contained under git.util

This commit is contained in:
2026-04-28 09:05:01 +02:00
parent 4390b55dfe
commit 37e5582795
10 changed files with 195 additions and 75 deletions
+10 -14
View File
@@ -1,6 +1,5 @@
local log = require("log")
local repo = require("git.repo")
local util = require("util")
local util = require("git.util")
local M = {}
@@ -15,7 +14,7 @@ local function attach_index_writer(buf, worktree, path)
vim.api.nvim_buf_get_lines(buf, 0, -1, false),
"\n"
) .. "\n"
local hash_stdout = util.system_sync(
local hash_stdout = util.exec(
{ "git", "hash-object", "-w", "--stdin" },
{ cwd = worktree, stdin = body }
)
@@ -26,7 +25,7 @@ local function attach_index_writer(buf, worktree, path)
local mode = vim.b[buf].git_index_mode
if not mode then
mode = "100644"
local ls = util.system_sync(
local ls = util.exec(
{ "git", "ls-files", "-s", "--", path },
{ cwd = worktree, silent = true }
)
@@ -42,7 +41,7 @@ local function attach_index_writer(buf, worktree, path)
-- (mode,sha,path), which doesn't survive paths containing a
-- comma.
if
not util.system_sync({
not util.exec({
"git",
"update-index",
"--cacheinfo",
@@ -87,7 +86,7 @@ function M.read_uri(buf)
local worktree = vim.b[buf].git_worktree or select(2, repo.resolve_cwd())
if not worktree then
log.error("git BufReadCmd %s: cannot resolve worktree", name)
util.error("git BufReadCmd %s: cannot resolve worktree", name)
return
end
vim.b[buf].git_worktree = worktree
@@ -104,10 +103,7 @@ function M.read_uri(buf)
end
local revspec = ref == "index" and (":" .. path) or (ref .. ":" .. path)
local stdout = util.system_sync(
{ "git", "show", revspec },
{ cwd = worktree }
)
local stdout = util.exec({ "git", "show", revspec }, { cwd = worktree })
if stdout then
vim.api.nvim_buf_set_lines(buf, 0, -1, false, util.split_lines(stdout))
end
@@ -207,21 +203,21 @@ function M.split(opts)
local cur_buf = vim.api.nvim_get_current_buf()
local cur_path = vim.api.nvim_buf_get_name(cur_buf)
if cur_path == "" then
log.warning("no file in current buffer")
util.warning("no file in current buffer")
return
end
if vim.bo[cur_buf].buftype ~= "" then
log.warning("cannot diff this buffer (not a worktree file)")
util.warning("cannot diff this buffer (not a worktree file)")
return
end
local _, worktree = repo.resolve(cur_path)
if not worktree then
log.warning("not in a git repository")
util.warning("not in a git repository")
return
end
local rel = vim.fs.relpath(worktree, cur_path)
if not rel then
log.warning("file is outside the worktree")
util.warning("file is outside the worktree")
return
end