From a01a3210c12872ec52c735a3e7a84b1b4f3fdf47 Mon Sep 17 00:00:00 2001 From: d-karl Date: Sun, 21 Jan 2024 00:06:24 +0100 Subject: [PATCH] Allow use of --imply_local when calling DiffviewOpen, allowing LSP use and linting in MR review (#162) Adds the ability to use the local filesystem when doing reviews by passing the `--imply-local` flag to the reviewer. This functionality is disabled by default. If users make changes to their filesystem (the files are "dirty") the default reviewer (hashes not files) will be use. This is a MINOR release. --- README.md | 5 +++++ doc/gitlab.nvim.txt | 5 +++++ lua/gitlab/reviewer/diffview.lua | 24 +++++++++++++++++++++++- lua/gitlab/state.lua | 5 +++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2892189..ecd7a33 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,11 @@ require("gitlab").setup({ config_path = nil, -- Custom path for `.gitlab.nvim` file, please read the "Connecting to Gitlab" section debug = { go_request = false, go_response = false }, -- Which values to log attachment_dir = nil, -- The local directory for files (see the "summary" section) + reviewer_settings = { + diffview = { + imply_local = false, -- If true, will attempt to use --imply_local option when calling |:DiffviewOpen| + }, + }, help = "?", -- Opens a help popup for local keymaps when a relevant view is focused (popup, discussion panel, etc) popup = { -- The popup for comment creation, editing, and replying exit = "", diff --git a/doc/gitlab.nvim.txt b/doc/gitlab.nvim.txt index 638ce37..23806a9 100644 --- a/doc/gitlab.nvim.txt +++ b/doc/gitlab.nvim.txt @@ -136,6 +136,11 @@ you call this function with no values the defaults will be used: config_path = nil, -- Custom path for `.gitlab.nvim` file, please read the "Connecting to Gitlab" section debug = { go_request = false, go_response = false }, -- Which values to log attachment_dir = nil, -- The local directory for files (see the "summary" section) + reviewer_settings = { + diffview = { + imply_local = false, -- If true, will attempt to use --imply_local option when calling |:DiffviewOpen| + }, + }, help = "?", -- Opens a help popup for local keymaps when a relevant view is focused (popup, discussion panel, etc) popup = { -- The popup for comment creation, editing, and replying exit = "", diff --git a/lua/gitlab/reviewer/diffview.lua b/lua/gitlab/reviewer/diffview.lua index 9163585..79919b5 100644 --- a/lua/gitlab/reviewer/diffview.lua +++ b/lua/gitlab/reviewer/diffview.lua @@ -9,6 +9,12 @@ local M = { tabnr = nil, } +local all_git_manged_files_unmodified = function() + -- check local managed files are unmodified, matching the state in the MR + -- TODO: ensure correct CWD? + return vim.fn.trim(vim.fn.system({ "git", "status", "--short", "--untracked-files=no" })) == "" +end + M.open = function() local diff_refs = state.INFO.diff_refs if diff_refs == nil then @@ -21,9 +27,25 @@ M.open = function() return end - vim.api.nvim_command(string.format("DiffviewOpen %s..%s", diff_refs.base_sha, diff_refs.head_sha)) + local diffview_open_command = "DiffviewOpen" + local diffview_feature_imply_local = { + user_requested = state.settings.reviewer_settings.diffview.imply_local, + usable = all_git_manged_files_unmodified(), + } + if diffview_feature_imply_local.user_requested and diffview_feature_imply_local.usable then + diffview_open_command = diffview_open_command .. " --imply-local" + end + + vim.api.nvim_command(string.format("%s %s..%s", diffview_open_command, diff_refs.base_sha, diff_refs.head_sha)) M.tabnr = vim.api.nvim_get_current_tabpage() + if diffview_feature_imply_local.user_requested and not diffview_feature_imply_local.usable then + u.notify( + "There are uncommited changes in the working tree, cannot use 'imply_local' setting for gitlab reviews. Stash or commit all changes to use.", + vim.log.levels.WARN + ) + end + if state.INFO.has_conflicts then u.notify("This merge request has conflicts!", vim.log.levels.WARN) end diff --git a/lua/gitlab/state.lua b/lua/gitlab/state.lua index 62758cc..1531d33 100644 --- a/lua/gitlab/state.lua +++ b/lua/gitlab/state.lua @@ -13,6 +13,11 @@ M.settings = { log_path = (vim.fn.stdpath("cache") .. "/gitlab.nvim.log"), config_path = nil, reviewer = "diffview", + reviewer_settings = { + diffview = { + imply_local = false, + }, + }, attachment_dir = "", help = "?", popup = {