feat: add :Review command

This commit is contained in:
Job Jobse committed 2026-11-07 11:44:32 +01:00
1 parent cdcea08071
commit ab59364e78
5 files changed
+102 -39

No files matched your search

+92 -38
View File
@@ -1,52 +1,106 @@
local ns = vim.api.nvim_create_namespace('git_status')
local hls = { M = 'DiagnosticWarn', ['?'] = 'DiagnosticInfo', D = 'DiagnosticError' }
local ns = vim.api.nvim_create_namespace('git')
local status_hl = { M = 'DiagnosticWarn', A = 'DiagnosticOk', D = 'DiagnosticError', ['?'] = 'DiagnosticInfo' }
local function show(buf_id, items, query)
MiniPick.default_show(buf_id, items, query)
vim.api.nvim_buf_clear_namespace(buf_id, ns, 0, -1)
local function git(args)
local result = vim.system(vim.list_extend({ 'git', '-c', 'core.quotepath=false' }, args)):wait()
if result.code ~= 0 then error(vim.trim(result.stderr), 0) end
return result.stdout
end
-- Picker lines look like ' M path'; colour the status letter
local function show(buf, items, query)
MiniPick.default_show(buf, items, query)
vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1)
for i, item in ipairs(items) do
if item.hl then
vim.api.nvim_buf_set_extmark(buf_id, ns, i - 1, 1, { end_col = 3, hl_group = item.hl, priority = 202, hl_mode = 'combine' })
end
vim.api.nvim_buf_set_extmark(buf, ns, i - 1, 1, { end_col = 2, hl_group = status_hl[item.status], priority = 202, hl_mode = 'combine' })
end
end
-- Diff current file against the index (staged version)
-- Diff the current file against its staged version
local function diff()
vim.cmd.diffthis()
vim.cmd('vert Git show :./%')
vim.cmd.diffthis()
vim.cmd('diffthis')
vim.cmd('vertical Git show :./%')
vim.cmd('diffthis')
end
-- Deleted files can't be opened, so choosing one stages the deletion instead
local function choose(item)
if vim.uv.fs_stat(item.path) then return MiniPick.default_choose(item) end
vim.fn.system({ 'git', 'add', '--', item.path })
-- Pick an unstaged file; picking a deleted file stages the deletion
local function status()
local items = {}
for _, line in ipairs(vim.split(git({ 'status', '--short', '--untracked-files' }), '\n', { trimempty = true })) do
local s = line:sub(2, 2)
local path = line:sub(4):gsub('.* %-> ', ''):gsub('^"(.*)"$', '%1')
if s ~= ' ' then table.insert(items, { status = s, path = path, text = ' ' .. s .. ' ' .. path }) end
end
local function choose(item)
if vim.uv.fs_stat(item.path) then return MiniPick.default_choose(item) end
git({ 'add', '--', item.path })
end
MiniPick.start({ source = { name = 'Git status', items = items, show = show, choose = choose } })
end
return {
diff = diff,
status = function()
local lines = vim.fn.systemlist('git -c core.quotepath=false status -s -u')
if vim.v.shell_error ~= 0 then
return vim.notify('Not a git repository', vim.log.levels.WARN)
end
-- Write `path` as it is at `rev` to a temp file (empty if it doesn't exist there)
local function file_at(rev, path)
local file = vim.fn.tempname() .. '/' .. path
vim.fn.mkdir(vim.fs.dirname(file), 'p')
local content = rev and vim.F.npcall(git, { 'show', rev .. ':' .. path }) or ''
vim.fn.writefile(vim.split(content, '\n'), file, 'b')
return file
end
local items = {}
for _, l in ipairs(lines) do
-- Skip fully staged files (nothing left in the worktree column)
if l:sub(2, 2) ~= ' ' then
local s = l:sub(1, 2):match('%S+')
local p = l:sub(4):gsub('.* %-> ', ''):gsub('^"(.*)"$', '%1')
items[#items + 1] = {
text = string.format(' %-2s %s', s == '??' and '?' or s, p),
path = p,
hl = hls[l:sub(2, 2)],
}
-- Open `right` in the current window and diff it against `left` in a split, replacing the previous split
local split
local function open_diff(left, right)
if split and vim.api.nvim_win_is_valid(split) then vim.api.nvim_win_close(split, true) end
vim.cmd('edit ' .. vim.fn.fnameescape(right))
vim.cmd('leftabove vertical diffsplit ' .. vim.fn.fnameescape(left))
split = vim.api.nvim_get_current_win()
vim.cmd('wincmd p')
end
-- Last review, refreshed by `review()` without args
local last = {}
-- Review changes since the merge-base with `base` (default origin/HEAD) up to `head`.
-- Without `head` the working tree is used, so the right side is your real, editable file.
local function review(base, head)
if base or not last.base then last = { base = base or 'origin/HEAD', head = head, seen = {} } end
local root = vim.trim(git({ 'rev-parse', '--show-toplevel' }))
local merge_base = vim.trim(git({ 'merge-base', last.base, last.head or 'HEAD' }))
local range = last.head and (merge_base .. '..' .. last.head) or merge_base
local items = {}
for _, line in ipairs(vim.split(git({ 'diff', '--name-status', '--no-renames', range }), '\n', { trimempty = true })) do
local s, path = line:match('^(%a)\t(.*)$')
table.insert(items, { status = s, path = path, text = ' ' .. s .. ' ' .. path })
end
-- ✓ is drawn on display (not part of the text) so it stays current when the picker is resumed
local function show_seen(buf, shown, query)
show(buf, shown, query)
for i, item in ipairs(shown) do
if last.seen[item.path] then
vim.api.nvim_buf_set_extmark(buf, ns, i - 1, 3, { virt_text = { { '✓' } }, virt_text_pos = 'overlay' })
end
end
if #items == 0 then return vim.notify('No unstaged changes', vim.log.levels.INFO) end
end
MiniPick.start({ source = { items = items, name = 'Git Status', show = show, choose = choose } })
end,
}
local function preview(buf, item)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(git({ 'diff', range, '--', ':/' .. item.path }), '\n'))
vim.bo[buf].filetype = 'diff'
end
local function choose(item)
last.seen[item.path] = true
local real_file = root .. '/' .. item.path
local left = file_at(merge_base, item.path)
local right = (not last.head and vim.uv.fs_stat(real_file)) and real_file or file_at(last.head, item.path)
vim.schedule(function() open_diff(left, right) end)
end
local name = 'Review ' .. last.base .. '..' .. (last.head or 'worktree')
MiniPick.start({ source = { name = name, items = items, show = show_seen, preview = preview, choose = choose } })
end
return { status = status, diff = diff, review = review }