Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<C-R>` | 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 |
Expand Down
11 changes: 11 additions & 0 deletions doc/gitlab.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<C-R>", -- 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
Expand Down Expand Up @@ -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() ~
Expand Down
1 change: 1 addition & 0 deletions lua/gitlab/actions/rebase.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lua/gitlab/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
56 changes: 56 additions & 0 deletions lua/gitlab/reviewer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local M = {
is_open = false,
bufnr = nil,
tabid = nil,
history_tabid = nil,
stored_win = nil,
}

Expand Down Expand Up @@ -91,6 +92,54 @@ 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

---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)
Expand Down Expand Up @@ -510,6 +559,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
Expand Down
7 changes: 7 additions & 0 deletions lua/gitlab/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ M.settings = {
create_mr = "glC",
choose_merge_request = "glc",
start_review = "glS",
browse_commits = "glh",
reload_review = "gl<C-R>",
close_review = "glQ",
summary = "gls",
Expand Down Expand Up @@ -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()
Expand Down
80 changes: 80 additions & 0 deletions tests/spec/history_browse_commits_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
-- 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)

-- 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)
36 changes: 36 additions & 0 deletions tests/spec/reviewer_autocommands_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
Loading