diff --git a/lua/neogit/buffers/commit_editor/init.lua b/lua/neogit/buffers/commit_editor/init.lua index 6abf89e97..9aac32990 100644 --- a/lua/neogit/buffers/commit_editor/init.lua +++ b/lua/neogit/buffers/commit_editor/init.lua @@ -30,7 +30,7 @@ function M.new(filename, on_unload) end function M:open() - local written = false + local should_commit = false self.buffer = Buffer.create { name = self.filename, filetype = "NeogitCommitMessage", @@ -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 should_commit 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 should_commit then + self.on_unload(true) end require("neogit.process").defer_show_preview_buffers() @@ -67,7 +67,12 @@ function M:open() mappings = { n = { ["q"] = function(buffer) - buffer:close(true) + if not buffer:get_option("modified") then + buffer:close(true) + elseif input.get_confirmation("Commit message hasn't been saved. Abort?") then + should_commit = true + buffer:close(true) + end end, }, }, diff --git a/lua/neogit/lib/buffer.lua b/lua/neogit/lib/buffer.lua index b2ee917e7..615f52956 100644 --- a/lua/neogit/lib/buffer.lua +++ b/lua/neogit/lib/buffer.lua @@ -226,7 +226,7 @@ function Buffer:unlock() end function Buffer:get_option(name) - api.nvim_buf_get_option(self.handle, name) + return api.nvim_buf_get_option(self.handle, name) end function Buffer:set_option(name, value)