From c674ee3e931c6797985a65cf495fad13620d6680 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Mon, 31 Aug 2026 21:37:00 +0100 Subject: [PATCH] Add keys to review a single commit and a branch's commits gd/gm cover uncommitted work and a whole branch, but neither answers "what did that one commit on main do". gr opens the explorer on one commit's files, defaulting to a sha under the cursor so it works straight from a log or a commit message. gR lists the commits HEAD has that a base branch does not, oldest first. Co-Authored-By: Claude Opus 5 (1M context) --- neovim/config/plugins/codediff.lua | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/neovim/config/plugins/codediff.lua b/neovim/config/plugins/codediff.lua index 5245f96..bda4744 100644 --- a/neovim/config/plugins/codediff.lua +++ b/neovim/config/plugins/codediff.lua @@ -56,3 +56,41 @@ map('gm', function() end end) end, 'Diff against branch') + +-- A commit-ish under the cursor, or nil. Lets gr be pressed straight on +-- a sha pasted in a commit message, a code comment or a fugitive/log buffer. +-- ^{commit} so a tag or tree does not pass as something diffable, and --quiet +-- so a cword like 'function' fails silently instead of shouting. +local function commit_under_cursor() + local cword = vim.fn.expand('') + if cword == '' then + return nil + end + vim.fn.systemlist('git rev-parse --verify --quiet ' .. vim.fn.shellescape(cword) .. '^{commit}') + if vim.v.shell_error ~= 0 then + return nil + end + return cword +end + +map('gr', function() + local default = commit_under_cursor() or 'HEAD' + vim.ui.input({ prompt = 'Review commit: ', default = default }, function(rev) + if rev and rev ~= '' then + -- rev^ rev, not `history`, so the explorer opens on that one commit's + -- files. A merge commit diffs against its first parent, as git does. + vim.cmd('CodeDiff ' .. rev .. '^ ' .. rev) + end + end) +end, 'Review a commit') + +map('gR', function() + vim.ui.input({ prompt = 'Commits since: ', default = default_branch() }, function(rev) + if rev and rev ~= '' then + -- Commit-by-commit rather than squashed: the history panel lists each + -- commit the branch is missing, oldest first, so they read in the order + -- they were made. + vim.cmd('CodeDiff history ' .. rev .. '..HEAD --reverse') + end + end) +end, 'Review commits since branch')