From 93e2652b863b9f7050652338c214fec95768d96b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Fl=C3=BCgge?= <952313+seflue@users.noreply.github.com> Date: Tue, 28 Jul 2026 18:07:37 +0200 Subject: [PATCH 1/2] feat: browse MR's commit history --- README.md | 1 + doc/gitlab.nvim.txt | 11 +++++++ lua/gitlab/init.lua | 3 ++ lua/gitlab/reviewer/init.lua | 34 ++++++++++++++++++++ lua/gitlab/state.lua | 7 +++++ tests/spec/history_browse_commits_spec.lua | 21 +++++++++++++ tests/spec/reviewer_autocommands_spec.lua | 36 ++++++++++++++++++++++ 7 files changed, 113 insertions(+) create mode 100644 tests/spec/history_browse_commits_spec.lua create mode 100644 tests/spec/reviewer_autocommands_spec.lua diff --git a/README.md b/README.md index 9166d227..1b371c8b 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,7 @@ These keymaps are available globally (i.e., in any buffer). | `glc` | Chose MR for review | | `glS` | Start review for the currently checked-out branch | | `glQ` | Close the reviewer, discussions, and shut down the Go server | +| `glh` | Browse the MR's commit history, one commit at a time (read-only) | | `gl` | Load new MR state from Gitlab and apply new diff refs to the diff view | | `gls` | Show the editable summary of the MR | | `glu` | Copy the URL of the MR to the system clipboard | diff --git a/doc/gitlab.nvim.txt b/doc/gitlab.nvim.txt index 936ca76a..b383cbf2 100644 --- a/doc/gitlab.nvim.txt +++ b/doc/gitlab.nvim.txt @@ -255,6 +255,7 @@ you call this function with no values the defaults will be used: create_mr = "glC", -- Create a new MR for currently checked-out feature branch choose_merge_request = "glc", -- Chose MR for review (if necessary check out the feature branch) start_review = "glS", -- Start review for the currently checked-out branch + browse_commits = "glh", -- Browse the MR's commit history, one commit at a time (read-only) reload_review = "gl", -- Load new MR state from Gitlab and apply new diff refs to the diff view close_review = "glQ", -- Close the reviewer, discussions, and shut down the Go server summary = "gls", -- Show the editable summary of the MR @@ -922,6 +923,16 @@ Opens the reviewer pane. Can be used from anywhere within Neovim after the plugin is loaded. If run twice, will open a second reviewer pane. >lua require("gitlab").review() +< + *gitlab.nvim.browse_commits* +gitlab.browse_commits() ~ + +Opens a read-only view of the MR's commit history (powered by Diffview's +file-history view), letting you step through it one commit at a time. Each entry +shows a single commit's isolated diff, to understand how the MR was built up. +Commenting is not available here; use `gitlab.review()` for that. +>lua + require("gitlab").browse_commits() < *gitlab.nvim.reload_review* gitlab.reload_review() ~ diff --git a/lua/gitlab/init.lua b/lua/gitlab/init.lua index 6389f1cf..0e8a669e 100644 --- a/lua/gitlab/init.lua +++ b/lua/gitlab/init.lua @@ -81,6 +81,9 @@ return { close_review = function(opts) reviewer.close(opts) end, + browse_commits = async.sequence({ info }, function() + reviewer.browse_commits() + end), pipeline = async.sequence({ latest_pipeline }, pipeline.open), merge = async.sequence({ u.merge(info, { refresh = true }) }, merge.merge), rebase = async.sequence({ u.merge(mergeability, { refresh = true }), info }, rebase.rebase), diff --git a/lua/gitlab/reviewer/init.lua b/lua/gitlab/reviewer/init.lua index 166788d9..68580fff 100644 --- a/lua/gitlab/reviewer/init.lua +++ b/lua/gitlab/reviewer/init.lua @@ -13,6 +13,7 @@ local M = { is_open = false, bufnr = nil, tabid = nil, + history_tabid = nil, stored_win = nil, } @@ -91,6 +92,32 @@ M.open = function() git.check_mr_in_good_condition() end +-- Opens a read-only, commit-by-commit browser for the MR range using Diffview's +-- FileHistory. Each entry shows a single commit's isolated diff, for understanding +-- how the MR was built up; commenting is not supported here (that stays in M.open). +M.browse_commits = function() + -- Diffview does not deduplicate views: DiffviewFileHistory always opens a new tabpage. + -- Focus the existing browser instead of stacking a second, orphaning the first. + if M.history_tabid ~= nil and vim.api.nvim_tabpage_is_valid(M.history_tabid) then + vim.api.nvim_set_current_tabpage(M.history_tabid) + return + end + + local diff_refs = state.INFO.diff_refs + if diff_refs == nil then + u.notify("Gitlab did not provide diff refs required to browse this MR", vim.log.levels.ERROR) + return + end + + if diff_refs.base_sha == "" or diff_refs.head_sha == "" then + u.notify("Merge request contains no changes", vim.log.levels.ERROR) + return + end + + vim.api.nvim_command(string.format("DiffviewFileHistory --range=%s..%s", diff_refs.base_sha, diff_refs.head_sha)) + M.history_tabid = vim.api.nvim_get_current_tabpage() +end + ---Close the reviewer, clean up, and shut down the Go server. ---@param opts? CloseReviewerOpts M.close = function(opts) @@ -510,6 +537,13 @@ M.set_reviewer_autocommands = function(bufnr) }, { buffer = bufnr, callback = function() + -- These autocommands manage the reviewer's own two windows, but they are + -- buffer-local and Diffview shares a revision's buffer across views, so the same + -- buffer shows up in the commit browser's tab. Acting there would delete the + -- buffer's keymaps and make it writable. Gate matches set_callback_for_buf_read. + if not (vim.api.nvim_get_current_tabpage() == M.tabid or (M.is_open and M.tabid == nil)) then + return + end local _diffview_layout = M.get_layout() if _diffview_layout == nil then return false diff --git a/lua/gitlab/state.lua b/lua/gitlab/state.lua index 6f53607a..4631f197 100644 --- a/lua/gitlab/state.lua +++ b/lua/gitlab/state.lua @@ -107,6 +107,7 @@ M.settings = { create_mr = "glC", choose_merge_request = "glc", start_review = "glS", + browse_commits = "glh", reload_review = "gl", close_review = "glQ", summary = "gls", @@ -343,6 +344,12 @@ M.set_global_keymaps = function() end, { desc = "Start Gitlab review", nowait = keymaps.global.start_review_nowait }) end + if keymaps.global.browse_commits then + vim.keymap.set("n", keymaps.global.browse_commits, function() + require("gitlab").browse_commits() + end, { desc = "Browse MR commit history", nowait = keymaps.global.browse_commits_nowait }) + end + if keymaps.global.reload_review then vim.keymap.set("n", keymaps.global.reload_review, function() require("gitlab").reload_review() diff --git a/tests/spec/history_browse_commits_spec.lua b/tests/spec/history_browse_commits_spec.lua new file mode 100644 index 00000000..38985e9d --- /dev/null +++ b/tests/spec/history_browse_commits_spec.lua @@ -0,0 +1,21 @@ +-- Verifies M.browse_commits focuses an already-open commit browser instead of opening a +-- second one: Diffview does not dedupe FileHistory views, so a second DiffviewFileHistory +-- call would orphan the first tab. + +local reviewer = require("gitlab.reviewer") + +describe("reviewer.browse_commits", function() + it("Switches to the existing tab instead of opening a new browser", function() + vim.cmd("tabnew") + local existing_tabid = vim.api.nvim_get_current_tabpage() + vim.cmd("tabnew") + + reviewer.history_tabid = existing_tabid + reviewer.browse_commits() + + assert.are.equal(existing_tabid, vim.api.nvim_get_current_tabpage()) + + vim.cmd("silent! tabonly") + reviewer.history_tabid = nil + end) +end) diff --git a/tests/spec/reviewer_autocommands_spec.lua b/tests/spec/reviewer_autocommands_spec.lua new file mode 100644 index 00000000..fd6db22c --- /dev/null +++ b/tests/spec/reviewer_autocommands_spec.lua @@ -0,0 +1,36 @@ +-- The reviewer's window autocommands are buffer-local, and Diffview shares a revision's +-- buffer between the reviewer and the commit browser, so they also fire in the browser's +-- tab. There they must do nothing: the else branch would delete the browse keymaps and +-- make the blob writable. + +describe("reviewer.set_reviewer_autocommands", function() + local reviewer = require("gitlab.reviewer") + local state = require("gitlab.state") + + it("Leaves the buffer alone outside the reviewer tab", function() + local reviewer_tabid = vim.api.nvim_get_current_tabpage() + vim.cmd("tabnew") + local bufnr = vim.api.nvim_create_buf(false, true) + vim.api.nvim_win_set_buf(0, bufnr) + vim.keymap.set("n", state.settings.keymaps.reviewer.create_comment, function() end, { buffer = bufnr }) + vim.api.nvim_set_option_value("modifiable", false, { buf = bufnr }) + + reviewer.tabid = reviewer_tabid + reviewer.is_open = true + -- Matching ids drive the else branch into its modifiable-and-unmap path, which the + -- tab gate has to prevent from being reached at all. + reviewer.diffview = { cur_layout = { a = { id = -2 }, b = { id = -1, file = { bufnr = bufnr } } } } + reviewer.set_reviewer_autocommands(bufnr) + + vim.api.nvim_exec_autocmds("WinEnter", { buffer = bufnr }) + + assert.is_false(vim.api.nvim_get_option_value("modifiable", { buf = bufnr })) + assert.are.equal(1, #vim.api.nvim_buf_get_keymap(bufnr, "n")) + + vim.cmd("tabclose") + reviewer.tabid = nil + reviewer.is_open = false + reviewer.diffview = nil + vim.api.nvim_buf_delete(bufnr, { force = true }) + end) +end) From 8c6892f661614a643a0b0fec8f16cdb126f4bb36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Fl=C3=BCgge?= <952313+seflue@users.noreply.github.com> Date: Sat, 28 Nov 2026 16:35:27 +0100 Subject: [PATCH 2/2] fix: reopen the commit browser after a rebase The browser is opened on fixed SHAs, so after a rebase it kept showing the old commits, and glh only refocused that stale tab. After the pull, reload the MR info and replace the browser with one on the new range. Focus stays on the current tab. --- lua/gitlab/actions/rebase.lua | 1 + lua/gitlab/reviewer/init.lua | 22 ++++++++ tests/spec/history_browse_commits_spec.lua | 59 ++++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/lua/gitlab/actions/rebase.lua b/lua/gitlab/actions/rebase.lua index 49670ea6..4e0bb2bb 100644 --- a/lua/gitlab/actions/rebase.lua +++ b/lua/gitlab/actions/rebase.lua @@ -62,6 +62,7 @@ local on_pull_exit = function(result, err) if reviewer.tabid ~= nil then reviewer.reload() end + reviewer.reload_history() elseif err ~= nil then require("gitlab.utils").notify(err, vim.log.levels.ERROR) end diff --git a/lua/gitlab/reviewer/init.lua b/lua/gitlab/reviewer/init.lua index 68580fff..facdceba 100644 --- a/lua/gitlab/reviewer/init.lua +++ b/lua/gitlab/reviewer/init.lua @@ -118,6 +118,28 @@ M.browse_commits = function() M.history_tabid = vim.api.nvim_get_current_tabpage() end +---Reopen an open commit browser on the MR's current diff refs, e.g. after a rebase. +M.reload_history = function() + if M.history_tabid == nil or not vim.api.nvim_tabpage_is_valid(M.history_tabid) then + return + end + state.load_new_state("info", function() + local old_tabid = M.history_tabid + local current_tabid = vim.api.nvim_get_current_tabpage() + -- Open the new browser before closing the old one: the old tab may be the last one. + M.history_tabid = nil + M.browse_commits() + if M.history_tabid == nil then + M.history_tabid = old_tabid + return + end + vim.cmd.tabclose(vim.api.nvim_tabpage_get_number(old_tabid)) + if current_tabid ~= old_tabid and vim.api.nvim_tabpage_is_valid(current_tabid) then + vim.api.nvim_set_current_tabpage(current_tabid) + end + end) +end + ---Close the reviewer, clean up, and shut down the Go server. ---@param opts? CloseReviewerOpts M.close = function(opts) diff --git a/tests/spec/history_browse_commits_spec.lua b/tests/spec/history_browse_commits_spec.lua index 38985e9d..8b83ea69 100644 --- a/tests/spec/history_browse_commits_spec.lua +++ b/tests/spec/history_browse_commits_spec.lua @@ -19,3 +19,62 @@ describe("reviewer.browse_commits", function() reviewer.history_tabid = nil end) end) + +-- A rebase rewrites the MR's commits, but the browser was opened on fixed SHAs, so it +-- has to be reopened on the new range. +describe("reviewer.reload_history", function() + local state = require("gitlab.state") + + it("Replaces the open browser and keeps focus on the current tab", function() + local original_load_new_state = state.load_new_state + local original_browse_commits = reviewer.browse_commits + state.load_new_state = function(_, cb) + cb() + end + reviewer.browse_commits = function() + vim.cmd("tabnew") + reviewer.history_tabid = vim.api.nvim_get_current_tabpage() + end + + vim.cmd("tabnew") + local old_history_tabid = vim.api.nvim_get_current_tabpage() + vim.cmd("tabnew") + local current_tabid = vim.api.nvim_get_current_tabpage() + reviewer.history_tabid = old_history_tabid + + reviewer.reload_history() + + assert.is_false(vim.api.nvim_tabpage_is_valid(old_history_tabid)) + assert.is_true(vim.api.nvim_tabpage_is_valid(reviewer.history_tabid)) + assert.are_not.equal(old_history_tabid, reviewer.history_tabid) + assert.are.equal(current_tabid, vim.api.nvim_get_current_tabpage()) + + state.load_new_state = original_load_new_state + reviewer.browse_commits = original_browse_commits + vim.cmd("silent! tabonly") + reviewer.history_tabid = nil + end) + + it("Keeps the old browser when the new one cannot open", function() + local original_load_new_state = state.load_new_state + local original_browse_commits = reviewer.browse_commits + state.load_new_state = function(_, cb) + cb() + end + reviewer.browse_commits = function() end + + vim.cmd("tabnew") + local old_history_tabid = vim.api.nvim_get_current_tabpage() + reviewer.history_tabid = old_history_tabid + + reviewer.reload_history() + + assert.are.equal(old_history_tabid, reviewer.history_tabid) + assert.is_true(vim.api.nvim_tabpage_is_valid(old_history_tabid)) + + state.load_new_state = original_load_new_state + reviewer.browse_commits = original_browse_commits + vim.cmd("silent! tabonly") + reviewer.history_tabid = nil + end) +end)