From 0e58b1467db5c31157b1772ebeb3b199c2186547 Mon Sep 17 00:00:00 2001 From: Felipe Mozer Barina Date: Mon, 10 Mar 2025 06:12:35 -0300 Subject: [PATCH] feat: add prompt_force_push option Allows users to disable the prompt to force push when branches diverge. --- README.md | 2 ++ doc/neogit.txt | 2 ++ lua/neogit/config.lua | 2 ++ lua/neogit/popups/push/actions.lua | 5 +++-- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f808a1f87..3f09469cb 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,8 @@ neogit.setup { disable_context_highlighting = false, -- Disables signs for sections/items/hunks disable_signs = false, + -- Offer to force push when branches diverge + prompt_force_push = true, -- Changes what mode the Commit Editor starts in. `true` will leave nvim in normal mode, `false` will change nvim to -- insert mode, and `"auto"` will change nvim to insert mode IF the commit message is empty, otherwise leaving it in -- normal mode. diff --git a/doc/neogit.txt b/doc/neogit.txt index 90bf911d8..35665bfae 100644 --- a/doc/neogit.txt +++ b/doc/neogit.txt @@ -92,6 +92,8 @@ to Neovim users. disable_context_highlighting = false, -- Disables signs for sections/items/hunks disable_signs = false, + -- Offer to force push when branches diverge + prompt_force_push = true, -- Changes what mode the Commit Editor starts in. `true` will leave nvim in normal mode, `false` will change nvim to -- insert mode, and `"auto"` will change nvim to insert mode IF the commit message is empty, otherwise leaving it in -- normal mode. diff --git a/lua/neogit/config.lua b/lua/neogit/config.lua index 86a1bd094..11c3e68a9 100644 --- a/lua/neogit/config.lua +++ b/lua/neogit/config.lua @@ -309,6 +309,7 @@ end ---@field disable_hint? boolean Remove the top hint in the Status buffer ---@field disable_context_highlighting? boolean Disable context highlights based on cursor position ---@field disable_signs? boolean Special signs to draw for sections etc. in Neogit +---@field prompt_force_push? boolean Offer to force push when branches diverge ---@field git_services? table Templartes to use when opening a pull request for a branch ---@field fetch_after_checkout? boolean Perform a fetch if the newly checked out branch has an upstream or pushRemote set ---@field telescope_sorter? function The sorter telescope will use @@ -356,6 +357,7 @@ function M.get_default_values() disable_hint = false, disable_context_highlighting = false, disable_signs = false, + prompt_force_push = true, graph_style = "ascii", commit_date_format = nil, log_date_format = nil, diff --git a/lua/neogit/popups/push/actions.lua b/lua/neogit/popups/push/actions.lua index 2304c302e..87c52a486 100644 --- a/lua/neogit/popups/push/actions.lua +++ b/lua/neogit/popups/push/actions.lua @@ -4,6 +4,7 @@ local logger = require("neogit.logger") local notification = require("neogit.lib.notification") local input = require("neogit.lib.input") local util = require("neogit.lib.util") +local config = require("neogit.config") local FuzzyFinderBuffer = require("neogit.buffers.fuzzy_finder") @@ -41,8 +42,8 @@ local function push_to(args, remote, branch, opts) local using_force = vim.tbl_contains(args, "--force") or vim.tbl_contains(args, "--force-with-lease") local updates_rejected = string.find(table.concat(res.stdout), "Updates were rejected") ~= nil - -- Only ask the user whether to force push if not already specified - if res and res.code ~= 0 and not using_force and updates_rejected then + -- Only ask the user whether to force push if not already specified and feature enabled + if res and res.code ~= 0 and not using_force and updates_rejected and config.values.prompt_force_push then logger.error("Attempting force push to " .. name) local message = "Your branch has diverged from the remote branch. Do you want to force push?"