feat(git): rev completion for :Gdiffsplit family

This commit is contained in:
2026-04-30 17:20:12 +02:00
parent 7fa05d4895
commit 9f44c9de40
2 changed files with 53 additions and 15 deletions
+18 -15
View File
@@ -85,21 +85,24 @@ function M.init()
})
end
end
vim.api.nvim_create_user_command(
"Gdiffsplit",
diff_split_cmd(true),
{ nargs = "?", desc = "Diff against <rev>" }
)
vim.api.nvim_create_user_command(
"Gvdiffsplit",
diff_split_cmd(true),
{ nargs = "?", desc = "Diff against <rev> (vertical split)" }
)
vim.api.nvim_create_user_command(
"Ghdiffsplit",
diff_split_cmd(false),
{ nargs = "?", desc = "Diff against <rev> (horizontal split)" }
)
local function complete_rev(...)
return require("git.repo").complete_rev(...)
end
vim.api.nvim_create_user_command("Gdiffsplit", diff_split_cmd(true), {
nargs = "?",
complete = complete_rev,
desc = "Diff against <rev>",
})
vim.api.nvim_create_user_command("Gvdiffsplit", diff_split_cmd(true), {
nargs = "?",
complete = complete_rev,
desc = "Diff against <rev> (vertical split)",
})
vim.api.nvim_create_user_command("Ghdiffsplit", diff_split_cmd(false), {
nargs = "?",
complete = complete_rev,
desc = "Diff against <rev> (horizontal split)",
})
vim.api.nvim_create_user_command("G", function(opts)
require("git.cmd").run(opts.fargs)
+35
View File
@@ -111,6 +111,41 @@ function M.head(path)
return nil
end
---@param worktree string
---@return string[]
function M.list_refs(worktree)
local out = util.exec({
"git",
"for-each-ref",
"--format=%(refname:short)",
"refs/heads",
"refs/tags",
"refs/remotes",
}, { cwd = worktree, silent = true })
if not out then
return {}
end
local refs = util.split_lines(out)
table.insert(refs, 1, "HEAD")
return refs
end
---@param arg_lead string
---@return string[]
function M.complete_rev(arg_lead)
local _, worktree = M.resolve_cwd()
if not worktree then
return {}
end
local matches = {}
for _, ref in ipairs(M.list_refs(worktree)) do
if ref:sub(1, #arg_lead) == arg_lead then
table.insert(matches, ref)
end
end
return matches
end
---@param worktree string
---@param rev string
---@param short boolean