diff --git a/neovim/config/plugins/diffview.lua b/neovim/config/plugins/diffview.lua index f9630bb..0f7befd 100644 --- a/neovim/config/plugins/diffview.lua +++ b/neovim/config/plugins/diffview.lua @@ -10,3 +10,25 @@ map('gd', 'DiffviewOpen', 'Diff against index') map('gc', 'DiffviewClose', 'Close diff view') map('gh', 'DiffviewFileHistory %', 'File history') map('gH', 'DiffviewFileHistory', 'Branch history') + +-- The base branch name varies per repo (main, master, develop, ...), so ask +-- the first remote's HEAD what it is rather than hardcoding one. +local function default_branch() + local remote = vim.fn.systemlist('git remote')[1] + if not remote or remote == '' then + return '' + end + local ref = vim.fn.systemlist('git symbolic-ref refs/remotes/' .. remote .. '/HEAD')[1] + if not ref or ref == '' then + return '' + end + return ref:gsub('^refs/remotes/' .. remote .. '/', '') +end + +map('gm', function() + vim.ui.input({ prompt = 'Diff against: ', default = default_branch() }, function(rev) + if rev and rev ~= '' then + vim.cmd('DiffviewOpen ' .. rev .. '...HEAD') + end + end) +end, 'Diff against branch')