Skip to content

Commit

Permalink
refactor: get user confirmation for aborting a commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PriceHiller committed Jul 15, 2023
1 parent 2b99f1b commit 549da37
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions lua/neogit/buffers/commit_editor/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function M.new(filename, on_unload)
end

function M:open()
local written = false
local commit_aborted = false
self.buffer = Buffer.create {
name = self.filename,
filetype = "NeogitCommitMessage",
Expand All @@ -40,25 +40,25 @@ function M:open()
modifiable = true,
readonly = false,
autocmds = {
["BufWritePre"] = function()
written = true
end,
["BufUnload"] = function(o)
if written then
local buf = Buffer.create {
name = o.buf,
}
if not commit_aborted and buf:get_option("modified") then
if
not config.values.disable_commit_confirmation
and not input.get_confirmation("Are you sure you want to commit?")
then
-- Clear the buffer, without filling the register
vim.api.nvim_buf_set_lines(o.buf, 0, -1, false, {})
vim.api.nvim_buf_call(o.buf, function()
buf:set_lines(0, -1, false, {})
buf:call(function()
vim.cmd("silent w!")
end)
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()
Expand All @@ -67,13 +67,10 @@ 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
buffer:close(true)
elseif input.get_confirmation("Commit message hasn't been saved. Abort?") then
commit_aborted = true
buffer:close(true)
end
end,
Expand Down

0 comments on commit 549da37

Please sign in to comment.