From 40c665b28163548cfee70828059cd912cf2c81f8 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 14:01:16 -0500 Subject: [PATCH] refactor: get user confirmation for aborting a commit --- lua/neogit/buffers/commit_editor/init.lua | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/lua/neogit/buffers/commit_editor/init.lua b/lua/neogit/buffers/commit_editor/init.lua index e1ae6b191..034158c53 100644 --- a/lua/neogit/buffers/commit_editor/init.lua +++ b/lua/neogit/buffers/commit_editor/init.lua @@ -30,6 +30,7 @@ function M.new(filename, on_unload) end function M:open() + local commit_aborted = false local written = false self.buffer = Buffer.create { name = self.filename, @@ -40,11 +41,8 @@ function M:open() modifiable = true, readonly = false, autocmds = { - ["BufWritePre"] = function() - written = true - end, ["BufUnload"] = function(o) - if written then + if not commit_aborted and written then if not config.values.disable_commit_confirmation and not input.get_confirmation("Are you sure you want to commit?") @@ -57,8 +55,8 @@ function M:open() end end - if self.on_unload then - self.on_unload(written) + if self.on_unload and not commit_aborted then + self.on_unload(true) end require("neogit.process").defer_show_preview_buffers() @@ -67,13 +65,11 @@ function M:open() mappings = { n = { ["q"] = function(buffer) - if buffer:get_option("modified") then - require("neogit.lib.notification").create( - "Commit message has not been saved! Try `:w`.", - vim.log.levels.WARN - ) - else - self:close() + if not buffer:get_option("modified") then + written = true + buffer:close(true) + elseif input.get_confirmation("Commit message hasn't been saved. Abort?") then + commit_aborted = true buffer:close(true) end end,