From ab59364e78b66f315462a3c1dba5b15ecf5e723c Mon Sep 17 00:00:00 2001 From: Job Jobse Date: Sat, 7 Nov 2026 11:44:32 +0100 Subject: [PATCH] feat: add :Review command --- init.lua | 5 +- lua/git.lua | 130 +++++++++++++++------- plugin/30_commands.lua | 6 + plugin/{30_mini.lua => 40_mini.lua} | 0 plugin/{40_plugins.lua => 50_plugins.lua} | 0 5 files changed, 102 insertions(+), 39 deletions(-) create mode 100644 plugin/30_commands.lua rename plugin/{30_mini.lua => 40_mini.lua} (100%) rename plugin/{40_plugins.lua => 50_plugins.lua} (100%) diff --git a/init.lua b/init.lua index 44df3e4..39e1904 100644 --- a/init.lua +++ b/init.lua @@ -13,5 +13,8 @@ _G.Config = { later = function(f) misc.safely('later', f) end, autocmd = function(event, pattern, callback, desc, once) vim.api.nvim_create_autocmd(event, { group = gr, pattern = pattern, callback = callback, desc = desc, once = once }) - end + end, + command = function(name, callback, desc, opts) + vim.api.nvim_create_user_command(name, callback, vim.tbl_extend('force', { desc = desc }, opts or {})) + end, } diff --git a/lua/git.lua b/lua/git.lua index 601fc54..fe3b162 100644 --- a/lua/git.lua +++ b/lua/git.lua @@ -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 } diff --git a/plugin/30_commands.lua b/plugin/30_commands.lua new file mode 100644 index 0000000..1f42af8 --- /dev/null +++ b/plugin/30_commands.lua @@ -0,0 +1,6 @@ +-- :Review [base] [head] e.g. `:Review origin/develop`, `:Review origin/main origin/feat-x` +-- Without args it refreshes the last review (or starts one against origin/HEAD) +Config.command('Review', function(o) require('git').review(o.fargs[1], o.fargs[2]) end, 'Review changes against merge-base', { + nargs = '*', + complete = function() return vim.fn.systemlist('git branch -a --format=%(refname:short)') end, +}) diff --git a/plugin/30_mini.lua b/plugin/40_mini.lua similarity index 100% rename from plugin/30_mini.lua rename to plugin/40_mini.lua diff --git a/plugin/40_plugins.lua b/plugin/50_plugins.lua similarity index 100% rename from plugin/40_plugins.lua rename to plugin/50_plugins.lua